1 /* DWARF 2 debugging format support for GDB.
3 Copyright (C) 1994-2018 Free Software Foundation, Inc.
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
12 This file is part of GDB.
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
32 #include "dwarf2read.h"
33 #include "dwarf-index-common.h"
42 #include "gdb-demangle.h"
43 #include "expression.h"
44 #include "filenames.h" /* for DOSish file names */
47 #include "complaints.h"
49 #include "dwarf2expr.h"
50 #include "dwarf2loc.h"
51 #include "cp-support.h"
57 #include "typeprint.h"
60 #include "completer.h"
65 #include "gdbcore.h" /* for gnutarget */
66 #include "gdb/gdb-index.h"
71 #include "filestuff.h"
73 #include "namespace.h"
74 #include "common/gdb_unlinker.h"
75 #include "common/function-view.h"
76 #include "common/gdb_optional.h"
77 #include "common/underlying.h"
78 #include "common/byte-vector.h"
79 #include "common/hash_enum.h"
80 #include "filename-seen-cache.h"
83 #include <sys/types.h>
85 #include <unordered_set>
86 #include <unordered_map>
90 #include <forward_list>
91 #include "rust-lang.h"
92 #include "common/pathstuff.h"
94 /* When == 1, print basic high level tracing messages.
95 When > 1, be more verbose.
96 This is in contrast to the low level DIE reading of dwarf_die_debug. */
97 static unsigned int dwarf_read_debug = 0;
99 /* When non-zero, dump DIEs after they are read in. */
100 static unsigned int dwarf_die_debug = 0;
102 /* When non-zero, dump line number entries as they are read in. */
103 static unsigned int dwarf_line_debug = 0;
105 /* When non-zero, cross-check physname against demangler. */
106 static int check_physname = 0;
108 /* When non-zero, do not reject deprecated .gdb_index sections. */
109 static int use_deprecated_index_sections = 0;
111 static const struct objfile_data *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;
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 ((struct dwarf2_per_objfile *)
285 objfile_data (objfile, dwarf2_objfile_data_key));
288 /* Set the dwarf2_per_objfile associated to OBJFILE. */
291 set_dwarf2_per_objfile (struct objfile *objfile,
292 struct dwarf2_per_objfile *dwarf2_per_objfile)
294 gdb_assert (get_dwarf2_per_objfile (objfile) == NULL);
295 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
298 /* Default names of the debugging sections. */
300 /* Note that if the debugging section has been compressed, it might
301 have a name like .zdebug_info. */
303 static const struct dwarf2_debug_sections dwarf2_elf_names =
305 { ".debug_info", ".zdebug_info" },
306 { ".debug_abbrev", ".zdebug_abbrev" },
307 { ".debug_line", ".zdebug_line" },
308 { ".debug_loc", ".zdebug_loc" },
309 { ".debug_loclists", ".zdebug_loclists" },
310 { ".debug_macinfo", ".zdebug_macinfo" },
311 { ".debug_macro", ".zdebug_macro" },
312 { ".debug_str", ".zdebug_str" },
313 { ".debug_line_str", ".zdebug_line_str" },
314 { ".debug_ranges", ".zdebug_ranges" },
315 { ".debug_rnglists", ".zdebug_rnglists" },
316 { ".debug_types", ".zdebug_types" },
317 { ".debug_addr", ".zdebug_addr" },
318 { ".debug_frame", ".zdebug_frame" },
319 { ".eh_frame", NULL },
320 { ".gdb_index", ".zgdb_index" },
321 { ".debug_names", ".zdebug_names" },
322 { ".debug_aranges", ".zdebug_aranges" },
326 /* List of DWO/DWP sections. */
328 static const struct dwop_section_names
330 struct dwarf2_section_names abbrev_dwo;
331 struct dwarf2_section_names info_dwo;
332 struct dwarf2_section_names line_dwo;
333 struct dwarf2_section_names loc_dwo;
334 struct dwarf2_section_names loclists_dwo;
335 struct dwarf2_section_names macinfo_dwo;
336 struct dwarf2_section_names macro_dwo;
337 struct dwarf2_section_names str_dwo;
338 struct dwarf2_section_names str_offsets_dwo;
339 struct dwarf2_section_names types_dwo;
340 struct dwarf2_section_names cu_index;
341 struct dwarf2_section_names tu_index;
345 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
346 { ".debug_info.dwo", ".zdebug_info.dwo" },
347 { ".debug_line.dwo", ".zdebug_line.dwo" },
348 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
349 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
350 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
351 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
352 { ".debug_str.dwo", ".zdebug_str.dwo" },
353 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
354 { ".debug_types.dwo", ".zdebug_types.dwo" },
355 { ".debug_cu_index", ".zdebug_cu_index" },
356 { ".debug_tu_index", ".zdebug_tu_index" },
359 /* local data types */
361 /* The data in a compilation unit header, after target2host
362 translation, looks like this. */
363 struct comp_unit_head
367 unsigned char addr_size;
368 unsigned char signed_addr_p;
369 sect_offset abbrev_sect_off;
371 /* Size of file offsets; either 4 or 8. */
372 unsigned int offset_size;
374 /* Size of the length field; either 4 or 12. */
375 unsigned int initial_length_size;
377 enum dwarf_unit_type unit_type;
379 /* Offset to the first byte of this compilation unit header in the
380 .debug_info section, for resolving relative reference dies. */
381 sect_offset sect_off;
383 /* Offset to first die in this cu from the start of the cu.
384 This will be the first byte following the compilation unit header. */
385 cu_offset first_die_cu_offset;
387 /* 64-bit signature of this type unit - it is valid only for
388 UNIT_TYPE DW_UT_type. */
391 /* For types, offset in the type's DIE of the type defined by this TU. */
392 cu_offset type_cu_offset_in_tu;
395 /* Type used for delaying computation of method physnames.
396 See comments for compute_delayed_physnames. */
397 struct delayed_method_info
399 /* The type to which the method is attached, i.e., its parent class. */
402 /* The index of the method in the type's function fieldlists. */
405 /* The index of the method in the fieldlist. */
408 /* The name of the DIE. */
411 /* The DIE associated with this method. */
412 struct die_info *die;
415 /* Internal state when decoding a particular compilation unit. */
418 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
421 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
423 /* The header of the compilation unit. */
424 struct comp_unit_head header {};
426 /* Base address of this compilation unit. */
427 CORE_ADDR base_address = 0;
429 /* Non-zero if base_address has been set. */
432 /* The language we are debugging. */
433 enum language language = language_unknown;
434 const struct language_defn *language_defn = nullptr;
436 const char *producer = nullptr;
438 /* The generic symbol table building routines have separate lists for
439 file scope symbols and all all other scopes (local scopes). So
440 we need to select the right one to pass to add_symbol_to_list().
441 We do it by keeping a pointer to the correct list in list_in_scope.
443 FIXME: The original dwarf code just treated the file scope as the
444 first local scope, and all other local scopes as nested local
445 scopes, and worked fine. Check to see if we really need to
446 distinguish these in buildsym.c. */
447 struct pending **list_in_scope = nullptr;
449 /* Hash table holding all the loaded partial DIEs
450 with partial_die->offset.SECT_OFF as hash. */
451 htab_t partial_dies = nullptr;
453 /* Storage for things with the same lifetime as this read-in compilation
454 unit, including partial DIEs. */
455 auto_obstack comp_unit_obstack;
457 /* When multiple dwarf2_cu structures are living in memory, this field
458 chains them all together, so that they can be released efficiently.
459 We will probably also want a generation counter so that most-recently-used
460 compilation units are cached... */
461 struct dwarf2_per_cu_data *read_in_chain = nullptr;
463 /* Backlink to our per_cu entry. */
464 struct dwarf2_per_cu_data *per_cu;
466 /* How many compilation units ago was this CU last referenced? */
469 /* A hash table of DIE cu_offset for following references with
470 die_info->offset.sect_off as hash. */
471 htab_t die_hash = nullptr;
473 /* Full DIEs if read in. */
474 struct die_info *dies = nullptr;
476 /* A set of pointers to dwarf2_per_cu_data objects for compilation
477 units referenced by this one. Only set during full symbol processing;
478 partial symbol tables do not have dependencies. */
479 htab_t dependencies = nullptr;
481 /* Header data from the line table, during full symbol processing. */
482 struct line_header *line_header = nullptr;
483 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
484 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
485 this is the DW_TAG_compile_unit die for this CU. We'll hold on
486 to the line header as long as this DIE is being processed. See
487 process_die_scope. */
488 die_info *line_header_die_owner = nullptr;
490 /* A list of methods which need to have physnames computed
491 after all type information has been read. */
492 std::vector<delayed_method_info> method_list;
494 /* To be copied to symtab->call_site_htab. */
495 htab_t call_site_htab = nullptr;
497 /* Non-NULL if this CU came from a DWO file.
498 There is an invariant here that is important to remember:
499 Except for attributes copied from the top level DIE in the "main"
500 (or "stub") file in preparation for reading the DWO file
501 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
502 Either there isn't a DWO file (in which case this is NULL and the point
503 is moot), or there is and either we're not going to read it (in which
504 case this is NULL) or there is and we are reading it (in which case this
506 struct dwo_unit *dwo_unit = nullptr;
508 /* The DW_AT_addr_base attribute if present, zero otherwise
509 (zero is a valid value though).
510 Note this value comes from the Fission stub CU/TU's DIE. */
511 ULONGEST addr_base = 0;
513 /* The DW_AT_ranges_base attribute if present, zero otherwise
514 (zero is a valid value though).
515 Note this value comes from the Fission stub CU/TU's DIE.
516 Also note that the value is zero in the non-DWO case so this value can
517 be used without needing to know whether DWO files are in use or not.
518 N.B. This does not apply to DW_AT_ranges appearing in
519 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
520 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
521 DW_AT_ranges_base *would* have to be applied, and we'd have to care
522 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
523 ULONGEST ranges_base = 0;
525 /* When reading debug info generated by older versions of rustc, we
526 have to rewrite some union types to be struct types with a
527 variant part. This rewriting must be done after the CU is fully
528 read in, because otherwise at the point of rewriting some struct
529 type might not have been fully processed. So, we keep a list of
530 all such types here and process them after expansion. */
531 std::vector<struct type *> rust_unions;
533 /* Mark used when releasing cached dies. */
534 unsigned int mark : 1;
536 /* This CU references .debug_loc. See the symtab->locations_valid field.
537 This test is imperfect as there may exist optimized debug code not using
538 any location list and still facing inlining issues if handled as
539 unoptimized code. For a future better test see GCC PR other/32998. */
540 unsigned int has_loclist : 1;
542 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
543 if all the producer_is_* fields are valid. This information is cached
544 because profiling CU expansion showed excessive time spent in
545 producer_is_gxx_lt_4_6. */
546 unsigned int checked_producer : 1;
547 unsigned int producer_is_gxx_lt_4_6 : 1;
548 unsigned int producer_is_gcc_lt_4_3 : 1;
549 unsigned int producer_is_icc_lt_14 : 1;
551 /* When set, the file that we're processing is known to have
552 debugging info for C++ namespaces. GCC 3.3.x did not produce
553 this information, but later versions do. */
555 unsigned int processing_has_namespace_info : 1;
557 struct partial_die_info *find_partial_die (sect_offset sect_off);
560 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
561 This includes type_unit_group and quick_file_names. */
563 struct stmt_list_hash
565 /* The DWO unit this table is from or NULL if there is none. */
566 struct dwo_unit *dwo_unit;
568 /* Offset in .debug_line or .debug_line.dwo. */
569 sect_offset line_sect_off;
572 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
573 an object of this type. */
575 struct type_unit_group
577 /* dwarf2read.c's main "handle" on a TU symtab.
578 To simplify things we create an artificial CU that "includes" all the
579 type units using this stmt_list so that the rest of the code still has
580 a "per_cu" handle on the symtab.
581 This PER_CU is recognized by having no section. */
582 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
583 struct dwarf2_per_cu_data per_cu;
585 /* The TUs that share this DW_AT_stmt_list entry.
586 This is added to while parsing type units to build partial symtabs,
587 and is deleted afterwards and not used again. */
588 VEC (sig_type_ptr) *tus;
590 /* The compunit symtab.
591 Type units in a group needn't all be defined in the same source file,
592 so we create an essentially anonymous symtab as the compunit symtab. */
593 struct compunit_symtab *compunit_symtab;
595 /* The data used to construct the hash key. */
596 struct stmt_list_hash hash;
598 /* The number of symtabs from the line header.
599 The value here must match line_header.num_file_names. */
600 unsigned int num_symtabs;
602 /* The symbol tables for this TU (obtained from the files listed in
604 WARNING: The order of entries here must match the order of entries
605 in the line header. After the first TU using this type_unit_group, the
606 line header for the subsequent TUs is recreated from this. This is done
607 because we need to use the same symtabs for each TU using the same
608 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
609 there's no guarantee the line header doesn't have duplicate entries. */
610 struct symtab **symtabs;
613 /* These sections are what may appear in a (real or virtual) DWO file. */
617 struct dwarf2_section_info abbrev;
618 struct dwarf2_section_info line;
619 struct dwarf2_section_info loc;
620 struct dwarf2_section_info loclists;
621 struct dwarf2_section_info macinfo;
622 struct dwarf2_section_info macro;
623 struct dwarf2_section_info str;
624 struct dwarf2_section_info str_offsets;
625 /* In the case of a virtual DWO file, these two are unused. */
626 struct dwarf2_section_info info;
627 VEC (dwarf2_section_info_def) *types;
630 /* CUs/TUs in DWP/DWO files. */
634 /* Backlink to the containing struct dwo_file. */
635 struct dwo_file *dwo_file;
637 /* The "id" that distinguishes this CU/TU.
638 .debug_info calls this "dwo_id", .debug_types calls this "signature".
639 Since signatures came first, we stick with it for consistency. */
642 /* The section this CU/TU lives in, in the DWO file. */
643 struct dwarf2_section_info *section;
645 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
646 sect_offset sect_off;
649 /* For types, offset in the type's DIE of the type defined by this TU. */
650 cu_offset type_offset_in_tu;
653 /* include/dwarf2.h defines the DWP section codes.
654 It defines a max value but it doesn't define a min value, which we
655 use for error checking, so provide one. */
657 enum dwp_v2_section_ids
662 /* Data for one DWO file.
664 This includes virtual DWO files (a virtual DWO file is a DWO file as it
665 appears in a DWP file). DWP files don't really have DWO files per se -
666 comdat folding of types "loses" the DWO file they came from, and from
667 a high level view DWP files appear to contain a mass of random types.
668 However, to maintain consistency with the non-DWP case we pretend DWP
669 files contain virtual DWO files, and we assign each TU with one virtual
670 DWO file (generally based on the line and abbrev section offsets -
671 a heuristic that seems to work in practice). */
675 /* The DW_AT_GNU_dwo_name attribute.
676 For virtual DWO files the name is constructed from the section offsets
677 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
678 from related CU+TUs. */
679 const char *dwo_name;
681 /* The DW_AT_comp_dir attribute. */
682 const char *comp_dir;
684 /* The bfd, when the file is open. Otherwise this is NULL.
685 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
688 /* The sections that make up this DWO file.
689 Remember that for virtual DWO files in DWP V2, these are virtual
690 sections (for lack of a better name). */
691 struct dwo_sections sections;
693 /* The CUs in the file.
694 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
695 an extension to handle LLVM's Link Time Optimization output (where
696 multiple source files may be compiled into a single object/dwo pair). */
699 /* Table of TUs in the file.
700 Each element is a struct dwo_unit. */
704 /* These sections are what may appear in a DWP file. */
708 /* These are used by both DWP version 1 and 2. */
709 struct dwarf2_section_info str;
710 struct dwarf2_section_info cu_index;
711 struct dwarf2_section_info tu_index;
713 /* These are only used by DWP version 2 files.
714 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
715 sections are referenced by section number, and are not recorded here.
716 In DWP version 2 there is at most one copy of all these sections, each
717 section being (effectively) comprised of the concatenation of all of the
718 individual sections that exist in the version 1 format.
719 To keep the code simple we treat each of these concatenated pieces as a
720 section itself (a virtual section?). */
721 struct dwarf2_section_info abbrev;
722 struct dwarf2_section_info info;
723 struct dwarf2_section_info line;
724 struct dwarf2_section_info loc;
725 struct dwarf2_section_info macinfo;
726 struct dwarf2_section_info macro;
727 struct dwarf2_section_info str_offsets;
728 struct dwarf2_section_info types;
731 /* These sections are what may appear in a virtual DWO file in DWP version 1.
732 A virtual DWO file is a DWO file as it appears in a DWP file. */
734 struct virtual_v1_dwo_sections
736 struct dwarf2_section_info abbrev;
737 struct dwarf2_section_info line;
738 struct dwarf2_section_info loc;
739 struct dwarf2_section_info macinfo;
740 struct dwarf2_section_info macro;
741 struct dwarf2_section_info str_offsets;
742 /* Each DWP hash table entry records one CU or one TU.
743 That is recorded here, and copied to dwo_unit.section. */
744 struct dwarf2_section_info info_or_types;
747 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
748 In version 2, the sections of the DWO files are concatenated together
749 and stored in one section of that name. Thus each ELF section contains
750 several "virtual" sections. */
752 struct virtual_v2_dwo_sections
754 bfd_size_type abbrev_offset;
755 bfd_size_type abbrev_size;
757 bfd_size_type line_offset;
758 bfd_size_type line_size;
760 bfd_size_type loc_offset;
761 bfd_size_type loc_size;
763 bfd_size_type macinfo_offset;
764 bfd_size_type macinfo_size;
766 bfd_size_type macro_offset;
767 bfd_size_type macro_size;
769 bfd_size_type str_offsets_offset;
770 bfd_size_type str_offsets_size;
772 /* Each DWP hash table entry records one CU or one TU.
773 That is recorded here, and copied to dwo_unit.section. */
774 bfd_size_type info_or_types_offset;
775 bfd_size_type info_or_types_size;
778 /* Contents of DWP hash tables. */
780 struct dwp_hash_table
782 uint32_t version, nr_columns;
783 uint32_t nr_units, nr_slots;
784 const gdb_byte *hash_table, *unit_table;
789 const gdb_byte *indices;
793 /* This is indexed by column number and gives the id of the section
795 #define MAX_NR_V2_DWO_SECTIONS \
796 (1 /* .debug_info or .debug_types */ \
797 + 1 /* .debug_abbrev */ \
798 + 1 /* .debug_line */ \
799 + 1 /* .debug_loc */ \
800 + 1 /* .debug_str_offsets */ \
801 + 1 /* .debug_macro or .debug_macinfo */)
802 int section_ids[MAX_NR_V2_DWO_SECTIONS];
803 const gdb_byte *offsets;
804 const gdb_byte *sizes;
809 /* Data for one DWP file. */
813 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
815 dbfd (std::move (abfd))
819 /* Name of the file. */
822 /* File format version. */
826 gdb_bfd_ref_ptr dbfd;
828 /* Section info for this file. */
829 struct dwp_sections sections {};
831 /* Table of CUs in the file. */
832 const struct dwp_hash_table *cus = nullptr;
834 /* Table of TUs in the file. */
835 const struct dwp_hash_table *tus = nullptr;
837 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
838 htab_t loaded_cus {};
839 htab_t loaded_tus {};
841 /* Table to map ELF section numbers to their sections.
842 This is only needed for the DWP V1 file format. */
843 unsigned int num_sections = 0;
844 asection **elf_sections = nullptr;
847 /* This represents a '.dwz' file. */
851 dwz_file (gdb_bfd_ref_ptr &&bfd)
852 : dwz_bfd (std::move (bfd))
856 /* A dwz file can only contain a few sections. */
857 struct dwarf2_section_info abbrev {};
858 struct dwarf2_section_info info {};
859 struct dwarf2_section_info str {};
860 struct dwarf2_section_info line {};
861 struct dwarf2_section_info macro {};
862 struct dwarf2_section_info gdb_index {};
863 struct dwarf2_section_info debug_names {};
866 gdb_bfd_ref_ptr dwz_bfd;
869 /* Struct used to pass misc. parameters to read_die_and_children, et
870 al. which are used for both .debug_info and .debug_types dies.
871 All parameters here are unchanging for the life of the call. This
872 struct exists to abstract away the constant parameters of die reading. */
874 struct die_reader_specs
876 /* The bfd of die_section. */
879 /* The CU of the DIE we are parsing. */
880 struct dwarf2_cu *cu;
882 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
883 struct dwo_file *dwo_file;
885 /* The section the die comes from.
886 This is either .debug_info or .debug_types, or the .dwo variants. */
887 struct dwarf2_section_info *die_section;
889 /* die_section->buffer. */
890 const gdb_byte *buffer;
892 /* The end of the buffer. */
893 const gdb_byte *buffer_end;
895 /* The value of the DW_AT_comp_dir attribute. */
896 const char *comp_dir;
898 /* The abbreviation table to use when reading the DIEs. */
899 struct abbrev_table *abbrev_table;
902 /* Type of function passed to init_cutu_and_read_dies, et.al. */
903 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
904 const gdb_byte *info_ptr,
905 struct die_info *comp_unit_die,
909 /* A 1-based directory index. This is a strong typedef to prevent
910 accidentally using a directory index as a 0-based index into an
912 enum class dir_index : unsigned int {};
914 /* Likewise, a 1-based file name index. */
915 enum class file_name_index : unsigned int {};
919 file_entry () = default;
921 file_entry (const char *name_, dir_index d_index_,
922 unsigned int mod_time_, unsigned int length_)
925 mod_time (mod_time_),
929 /* Return the include directory at D_INDEX stored in LH. Returns
930 NULL if D_INDEX is out of bounds. */
931 const char *include_dir (const line_header *lh) const;
933 /* The file name. Note this is an observing pointer. The memory is
934 owned by debug_line_buffer. */
937 /* The directory index (1-based). */
938 dir_index d_index {};
940 unsigned int mod_time {};
942 unsigned int length {};
944 /* True if referenced by the Line Number Program. */
947 /* The associated symbol table, if any. */
948 struct symtab *symtab {};
951 /* The line number information for a compilation unit (found in the
952 .debug_line section) begins with a "statement program header",
953 which contains the following information. */
960 /* Add an entry to the include directory table. */
961 void add_include_dir (const char *include_dir);
963 /* Add an entry to the file name table. */
964 void add_file_name (const char *name, dir_index d_index,
965 unsigned int mod_time, unsigned int length);
967 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
969 const char *include_dir_at (dir_index index) const
971 /* Convert directory index number (1-based) to vector index
973 size_t vec_index = to_underlying (index) - 1;
975 if (vec_index >= include_dirs.size ())
977 return include_dirs[vec_index];
980 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
982 file_entry *file_name_at (file_name_index index)
984 /* Convert file name index number (1-based) to vector index
986 size_t vec_index = to_underlying (index) - 1;
988 if (vec_index >= file_names.size ())
990 return &file_names[vec_index];
993 /* Const version of the above. */
994 const file_entry *file_name_at (unsigned int index) const
996 if (index >= file_names.size ())
998 return &file_names[index];
1001 /* Offset of line number information in .debug_line section. */
1002 sect_offset sect_off {};
1004 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1005 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1007 unsigned int total_length {};
1008 unsigned short version {};
1009 unsigned int header_length {};
1010 unsigned char minimum_instruction_length {};
1011 unsigned char maximum_ops_per_instruction {};
1012 unsigned char default_is_stmt {};
1014 unsigned char line_range {};
1015 unsigned char opcode_base {};
1017 /* standard_opcode_lengths[i] is the number of operands for the
1018 standard opcode whose value is i. This means that
1019 standard_opcode_lengths[0] is unused, and the last meaningful
1020 element is standard_opcode_lengths[opcode_base - 1]. */
1021 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1023 /* The include_directories table. Note these are observing
1024 pointers. The memory is owned by debug_line_buffer. */
1025 std::vector<const char *> include_dirs;
1027 /* The file_names table. */
1028 std::vector<file_entry> file_names;
1030 /* The start and end of the statement program following this
1031 header. These point into dwarf2_per_objfile->line_buffer. */
1032 const gdb_byte *statement_program_start {}, *statement_program_end {};
1035 typedef std::unique_ptr<line_header> line_header_up;
1038 file_entry::include_dir (const line_header *lh) const
1040 return lh->include_dir_at (d_index);
1043 /* When we construct a partial symbol table entry we only
1044 need this much information. */
1045 struct partial_die_info : public allocate_on_obstack
1047 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1049 /* Disable assign but still keep copy ctor, which is needed
1050 load_partial_dies. */
1051 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1053 /* Adjust the partial die before generating a symbol for it. This
1054 function may set the is_external flag or change the DIE's
1056 void fixup (struct dwarf2_cu *cu);
1058 /* Read a minimal amount of information into the minimal die
1060 const gdb_byte *read (const struct die_reader_specs *reader,
1061 const struct abbrev_info &abbrev,
1062 const gdb_byte *info_ptr);
1064 /* Offset of this DIE. */
1065 const sect_offset sect_off;
1067 /* DWARF-2 tag for this DIE. */
1068 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1070 /* Assorted flags describing the data found in this DIE. */
1071 const unsigned int has_children : 1;
1073 unsigned int is_external : 1;
1074 unsigned int is_declaration : 1;
1075 unsigned int has_type : 1;
1076 unsigned int has_specification : 1;
1077 unsigned int has_pc_info : 1;
1078 unsigned int may_be_inlined : 1;
1080 /* This DIE has been marked DW_AT_main_subprogram. */
1081 unsigned int main_subprogram : 1;
1083 /* Flag set if the SCOPE field of this structure has been
1085 unsigned int scope_set : 1;
1087 /* Flag set if the DIE has a byte_size attribute. */
1088 unsigned int has_byte_size : 1;
1090 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1091 unsigned int has_const_value : 1;
1093 /* Flag set if any of the DIE's children are template arguments. */
1094 unsigned int has_template_arguments : 1;
1096 /* Flag set if fixup has been called on this die. */
1097 unsigned int fixup_called : 1;
1099 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1100 unsigned int is_dwz : 1;
1102 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1103 unsigned int spec_is_dwz : 1;
1105 /* The name of this DIE. Normally the value of DW_AT_name, but
1106 sometimes a default name for unnamed DIEs. */
1107 const char *name = nullptr;
1109 /* The linkage name, if present. */
1110 const char *linkage_name = nullptr;
1112 /* The scope to prepend to our children. This is generally
1113 allocated on the comp_unit_obstack, so will disappear
1114 when this compilation unit leaves the cache. */
1115 const char *scope = nullptr;
1117 /* Some data associated with the partial DIE. The tag determines
1118 which field is live. */
1121 /* The location description associated with this DIE, if any. */
1122 struct dwarf_block *locdesc;
1123 /* The offset of an import, for DW_TAG_imported_unit. */
1124 sect_offset sect_off;
1127 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1128 CORE_ADDR lowpc = 0;
1129 CORE_ADDR highpc = 0;
1131 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1132 DW_AT_sibling, if any. */
1133 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1134 could return DW_AT_sibling values to its caller load_partial_dies. */
1135 const gdb_byte *sibling = nullptr;
1137 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1138 DW_AT_specification (or DW_AT_abstract_origin or
1139 DW_AT_extension). */
1140 sect_offset spec_offset {};
1142 /* Pointers to this DIE's parent, first child, and next sibling,
1144 struct partial_die_info *die_parent = nullptr;
1145 struct partial_die_info *die_child = nullptr;
1146 struct partial_die_info *die_sibling = nullptr;
1148 friend struct partial_die_info *
1149 dwarf2_cu::find_partial_die (sect_offset sect_off);
1152 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1153 partial_die_info (sect_offset sect_off)
1154 : partial_die_info (sect_off, DW_TAG_padding, 0)
1158 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1160 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1165 has_specification = 0;
1168 main_subprogram = 0;
1171 has_const_value = 0;
1172 has_template_arguments = 0;
1179 /* This data structure holds the information of an abbrev. */
1182 unsigned int number; /* number identifying abbrev */
1183 enum dwarf_tag tag; /* dwarf tag */
1184 unsigned short has_children; /* boolean */
1185 unsigned short num_attrs; /* number of attributes */
1186 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1187 struct abbrev_info *next; /* next in chain */
1192 ENUM_BITFIELD(dwarf_attribute) name : 16;
1193 ENUM_BITFIELD(dwarf_form) form : 16;
1195 /* It is valid only if FORM is DW_FORM_implicit_const. */
1196 LONGEST implicit_const;
1199 /* Size of abbrev_table.abbrev_hash_table. */
1200 #define ABBREV_HASH_SIZE 121
1202 /* Top level data structure to contain an abbreviation table. */
1206 explicit abbrev_table (sect_offset off)
1210 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1211 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1214 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1216 /* Allocate space for a struct abbrev_info object in
1218 struct abbrev_info *alloc_abbrev ();
1220 /* Add an abbreviation to the table. */
1221 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1223 /* Look up an abbrev in the table.
1224 Returns NULL if the abbrev is not found. */
1226 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1229 /* Where the abbrev table came from.
1230 This is used as a sanity check when the table is used. */
1231 const sect_offset sect_off;
1233 /* Storage for the abbrev table. */
1234 auto_obstack abbrev_obstack;
1238 /* Hash table of abbrevs.
1239 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1240 It could be statically allocated, but the previous code didn't so we
1242 struct abbrev_info **m_abbrevs;
1245 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1247 /* Attributes have a name and a value. */
1250 ENUM_BITFIELD(dwarf_attribute) name : 16;
1251 ENUM_BITFIELD(dwarf_form) form : 15;
1253 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1254 field should be in u.str (existing only for DW_STRING) but it is kept
1255 here for better struct attribute alignment. */
1256 unsigned int string_is_canonical : 1;
1261 struct dwarf_block *blk;
1270 /* This data structure holds a complete die structure. */
1273 /* DWARF-2 tag for this DIE. */
1274 ENUM_BITFIELD(dwarf_tag) tag : 16;
1276 /* Number of attributes */
1277 unsigned char num_attrs;
1279 /* True if we're presently building the full type name for the
1280 type derived from this DIE. */
1281 unsigned char building_fullname : 1;
1283 /* True if this die is in process. PR 16581. */
1284 unsigned char in_process : 1;
1287 unsigned int abbrev;
1289 /* Offset in .debug_info or .debug_types section. */
1290 sect_offset sect_off;
1292 /* The dies in a compilation unit form an n-ary tree. PARENT
1293 points to this die's parent; CHILD points to the first child of
1294 this node; and all the children of a given node are chained
1295 together via their SIBLING fields. */
1296 struct die_info *child; /* Its first child, if any. */
1297 struct die_info *sibling; /* Its next sibling, if any. */
1298 struct die_info *parent; /* Its parent, if any. */
1300 /* An array of attributes, with NUM_ATTRS elements. There may be
1301 zero, but it's not common and zero-sized arrays are not
1302 sufficiently portable C. */
1303 struct attribute attrs[1];
1306 /* Get at parts of an attribute structure. */
1308 #define DW_STRING(attr) ((attr)->u.str)
1309 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1310 #define DW_UNSND(attr) ((attr)->u.unsnd)
1311 #define DW_BLOCK(attr) ((attr)->u.blk)
1312 #define DW_SND(attr) ((attr)->u.snd)
1313 #define DW_ADDR(attr) ((attr)->u.addr)
1314 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1316 /* Blocks are a bunch of untyped bytes. */
1321 /* Valid only if SIZE is not zero. */
1322 const gdb_byte *data;
1325 #ifndef ATTR_ALLOC_CHUNK
1326 #define ATTR_ALLOC_CHUNK 4
1329 /* Allocate fields for structs, unions and enums in this size. */
1330 #ifndef DW_FIELD_ALLOC_CHUNK
1331 #define DW_FIELD_ALLOC_CHUNK 4
1334 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1335 but this would require a corresponding change in unpack_field_as_long
1337 static int bits_per_byte = 8;
1339 /* When reading a variant or variant part, we track a bit more
1340 information about the field, and store it in an object of this
1343 struct variant_field
1345 /* If we see a DW_TAG_variant, then this will be the discriminant
1347 ULONGEST discriminant_value;
1348 /* If we see a DW_TAG_variant, then this will be set if this is the
1350 bool default_branch;
1351 /* While reading a DW_TAG_variant_part, this will be set if this
1352 field is the discriminant. */
1353 bool is_discriminant;
1358 int accessibility = 0;
1360 /* Extra information to describe a variant or variant part. */
1361 struct variant_field variant {};
1362 struct field field {};
1367 const char *name = nullptr;
1368 std::vector<struct fn_field> fnfields;
1371 /* The routines that read and process dies for a C struct or C++ class
1372 pass lists of data member fields and lists of member function fields
1373 in an instance of a field_info structure, as defined below. */
1376 /* List of data member and baseclasses fields. */
1377 std::vector<struct nextfield> fields;
1378 std::vector<struct nextfield> baseclasses;
1380 /* Number of fields (including baseclasses). */
1383 /* Set if the accesibility of one of the fields is not public. */
1384 int non_public_fields = 0;
1386 /* Member function fieldlist array, contains name of possibly overloaded
1387 member function, number of overloaded member functions and a pointer
1388 to the head of the member function field chain. */
1389 std::vector<struct fnfieldlist> fnfieldlists;
1391 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1392 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1393 std::vector<struct decl_field> typedef_field_list;
1395 /* Nested types defined by this class and the number of elements in this
1397 std::vector<struct decl_field> nested_types_list;
1400 /* One item on the queue of compilation units to read in full symbols
1402 struct dwarf2_queue_item
1404 struct dwarf2_per_cu_data *per_cu;
1405 enum language pretend_language;
1406 struct dwarf2_queue_item *next;
1409 /* The current queue. */
1410 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1412 /* Loaded secondary compilation units are kept in memory until they
1413 have not been referenced for the processing of this many
1414 compilation units. Set this to zero to disable caching. Cache
1415 sizes of up to at least twenty will improve startup time for
1416 typical inter-CU-reference binaries, at an obvious memory cost. */
1417 static int dwarf_max_cache_age = 5;
1419 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1420 struct cmd_list_element *c, const char *value)
1422 fprintf_filtered (file, _("The upper bound on the age of cached "
1423 "DWARF compilation units is %s.\n"),
1427 /* local function prototypes */
1429 static const char *get_section_name (const struct dwarf2_section_info *);
1431 static const char *get_section_file_name (const struct dwarf2_section_info *);
1433 static void dwarf2_find_base_address (struct die_info *die,
1434 struct dwarf2_cu *cu);
1436 static struct partial_symtab *create_partial_symtab
1437 (struct dwarf2_per_cu_data *per_cu, const char *name);
1439 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1440 const gdb_byte *info_ptr,
1441 struct die_info *type_unit_die,
1442 int has_children, void *data);
1444 static void dwarf2_build_psymtabs_hard
1445 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1447 static void scan_partial_symbols (struct partial_die_info *,
1448 CORE_ADDR *, CORE_ADDR *,
1449 int, struct dwarf2_cu *);
1451 static void add_partial_symbol (struct partial_die_info *,
1452 struct dwarf2_cu *);
1454 static void add_partial_namespace (struct partial_die_info *pdi,
1455 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1456 int set_addrmap, struct dwarf2_cu *cu);
1458 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1459 CORE_ADDR *highpc, int set_addrmap,
1460 struct dwarf2_cu *cu);
1462 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1463 struct dwarf2_cu *cu);
1465 static void add_partial_subprogram (struct partial_die_info *pdi,
1466 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1467 int need_pc, struct dwarf2_cu *cu);
1469 static void dwarf2_read_symtab (struct partial_symtab *,
1472 static void psymtab_to_symtab_1 (struct partial_symtab *);
1474 static abbrev_table_up abbrev_table_read_table
1475 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1478 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1480 static struct partial_die_info *load_partial_dies
1481 (const struct die_reader_specs *, const gdb_byte *, int);
1483 static struct partial_die_info *find_partial_die (sect_offset, int,
1484 struct dwarf2_cu *);
1486 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1487 struct attribute *, struct attr_abbrev *,
1490 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1492 static int read_1_signed_byte (bfd *, const gdb_byte *);
1494 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1496 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1498 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1500 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1503 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1505 static LONGEST read_checked_initial_length_and_offset
1506 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1507 unsigned int *, unsigned int *);
1509 static LONGEST read_offset (bfd *, const gdb_byte *,
1510 const struct comp_unit_head *,
1513 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1515 static sect_offset read_abbrev_offset
1516 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1517 struct dwarf2_section_info *, sect_offset);
1519 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1521 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1523 static const char *read_indirect_string
1524 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1525 const struct comp_unit_head *, unsigned int *);
1527 static const char *read_indirect_line_string
1528 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1529 const struct comp_unit_head *, unsigned int *);
1531 static const char *read_indirect_string_at_offset
1532 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1533 LONGEST str_offset);
1535 static const char *read_indirect_string_from_dwz
1536 (struct objfile *objfile, struct dwz_file *, LONGEST);
1538 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1540 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1544 static const char *read_str_index (const struct die_reader_specs *reader,
1545 ULONGEST str_index);
1547 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1549 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1550 struct dwarf2_cu *);
1552 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1555 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1556 struct dwarf2_cu *cu);
1558 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1559 struct dwarf2_cu *cu);
1561 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1563 static struct die_info *die_specification (struct die_info *die,
1564 struct dwarf2_cu **);
1566 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1567 struct dwarf2_cu *cu);
1569 static void dwarf_decode_lines (struct line_header *, const char *,
1570 struct dwarf2_cu *, struct partial_symtab *,
1571 CORE_ADDR, int decode_mapping);
1573 static void dwarf2_start_subfile (const char *, const char *);
1575 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1576 const char *, const char *,
1579 static struct symbol *new_symbol (struct die_info *, struct type *,
1580 struct dwarf2_cu *, struct symbol * = NULL);
1582 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1583 struct dwarf2_cu *);
1585 static void dwarf2_const_value_attr (const struct attribute *attr,
1588 struct obstack *obstack,
1589 struct dwarf2_cu *cu, LONGEST *value,
1590 const gdb_byte **bytes,
1591 struct dwarf2_locexpr_baton **baton);
1593 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1595 static int need_gnat_info (struct dwarf2_cu *);
1597 static struct type *die_descriptive_type (struct die_info *,
1598 struct dwarf2_cu *);
1600 static void set_descriptive_type (struct type *, struct die_info *,
1601 struct dwarf2_cu *);
1603 static struct type *die_containing_type (struct die_info *,
1604 struct dwarf2_cu *);
1606 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1607 struct dwarf2_cu *);
1609 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1611 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1613 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1615 static char *typename_concat (struct obstack *obs, const char *prefix,
1616 const char *suffix, int physname,
1617 struct dwarf2_cu *cu);
1619 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1621 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1623 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1625 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1627 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1629 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1631 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1632 struct dwarf2_cu *, struct partial_symtab *);
1634 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1635 values. Keep the items ordered with increasing constraints compliance. */
1638 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1639 PC_BOUNDS_NOT_PRESENT,
1641 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1642 were present but they do not form a valid range of PC addresses. */
1645 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1648 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1652 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1653 CORE_ADDR *, CORE_ADDR *,
1655 struct partial_symtab *);
1657 static void get_scope_pc_bounds (struct die_info *,
1658 CORE_ADDR *, CORE_ADDR *,
1659 struct dwarf2_cu *);
1661 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1662 CORE_ADDR, struct dwarf2_cu *);
1664 static void dwarf2_add_field (struct field_info *, struct die_info *,
1665 struct dwarf2_cu *);
1667 static void dwarf2_attach_fields_to_type (struct field_info *,
1668 struct type *, struct dwarf2_cu *);
1670 static void dwarf2_add_member_fn (struct field_info *,
1671 struct die_info *, struct type *,
1672 struct dwarf2_cu *);
1674 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1676 struct dwarf2_cu *);
1678 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1680 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1682 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1684 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1686 static struct using_direct **using_directives (enum language);
1688 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1690 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1692 static struct type *read_module_type (struct die_info *die,
1693 struct dwarf2_cu *cu);
1695 static const char *namespace_name (struct die_info *die,
1696 int *is_anonymous, struct dwarf2_cu *);
1698 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1700 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1702 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1703 struct dwarf2_cu *);
1705 static struct die_info *read_die_and_siblings_1
1706 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1709 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1710 const gdb_byte *info_ptr,
1711 const gdb_byte **new_info_ptr,
1712 struct die_info *parent);
1714 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1715 struct die_info **, const gdb_byte *,
1718 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1719 struct die_info **, const gdb_byte *,
1722 static void process_die (struct die_info *, struct dwarf2_cu *);
1724 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1727 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1729 static const char *dwarf2_full_name (const char *name,
1730 struct die_info *die,
1731 struct dwarf2_cu *cu);
1733 static const char *dwarf2_physname (const char *name, struct die_info *die,
1734 struct dwarf2_cu *cu);
1736 static struct die_info *dwarf2_extension (struct die_info *die,
1737 struct dwarf2_cu **);
1739 static const char *dwarf_tag_name (unsigned int);
1741 static const char *dwarf_attr_name (unsigned int);
1743 static const char *dwarf_form_name (unsigned int);
1745 static const char *dwarf_bool_name (unsigned int);
1747 static const char *dwarf_type_encoding_name (unsigned int);
1749 static struct die_info *sibling_die (struct die_info *);
1751 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1753 static void dump_die_for_error (struct die_info *);
1755 static void dump_die_1 (struct ui_file *, int level, int max_level,
1758 /*static*/ void dump_die (struct die_info *, int max_level);
1760 static void store_in_ref_table (struct die_info *,
1761 struct dwarf2_cu *);
1763 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1765 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1767 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1768 const struct attribute *,
1769 struct dwarf2_cu **);
1771 static struct die_info *follow_die_ref (struct die_info *,
1772 const struct attribute *,
1773 struct dwarf2_cu **);
1775 static struct die_info *follow_die_sig (struct die_info *,
1776 const struct attribute *,
1777 struct dwarf2_cu **);
1779 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1780 struct dwarf2_cu *);
1782 static struct type *get_DW_AT_signature_type (struct die_info *,
1783 const struct attribute *,
1784 struct dwarf2_cu *);
1786 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1788 static void read_signatured_type (struct signatured_type *);
1790 static int attr_to_dynamic_prop (const struct attribute *attr,
1791 struct die_info *die, struct dwarf2_cu *cu,
1792 struct dynamic_prop *prop);
1794 /* memory allocation interface */
1796 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1798 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1800 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1802 static int attr_form_is_block (const struct attribute *);
1804 static int attr_form_is_section_offset (const struct attribute *);
1806 static int attr_form_is_constant (const struct attribute *);
1808 static int attr_form_is_ref (const struct attribute *);
1810 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1811 struct dwarf2_loclist_baton *baton,
1812 const struct attribute *attr);
1814 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1816 struct dwarf2_cu *cu,
1819 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1820 const gdb_byte *info_ptr,
1821 struct abbrev_info *abbrev);
1823 static hashval_t partial_die_hash (const void *item);
1825 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1827 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1828 (sect_offset sect_off, unsigned int offset_in_dwz,
1829 struct dwarf2_per_objfile *dwarf2_per_objfile);
1831 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1832 struct die_info *comp_unit_die,
1833 enum language pretend_language);
1835 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1837 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1839 static struct type *set_die_type (struct die_info *, struct type *,
1840 struct dwarf2_cu *);
1842 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1844 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1846 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1849 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1852 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1855 static void dwarf2_add_dependence (struct dwarf2_cu *,
1856 struct dwarf2_per_cu_data *);
1858 static void dwarf2_mark (struct dwarf2_cu *);
1860 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1862 static struct type *get_die_type_at_offset (sect_offset,
1863 struct dwarf2_per_cu_data *);
1865 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1867 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1868 enum language pretend_language);
1870 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1872 /* Class, the destructor of which frees all allocated queue entries. This
1873 will only have work to do if an error was thrown while processing the
1874 dwarf. If no error was thrown then the queue entries should have all
1875 been processed, and freed, as we went along. */
1877 class dwarf2_queue_guard
1880 dwarf2_queue_guard () = default;
1882 /* Free any entries remaining on the queue. There should only be
1883 entries left if we hit an error while processing the dwarf. */
1884 ~dwarf2_queue_guard ()
1886 struct dwarf2_queue_item *item, *last;
1888 item = dwarf2_queue;
1891 /* Anything still marked queued is likely to be in an
1892 inconsistent state, so discard it. */
1893 if (item->per_cu->queued)
1895 if (item->per_cu->cu != NULL)
1896 free_one_cached_comp_unit (item->per_cu);
1897 item->per_cu->queued = 0;
1905 dwarf2_queue = dwarf2_queue_tail = NULL;
1909 /* The return type of find_file_and_directory. Note, the enclosed
1910 string pointers are only valid while this object is valid. */
1912 struct file_and_directory
1914 /* The filename. This is never NULL. */
1917 /* The compilation directory. NULL if not known. If we needed to
1918 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1919 points directly to the DW_AT_comp_dir string attribute owned by
1920 the obstack that owns the DIE. */
1921 const char *comp_dir;
1923 /* If we needed to build a new string for comp_dir, this is what
1924 owns the storage. */
1925 std::string comp_dir_storage;
1928 static file_and_directory find_file_and_directory (struct die_info *die,
1929 struct dwarf2_cu *cu);
1931 static char *file_full_name (int file, struct line_header *lh,
1932 const char *comp_dir);
1934 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1935 enum class rcuh_kind { COMPILE, TYPE };
1937 static const gdb_byte *read_and_check_comp_unit_head
1938 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1939 struct comp_unit_head *header,
1940 struct dwarf2_section_info *section,
1941 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1942 rcuh_kind section_kind);
1944 static void init_cutu_and_read_dies
1945 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1946 int use_existing_cu, int keep, bool skip_partial,
1947 die_reader_func_ftype *die_reader_func, void *data);
1949 static void init_cutu_and_read_dies_simple
1950 (struct dwarf2_per_cu_data *this_cu,
1951 die_reader_func_ftype *die_reader_func, void *data);
1953 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1955 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1957 static struct dwo_unit *lookup_dwo_unit_in_dwp
1958 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1959 struct dwp_file *dwp_file, const char *comp_dir,
1960 ULONGEST signature, int is_debug_types);
1962 static struct dwp_file *get_dwp_file
1963 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1965 static struct dwo_unit *lookup_dwo_comp_unit
1966 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1968 static struct dwo_unit *lookup_dwo_type_unit
1969 (struct signatured_type *, const char *, const char *);
1971 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1973 static void free_dwo_file (struct dwo_file *);
1975 /* A unique_ptr helper to free a dwo_file. */
1977 struct dwo_file_deleter
1979 void operator() (struct dwo_file *df) const
1985 /* A unique pointer to a dwo_file. */
1987 typedef std::unique_ptr<struct dwo_file, dwo_file_deleter> dwo_file_up;
1989 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1991 static void check_producer (struct dwarf2_cu *cu);
1993 static void free_line_header_voidp (void *arg);
1995 /* Various complaints about symbol reading that don't abort the process. */
1998 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2000 complaint (_("statement list doesn't fit in .debug_line section"));
2004 dwarf2_debug_line_missing_file_complaint (void)
2006 complaint (_(".debug_line section has line data without a file"));
2010 dwarf2_debug_line_missing_end_sequence_complaint (void)
2012 complaint (_(".debug_line section has line "
2013 "program sequence without an end"));
2017 dwarf2_complex_location_expr_complaint (void)
2019 complaint (_("location expression too complex"));
2023 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2026 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
2031 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2033 complaint (_("debug info runs off end of %s section"
2035 get_section_name (section),
2036 get_section_file_name (section));
2040 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2042 complaint (_("macro debug info contains a "
2043 "malformed macro definition:\n`%s'"),
2048 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2050 complaint (_("invalid attribute class or form for '%s' in '%s'"),
2054 /* Hash function for line_header_hash. */
2057 line_header_hash (const struct line_header *ofs)
2059 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2062 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2065 line_header_hash_voidp (const void *item)
2067 const struct line_header *ofs = (const struct line_header *) item;
2069 return line_header_hash (ofs);
2072 /* Equality function for line_header_hash. */
2075 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2077 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2078 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2080 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2081 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2086 /* Read the given attribute value as an address, taking the attribute's
2087 form into account. */
2090 attr_value_as_address (struct attribute *attr)
2094 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2096 /* Aside from a few clearly defined exceptions, attributes that
2097 contain an address must always be in DW_FORM_addr form.
2098 Unfortunately, some compilers happen to be violating this
2099 requirement by encoding addresses using other forms, such
2100 as DW_FORM_data4 for example. For those broken compilers,
2101 we try to do our best, without any guarantee of success,
2102 to interpret the address correctly. It would also be nice
2103 to generate a complaint, but that would require us to maintain
2104 a list of legitimate cases where a non-address form is allowed,
2105 as well as update callers to pass in at least the CU's DWARF
2106 version. This is more overhead than what we're willing to
2107 expand for a pretty rare case. */
2108 addr = DW_UNSND (attr);
2111 addr = DW_ADDR (attr);
2116 /* See declaration. */
2118 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2119 const dwarf2_debug_sections *names)
2120 : objfile (objfile_)
2123 names = &dwarf2_elf_names;
2125 bfd *obfd = objfile->obfd;
2127 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2128 locate_sections (obfd, sec, *names);
2131 static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2133 dwarf2_per_objfile::~dwarf2_per_objfile ()
2135 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2136 free_cached_comp_units ();
2138 if (quick_file_names_table)
2139 htab_delete (quick_file_names_table);
2141 if (line_header_hash)
2142 htab_delete (line_header_hash);
2144 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2145 VEC_free (dwarf2_per_cu_ptr, per_cu->imported_symtabs);
2147 for (signatured_type *sig_type : all_type_units)
2148 VEC_free (dwarf2_per_cu_ptr, sig_type->per_cu.imported_symtabs);
2150 VEC_free (dwarf2_section_info_def, types);
2152 if (dwo_files != NULL)
2153 free_dwo_files (dwo_files, objfile);
2155 /* Everything else should be on the objfile obstack. */
2158 /* See declaration. */
2161 dwarf2_per_objfile::free_cached_comp_units ()
2163 dwarf2_per_cu_data *per_cu = read_in_chain;
2164 dwarf2_per_cu_data **last_chain = &read_in_chain;
2165 while (per_cu != NULL)
2167 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2170 *last_chain = next_cu;
2175 /* A helper class that calls free_cached_comp_units on
2178 class free_cached_comp_units
2182 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2183 : m_per_objfile (per_objfile)
2187 ~free_cached_comp_units ()
2189 m_per_objfile->free_cached_comp_units ();
2192 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2196 dwarf2_per_objfile *m_per_objfile;
2199 /* Try to locate the sections we need for DWARF 2 debugging
2200 information and return true if we have enough to do something.
2201 NAMES points to the dwarf2 section names, or is NULL if the standard
2202 ELF names are used. */
2205 dwarf2_has_info (struct objfile *objfile,
2206 const struct dwarf2_debug_sections *names)
2208 if (objfile->flags & OBJF_READNEVER)
2211 struct dwarf2_per_objfile *dwarf2_per_objfile
2212 = get_dwarf2_per_objfile (objfile);
2214 if (dwarf2_per_objfile == NULL)
2216 /* Initialize per-objfile state. */
2218 = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
2220 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
2222 return (!dwarf2_per_objfile->info.is_virtual
2223 && dwarf2_per_objfile->info.s.section != NULL
2224 && !dwarf2_per_objfile->abbrev.is_virtual
2225 && dwarf2_per_objfile->abbrev.s.section != NULL);
2228 /* Return the containing section of virtual section SECTION. */
2230 static struct dwarf2_section_info *
2231 get_containing_section (const struct dwarf2_section_info *section)
2233 gdb_assert (section->is_virtual);
2234 return section->s.containing_section;
2237 /* Return the bfd owner of SECTION. */
2240 get_section_bfd_owner (const struct dwarf2_section_info *section)
2242 if (section->is_virtual)
2244 section = get_containing_section (section);
2245 gdb_assert (!section->is_virtual);
2247 return section->s.section->owner;
2250 /* Return the bfd section of SECTION.
2251 Returns NULL if the section is not present. */
2254 get_section_bfd_section (const struct dwarf2_section_info *section)
2256 if (section->is_virtual)
2258 section = get_containing_section (section);
2259 gdb_assert (!section->is_virtual);
2261 return section->s.section;
2264 /* Return the name of SECTION. */
2267 get_section_name (const struct dwarf2_section_info *section)
2269 asection *sectp = get_section_bfd_section (section);
2271 gdb_assert (sectp != NULL);
2272 return bfd_section_name (get_section_bfd_owner (section), sectp);
2275 /* Return the name of the file SECTION is in. */
2278 get_section_file_name (const struct dwarf2_section_info *section)
2280 bfd *abfd = get_section_bfd_owner (section);
2282 return bfd_get_filename (abfd);
2285 /* Return the id of SECTION.
2286 Returns 0 if SECTION doesn't exist. */
2289 get_section_id (const struct dwarf2_section_info *section)
2291 asection *sectp = get_section_bfd_section (section);
2298 /* Return the flags of SECTION.
2299 SECTION (or containing section if this is a virtual section) must exist. */
2302 get_section_flags (const struct dwarf2_section_info *section)
2304 asection *sectp = get_section_bfd_section (section);
2306 gdb_assert (sectp != NULL);
2307 return bfd_get_section_flags (sectp->owner, sectp);
2310 /* When loading sections, we look either for uncompressed section or for
2311 compressed section names. */
2314 section_is_p (const char *section_name,
2315 const struct dwarf2_section_names *names)
2317 if (names->normal != NULL
2318 && strcmp (section_name, names->normal) == 0)
2320 if (names->compressed != NULL
2321 && strcmp (section_name, names->compressed) == 0)
2326 /* See declaration. */
2329 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2330 const dwarf2_debug_sections &names)
2332 flagword aflag = bfd_get_section_flags (abfd, sectp);
2334 if ((aflag & SEC_HAS_CONTENTS) == 0)
2337 else if (section_is_p (sectp->name, &names.info))
2339 this->info.s.section = sectp;
2340 this->info.size = bfd_get_section_size (sectp);
2342 else if (section_is_p (sectp->name, &names.abbrev))
2344 this->abbrev.s.section = sectp;
2345 this->abbrev.size = bfd_get_section_size (sectp);
2347 else if (section_is_p (sectp->name, &names.line))
2349 this->line.s.section = sectp;
2350 this->line.size = bfd_get_section_size (sectp);
2352 else if (section_is_p (sectp->name, &names.loc))
2354 this->loc.s.section = sectp;
2355 this->loc.size = bfd_get_section_size (sectp);
2357 else if (section_is_p (sectp->name, &names.loclists))
2359 this->loclists.s.section = sectp;
2360 this->loclists.size = bfd_get_section_size (sectp);
2362 else if (section_is_p (sectp->name, &names.macinfo))
2364 this->macinfo.s.section = sectp;
2365 this->macinfo.size = bfd_get_section_size (sectp);
2367 else if (section_is_p (sectp->name, &names.macro))
2369 this->macro.s.section = sectp;
2370 this->macro.size = bfd_get_section_size (sectp);
2372 else if (section_is_p (sectp->name, &names.str))
2374 this->str.s.section = sectp;
2375 this->str.size = bfd_get_section_size (sectp);
2377 else if (section_is_p (sectp->name, &names.line_str))
2379 this->line_str.s.section = sectp;
2380 this->line_str.size = bfd_get_section_size (sectp);
2382 else if (section_is_p (sectp->name, &names.addr))
2384 this->addr.s.section = sectp;
2385 this->addr.size = bfd_get_section_size (sectp);
2387 else if (section_is_p (sectp->name, &names.frame))
2389 this->frame.s.section = sectp;
2390 this->frame.size = bfd_get_section_size (sectp);
2392 else if (section_is_p (sectp->name, &names.eh_frame))
2394 this->eh_frame.s.section = sectp;
2395 this->eh_frame.size = bfd_get_section_size (sectp);
2397 else if (section_is_p (sectp->name, &names.ranges))
2399 this->ranges.s.section = sectp;
2400 this->ranges.size = bfd_get_section_size (sectp);
2402 else if (section_is_p (sectp->name, &names.rnglists))
2404 this->rnglists.s.section = sectp;
2405 this->rnglists.size = bfd_get_section_size (sectp);
2407 else if (section_is_p (sectp->name, &names.types))
2409 struct dwarf2_section_info type_section;
2411 memset (&type_section, 0, sizeof (type_section));
2412 type_section.s.section = sectp;
2413 type_section.size = bfd_get_section_size (sectp);
2415 VEC_safe_push (dwarf2_section_info_def, this->types,
2418 else if (section_is_p (sectp->name, &names.gdb_index))
2420 this->gdb_index.s.section = sectp;
2421 this->gdb_index.size = bfd_get_section_size (sectp);
2423 else if (section_is_p (sectp->name, &names.debug_names))
2425 this->debug_names.s.section = sectp;
2426 this->debug_names.size = bfd_get_section_size (sectp);
2428 else if (section_is_p (sectp->name, &names.debug_aranges))
2430 this->debug_aranges.s.section = sectp;
2431 this->debug_aranges.size = bfd_get_section_size (sectp);
2434 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2435 && bfd_section_vma (abfd, sectp) == 0)
2436 this->has_section_at_zero = true;
2439 /* A helper function that decides whether a section is empty,
2443 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2445 if (section->is_virtual)
2446 return section->size == 0;
2447 return section->s.section == NULL || section->size == 0;
2450 /* See dwarf2read.h. */
2453 dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
2457 gdb_byte *buf, *retbuf;
2461 info->buffer = NULL;
2464 if (dwarf2_section_empty_p (info))
2467 sectp = get_section_bfd_section (info);
2469 /* If this is a virtual section we need to read in the real one first. */
2470 if (info->is_virtual)
2472 struct dwarf2_section_info *containing_section =
2473 get_containing_section (info);
2475 gdb_assert (sectp != NULL);
2476 if ((sectp->flags & SEC_RELOC) != 0)
2478 error (_("Dwarf Error: DWP format V2 with relocations is not"
2479 " supported in section %s [in module %s]"),
2480 get_section_name (info), get_section_file_name (info));
2482 dwarf2_read_section (objfile, containing_section);
2483 /* Other code should have already caught virtual sections that don't
2485 gdb_assert (info->virtual_offset + info->size
2486 <= containing_section->size);
2487 /* If the real section is empty or there was a problem reading the
2488 section we shouldn't get here. */
2489 gdb_assert (containing_section->buffer != NULL);
2490 info->buffer = containing_section->buffer + info->virtual_offset;
2494 /* If the section has relocations, we must read it ourselves.
2495 Otherwise we attach it to the BFD. */
2496 if ((sectp->flags & SEC_RELOC) == 0)
2498 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2502 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2505 /* When debugging .o files, we may need to apply relocations; see
2506 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2507 We never compress sections in .o files, so we only need to
2508 try this when the section is not compressed. */
2509 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2512 info->buffer = retbuf;
2516 abfd = get_section_bfd_owner (info);
2517 gdb_assert (abfd != NULL);
2519 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2520 || bfd_bread (buf, info->size, abfd) != info->size)
2522 error (_("Dwarf Error: Can't read DWARF data"
2523 " in section %s [in module %s]"),
2524 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2528 /* A helper function that returns the size of a section in a safe way.
2529 If you are positive that the section has been read before using the
2530 size, then it is safe to refer to the dwarf2_section_info object's
2531 "size" field directly. In other cases, you must call this
2532 function, because for compressed sections the size field is not set
2533 correctly until the section has been read. */
2535 static bfd_size_type
2536 dwarf2_section_size (struct objfile *objfile,
2537 struct dwarf2_section_info *info)
2540 dwarf2_read_section (objfile, info);
2544 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2548 dwarf2_get_section_info (struct objfile *objfile,
2549 enum dwarf2_section_enum sect,
2550 asection **sectp, const gdb_byte **bufp,
2551 bfd_size_type *sizep)
2553 struct dwarf2_per_objfile *data
2554 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2555 dwarf2_objfile_data_key);
2556 struct dwarf2_section_info *info;
2558 /* We may see an objfile without any DWARF, in which case we just
2569 case DWARF2_DEBUG_FRAME:
2570 info = &data->frame;
2572 case DWARF2_EH_FRAME:
2573 info = &data->eh_frame;
2576 gdb_assert_not_reached ("unexpected section");
2579 dwarf2_read_section (objfile, info);
2581 *sectp = get_section_bfd_section (info);
2582 *bufp = info->buffer;
2583 *sizep = info->size;
2586 /* A helper function to find the sections for a .dwz file. */
2589 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2591 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2593 /* Note that we only support the standard ELF names, because .dwz
2594 is ELF-only (at the time of writing). */
2595 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2597 dwz_file->abbrev.s.section = sectp;
2598 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2600 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2602 dwz_file->info.s.section = sectp;
2603 dwz_file->info.size = bfd_get_section_size (sectp);
2605 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2607 dwz_file->str.s.section = sectp;
2608 dwz_file->str.size = bfd_get_section_size (sectp);
2610 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2612 dwz_file->line.s.section = sectp;
2613 dwz_file->line.size = bfd_get_section_size (sectp);
2615 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2617 dwz_file->macro.s.section = sectp;
2618 dwz_file->macro.size = bfd_get_section_size (sectp);
2620 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2622 dwz_file->gdb_index.s.section = sectp;
2623 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2625 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2627 dwz_file->debug_names.s.section = sectp;
2628 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2632 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2633 there is no .gnu_debugaltlink section in the file. Error if there
2634 is such a section but the file cannot be found. */
2636 static struct dwz_file *
2637 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2639 const char *filename;
2640 bfd_size_type buildid_len_arg;
2644 if (dwarf2_per_objfile->dwz_file != NULL)
2645 return dwarf2_per_objfile->dwz_file.get ();
2647 bfd_set_error (bfd_error_no_error);
2648 gdb::unique_xmalloc_ptr<char> data
2649 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2650 &buildid_len_arg, &buildid));
2653 if (bfd_get_error () == bfd_error_no_error)
2655 error (_("could not read '.gnu_debugaltlink' section: %s"),
2656 bfd_errmsg (bfd_get_error ()));
2659 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2661 buildid_len = (size_t) buildid_len_arg;
2663 filename = data.get ();
2665 std::string abs_storage;
2666 if (!IS_ABSOLUTE_PATH (filename))
2668 gdb::unique_xmalloc_ptr<char> abs
2669 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2671 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2672 filename = abs_storage.c_str ();
2675 /* First try the file name given in the section. If that doesn't
2676 work, try to use the build-id instead. */
2677 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2678 if (dwz_bfd != NULL)
2680 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2684 if (dwz_bfd == NULL)
2685 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2687 if (dwz_bfd == NULL)
2688 error (_("could not find '.gnu_debugaltlink' file for %s"),
2689 objfile_name (dwarf2_per_objfile->objfile));
2691 std::unique_ptr<struct dwz_file> result
2692 (new struct dwz_file (std::move (dwz_bfd)));
2694 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2697 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2698 result->dwz_bfd.get ());
2699 dwarf2_per_objfile->dwz_file = std::move (result);
2700 return dwarf2_per_objfile->dwz_file.get ();
2703 /* DWARF quick_symbols_functions support. */
2705 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2706 unique line tables, so we maintain a separate table of all .debug_line
2707 derived entries to support the sharing.
2708 All the quick functions need is the list of file names. We discard the
2709 line_header when we're done and don't need to record it here. */
2710 struct quick_file_names
2712 /* The data used to construct the hash key. */
2713 struct stmt_list_hash hash;
2715 /* The number of entries in file_names, real_names. */
2716 unsigned int num_file_names;
2718 /* The file names from the line table, after being run through
2720 const char **file_names;
2722 /* The file names from the line table after being run through
2723 gdb_realpath. These are computed lazily. */
2724 const char **real_names;
2727 /* When using the index (and thus not using psymtabs), each CU has an
2728 object of this type. This is used to hold information needed by
2729 the various "quick" methods. */
2730 struct dwarf2_per_cu_quick_data
2732 /* The file table. This can be NULL if there was no file table
2733 or it's currently not read in.
2734 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2735 struct quick_file_names *file_names;
2737 /* The corresponding symbol table. This is NULL if symbols for this
2738 CU have not yet been read. */
2739 struct compunit_symtab *compunit_symtab;
2741 /* A temporary mark bit used when iterating over all CUs in
2742 expand_symtabs_matching. */
2743 unsigned int mark : 1;
2745 /* True if we've tried to read the file table and found there isn't one.
2746 There will be no point in trying to read it again next time. */
2747 unsigned int no_file_data : 1;
2750 /* Utility hash function for a stmt_list_hash. */
2753 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2757 if (stmt_list_hash->dwo_unit != NULL)
2758 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2759 v += to_underlying (stmt_list_hash->line_sect_off);
2763 /* Utility equality function for a stmt_list_hash. */
2766 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2767 const struct stmt_list_hash *rhs)
2769 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2771 if (lhs->dwo_unit != NULL
2772 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2775 return lhs->line_sect_off == rhs->line_sect_off;
2778 /* Hash function for a quick_file_names. */
2781 hash_file_name_entry (const void *e)
2783 const struct quick_file_names *file_data
2784 = (const struct quick_file_names *) e;
2786 return hash_stmt_list_entry (&file_data->hash);
2789 /* Equality function for a quick_file_names. */
2792 eq_file_name_entry (const void *a, const void *b)
2794 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2795 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2797 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2800 /* Delete function for a quick_file_names. */
2803 delete_file_name_entry (void *e)
2805 struct quick_file_names *file_data = (struct quick_file_names *) e;
2808 for (i = 0; i < file_data->num_file_names; ++i)
2810 xfree ((void*) file_data->file_names[i]);
2811 if (file_data->real_names)
2812 xfree ((void*) file_data->real_names[i]);
2815 /* The space for the struct itself lives on objfile_obstack,
2816 so we don't free it here. */
2819 /* Create a quick_file_names hash table. */
2822 create_quick_file_names_table (unsigned int nr_initial_entries)
2824 return htab_create_alloc (nr_initial_entries,
2825 hash_file_name_entry, eq_file_name_entry,
2826 delete_file_name_entry, xcalloc, xfree);
2829 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2830 have to be created afterwards. You should call age_cached_comp_units after
2831 processing PER_CU->CU. dw2_setup must have been already called. */
2834 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2836 if (per_cu->is_debug_types)
2837 load_full_type_unit (per_cu);
2839 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2841 if (per_cu->cu == NULL)
2842 return; /* Dummy CU. */
2844 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2847 /* Read in the symbols for PER_CU. */
2850 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2852 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2854 /* Skip type_unit_groups, reading the type units they contain
2855 is handled elsewhere. */
2856 if (IS_TYPE_UNIT_GROUP (per_cu))
2859 /* The destructor of dwarf2_queue_guard frees any entries left on
2860 the queue. After this point we're guaranteed to leave this function
2861 with the dwarf queue empty. */
2862 dwarf2_queue_guard q_guard;
2864 if (dwarf2_per_objfile->using_index
2865 ? per_cu->v.quick->compunit_symtab == NULL
2866 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2868 queue_comp_unit (per_cu, language_minimal);
2869 load_cu (per_cu, skip_partial);
2871 /* If we just loaded a CU from a DWO, and we're working with an index
2872 that may badly handle TUs, load all the TUs in that DWO as well.
2873 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2874 if (!per_cu->is_debug_types
2875 && per_cu->cu != NULL
2876 && per_cu->cu->dwo_unit != NULL
2877 && dwarf2_per_objfile->index_table != NULL
2878 && dwarf2_per_objfile->index_table->version <= 7
2879 /* DWP files aren't supported yet. */
2880 && get_dwp_file (dwarf2_per_objfile) == NULL)
2881 queue_and_load_all_dwo_tus (per_cu);
2884 process_queue (dwarf2_per_objfile);
2886 /* Age the cache, releasing compilation units that have not
2887 been used recently. */
2888 age_cached_comp_units (dwarf2_per_objfile);
2891 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2892 the objfile from which this CU came. Returns the resulting symbol
2895 static struct compunit_symtab *
2896 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2898 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2900 gdb_assert (dwarf2_per_objfile->using_index);
2901 if (!per_cu->v.quick->compunit_symtab)
2903 free_cached_comp_units freer (dwarf2_per_objfile);
2904 scoped_restore decrementer = increment_reading_symtab ();
2905 dw2_do_instantiate_symtab (per_cu, skip_partial);
2906 process_cu_includes (dwarf2_per_objfile);
2909 return per_cu->v.quick->compunit_symtab;
2912 /* See declaration. */
2914 dwarf2_per_cu_data *
2915 dwarf2_per_objfile::get_cutu (int index)
2917 if (index >= this->all_comp_units.size ())
2919 index -= this->all_comp_units.size ();
2920 gdb_assert (index < this->all_type_units.size ());
2921 return &this->all_type_units[index]->per_cu;
2924 return this->all_comp_units[index];
2927 /* See declaration. */
2929 dwarf2_per_cu_data *
2930 dwarf2_per_objfile::get_cu (int index)
2932 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2934 return this->all_comp_units[index];
2937 /* See declaration. */
2940 dwarf2_per_objfile::get_tu (int index)
2942 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2944 return this->all_type_units[index];
2947 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2948 objfile_obstack, and constructed with the specified field
2951 static dwarf2_per_cu_data *
2952 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2953 struct dwarf2_section_info *section,
2955 sect_offset sect_off, ULONGEST length)
2957 struct objfile *objfile = dwarf2_per_objfile->objfile;
2958 dwarf2_per_cu_data *the_cu
2959 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2960 struct dwarf2_per_cu_data);
2961 the_cu->sect_off = sect_off;
2962 the_cu->length = length;
2963 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2964 the_cu->section = section;
2965 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2966 struct dwarf2_per_cu_quick_data);
2967 the_cu->is_dwz = is_dwz;
2971 /* A helper for create_cus_from_index that handles a given list of
2975 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2976 const gdb_byte *cu_list, offset_type n_elements,
2977 struct dwarf2_section_info *section,
2980 for (offset_type i = 0; i < n_elements; i += 2)
2982 gdb_static_assert (sizeof (ULONGEST) >= 8);
2984 sect_offset sect_off
2985 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2986 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2989 dwarf2_per_cu_data *per_cu
2990 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2992 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2996 /* Read the CU list from the mapped index, and use it to create all
2997 the CU objects for this objfile. */
3000 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3001 const gdb_byte *cu_list, offset_type cu_list_elements,
3002 const gdb_byte *dwz_list, offset_type dwz_elements)
3004 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3005 dwarf2_per_objfile->all_comp_units.reserve
3006 ((cu_list_elements + dwz_elements) / 2);
3008 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
3009 &dwarf2_per_objfile->info, 0);
3011 if (dwz_elements == 0)
3014 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3015 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
3019 /* Create the signatured type hash table from the index. */
3022 create_signatured_type_table_from_index
3023 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3024 struct dwarf2_section_info *section,
3025 const gdb_byte *bytes,
3026 offset_type elements)
3028 struct objfile *objfile = dwarf2_per_objfile->objfile;
3030 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3031 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
3033 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3035 for (offset_type i = 0; i < elements; i += 3)
3037 struct signatured_type *sig_type;
3040 cu_offset type_offset_in_tu;
3042 gdb_static_assert (sizeof (ULONGEST) >= 8);
3043 sect_offset sect_off
3044 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3046 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3048 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3051 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3052 struct signatured_type);
3053 sig_type->signature = signature;
3054 sig_type->type_offset_in_tu = type_offset_in_tu;
3055 sig_type->per_cu.is_debug_types = 1;
3056 sig_type->per_cu.section = section;
3057 sig_type->per_cu.sect_off = sect_off;
3058 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3059 sig_type->per_cu.v.quick
3060 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3061 struct dwarf2_per_cu_quick_data);
3063 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3066 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3069 dwarf2_per_objfile->signatured_types = sig_types_hash;
3072 /* Create the signatured type hash table from .debug_names. */
3075 create_signatured_type_table_from_debug_names
3076 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3077 const mapped_debug_names &map,
3078 struct dwarf2_section_info *section,
3079 struct dwarf2_section_info *abbrev_section)
3081 struct objfile *objfile = dwarf2_per_objfile->objfile;
3083 dwarf2_read_section (objfile, section);
3084 dwarf2_read_section (objfile, abbrev_section);
3086 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3087 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
3089 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3091 for (uint32_t i = 0; i < map.tu_count; ++i)
3093 struct signatured_type *sig_type;
3096 sect_offset sect_off
3097 = (sect_offset) (extract_unsigned_integer
3098 (map.tu_table_reordered + i * map.offset_size,
3100 map.dwarf5_byte_order));
3102 comp_unit_head cu_header;
3103 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3105 section->buffer + to_underlying (sect_off),
3108 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3109 struct signatured_type);
3110 sig_type->signature = cu_header.signature;
3111 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3112 sig_type->per_cu.is_debug_types = 1;
3113 sig_type->per_cu.section = section;
3114 sig_type->per_cu.sect_off = sect_off;
3115 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3116 sig_type->per_cu.v.quick
3117 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3118 struct dwarf2_per_cu_quick_data);
3120 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3123 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3126 dwarf2_per_objfile->signatured_types = sig_types_hash;
3129 /* Read the address map data from the mapped index, and use it to
3130 populate the objfile's psymtabs_addrmap. */
3133 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3134 struct mapped_index *index)
3136 struct objfile *objfile = dwarf2_per_objfile->objfile;
3137 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3138 const gdb_byte *iter, *end;
3139 struct addrmap *mutable_map;
3142 auto_obstack temp_obstack;
3144 mutable_map = addrmap_create_mutable (&temp_obstack);
3146 iter = index->address_table.data ();
3147 end = iter + index->address_table.size ();
3149 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3153 ULONGEST hi, lo, cu_index;
3154 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3156 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3158 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3163 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
3164 hex_string (lo), hex_string (hi));
3168 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
3170 complaint (_(".gdb_index address table has invalid CU number %u"),
3171 (unsigned) cu_index);
3175 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3176 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3177 addrmap_set_empty (mutable_map, lo, hi - 1,
3178 dwarf2_per_objfile->get_cu (cu_index));
3181 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3182 &objfile->objfile_obstack);
3185 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3186 populate the objfile's psymtabs_addrmap. */
3189 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3190 struct dwarf2_section_info *section)
3192 struct objfile *objfile = dwarf2_per_objfile->objfile;
3193 bfd *abfd = objfile->obfd;
3194 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3195 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3196 SECT_OFF_TEXT (objfile));
3198 auto_obstack temp_obstack;
3199 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3201 std::unordered_map<sect_offset,
3202 dwarf2_per_cu_data *,
3203 gdb::hash_enum<sect_offset>>
3204 debug_info_offset_to_per_cu;
3205 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3207 const auto insertpair
3208 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3209 if (!insertpair.second)
3211 warning (_("Section .debug_aranges in %s has duplicate "
3212 "debug_info_offset %s, ignoring .debug_aranges."),
3213 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3218 dwarf2_read_section (objfile, section);
3220 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3222 const gdb_byte *addr = section->buffer;
3224 while (addr < section->buffer + section->size)
3226 const gdb_byte *const entry_addr = addr;
3227 unsigned int bytes_read;
3229 const LONGEST entry_length = read_initial_length (abfd, addr,
3233 const gdb_byte *const entry_end = addr + entry_length;
3234 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3235 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3236 if (addr + entry_length > section->buffer + section->size)
3238 warning (_("Section .debug_aranges in %s entry at offset %zu "
3239 "length %s exceeds section length %s, "
3240 "ignoring .debug_aranges."),
3241 objfile_name (objfile), entry_addr - section->buffer,
3242 plongest (bytes_read + entry_length),
3243 pulongest (section->size));
3247 /* The version number. */
3248 const uint16_t version = read_2_bytes (abfd, addr);
3252 warning (_("Section .debug_aranges in %s entry at offset %zu "
3253 "has unsupported version %d, ignoring .debug_aranges."),
3254 objfile_name (objfile), entry_addr - section->buffer,
3259 const uint64_t debug_info_offset
3260 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3261 addr += offset_size;
3262 const auto per_cu_it
3263 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3264 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3266 warning (_("Section .debug_aranges in %s entry at offset %zu "
3267 "debug_info_offset %s does not exists, "
3268 "ignoring .debug_aranges."),
3269 objfile_name (objfile), entry_addr - section->buffer,
3270 pulongest (debug_info_offset));
3273 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3275 const uint8_t address_size = *addr++;
3276 if (address_size < 1 || address_size > 8)
3278 warning (_("Section .debug_aranges in %s entry at offset %zu "
3279 "address_size %u is invalid, ignoring .debug_aranges."),
3280 objfile_name (objfile), entry_addr - section->buffer,
3285 const uint8_t segment_selector_size = *addr++;
3286 if (segment_selector_size != 0)
3288 warning (_("Section .debug_aranges in %s entry at offset %zu "
3289 "segment_selector_size %u is not supported, "
3290 "ignoring .debug_aranges."),
3291 objfile_name (objfile), entry_addr - section->buffer,
3292 segment_selector_size);
3296 /* Must pad to an alignment boundary that is twice the address
3297 size. It is undocumented by the DWARF standard but GCC does
3299 for (size_t padding = ((-(addr - section->buffer))
3300 & (2 * address_size - 1));
3301 padding > 0; padding--)
3304 warning (_("Section .debug_aranges in %s entry at offset %zu "
3305 "padding is not zero, ignoring .debug_aranges."),
3306 objfile_name (objfile), entry_addr - section->buffer);
3312 if (addr + 2 * address_size > entry_end)
3314 warning (_("Section .debug_aranges in %s entry at offset %zu "
3315 "address list is not properly terminated, "
3316 "ignoring .debug_aranges."),
3317 objfile_name (objfile), entry_addr - section->buffer);
3320 ULONGEST start = extract_unsigned_integer (addr, address_size,
3322 addr += address_size;
3323 ULONGEST length = extract_unsigned_integer (addr, address_size,
3325 addr += address_size;
3326 if (start == 0 && length == 0)
3328 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3330 /* Symbol was eliminated due to a COMDAT group. */
3333 ULONGEST end = start + length;
3334 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3335 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3336 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3340 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3341 &objfile->objfile_obstack);
3344 /* Find a slot in the mapped index INDEX for the object named NAME.
3345 If NAME is found, set *VEC_OUT to point to the CU vector in the
3346 constant pool and return true. If NAME cannot be found, return
3350 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3351 offset_type **vec_out)
3354 offset_type slot, step;
3355 int (*cmp) (const char *, const char *);
3357 gdb::unique_xmalloc_ptr<char> without_params;
3358 if (current_language->la_language == language_cplus
3359 || current_language->la_language == language_fortran
3360 || current_language->la_language == language_d)
3362 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3365 if (strchr (name, '(') != NULL)
3367 without_params = cp_remove_params (name);
3369 if (without_params != NULL)
3370 name = without_params.get ();
3374 /* Index version 4 did not support case insensitive searches. But the
3375 indices for case insensitive languages are built in lowercase, therefore
3376 simulate our NAME being searched is also lowercased. */
3377 hash = mapped_index_string_hash ((index->version == 4
3378 && case_sensitivity == case_sensitive_off
3379 ? 5 : index->version),
3382 slot = hash & (index->symbol_table.size () - 1);
3383 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3384 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3390 const auto &bucket = index->symbol_table[slot];
3391 if (bucket.name == 0 && bucket.vec == 0)
3394 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3395 if (!cmp (name, str))
3397 *vec_out = (offset_type *) (index->constant_pool
3398 + MAYBE_SWAP (bucket.vec));
3402 slot = (slot + step) & (index->symbol_table.size () - 1);
3406 /* A helper function that reads the .gdb_index from SECTION and fills
3407 in MAP. FILENAME is the name of the file containing the section;
3408 it is used for error reporting. DEPRECATED_OK is true if it is
3409 ok to use deprecated sections.
3411 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3412 out parameters that are filled in with information about the CU and
3413 TU lists in the section.
3415 Returns 1 if all went well, 0 otherwise. */
3418 read_gdb_index_from_section (struct objfile *objfile,
3419 const char *filename,
3421 struct dwarf2_section_info *section,
3422 struct mapped_index *map,
3423 const gdb_byte **cu_list,
3424 offset_type *cu_list_elements,
3425 const gdb_byte **types_list,
3426 offset_type *types_list_elements)
3428 const gdb_byte *addr;
3429 offset_type version;
3430 offset_type *metadata;
3433 if (dwarf2_section_empty_p (section))
3436 /* Older elfutils strip versions could keep the section in the main
3437 executable while splitting it for the separate debug info file. */
3438 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3441 dwarf2_read_section (objfile, section);
3443 addr = section->buffer;
3444 /* Version check. */
3445 version = MAYBE_SWAP (*(offset_type *) addr);
3446 /* Versions earlier than 3 emitted every copy of a psymbol. This
3447 causes the index to behave very poorly for certain requests. Version 3
3448 contained incomplete addrmap. So, it seems better to just ignore such
3452 static int warning_printed = 0;
3453 if (!warning_printed)
3455 warning (_("Skipping obsolete .gdb_index section in %s."),
3457 warning_printed = 1;
3461 /* Index version 4 uses a different hash function than index version
3464 Versions earlier than 6 did not emit psymbols for inlined
3465 functions. Using these files will cause GDB not to be able to
3466 set breakpoints on inlined functions by name, so we ignore these
3467 indices unless the user has done
3468 "set use-deprecated-index-sections on". */
3469 if (version < 6 && !deprecated_ok)
3471 static int warning_printed = 0;
3472 if (!warning_printed)
3475 Skipping deprecated .gdb_index section in %s.\n\
3476 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3477 to use the section anyway."),
3479 warning_printed = 1;
3483 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3484 of the TU (for symbols coming from TUs),
3485 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3486 Plus gold-generated indices can have duplicate entries for global symbols,
3487 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3488 These are just performance bugs, and we can't distinguish gdb-generated
3489 indices from gold-generated ones, so issue no warning here. */
3491 /* Indexes with higher version than the one supported by GDB may be no
3492 longer backward compatible. */
3496 map->version = version;
3498 metadata = (offset_type *) (addr + sizeof (offset_type));
3501 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3502 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3506 *types_list = addr + MAYBE_SWAP (metadata[i]);
3507 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3508 - MAYBE_SWAP (metadata[i]))
3512 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3513 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3515 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3518 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3519 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3521 = gdb::array_view<mapped_index::symbol_table_slot>
3522 ((mapped_index::symbol_table_slot *) symbol_table,
3523 (mapped_index::symbol_table_slot *) symbol_table_end);
3526 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3531 /* Read .gdb_index. If everything went ok, initialize the "quick"
3532 elements of all the CUs and return 1. Otherwise, return 0. */
3535 dwarf2_read_gdb_index (struct dwarf2_per_objfile *dwarf2_per_objfile)
3537 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3538 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3539 struct dwz_file *dwz;
3540 struct objfile *objfile = dwarf2_per_objfile->objfile;
3542 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3543 if (!read_gdb_index_from_section (objfile, objfile_name (objfile),
3544 use_deprecated_index_sections,
3545 &dwarf2_per_objfile->gdb_index, map.get (),
3546 &cu_list, &cu_list_elements,
3547 &types_list, &types_list_elements))
3550 /* Don't use the index if it's empty. */
3551 if (map->symbol_table.empty ())
3554 /* If there is a .dwz file, read it so we can get its CU list as
3556 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3559 struct mapped_index dwz_map;
3560 const gdb_byte *dwz_types_ignore;
3561 offset_type dwz_types_elements_ignore;
3563 if (!read_gdb_index_from_section (objfile,
3564 bfd_get_filename (dwz->dwz_bfd), 1,
3565 &dwz->gdb_index, &dwz_map,
3566 &dwz_list, &dwz_list_elements,
3568 &dwz_types_elements_ignore))
3570 warning (_("could not read '.gdb_index' section from %s; skipping"),
3571 bfd_get_filename (dwz->dwz_bfd));
3576 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3577 dwz_list, dwz_list_elements);
3579 if (types_list_elements)
3581 struct dwarf2_section_info *section;
3583 /* We can only handle a single .debug_types when we have an
3585 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3588 section = VEC_index (dwarf2_section_info_def,
3589 dwarf2_per_objfile->types, 0);
3591 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3592 types_list, types_list_elements);
3595 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3597 dwarf2_per_objfile->index_table = std::move (map);
3598 dwarf2_per_objfile->using_index = 1;
3599 dwarf2_per_objfile->quick_file_names_table =
3600 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3605 /* die_reader_func for dw2_get_file_names. */
3608 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3609 const gdb_byte *info_ptr,
3610 struct die_info *comp_unit_die,
3614 struct dwarf2_cu *cu = reader->cu;
3615 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3616 struct dwarf2_per_objfile *dwarf2_per_objfile
3617 = cu->per_cu->dwarf2_per_objfile;
3618 struct objfile *objfile = dwarf2_per_objfile->objfile;
3619 struct dwarf2_per_cu_data *lh_cu;
3620 struct attribute *attr;
3623 struct quick_file_names *qfn;
3625 gdb_assert (! this_cu->is_debug_types);
3627 /* Our callers never want to match partial units -- instead they
3628 will match the enclosing full CU. */
3629 if (comp_unit_die->tag == DW_TAG_partial_unit)
3631 this_cu->v.quick->no_file_data = 1;
3639 sect_offset line_offset {};
3641 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3644 struct quick_file_names find_entry;
3646 line_offset = (sect_offset) DW_UNSND (attr);
3648 /* We may have already read in this line header (TU line header sharing).
3649 If we have we're done. */
3650 find_entry.hash.dwo_unit = cu->dwo_unit;
3651 find_entry.hash.line_sect_off = line_offset;
3652 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3653 &find_entry, INSERT);
3656 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3660 lh = dwarf_decode_line_header (line_offset, cu);
3664 lh_cu->v.quick->no_file_data = 1;
3668 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3669 qfn->hash.dwo_unit = cu->dwo_unit;
3670 qfn->hash.line_sect_off = line_offset;
3671 gdb_assert (slot != NULL);
3674 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3676 qfn->num_file_names = lh->file_names.size ();
3678 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3679 for (i = 0; i < lh->file_names.size (); ++i)
3680 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3681 qfn->real_names = NULL;
3683 lh_cu->v.quick->file_names = qfn;
3686 /* A helper for the "quick" functions which attempts to read the line
3687 table for THIS_CU. */
3689 static struct quick_file_names *
3690 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3692 /* This should never be called for TUs. */
3693 gdb_assert (! this_cu->is_debug_types);
3694 /* Nor type unit groups. */
3695 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3697 if (this_cu->v.quick->file_names != NULL)
3698 return this_cu->v.quick->file_names;
3699 /* If we know there is no line data, no point in looking again. */
3700 if (this_cu->v.quick->no_file_data)
3703 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3705 if (this_cu->v.quick->no_file_data)
3707 return this_cu->v.quick->file_names;
3710 /* A helper for the "quick" functions which computes and caches the
3711 real path for a given file name from the line table. */
3714 dw2_get_real_path (struct objfile *objfile,
3715 struct quick_file_names *qfn, int index)
3717 if (qfn->real_names == NULL)
3718 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3719 qfn->num_file_names, const char *);
3721 if (qfn->real_names[index] == NULL)
3722 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3724 return qfn->real_names[index];
3727 static struct symtab *
3728 dw2_find_last_source_symtab (struct objfile *objfile)
3730 struct dwarf2_per_objfile *dwarf2_per_objfile
3731 = get_dwarf2_per_objfile (objfile);
3732 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3733 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3738 return compunit_primary_filetab (cust);
3741 /* Traversal function for dw2_forget_cached_source_info. */
3744 dw2_free_cached_file_names (void **slot, void *info)
3746 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3748 if (file_data->real_names)
3752 for (i = 0; i < file_data->num_file_names; ++i)
3754 xfree ((void*) file_data->real_names[i]);
3755 file_data->real_names[i] = NULL;
3763 dw2_forget_cached_source_info (struct objfile *objfile)
3765 struct dwarf2_per_objfile *dwarf2_per_objfile
3766 = get_dwarf2_per_objfile (objfile);
3768 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3769 dw2_free_cached_file_names, NULL);
3772 /* Helper function for dw2_map_symtabs_matching_filename that expands
3773 the symtabs and calls the iterator. */
3776 dw2_map_expand_apply (struct objfile *objfile,
3777 struct dwarf2_per_cu_data *per_cu,
3778 const char *name, const char *real_path,
3779 gdb::function_view<bool (symtab *)> callback)
3781 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3783 /* Don't visit already-expanded CUs. */
3784 if (per_cu->v.quick->compunit_symtab)
3787 /* This may expand more than one symtab, and we want to iterate over
3789 dw2_instantiate_symtab (per_cu, false);
3791 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3792 last_made, callback);
3795 /* Implementation of the map_symtabs_matching_filename method. */
3798 dw2_map_symtabs_matching_filename
3799 (struct objfile *objfile, const char *name, const char *real_path,
3800 gdb::function_view<bool (symtab *)> callback)
3802 const char *name_basename = lbasename (name);
3803 struct dwarf2_per_objfile *dwarf2_per_objfile
3804 = get_dwarf2_per_objfile (objfile);
3806 /* The rule is CUs specify all the files, including those used by
3807 any TU, so there's no need to scan TUs here. */
3809 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3811 /* We only need to look at symtabs not already expanded. */
3812 if (per_cu->v.quick->compunit_symtab)
3815 quick_file_names *file_data = dw2_get_file_names (per_cu);
3816 if (file_data == NULL)
3819 for (int j = 0; j < file_data->num_file_names; ++j)
3821 const char *this_name = file_data->file_names[j];
3822 const char *this_real_name;
3824 if (compare_filenames_for_search (this_name, name))
3826 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3832 /* Before we invoke realpath, which can get expensive when many
3833 files are involved, do a quick comparison of the basenames. */
3834 if (! basenames_may_differ
3835 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3838 this_real_name = dw2_get_real_path (objfile, file_data, j);
3839 if (compare_filenames_for_search (this_real_name, name))
3841 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3847 if (real_path != NULL)
3849 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3850 gdb_assert (IS_ABSOLUTE_PATH (name));
3851 if (this_real_name != NULL
3852 && FILENAME_CMP (real_path, this_real_name) == 0)
3854 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3866 /* Struct used to manage iterating over all CUs looking for a symbol. */
3868 struct dw2_symtab_iterator
3870 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3871 struct dwarf2_per_objfile *dwarf2_per_objfile;
3872 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3873 int want_specific_block;
3874 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3875 Unused if !WANT_SPECIFIC_BLOCK. */
3877 /* The kind of symbol we're looking for. */
3879 /* The list of CUs from the index entry of the symbol,
3880 or NULL if not found. */
3882 /* The next element in VEC to look at. */
3884 /* The number of elements in VEC, or zero if there is no match. */
3886 /* Have we seen a global version of the symbol?
3887 If so we can ignore all further global instances.
3888 This is to work around gold/15646, inefficient gold-generated
3893 /* Initialize the index symtab iterator ITER.
3894 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3895 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3898 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3899 struct dwarf2_per_objfile *dwarf2_per_objfile,
3900 int want_specific_block,
3905 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3906 iter->want_specific_block = want_specific_block;
3907 iter->block_index = block_index;
3908 iter->domain = domain;
3910 iter->global_seen = 0;
3912 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3914 /* index is NULL if OBJF_READNOW. */
3915 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3916 iter->length = MAYBE_SWAP (*iter->vec);
3924 /* Return the next matching CU or NULL if there are no more. */
3926 static struct dwarf2_per_cu_data *
3927 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3929 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3931 for ( ; iter->next < iter->length; ++iter->next)
3933 offset_type cu_index_and_attrs =
3934 MAYBE_SWAP (iter->vec[iter->next + 1]);
3935 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3936 int want_static = iter->block_index != GLOBAL_BLOCK;
3937 /* This value is only valid for index versions >= 7. */
3938 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3939 gdb_index_symbol_kind symbol_kind =
3940 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3941 /* Only check the symbol attributes if they're present.
3942 Indices prior to version 7 don't record them,
3943 and indices >= 7 may elide them for certain symbols
3944 (gold does this). */
3946 (dwarf2_per_objfile->index_table->version >= 7
3947 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3949 /* Don't crash on bad data. */
3950 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3951 + dwarf2_per_objfile->all_type_units.size ()))
3953 complaint (_(".gdb_index entry has bad CU index"
3955 objfile_name (dwarf2_per_objfile->objfile));
3959 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3961 /* Skip if already read in. */
3962 if (per_cu->v.quick->compunit_symtab)
3965 /* Check static vs global. */
3968 if (iter->want_specific_block
3969 && want_static != is_static)
3971 /* Work around gold/15646. */
3972 if (!is_static && iter->global_seen)
3975 iter->global_seen = 1;
3978 /* Only check the symbol's kind if it has one. */
3981 switch (iter->domain)
3984 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3985 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3986 /* Some types are also in VAR_DOMAIN. */
3987 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3991 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3995 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4010 static struct compunit_symtab *
4011 dw2_lookup_symbol (struct objfile *objfile, int block_index,
4012 const char *name, domain_enum domain)
4014 struct compunit_symtab *stab_best = NULL;
4015 struct dwarf2_per_objfile *dwarf2_per_objfile
4016 = get_dwarf2_per_objfile (objfile);
4018 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4020 struct dw2_symtab_iterator iter;
4021 struct dwarf2_per_cu_data *per_cu;
4023 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
4025 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4027 struct symbol *sym, *with_opaque = NULL;
4028 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
4029 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4030 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4032 sym = block_find_symbol (block, name, domain,
4033 block_find_non_opaque_type_preferred,
4036 /* Some caution must be observed with overloaded functions
4037 and methods, since the index will not contain any overload
4038 information (but NAME might contain it). */
4041 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4043 if (with_opaque != NULL
4044 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4047 /* Keep looking through other CUs. */
4054 dw2_print_stats (struct objfile *objfile)
4056 struct dwarf2_per_objfile *dwarf2_per_objfile
4057 = get_dwarf2_per_objfile (objfile);
4058 int total = (dwarf2_per_objfile->all_comp_units.size ()
4059 + dwarf2_per_objfile->all_type_units.size ());
4062 for (int i = 0; i < total; ++i)
4064 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4066 if (!per_cu->v.quick->compunit_symtab)
4069 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4070 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4073 /* This dumps minimal information about the index.
4074 It is called via "mt print objfiles".
4075 One use is to verify .gdb_index has been loaded by the
4076 gdb.dwarf2/gdb-index.exp testcase. */
4079 dw2_dump (struct objfile *objfile)
4081 struct dwarf2_per_objfile *dwarf2_per_objfile
4082 = get_dwarf2_per_objfile (objfile);
4084 gdb_assert (dwarf2_per_objfile->using_index);
4085 printf_filtered (".gdb_index:");
4086 if (dwarf2_per_objfile->index_table != NULL)
4088 printf_filtered (" version %d\n",
4089 dwarf2_per_objfile->index_table->version);
4092 printf_filtered (" faked for \"readnow\"\n");
4093 printf_filtered ("\n");
4097 dw2_relocate (struct objfile *objfile,
4098 const struct section_offsets *new_offsets,
4099 const struct section_offsets *delta)
4101 /* There's nothing to relocate here. */
4105 dw2_expand_symtabs_for_function (struct objfile *objfile,
4106 const char *func_name)
4108 struct dwarf2_per_objfile *dwarf2_per_objfile
4109 = get_dwarf2_per_objfile (objfile);
4111 struct dw2_symtab_iterator iter;
4112 struct dwarf2_per_cu_data *per_cu;
4114 /* Note: It doesn't matter what we pass for block_index here. */
4115 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4118 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4119 dw2_instantiate_symtab (per_cu, false);
4124 dw2_expand_all_symtabs (struct objfile *objfile)
4126 struct dwarf2_per_objfile *dwarf2_per_objfile
4127 = get_dwarf2_per_objfile (objfile);
4128 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
4129 + dwarf2_per_objfile->all_type_units.size ());
4131 for (int i = 0; i < total_units; ++i)
4133 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4135 /* We don't want to directly expand a partial CU, because if we
4136 read it with the wrong language, then assertion failures can
4137 be triggered later on. See PR symtab/23010. So, tell
4138 dw2_instantiate_symtab to skip partial CUs -- any important
4139 partial CU will be read via DW_TAG_imported_unit anyway. */
4140 dw2_instantiate_symtab (per_cu, true);
4145 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4146 const char *fullname)
4148 struct dwarf2_per_objfile *dwarf2_per_objfile
4149 = get_dwarf2_per_objfile (objfile);
4151 /* We don't need to consider type units here.
4152 This is only called for examining code, e.g. expand_line_sal.
4153 There can be an order of magnitude (or more) more type units
4154 than comp units, and we avoid them if we can. */
4156 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4158 /* We only need to look at symtabs not already expanded. */
4159 if (per_cu->v.quick->compunit_symtab)
4162 quick_file_names *file_data = dw2_get_file_names (per_cu);
4163 if (file_data == NULL)
4166 for (int j = 0; j < file_data->num_file_names; ++j)
4168 const char *this_fullname = file_data->file_names[j];
4170 if (filename_cmp (this_fullname, fullname) == 0)
4172 dw2_instantiate_symtab (per_cu, false);
4180 dw2_map_matching_symbols (struct objfile *objfile,
4181 const char * name, domain_enum domain,
4183 int (*callback) (struct block *,
4184 struct symbol *, void *),
4185 void *data, symbol_name_match_type match,
4186 symbol_compare_ftype *ordered_compare)
4188 /* Currently unimplemented; used for Ada. The function can be called if the
4189 current language is Ada for a non-Ada objfile using GNU index. As Ada
4190 does not look for non-Ada symbols this function should just return. */
4193 /* Symbol name matcher for .gdb_index names.
4195 Symbol names in .gdb_index have a few particularities:
4197 - There's no indication of which is the language of each symbol.
4199 Since each language has its own symbol name matching algorithm,
4200 and we don't know which language is the right one, we must match
4201 each symbol against all languages. This would be a potential
4202 performance problem if it were not mitigated by the
4203 mapped_index::name_components lookup table, which significantly
4204 reduces the number of times we need to call into this matcher,
4205 making it a non-issue.
4207 - Symbol names in the index have no overload (parameter)
4208 information. I.e., in C++, "foo(int)" and "foo(long)" both
4209 appear as "foo" in the index, for example.
4211 This means that the lookup names passed to the symbol name
4212 matcher functions must have no parameter information either
4213 because (e.g.) symbol search name "foo" does not match
4214 lookup-name "foo(int)" [while swapping search name for lookup
4217 class gdb_index_symbol_name_matcher
4220 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4221 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4223 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4224 Returns true if any matcher matches. */
4225 bool matches (const char *symbol_name);
4228 /* A reference to the lookup name we're matching against. */
4229 const lookup_name_info &m_lookup_name;
4231 /* A vector holding all the different symbol name matchers, for all
4233 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4236 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4237 (const lookup_name_info &lookup_name)
4238 : m_lookup_name (lookup_name)
4240 /* Prepare the vector of comparison functions upfront, to avoid
4241 doing the same work for each symbol. Care is taken to avoid
4242 matching with the same matcher more than once if/when multiple
4243 languages use the same matcher function. */
4244 auto &matchers = m_symbol_name_matcher_funcs;
4245 matchers.reserve (nr_languages);
4247 matchers.push_back (default_symbol_name_matcher);
4249 for (int i = 0; i < nr_languages; i++)
4251 const language_defn *lang = language_def ((enum language) i);
4252 symbol_name_matcher_ftype *name_matcher
4253 = get_symbol_name_matcher (lang, m_lookup_name);
4255 /* Don't insert the same comparison routine more than once.
4256 Note that we do this linear walk instead of a seemingly
4257 cheaper sorted insert, or use a std::set or something like
4258 that, because relative order of function addresses is not
4259 stable. This is not a problem in practice because the number
4260 of supported languages is low, and the cost here is tiny
4261 compared to the number of searches we'll do afterwards using
4263 if (name_matcher != default_symbol_name_matcher
4264 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4265 == matchers.end ()))
4266 matchers.push_back (name_matcher);
4271 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4273 for (auto matches_name : m_symbol_name_matcher_funcs)
4274 if (matches_name (symbol_name, m_lookup_name, NULL))
4280 /* Starting from a search name, return the string that finds the upper
4281 bound of all strings that start with SEARCH_NAME in a sorted name
4282 list. Returns the empty string to indicate that the upper bound is
4283 the end of the list. */
4286 make_sort_after_prefix_name (const char *search_name)
4288 /* When looking to complete "func", we find the upper bound of all
4289 symbols that start with "func" by looking for where we'd insert
4290 the closest string that would follow "func" in lexicographical
4291 order. Usually, that's "func"-with-last-character-incremented,
4292 i.e. "fund". Mind non-ASCII characters, though. Usually those
4293 will be UTF-8 multi-byte sequences, but we can't be certain.
4294 Especially mind the 0xff character, which is a valid character in
4295 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4296 rule out compilers allowing it in identifiers. Note that
4297 conveniently, strcmp/strcasecmp are specified to compare
4298 characters interpreted as unsigned char. So what we do is treat
4299 the whole string as a base 256 number composed of a sequence of
4300 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4301 to 0, and carries 1 to the following more-significant position.
4302 If the very first character in SEARCH_NAME ends up incremented
4303 and carries/overflows, then the upper bound is the end of the
4304 list. The string after the empty string is also the empty
4307 Some examples of this operation:
4309 SEARCH_NAME => "+1" RESULT
4313 "\xff" "a" "\xff" => "\xff" "b"
4318 Then, with these symbols for example:
4324 completing "func" looks for symbols between "func" and
4325 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4326 which finds "func" and "func1", but not "fund".
4330 funcÿ (Latin1 'ÿ' [0xff])
4334 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4335 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4339 ÿÿ (Latin1 'ÿ' [0xff])
4342 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4343 the end of the list.
4345 std::string after = search_name;
4346 while (!after.empty () && (unsigned char) after.back () == 0xff)
4348 if (!after.empty ())
4349 after.back () = (unsigned char) after.back () + 1;
4353 /* See declaration. */
4355 std::pair<std::vector<name_component>::const_iterator,
4356 std::vector<name_component>::const_iterator>
4357 mapped_index_base::find_name_components_bounds
4358 (const lookup_name_info &lookup_name_without_params) const
4361 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4364 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4366 /* Comparison function object for lower_bound that matches against a
4367 given symbol name. */
4368 auto lookup_compare_lower = [&] (const name_component &elem,
4371 const char *elem_qualified = this->symbol_name_at (elem.idx);
4372 const char *elem_name = elem_qualified + elem.name_offset;
4373 return name_cmp (elem_name, name) < 0;
4376 /* Comparison function object for upper_bound that matches against a
4377 given symbol name. */
4378 auto lookup_compare_upper = [&] (const char *name,
4379 const name_component &elem)
4381 const char *elem_qualified = this->symbol_name_at (elem.idx);
4382 const char *elem_name = elem_qualified + elem.name_offset;
4383 return name_cmp (name, elem_name) < 0;
4386 auto begin = this->name_components.begin ();
4387 auto end = this->name_components.end ();
4389 /* Find the lower bound. */
4392 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4395 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4398 /* Find the upper bound. */
4401 if (lookup_name_without_params.completion_mode ())
4403 /* In completion mode, we want UPPER to point past all
4404 symbols names that have the same prefix. I.e., with
4405 these symbols, and completing "func":
4407 function << lower bound
4409 other_function << upper bound
4411 We find the upper bound by looking for the insertion
4412 point of "func"-with-last-character-incremented,
4414 std::string after = make_sort_after_prefix_name (cplus);
4417 return std::lower_bound (lower, end, after.c_str (),
4418 lookup_compare_lower);
4421 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4424 return {lower, upper};
4427 /* See declaration. */
4430 mapped_index_base::build_name_components ()
4432 if (!this->name_components.empty ())
4435 this->name_components_casing = case_sensitivity;
4437 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4439 /* The code below only knows how to break apart components of C++
4440 symbol names (and other languages that use '::' as
4441 namespace/module separator). If we add support for wild matching
4442 to some language that uses some other operator (E.g., Ada, Go and
4443 D use '.'), then we'll need to try splitting the symbol name
4444 according to that language too. Note that Ada does support wild
4445 matching, but doesn't currently support .gdb_index. */
4446 auto count = this->symbol_name_count ();
4447 for (offset_type idx = 0; idx < count; idx++)
4449 if (this->symbol_name_slot_invalid (idx))
4452 const char *name = this->symbol_name_at (idx);
4454 /* Add each name component to the name component table. */
4455 unsigned int previous_len = 0;
4456 for (unsigned int current_len = cp_find_first_component (name);
4457 name[current_len] != '\0';
4458 current_len += cp_find_first_component (name + current_len))
4460 gdb_assert (name[current_len] == ':');
4461 this->name_components.push_back ({previous_len, idx});
4462 /* Skip the '::'. */
4464 previous_len = current_len;
4466 this->name_components.push_back ({previous_len, idx});
4469 /* Sort name_components elements by name. */
4470 auto name_comp_compare = [&] (const name_component &left,
4471 const name_component &right)
4473 const char *left_qualified = this->symbol_name_at (left.idx);
4474 const char *right_qualified = this->symbol_name_at (right.idx);
4476 const char *left_name = left_qualified + left.name_offset;
4477 const char *right_name = right_qualified + right.name_offset;
4479 return name_cmp (left_name, right_name) < 0;
4482 std::sort (this->name_components.begin (),
4483 this->name_components.end (),
4487 /* Helper for dw2_expand_symtabs_matching that works with a
4488 mapped_index_base instead of the containing objfile. This is split
4489 to a separate function in order to be able to unit test the
4490 name_components matching using a mock mapped_index_base. For each
4491 symbol name that matches, calls MATCH_CALLBACK, passing it the
4492 symbol's index in the mapped_index_base symbol table. */
4495 dw2_expand_symtabs_matching_symbol
4496 (mapped_index_base &index,
4497 const lookup_name_info &lookup_name_in,
4498 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4499 enum search_domain kind,
4500 gdb::function_view<void (offset_type)> match_callback)
4502 lookup_name_info lookup_name_without_params
4503 = lookup_name_in.make_ignore_params ();
4504 gdb_index_symbol_name_matcher lookup_name_matcher
4505 (lookup_name_without_params);
4507 /* Build the symbol name component sorted vector, if we haven't
4509 index.build_name_components ();
4511 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4513 /* Now for each symbol name in range, check to see if we have a name
4514 match, and if so, call the MATCH_CALLBACK callback. */
4516 /* The same symbol may appear more than once in the range though.
4517 E.g., if we're looking for symbols that complete "w", and we have
4518 a symbol named "w1::w2", we'll find the two name components for
4519 that same symbol in the range. To be sure we only call the
4520 callback once per symbol, we first collect the symbol name
4521 indexes that matched in a temporary vector and ignore
4523 std::vector<offset_type> matches;
4524 matches.reserve (std::distance (bounds.first, bounds.second));
4526 for (; bounds.first != bounds.second; ++bounds.first)
4528 const char *qualified = index.symbol_name_at (bounds.first->idx);
4530 if (!lookup_name_matcher.matches (qualified)
4531 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4534 matches.push_back (bounds.first->idx);
4537 std::sort (matches.begin (), matches.end ());
4539 /* Finally call the callback, once per match. */
4541 for (offset_type idx : matches)
4545 match_callback (idx);
4550 /* Above we use a type wider than idx's for 'prev', since 0 and
4551 (offset_type)-1 are both possible values. */
4552 static_assert (sizeof (prev) > sizeof (offset_type), "");
4557 namespace selftests { namespace dw2_expand_symtabs_matching {
4559 /* A mock .gdb_index/.debug_names-like name index table, enough to
4560 exercise dw2_expand_symtabs_matching_symbol, which works with the
4561 mapped_index_base interface. Builds an index from the symbol list
4562 passed as parameter to the constructor. */
4563 class mock_mapped_index : public mapped_index_base
4566 mock_mapped_index (gdb::array_view<const char *> symbols)
4567 : m_symbol_table (symbols)
4570 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4572 /* Return the number of names in the symbol table. */
4573 size_t symbol_name_count () const override
4575 return m_symbol_table.size ();
4578 /* Get the name of the symbol at IDX in the symbol table. */
4579 const char *symbol_name_at (offset_type idx) const override
4581 return m_symbol_table[idx];
4585 gdb::array_view<const char *> m_symbol_table;
4588 /* Convenience function that converts a NULL pointer to a "<null>"
4589 string, to pass to print routines. */
4592 string_or_null (const char *str)
4594 return str != NULL ? str : "<null>";
4597 /* Check if a lookup_name_info built from
4598 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4599 index. EXPECTED_LIST is the list of expected matches, in expected
4600 matching order. If no match expected, then an empty list is
4601 specified. Returns true on success. On failure prints a warning
4602 indicating the file:line that failed, and returns false. */
4605 check_match (const char *file, int line,
4606 mock_mapped_index &mock_index,
4607 const char *name, symbol_name_match_type match_type,
4608 bool completion_mode,
4609 std::initializer_list<const char *> expected_list)
4611 lookup_name_info lookup_name (name, match_type, completion_mode);
4613 bool matched = true;
4615 auto mismatch = [&] (const char *expected_str,
4618 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4619 "expected=\"%s\", got=\"%s\"\n"),
4621 (match_type == symbol_name_match_type::FULL
4623 name, string_or_null (expected_str), string_or_null (got));
4627 auto expected_it = expected_list.begin ();
4628 auto expected_end = expected_list.end ();
4630 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4632 [&] (offset_type idx)
4634 const char *matched_name = mock_index.symbol_name_at (idx);
4635 const char *expected_str
4636 = expected_it == expected_end ? NULL : *expected_it++;
4638 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4639 mismatch (expected_str, matched_name);
4642 const char *expected_str
4643 = expected_it == expected_end ? NULL : *expected_it++;
4644 if (expected_str != NULL)
4645 mismatch (expected_str, NULL);
4650 /* The symbols added to the mock mapped_index for testing (in
4652 static const char *test_symbols[] = {
4661 "ns2::tmpl<int>::foo2",
4662 "(anonymous namespace)::A::B::C",
4664 /* These are used to check that the increment-last-char in the
4665 matching algorithm for completion doesn't match "t1_fund" when
4666 completing "t1_func". */
4672 /* A UTF-8 name with multi-byte sequences to make sure that
4673 cp-name-parser understands this as a single identifier ("função"
4674 is "function" in PT). */
4677 /* \377 (0xff) is Latin1 'ÿ'. */
4680 /* \377 (0xff) is Latin1 'ÿ'. */
4684 /* A name with all sorts of complications. Starts with "z" to make
4685 it easier for the completion tests below. */
4686 #define Z_SYM_NAME \
4687 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4688 "::tuple<(anonymous namespace)::ui*, " \
4689 "std::default_delete<(anonymous namespace)::ui>, void>"
4694 /* Returns true if the mapped_index_base::find_name_component_bounds
4695 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4696 in completion mode. */
4699 check_find_bounds_finds (mapped_index_base &index,
4700 const char *search_name,
4701 gdb::array_view<const char *> expected_syms)
4703 lookup_name_info lookup_name (search_name,
4704 symbol_name_match_type::FULL, true);
4706 auto bounds = index.find_name_components_bounds (lookup_name);
4708 size_t distance = std::distance (bounds.first, bounds.second);
4709 if (distance != expected_syms.size ())
4712 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4714 auto nc_elem = bounds.first + exp_elem;
4715 const char *qualified = index.symbol_name_at (nc_elem->idx);
4716 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4723 /* Test the lower-level mapped_index::find_name_component_bounds
4727 test_mapped_index_find_name_component_bounds ()
4729 mock_mapped_index mock_index (test_symbols);
4731 mock_index.build_name_components ();
4733 /* Test the lower-level mapped_index::find_name_component_bounds
4734 method in completion mode. */
4736 static const char *expected_syms[] = {
4741 SELF_CHECK (check_find_bounds_finds (mock_index,
4742 "t1_func", expected_syms));
4745 /* Check that the increment-last-char in the name matching algorithm
4746 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4748 static const char *expected_syms1[] = {
4752 SELF_CHECK (check_find_bounds_finds (mock_index,
4753 "\377", expected_syms1));
4755 static const char *expected_syms2[] = {
4758 SELF_CHECK (check_find_bounds_finds (mock_index,
4759 "\377\377", expected_syms2));
4763 /* Test dw2_expand_symtabs_matching_symbol. */
4766 test_dw2_expand_symtabs_matching_symbol ()
4768 mock_mapped_index mock_index (test_symbols);
4770 /* We let all tests run until the end even if some fails, for debug
4772 bool any_mismatch = false;
4774 /* Create the expected symbols list (an initializer_list). Needed
4775 because lists have commas, and we need to pass them to CHECK,
4776 which is a macro. */
4777 #define EXPECT(...) { __VA_ARGS__ }
4779 /* Wrapper for check_match that passes down the current
4780 __FILE__/__LINE__. */
4781 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4782 any_mismatch |= !check_match (__FILE__, __LINE__, \
4784 NAME, MATCH_TYPE, COMPLETION_MODE, \
4787 /* Identity checks. */
4788 for (const char *sym : test_symbols)
4790 /* Should be able to match all existing symbols. */
4791 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4794 /* Should be able to match all existing symbols with
4796 std::string with_params = std::string (sym) + "(int)";
4797 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4800 /* Should be able to match all existing symbols with
4801 parameters and qualifiers. */
4802 with_params = std::string (sym) + " ( int ) const";
4803 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4806 /* This should really find sym, but cp-name-parser.y doesn't
4807 know about lvalue/rvalue qualifiers yet. */
4808 with_params = std::string (sym) + " ( int ) &&";
4809 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4813 /* Check that the name matching algorithm for completion doesn't get
4814 confused with Latin1 'ÿ' / 0xff. */
4816 static const char str[] = "\377";
4817 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4818 EXPECT ("\377", "\377\377123"));
4821 /* Check that the increment-last-char in the matching algorithm for
4822 completion doesn't match "t1_fund" when completing "t1_func". */
4824 static const char str[] = "t1_func";
4825 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4826 EXPECT ("t1_func", "t1_func1"));
4829 /* Check that completion mode works at each prefix of the expected
4832 static const char str[] = "function(int)";
4833 size_t len = strlen (str);
4836 for (size_t i = 1; i < len; i++)
4838 lookup.assign (str, i);
4839 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4840 EXPECT ("function"));
4844 /* While "w" is a prefix of both components, the match function
4845 should still only be called once. */
4847 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4849 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4853 /* Same, with a "complicated" symbol. */
4855 static const char str[] = Z_SYM_NAME;
4856 size_t len = strlen (str);
4859 for (size_t i = 1; i < len; i++)
4861 lookup.assign (str, i);
4862 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4863 EXPECT (Z_SYM_NAME));
4867 /* In FULL mode, an incomplete symbol doesn't match. */
4869 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4873 /* A complete symbol with parameters matches any overload, since the
4874 index has no overload info. */
4876 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4877 EXPECT ("std::zfunction", "std::zfunction2"));
4878 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4879 EXPECT ("std::zfunction", "std::zfunction2"));
4880 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4881 EXPECT ("std::zfunction", "std::zfunction2"));
4884 /* Check that whitespace is ignored appropriately. A symbol with a
4885 template argument list. */
4887 static const char expected[] = "ns::foo<int>";
4888 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4890 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4894 /* Check that whitespace is ignored appropriately. A symbol with a
4895 template argument list that includes a pointer. */
4897 static const char expected[] = "ns::foo<char*>";
4898 /* Try both completion and non-completion modes. */
4899 static const bool completion_mode[2] = {false, true};
4900 for (size_t i = 0; i < 2; i++)
4902 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4903 completion_mode[i], EXPECT (expected));
4904 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4905 completion_mode[i], EXPECT (expected));
4907 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4908 completion_mode[i], EXPECT (expected));
4909 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4910 completion_mode[i], EXPECT (expected));
4915 /* Check method qualifiers are ignored. */
4916 static const char expected[] = "ns::foo<char*>";
4917 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4918 symbol_name_match_type::FULL, true, EXPECT (expected));
4919 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4920 symbol_name_match_type::FULL, true, EXPECT (expected));
4921 CHECK_MATCH ("foo < char * > ( int ) const",
4922 symbol_name_match_type::WILD, true, EXPECT (expected));
4923 CHECK_MATCH ("foo < char * > ( int ) &&",
4924 symbol_name_match_type::WILD, true, EXPECT (expected));
4927 /* Test lookup names that don't match anything. */
4929 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4932 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4936 /* Some wild matching tests, exercising "(anonymous namespace)",
4937 which should not be confused with a parameter list. */
4939 static const char *syms[] = {
4943 "A :: B :: C ( int )",
4948 for (const char *s : syms)
4950 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4951 EXPECT ("(anonymous namespace)::A::B::C"));
4956 static const char expected[] = "ns2::tmpl<int>::foo2";
4957 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4959 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4963 SELF_CHECK (!any_mismatch);
4972 test_mapped_index_find_name_component_bounds ();
4973 test_dw2_expand_symtabs_matching_symbol ();
4976 }} // namespace selftests::dw2_expand_symtabs_matching
4978 #endif /* GDB_SELF_TEST */
4980 /* If FILE_MATCHER is NULL or if PER_CU has
4981 dwarf2_per_cu_quick_data::MARK set (see
4982 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4983 EXPANSION_NOTIFY on it. */
4986 dw2_expand_symtabs_matching_one
4987 (struct dwarf2_per_cu_data *per_cu,
4988 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4989 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4991 if (file_matcher == NULL || per_cu->v.quick->mark)
4993 bool symtab_was_null
4994 = (per_cu->v.quick->compunit_symtab == NULL);
4996 dw2_instantiate_symtab (per_cu, false);
4998 if (expansion_notify != NULL
5000 && per_cu->v.quick->compunit_symtab != NULL)
5001 expansion_notify (per_cu->v.quick->compunit_symtab);
5005 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5006 matched, to expand corresponding CUs that were marked. IDX is the
5007 index of the symbol name that matched. */
5010 dw2_expand_marked_cus
5011 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5012 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5013 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5016 offset_type *vec, vec_len, vec_idx;
5017 bool global_seen = false;
5018 mapped_index &index = *dwarf2_per_objfile->index_table;
5020 vec = (offset_type *) (index.constant_pool
5021 + MAYBE_SWAP (index.symbol_table[idx].vec));
5022 vec_len = MAYBE_SWAP (vec[0]);
5023 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5025 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5026 /* This value is only valid for index versions >= 7. */
5027 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5028 gdb_index_symbol_kind symbol_kind =
5029 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5030 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5031 /* Only check the symbol attributes if they're present.
5032 Indices prior to version 7 don't record them,
5033 and indices >= 7 may elide them for certain symbols
5034 (gold does this). */
5037 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5039 /* Work around gold/15646. */
5042 if (!is_static && global_seen)
5048 /* Only check the symbol's kind if it has one. */
5053 case VARIABLES_DOMAIN:
5054 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5057 case FUNCTIONS_DOMAIN:
5058 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5062 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5070 /* Don't crash on bad data. */
5071 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
5072 + dwarf2_per_objfile->all_type_units.size ()))
5074 complaint (_(".gdb_index entry has bad CU index"
5076 objfile_name (dwarf2_per_objfile->objfile));
5080 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
5081 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5086 /* If FILE_MATCHER is non-NULL, set all the
5087 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5088 that match FILE_MATCHER. */
5091 dw_expand_symtabs_matching_file_matcher
5092 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5093 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5095 if (file_matcher == NULL)
5098 objfile *const objfile = dwarf2_per_objfile->objfile;
5100 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5102 NULL, xcalloc, xfree));
5103 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5105 NULL, xcalloc, xfree));
5107 /* The rule is CUs specify all the files, including those used by
5108 any TU, so there's no need to scan TUs here. */
5110 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5114 per_cu->v.quick->mark = 0;
5116 /* We only need to look at symtabs not already expanded. */
5117 if (per_cu->v.quick->compunit_symtab)
5120 quick_file_names *file_data = dw2_get_file_names (per_cu);
5121 if (file_data == NULL)
5124 if (htab_find (visited_not_found.get (), file_data) != NULL)
5126 else if (htab_find (visited_found.get (), file_data) != NULL)
5128 per_cu->v.quick->mark = 1;
5132 for (int j = 0; j < file_data->num_file_names; ++j)
5134 const char *this_real_name;
5136 if (file_matcher (file_data->file_names[j], false))
5138 per_cu->v.quick->mark = 1;
5142 /* Before we invoke realpath, which can get expensive when many
5143 files are involved, do a quick comparison of the basenames. */
5144 if (!basenames_may_differ
5145 && !file_matcher (lbasename (file_data->file_names[j]),
5149 this_real_name = dw2_get_real_path (objfile, file_data, j);
5150 if (file_matcher (this_real_name, false))
5152 per_cu->v.quick->mark = 1;
5157 void **slot = htab_find_slot (per_cu->v.quick->mark
5158 ? visited_found.get ()
5159 : visited_not_found.get (),
5166 dw2_expand_symtabs_matching
5167 (struct objfile *objfile,
5168 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5169 const lookup_name_info &lookup_name,
5170 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5171 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5172 enum search_domain kind)
5174 struct dwarf2_per_objfile *dwarf2_per_objfile
5175 = get_dwarf2_per_objfile (objfile);
5177 /* index_table is NULL if OBJF_READNOW. */
5178 if (!dwarf2_per_objfile->index_table)
5181 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5183 mapped_index &index = *dwarf2_per_objfile->index_table;
5185 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5187 kind, [&] (offset_type idx)
5189 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5190 expansion_notify, kind);
5194 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5197 static struct compunit_symtab *
5198 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5203 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5204 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5207 if (cust->includes == NULL)
5210 for (i = 0; cust->includes[i]; ++i)
5212 struct compunit_symtab *s = cust->includes[i];
5214 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5222 static struct compunit_symtab *
5223 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5224 struct bound_minimal_symbol msymbol,
5226 struct obj_section *section,
5229 struct dwarf2_per_cu_data *data;
5230 struct compunit_symtab *result;
5232 if (!objfile->psymtabs_addrmap)
5235 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5240 if (warn_if_readin && data->v.quick->compunit_symtab)
5241 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5242 paddress (get_objfile_arch (objfile), pc));
5245 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5248 gdb_assert (result != NULL);
5253 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5254 void *data, int need_fullname)
5256 struct dwarf2_per_objfile *dwarf2_per_objfile
5257 = get_dwarf2_per_objfile (objfile);
5259 if (!dwarf2_per_objfile->filenames_cache)
5261 dwarf2_per_objfile->filenames_cache.emplace ();
5263 htab_up visited (htab_create_alloc (10,
5264 htab_hash_pointer, htab_eq_pointer,
5265 NULL, xcalloc, xfree));
5267 /* The rule is CUs specify all the files, including those used
5268 by any TU, so there's no need to scan TUs here. We can
5269 ignore file names coming from already-expanded CUs. */
5271 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5273 if (per_cu->v.quick->compunit_symtab)
5275 void **slot = htab_find_slot (visited.get (),
5276 per_cu->v.quick->file_names,
5279 *slot = per_cu->v.quick->file_names;
5283 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5285 /* We only need to look at symtabs not already expanded. */
5286 if (per_cu->v.quick->compunit_symtab)
5289 quick_file_names *file_data = dw2_get_file_names (per_cu);
5290 if (file_data == NULL)
5293 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5296 /* Already visited. */
5301 for (int j = 0; j < file_data->num_file_names; ++j)
5303 const char *filename = file_data->file_names[j];
5304 dwarf2_per_objfile->filenames_cache->seen (filename);
5309 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5311 gdb::unique_xmalloc_ptr<char> this_real_name;
5314 this_real_name = gdb_realpath (filename);
5315 (*fun) (filename, this_real_name.get (), data);
5320 dw2_has_symbols (struct objfile *objfile)
5325 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5328 dw2_find_last_source_symtab,
5329 dw2_forget_cached_source_info,
5330 dw2_map_symtabs_matching_filename,
5335 dw2_expand_symtabs_for_function,
5336 dw2_expand_all_symtabs,
5337 dw2_expand_symtabs_with_fullname,
5338 dw2_map_matching_symbols,
5339 dw2_expand_symtabs_matching,
5340 dw2_find_pc_sect_compunit_symtab,
5342 dw2_map_symbol_filenames
5345 /* DWARF-5 debug_names reader. */
5347 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5348 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5350 /* A helper function that reads the .debug_names section in SECTION
5351 and fills in MAP. FILENAME is the name of the file containing the
5352 section; it is used for error reporting.
5354 Returns true if all went well, false otherwise. */
5357 read_debug_names_from_section (struct objfile *objfile,
5358 const char *filename,
5359 struct dwarf2_section_info *section,
5360 mapped_debug_names &map)
5362 if (dwarf2_section_empty_p (section))
5365 /* Older elfutils strip versions could keep the section in the main
5366 executable while splitting it for the separate debug info file. */
5367 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5370 dwarf2_read_section (objfile, section);
5372 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5374 const gdb_byte *addr = section->buffer;
5376 bfd *const abfd = get_section_bfd_owner (section);
5378 unsigned int bytes_read;
5379 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5382 map.dwarf5_is_dwarf64 = bytes_read != 4;
5383 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5384 if (bytes_read + length != section->size)
5386 /* There may be multiple per-CU indices. */
5387 warning (_("Section .debug_names in %s length %s does not match "
5388 "section length %s, ignoring .debug_names."),
5389 filename, plongest (bytes_read + length),
5390 pulongest (section->size));
5394 /* The version number. */
5395 uint16_t version = read_2_bytes (abfd, addr);
5399 warning (_("Section .debug_names in %s has unsupported version %d, "
5400 "ignoring .debug_names."),
5406 uint16_t padding = read_2_bytes (abfd, addr);
5410 warning (_("Section .debug_names in %s has unsupported padding %d, "
5411 "ignoring .debug_names."),
5416 /* comp_unit_count - The number of CUs in the CU list. */
5417 map.cu_count = read_4_bytes (abfd, addr);
5420 /* local_type_unit_count - The number of TUs in the local TU
5422 map.tu_count = read_4_bytes (abfd, addr);
5425 /* foreign_type_unit_count - The number of TUs in the foreign TU
5427 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5429 if (foreign_tu_count != 0)
5431 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5432 "ignoring .debug_names."),
5433 filename, static_cast<unsigned long> (foreign_tu_count));
5437 /* bucket_count - The number of hash buckets in the hash lookup
5439 map.bucket_count = read_4_bytes (abfd, addr);
5442 /* name_count - The number of unique names in the index. */
5443 map.name_count = read_4_bytes (abfd, addr);
5446 /* abbrev_table_size - The size in bytes of the abbreviations
5448 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5451 /* augmentation_string_size - The size in bytes of the augmentation
5452 string. This value is rounded up to a multiple of 4. */
5453 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5455 map.augmentation_is_gdb = ((augmentation_string_size
5456 == sizeof (dwarf5_augmentation))
5457 && memcmp (addr, dwarf5_augmentation,
5458 sizeof (dwarf5_augmentation)) == 0);
5459 augmentation_string_size += (-augmentation_string_size) & 3;
5460 addr += augmentation_string_size;
5463 map.cu_table_reordered = addr;
5464 addr += map.cu_count * map.offset_size;
5466 /* List of Local TUs */
5467 map.tu_table_reordered = addr;
5468 addr += map.tu_count * map.offset_size;
5470 /* Hash Lookup Table */
5471 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5472 addr += map.bucket_count * 4;
5473 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5474 addr += map.name_count * 4;
5477 map.name_table_string_offs_reordered = addr;
5478 addr += map.name_count * map.offset_size;
5479 map.name_table_entry_offs_reordered = addr;
5480 addr += map.name_count * map.offset_size;
5482 const gdb_byte *abbrev_table_start = addr;
5485 unsigned int bytes_read;
5486 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5491 const auto insertpair
5492 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5493 if (!insertpair.second)
5495 warning (_("Section .debug_names in %s has duplicate index %s, "
5496 "ignoring .debug_names."),
5497 filename, pulongest (index_num));
5500 mapped_debug_names::index_val &indexval = insertpair.first->second;
5501 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5506 mapped_debug_names::index_val::attr attr;
5507 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5509 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5511 if (attr.form == DW_FORM_implicit_const)
5513 attr.implicit_const = read_signed_leb128 (abfd, addr,
5517 if (attr.dw_idx == 0 && attr.form == 0)
5519 indexval.attr_vec.push_back (std::move (attr));
5522 if (addr != abbrev_table_start + abbrev_table_size)
5524 warning (_("Section .debug_names in %s has abbreviation_table "
5525 "of size %zu vs. written as %u, ignoring .debug_names."),
5526 filename, addr - abbrev_table_start, abbrev_table_size);
5529 map.entry_pool = addr;
5534 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5538 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5539 const mapped_debug_names &map,
5540 dwarf2_section_info §ion,
5543 sect_offset sect_off_prev;
5544 for (uint32_t i = 0; i <= map.cu_count; ++i)
5546 sect_offset sect_off_next;
5547 if (i < map.cu_count)
5550 = (sect_offset) (extract_unsigned_integer
5551 (map.cu_table_reordered + i * map.offset_size,
5553 map.dwarf5_byte_order));
5556 sect_off_next = (sect_offset) section.size;
5559 const ULONGEST length = sect_off_next - sect_off_prev;
5560 dwarf2_per_cu_data *per_cu
5561 = create_cu_from_index_list (dwarf2_per_objfile, §ion, is_dwz,
5562 sect_off_prev, length);
5563 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5565 sect_off_prev = sect_off_next;
5569 /* Read the CU list from the mapped index, and use it to create all
5570 the CU objects for this dwarf2_per_objfile. */
5573 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5574 const mapped_debug_names &map,
5575 const mapped_debug_names &dwz_map)
5577 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5578 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5580 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5581 dwarf2_per_objfile->info,
5582 false /* is_dwz */);
5584 if (dwz_map.cu_count == 0)
5587 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5588 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5592 /* Read .debug_names. If everything went ok, initialize the "quick"
5593 elements of all the CUs and return true. Otherwise, return false. */
5596 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5598 std::unique_ptr<mapped_debug_names> map
5599 (new mapped_debug_names (dwarf2_per_objfile));
5600 mapped_debug_names dwz_map (dwarf2_per_objfile);
5601 struct objfile *objfile = dwarf2_per_objfile->objfile;
5603 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5604 &dwarf2_per_objfile->debug_names,
5608 /* Don't use the index if it's empty. */
5609 if (map->name_count == 0)
5612 /* If there is a .dwz file, read it so we can get its CU list as
5614 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5617 if (!read_debug_names_from_section (objfile,
5618 bfd_get_filename (dwz->dwz_bfd),
5619 &dwz->debug_names, dwz_map))
5621 warning (_("could not read '.debug_names' section from %s; skipping"),
5622 bfd_get_filename (dwz->dwz_bfd));
5627 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5629 if (map->tu_count != 0)
5631 /* We can only handle a single .debug_types when we have an
5633 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5636 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5637 dwarf2_per_objfile->types, 0);
5639 create_signatured_type_table_from_debug_names
5640 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5643 create_addrmap_from_aranges (dwarf2_per_objfile,
5644 &dwarf2_per_objfile->debug_aranges);
5646 dwarf2_per_objfile->debug_names_table = std::move (map);
5647 dwarf2_per_objfile->using_index = 1;
5648 dwarf2_per_objfile->quick_file_names_table =
5649 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5654 /* Type used to manage iterating over all CUs looking for a symbol for
5657 class dw2_debug_names_iterator
5660 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
5661 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
5662 dw2_debug_names_iterator (const mapped_debug_names &map,
5663 bool want_specific_block,
5664 block_enum block_index, domain_enum domain,
5666 : m_map (map), m_want_specific_block (want_specific_block),
5667 m_block_index (block_index), m_domain (domain),
5668 m_addr (find_vec_in_debug_names (map, name))
5671 dw2_debug_names_iterator (const mapped_debug_names &map,
5672 search_domain search, uint32_t namei)
5675 m_addr (find_vec_in_debug_names (map, namei))
5678 /* Return the next matching CU or NULL if there are no more. */
5679 dwarf2_per_cu_data *next ();
5682 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5684 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5687 /* The internalized form of .debug_names. */
5688 const mapped_debug_names &m_map;
5690 /* If true, only look for symbols that match BLOCK_INDEX. */
5691 const bool m_want_specific_block = false;
5693 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
5694 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
5696 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
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 if (current_language->la_language == language_cplus
5730 || current_language->la_language == language_fortran
5731 || current_language->la_language == language_d)
5733 /* NAME is already canonical. Drop any qualifiers as
5734 .debug_names does not contain any. */
5736 if (strchr (name, '(') != NULL)
5738 gdb::unique_xmalloc_ptr<char> without_params
5739 = cp_remove_params (name);
5741 if (without_params != NULL)
5743 name = without_params.get();
5748 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5750 const uint32_t full_hash = dwarf5_djb_hash (name);
5752 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5753 (map.bucket_table_reordered
5754 + (full_hash % map.bucket_count)), 4,
5755 map.dwarf5_byte_order);
5759 if (namei >= map.name_count)
5761 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5763 namei, map.name_count,
5764 objfile_name (map.dwarf2_per_objfile->objfile));
5770 const uint32_t namei_full_hash
5771 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5772 (map.hash_table_reordered + namei), 4,
5773 map.dwarf5_byte_order);
5774 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5777 if (full_hash == namei_full_hash)
5779 const char *const namei_string = map.namei_to_name (namei);
5781 #if 0 /* An expensive sanity check. */
5782 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5784 complaint (_("Wrong .debug_names hash for string at index %u "
5786 namei, objfile_name (dwarf2_per_objfile->objfile));
5791 if (cmp (namei_string, name) == 0)
5793 const ULONGEST namei_entry_offs
5794 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5795 + namei * map.offset_size),
5796 map.offset_size, map.dwarf5_byte_order);
5797 return map.entry_pool + namei_entry_offs;
5802 if (namei >= map.name_count)
5808 dw2_debug_names_iterator::find_vec_in_debug_names
5809 (const mapped_debug_names &map, uint32_t namei)
5811 if (namei >= map.name_count)
5813 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5815 namei, map.name_count,
5816 objfile_name (map.dwarf2_per_objfile->objfile));
5820 const ULONGEST namei_entry_offs
5821 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5822 + namei * map.offset_size),
5823 map.offset_size, map.dwarf5_byte_order);
5824 return map.entry_pool + namei_entry_offs;
5827 /* See dw2_debug_names_iterator. */
5829 dwarf2_per_cu_data *
5830 dw2_debug_names_iterator::next ()
5835 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5836 struct objfile *objfile = dwarf2_per_objfile->objfile;
5837 bfd *const abfd = objfile->obfd;
5841 unsigned int bytes_read;
5842 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5843 m_addr += bytes_read;
5847 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5848 if (indexval_it == m_map.abbrev_map.cend ())
5850 complaint (_("Wrong .debug_names undefined abbrev code %s "
5852 pulongest (abbrev), objfile_name (objfile));
5855 const mapped_debug_names::index_val &indexval = indexval_it->second;
5856 bool have_is_static = false;
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 have_is_static = true;
5912 case DW_IDX_GNU_external:
5913 if (!m_map.augmentation_is_gdb)
5915 have_is_static = true;
5921 /* Skip if already read in. */
5922 if (per_cu->v.quick->compunit_symtab)
5925 /* Check static vs global. */
5928 const bool want_static = m_block_index != GLOBAL_BLOCK;
5929 if (m_want_specific_block && want_static != 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, int block_index_int,
6015 const char *name, domain_enum domain)
6017 const block_enum block_index = static_cast<block_enum> (block_index_int);
6018 struct dwarf2_per_objfile *dwarf2_per_objfile
6019 = get_dwarf2_per_objfile (objfile);
6021 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6024 /* index is NULL if OBJF_READNOW. */
6027 const auto &map = *mapp;
6029 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6030 block_index, domain, name);
6032 struct compunit_symtab *stab_best = NULL;
6033 struct dwarf2_per_cu_data *per_cu;
6034 while ((per_cu = iter.next ()) != NULL)
6036 struct symbol *sym, *with_opaque = NULL;
6037 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
6038 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6039 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6041 sym = block_find_symbol (block, name, domain,
6042 block_find_non_opaque_type_preferred,
6045 /* Some caution must be observed with overloaded functions and
6046 methods, since the index will not contain any overload
6047 information (but NAME might contain it). */
6050 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6052 if (with_opaque != NULL
6053 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6056 /* Keep looking through other CUs. */
6062 /* This dumps minimal information about .debug_names. It is called
6063 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6064 uses this to verify that .debug_names has been loaded. */
6067 dw2_debug_names_dump (struct objfile *objfile)
6069 struct dwarf2_per_objfile *dwarf2_per_objfile
6070 = get_dwarf2_per_objfile (objfile);
6072 gdb_assert (dwarf2_per_objfile->using_index);
6073 printf_filtered (".debug_names:");
6074 if (dwarf2_per_objfile->debug_names_table)
6075 printf_filtered (" exists\n");
6077 printf_filtered (" faked for \"readnow\"\n");
6078 printf_filtered ("\n");
6082 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6083 const char *func_name)
6085 struct dwarf2_per_objfile *dwarf2_per_objfile
6086 = get_dwarf2_per_objfile (objfile);
6088 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6089 if (dwarf2_per_objfile->debug_names_table)
6091 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6093 /* Note: It doesn't matter what we pass for block_index here. */
6094 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6095 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
6097 struct dwarf2_per_cu_data *per_cu;
6098 while ((per_cu = iter.next ()) != NULL)
6099 dw2_instantiate_symtab (per_cu, false);
6104 dw2_debug_names_expand_symtabs_matching
6105 (struct objfile *objfile,
6106 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6107 const lookup_name_info &lookup_name,
6108 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6109 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6110 enum search_domain kind)
6112 struct dwarf2_per_objfile *dwarf2_per_objfile
6113 = get_dwarf2_per_objfile (objfile);
6115 /* debug_names_table is NULL if OBJF_READNOW. */
6116 if (!dwarf2_per_objfile->debug_names_table)
6119 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6121 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6123 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6125 kind, [&] (offset_type namei)
6127 /* The name was matched, now expand corresponding CUs that were
6129 dw2_debug_names_iterator iter (map, kind, namei);
6131 struct dwarf2_per_cu_data *per_cu;
6132 while ((per_cu = iter.next ()) != NULL)
6133 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6138 const struct quick_symbol_functions dwarf2_debug_names_functions =
6141 dw2_find_last_source_symtab,
6142 dw2_forget_cached_source_info,
6143 dw2_map_symtabs_matching_filename,
6144 dw2_debug_names_lookup_symbol,
6146 dw2_debug_names_dump,
6148 dw2_debug_names_expand_symtabs_for_function,
6149 dw2_expand_all_symtabs,
6150 dw2_expand_symtabs_with_fullname,
6151 dw2_map_matching_symbols,
6152 dw2_debug_names_expand_symtabs_matching,
6153 dw2_find_pc_sect_compunit_symtab,
6155 dw2_map_symbol_filenames
6158 /* See symfile.h. */
6161 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6163 struct dwarf2_per_objfile *dwarf2_per_objfile
6164 = get_dwarf2_per_objfile (objfile);
6166 /* If we're about to read full symbols, don't bother with the
6167 indices. In this case we also don't care if some other debug
6168 format is making psymtabs, because they are all about to be
6170 if ((objfile->flags & OBJF_READNOW))
6172 dwarf2_per_objfile->using_index = 1;
6173 create_all_comp_units (dwarf2_per_objfile);
6174 create_all_type_units (dwarf2_per_objfile);
6175 dwarf2_per_objfile->quick_file_names_table
6176 = create_quick_file_names_table
6177 (dwarf2_per_objfile->all_comp_units.size ());
6179 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6180 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6182 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6184 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6185 struct dwarf2_per_cu_quick_data);
6188 /* Return 1 so that gdb sees the "quick" functions. However,
6189 these functions will be no-ops because we will have expanded
6191 *index_kind = dw_index_kind::GDB_INDEX;
6195 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6197 *index_kind = dw_index_kind::DEBUG_NAMES;
6201 if (dwarf2_read_gdb_index (dwarf2_per_objfile))
6203 *index_kind = dw_index_kind::GDB_INDEX;
6212 /* Build a partial symbol table. */
6215 dwarf2_build_psymtabs (struct objfile *objfile)
6217 struct dwarf2_per_objfile *dwarf2_per_objfile
6218 = get_dwarf2_per_objfile (objfile);
6220 if (objfile->global_psymbols.capacity () == 0
6221 && objfile->static_psymbols.capacity () == 0)
6222 init_psymbol_list (objfile, 1024);
6226 /* This isn't really ideal: all the data we allocate on the
6227 objfile's obstack is still uselessly kept around. However,
6228 freeing it seems unsafe. */
6229 psymtab_discarder psymtabs (objfile);
6230 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6233 CATCH (except, RETURN_MASK_ERROR)
6235 exception_print (gdb_stderr, except);
6240 /* Return the total length of the CU described by HEADER. */
6243 get_cu_length (const struct comp_unit_head *header)
6245 return header->initial_length_size + header->length;
6248 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6251 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6253 sect_offset bottom = cu_header->sect_off;
6254 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6256 return sect_off >= bottom && sect_off < top;
6259 /* Find the base address of the compilation unit for range lists and
6260 location lists. It will normally be specified by DW_AT_low_pc.
6261 In DWARF-3 draft 4, the base address could be overridden by
6262 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6263 compilation units with discontinuous ranges. */
6266 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6268 struct attribute *attr;
6271 cu->base_address = 0;
6273 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6276 cu->base_address = attr_value_as_address (attr);
6281 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6284 cu->base_address = attr_value_as_address (attr);
6290 /* Read in the comp unit header information from the debug_info at info_ptr.
6291 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6292 NOTE: This leaves members offset, first_die_offset to be filled in
6295 static const gdb_byte *
6296 read_comp_unit_head (struct comp_unit_head *cu_header,
6297 const gdb_byte *info_ptr,
6298 struct dwarf2_section_info *section,
6299 rcuh_kind section_kind)
6302 unsigned int bytes_read;
6303 const char *filename = get_section_file_name (section);
6304 bfd *abfd = get_section_bfd_owner (section);
6306 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6307 cu_header->initial_length_size = bytes_read;
6308 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6309 info_ptr += bytes_read;
6310 cu_header->version = read_2_bytes (abfd, info_ptr);
6311 if (cu_header->version < 2 || cu_header->version > 5)
6312 error (_("Dwarf Error: wrong version in compilation unit header "
6313 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6314 cu_header->version, filename);
6316 if (cu_header->version < 5)
6317 switch (section_kind)
6319 case rcuh_kind::COMPILE:
6320 cu_header->unit_type = DW_UT_compile;
6322 case rcuh_kind::TYPE:
6323 cu_header->unit_type = DW_UT_type;
6326 internal_error (__FILE__, __LINE__,
6327 _("read_comp_unit_head: invalid section_kind"));
6331 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6332 (read_1_byte (abfd, info_ptr));
6334 switch (cu_header->unit_type)
6337 if (section_kind != rcuh_kind::COMPILE)
6338 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6339 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6343 section_kind = rcuh_kind::TYPE;
6346 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6347 "(is %d, should be %d or %d) [in module %s]"),
6348 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6351 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6354 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6357 info_ptr += bytes_read;
6358 if (cu_header->version < 5)
6360 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6363 signed_addr = bfd_get_sign_extend_vma (abfd);
6364 if (signed_addr < 0)
6365 internal_error (__FILE__, __LINE__,
6366 _("read_comp_unit_head: dwarf from non elf file"));
6367 cu_header->signed_addr_p = signed_addr;
6369 if (section_kind == rcuh_kind::TYPE)
6371 LONGEST type_offset;
6373 cu_header->signature = read_8_bytes (abfd, info_ptr);
6376 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6377 info_ptr += bytes_read;
6378 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6379 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6380 error (_("Dwarf Error: Too big type_offset in compilation unit "
6381 "header (is %s) [in module %s]"), plongest (type_offset),
6388 /* Helper function that returns the proper abbrev section for
6391 static struct dwarf2_section_info *
6392 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6394 struct dwarf2_section_info *abbrev;
6395 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6397 if (this_cu->is_dwz)
6398 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6400 abbrev = &dwarf2_per_objfile->abbrev;
6405 /* Subroutine of read_and_check_comp_unit_head and
6406 read_and_check_type_unit_head to simplify them.
6407 Perform various error checking on the header. */
6410 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6411 struct comp_unit_head *header,
6412 struct dwarf2_section_info *section,
6413 struct dwarf2_section_info *abbrev_section)
6415 const char *filename = get_section_file_name (section);
6417 if (to_underlying (header->abbrev_sect_off)
6418 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6419 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6420 "(offset %s + 6) [in module %s]"),
6421 sect_offset_str (header->abbrev_sect_off),
6422 sect_offset_str (header->sect_off),
6425 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6426 avoid potential 32-bit overflow. */
6427 if (((ULONGEST) header->sect_off + get_cu_length (header))
6429 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6430 "(offset %s + 0) [in module %s]"),
6431 header->length, sect_offset_str (header->sect_off),
6435 /* Read in a CU/TU header and perform some basic error checking.
6436 The contents of the header are stored in HEADER.
6437 The result is a pointer to the start of the first DIE. */
6439 static const gdb_byte *
6440 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6441 struct comp_unit_head *header,
6442 struct dwarf2_section_info *section,
6443 struct dwarf2_section_info *abbrev_section,
6444 const gdb_byte *info_ptr,
6445 rcuh_kind section_kind)
6447 const gdb_byte *beg_of_comp_unit = info_ptr;
6449 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6451 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6453 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6455 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6461 /* Fetch the abbreviation table offset from a comp or type unit header. */
6464 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6465 struct dwarf2_section_info *section,
6466 sect_offset sect_off)
6468 bfd *abfd = get_section_bfd_owner (section);
6469 const gdb_byte *info_ptr;
6470 unsigned int initial_length_size, offset_size;
6473 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6474 info_ptr = section->buffer + to_underlying (sect_off);
6475 read_initial_length (abfd, info_ptr, &initial_length_size);
6476 offset_size = initial_length_size == 4 ? 4 : 8;
6477 info_ptr += initial_length_size;
6479 version = read_2_bytes (abfd, info_ptr);
6483 /* Skip unit type and address size. */
6487 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6490 /* Allocate a new partial symtab for file named NAME and mark this new
6491 partial symtab as being an include of PST. */
6494 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6495 struct objfile *objfile)
6497 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6499 if (!IS_ABSOLUTE_PATH (subpst->filename))
6501 /* It shares objfile->objfile_obstack. */
6502 subpst->dirname = pst->dirname;
6505 subpst->textlow = 0;
6506 subpst->texthigh = 0;
6508 subpst->dependencies
6509 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
6510 subpst->dependencies[0] = pst;
6511 subpst->number_of_dependencies = 1;
6513 subpst->globals_offset = 0;
6514 subpst->n_global_syms = 0;
6515 subpst->statics_offset = 0;
6516 subpst->n_static_syms = 0;
6517 subpst->compunit_symtab = NULL;
6518 subpst->read_symtab = pst->read_symtab;
6521 /* No private part is necessary for include psymtabs. This property
6522 can be used to differentiate between such include psymtabs and
6523 the regular ones. */
6524 subpst->read_symtab_private = NULL;
6527 /* Read the Line Number Program data and extract the list of files
6528 included by the source file represented by PST. Build an include
6529 partial symtab for each of these included files. */
6532 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6533 struct die_info *die,
6534 struct partial_symtab *pst)
6537 struct attribute *attr;
6539 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6541 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6543 return; /* No linetable, so no includes. */
6545 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
6546 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
6550 hash_signatured_type (const void *item)
6552 const struct signatured_type *sig_type
6553 = (const struct signatured_type *) item;
6555 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6556 return sig_type->signature;
6560 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6562 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6563 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6565 return lhs->signature == rhs->signature;
6568 /* Allocate a hash table for signatured types. */
6571 allocate_signatured_type_table (struct objfile *objfile)
6573 return htab_create_alloc_ex (41,
6574 hash_signatured_type,
6577 &objfile->objfile_obstack,
6578 hashtab_obstack_allocate,
6579 dummy_obstack_deallocate);
6582 /* A helper function to add a signatured type CU to a table. */
6585 add_signatured_type_cu_to_table (void **slot, void *datum)
6587 struct signatured_type *sigt = (struct signatured_type *) *slot;
6588 std::vector<signatured_type *> *all_type_units
6589 = (std::vector<signatured_type *> *) datum;
6591 all_type_units->push_back (sigt);
6596 /* A helper for create_debug_types_hash_table. Read types from SECTION
6597 and fill them into TYPES_HTAB. It will process only type units,
6598 therefore DW_UT_type. */
6601 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6602 struct dwo_file *dwo_file,
6603 dwarf2_section_info *section, htab_t &types_htab,
6604 rcuh_kind section_kind)
6606 struct objfile *objfile = dwarf2_per_objfile->objfile;
6607 struct dwarf2_section_info *abbrev_section;
6609 const gdb_byte *info_ptr, *end_ptr;
6611 abbrev_section = (dwo_file != NULL
6612 ? &dwo_file->sections.abbrev
6613 : &dwarf2_per_objfile->abbrev);
6615 if (dwarf_read_debug)
6616 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6617 get_section_name (section),
6618 get_section_file_name (abbrev_section));
6620 dwarf2_read_section (objfile, section);
6621 info_ptr = section->buffer;
6623 if (info_ptr == NULL)
6626 /* We can't set abfd until now because the section may be empty or
6627 not present, in which case the bfd is unknown. */
6628 abfd = get_section_bfd_owner (section);
6630 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6631 because we don't need to read any dies: the signature is in the
6634 end_ptr = info_ptr + section->size;
6635 while (info_ptr < end_ptr)
6637 struct signatured_type *sig_type;
6638 struct dwo_unit *dwo_tu;
6640 const gdb_byte *ptr = info_ptr;
6641 struct comp_unit_head header;
6642 unsigned int length;
6644 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6646 /* Initialize it due to a false compiler warning. */
6647 header.signature = -1;
6648 header.type_cu_offset_in_tu = (cu_offset) -1;
6650 /* We need to read the type's signature in order to build the hash
6651 table, but we don't need anything else just yet. */
6653 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6654 abbrev_section, ptr, section_kind);
6656 length = get_cu_length (&header);
6658 /* Skip dummy type units. */
6659 if (ptr >= info_ptr + length
6660 || peek_abbrev_code (abfd, ptr) == 0
6661 || header.unit_type != DW_UT_type)
6667 if (types_htab == NULL)
6670 types_htab = allocate_dwo_unit_table (objfile);
6672 types_htab = allocate_signatured_type_table (objfile);
6678 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6680 dwo_tu->dwo_file = dwo_file;
6681 dwo_tu->signature = header.signature;
6682 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6683 dwo_tu->section = section;
6684 dwo_tu->sect_off = sect_off;
6685 dwo_tu->length = length;
6689 /* N.B.: type_offset is not usable if this type uses a DWO file.
6690 The real type_offset is in the DWO file. */
6692 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6693 struct signatured_type);
6694 sig_type->signature = header.signature;
6695 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6696 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6697 sig_type->per_cu.is_debug_types = 1;
6698 sig_type->per_cu.section = section;
6699 sig_type->per_cu.sect_off = sect_off;
6700 sig_type->per_cu.length = length;
6703 slot = htab_find_slot (types_htab,
6704 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6706 gdb_assert (slot != NULL);
6709 sect_offset dup_sect_off;
6713 const struct dwo_unit *dup_tu
6714 = (const struct dwo_unit *) *slot;
6716 dup_sect_off = dup_tu->sect_off;
6720 const struct signatured_type *dup_tu
6721 = (const struct signatured_type *) *slot;
6723 dup_sect_off = dup_tu->per_cu.sect_off;
6726 complaint (_("debug type entry at offset %s is duplicate to"
6727 " the entry at offset %s, signature %s"),
6728 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6729 hex_string (header.signature));
6731 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6733 if (dwarf_read_debug > 1)
6734 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6735 sect_offset_str (sect_off),
6736 hex_string (header.signature));
6742 /* Create the hash table of all entries in the .debug_types
6743 (or .debug_types.dwo) section(s).
6744 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6745 otherwise it is NULL.
6747 The result is a pointer to the hash table or NULL if there are no types.
6749 Note: This function processes DWO files only, not DWP files. */
6752 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6753 struct dwo_file *dwo_file,
6754 VEC (dwarf2_section_info_def) *types,
6758 struct dwarf2_section_info *section;
6760 if (VEC_empty (dwarf2_section_info_def, types))
6764 VEC_iterate (dwarf2_section_info_def, types, ix, section);
6766 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
6767 types_htab, rcuh_kind::TYPE);
6770 /* Create the hash table of all entries in the .debug_types section,
6771 and initialize all_type_units.
6772 The result is zero if there is an error (e.g. missing .debug_types section),
6773 otherwise non-zero. */
6776 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6778 htab_t types_htab = NULL;
6780 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6781 &dwarf2_per_objfile->info, types_htab,
6782 rcuh_kind::COMPILE);
6783 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6784 dwarf2_per_objfile->types, types_htab);
6785 if (types_htab == NULL)
6787 dwarf2_per_objfile->signatured_types = NULL;
6791 dwarf2_per_objfile->signatured_types = types_htab;
6793 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6794 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6796 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6797 &dwarf2_per_objfile->all_type_units);
6802 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6803 If SLOT is non-NULL, it is the entry to use in the hash table.
6804 Otherwise we find one. */
6806 static struct signatured_type *
6807 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6810 struct objfile *objfile = dwarf2_per_objfile->objfile;
6812 if (dwarf2_per_objfile->all_type_units.size ()
6813 == dwarf2_per_objfile->all_type_units.capacity ())
6814 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6816 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6817 struct signatured_type);
6819 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6820 sig_type->signature = sig;
6821 sig_type->per_cu.is_debug_types = 1;
6822 if (dwarf2_per_objfile->using_index)
6824 sig_type->per_cu.v.quick =
6825 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6826 struct dwarf2_per_cu_quick_data);
6831 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6834 gdb_assert (*slot == NULL);
6836 /* The rest of sig_type must be filled in by the caller. */
6840 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6841 Fill in SIG_ENTRY with DWO_ENTRY. */
6844 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6845 struct signatured_type *sig_entry,
6846 struct dwo_unit *dwo_entry)
6848 /* Make sure we're not clobbering something we don't expect to. */
6849 gdb_assert (! sig_entry->per_cu.queued);
6850 gdb_assert (sig_entry->per_cu.cu == NULL);
6851 if (dwarf2_per_objfile->using_index)
6853 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6854 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6857 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6858 gdb_assert (sig_entry->signature == dwo_entry->signature);
6859 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6860 gdb_assert (sig_entry->type_unit_group == NULL);
6861 gdb_assert (sig_entry->dwo_unit == NULL);
6863 sig_entry->per_cu.section = dwo_entry->section;
6864 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6865 sig_entry->per_cu.length = dwo_entry->length;
6866 sig_entry->per_cu.reading_dwo_directly = 1;
6867 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6868 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6869 sig_entry->dwo_unit = dwo_entry;
6872 /* Subroutine of lookup_signatured_type.
6873 If we haven't read the TU yet, create the signatured_type data structure
6874 for a TU to be read in directly from a DWO file, bypassing the stub.
6875 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6876 using .gdb_index, then when reading a CU we want to stay in the DWO file
6877 containing that CU. Otherwise we could end up reading several other DWO
6878 files (due to comdat folding) to process the transitive closure of all the
6879 mentioned TUs, and that can be slow. The current DWO file will have every
6880 type signature that it needs.
6881 We only do this for .gdb_index because in the psymtab case we already have
6882 to read all the DWOs to build the type unit groups. */
6884 static struct signatured_type *
6885 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6887 struct dwarf2_per_objfile *dwarf2_per_objfile
6888 = cu->per_cu->dwarf2_per_objfile;
6889 struct objfile *objfile = dwarf2_per_objfile->objfile;
6890 struct dwo_file *dwo_file;
6891 struct dwo_unit find_dwo_entry, *dwo_entry;
6892 struct signatured_type find_sig_entry, *sig_entry;
6895 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6897 /* If TU skeletons have been removed then we may not have read in any
6899 if (dwarf2_per_objfile->signatured_types == NULL)
6901 dwarf2_per_objfile->signatured_types
6902 = allocate_signatured_type_table (objfile);
6905 /* We only ever need to read in one copy of a signatured type.
6906 Use the global signatured_types array to do our own comdat-folding
6907 of types. If this is the first time we're reading this TU, and
6908 the TU has an entry in .gdb_index, replace the recorded data from
6909 .gdb_index with this TU. */
6911 find_sig_entry.signature = sig;
6912 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6913 &find_sig_entry, INSERT);
6914 sig_entry = (struct signatured_type *) *slot;
6916 /* We can get here with the TU already read, *or* in the process of being
6917 read. Don't reassign the global entry to point to this DWO if that's
6918 the case. Also note that if the TU is already being read, it may not
6919 have come from a DWO, the program may be a mix of Fission-compiled
6920 code and non-Fission-compiled code. */
6922 /* Have we already tried to read this TU?
6923 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6924 needn't exist in the global table yet). */
6925 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6928 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6929 dwo_unit of the TU itself. */
6930 dwo_file = cu->dwo_unit->dwo_file;
6932 /* Ok, this is the first time we're reading this TU. */
6933 if (dwo_file->tus == NULL)
6935 find_dwo_entry.signature = sig;
6936 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
6937 if (dwo_entry == NULL)
6940 /* If the global table doesn't have an entry for this TU, add one. */
6941 if (sig_entry == NULL)
6942 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6944 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6945 sig_entry->per_cu.tu_read = 1;
6949 /* Subroutine of lookup_signatured_type.
6950 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6951 then try the DWP file. If the TU stub (skeleton) has been removed then
6952 it won't be in .gdb_index. */
6954 static struct signatured_type *
6955 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6957 struct dwarf2_per_objfile *dwarf2_per_objfile
6958 = cu->per_cu->dwarf2_per_objfile;
6959 struct objfile *objfile = dwarf2_per_objfile->objfile;
6960 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6961 struct dwo_unit *dwo_entry;
6962 struct signatured_type find_sig_entry, *sig_entry;
6965 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6966 gdb_assert (dwp_file != NULL);
6968 /* If TU skeletons have been removed then we may not have read in any
6970 if (dwarf2_per_objfile->signatured_types == NULL)
6972 dwarf2_per_objfile->signatured_types
6973 = allocate_signatured_type_table (objfile);
6976 find_sig_entry.signature = sig;
6977 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6978 &find_sig_entry, INSERT);
6979 sig_entry = (struct signatured_type *) *slot;
6981 /* Have we already tried to read this TU?
6982 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6983 needn't exist in the global table yet). */
6984 if (sig_entry != NULL)
6987 if (dwp_file->tus == NULL)
6989 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6990 sig, 1 /* is_debug_types */);
6991 if (dwo_entry == NULL)
6994 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6995 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7000 /* Lookup a signature based type for DW_FORM_ref_sig8.
7001 Returns NULL if signature SIG is not present in the table.
7002 It is up to the caller to complain about this. */
7004 static struct signatured_type *
7005 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7007 struct dwarf2_per_objfile *dwarf2_per_objfile
7008 = cu->per_cu->dwarf2_per_objfile;
7011 && dwarf2_per_objfile->using_index)
7013 /* We're in a DWO/DWP file, and we're using .gdb_index.
7014 These cases require special processing. */
7015 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7016 return lookup_dwo_signatured_type (cu, sig);
7018 return lookup_dwp_signatured_type (cu, sig);
7022 struct signatured_type find_entry, *entry;
7024 if (dwarf2_per_objfile->signatured_types == NULL)
7026 find_entry.signature = sig;
7027 entry = ((struct signatured_type *)
7028 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7033 /* Low level DIE reading support. */
7035 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7038 init_cu_die_reader (struct die_reader_specs *reader,
7039 struct dwarf2_cu *cu,
7040 struct dwarf2_section_info *section,
7041 struct dwo_file *dwo_file,
7042 struct abbrev_table *abbrev_table)
7044 gdb_assert (section->readin && section->buffer != NULL);
7045 reader->abfd = get_section_bfd_owner (section);
7047 reader->dwo_file = dwo_file;
7048 reader->die_section = section;
7049 reader->buffer = section->buffer;
7050 reader->buffer_end = section->buffer + section->size;
7051 reader->comp_dir = NULL;
7052 reader->abbrev_table = abbrev_table;
7055 /* Subroutine of init_cutu_and_read_dies to simplify it.
7056 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7057 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7060 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7061 from it to the DIE in the DWO. If NULL we are skipping the stub.
7062 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7063 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7064 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7065 STUB_COMP_DIR may be non-NULL.
7066 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7067 are filled in with the info of the DIE from the DWO file.
7068 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7069 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7070 kept around for at least as long as *RESULT_READER.
7072 The result is non-zero if a valid (non-dummy) DIE was found. */
7075 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7076 struct dwo_unit *dwo_unit,
7077 struct die_info *stub_comp_unit_die,
7078 const char *stub_comp_dir,
7079 struct die_reader_specs *result_reader,
7080 const gdb_byte **result_info_ptr,
7081 struct die_info **result_comp_unit_die,
7082 int *result_has_children,
7083 abbrev_table_up *result_dwo_abbrev_table)
7085 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7086 struct objfile *objfile = dwarf2_per_objfile->objfile;
7087 struct dwarf2_cu *cu = this_cu->cu;
7089 const gdb_byte *begin_info_ptr, *info_ptr;
7090 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7091 int i,num_extra_attrs;
7092 struct dwarf2_section_info *dwo_abbrev_section;
7093 struct attribute *attr;
7094 struct die_info *comp_unit_die;
7096 /* At most one of these may be provided. */
7097 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7099 /* These attributes aren't processed until later:
7100 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7101 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7102 referenced later. However, these attributes are found in the stub
7103 which we won't have later. In order to not impose this complication
7104 on the rest of the code, we read them here and copy them to the
7113 if (stub_comp_unit_die != NULL)
7115 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7117 if (! this_cu->is_debug_types)
7118 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7119 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7120 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7121 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7122 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7124 /* There should be a DW_AT_addr_base attribute here (if needed).
7125 We need the value before we can process DW_FORM_GNU_addr_index. */
7127 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7129 cu->addr_base = DW_UNSND (attr);
7131 /* There should be a DW_AT_ranges_base attribute here (if needed).
7132 We need the value before we can process DW_AT_ranges. */
7133 cu->ranges_base = 0;
7134 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7136 cu->ranges_base = DW_UNSND (attr);
7138 else if (stub_comp_dir != NULL)
7140 /* Reconstruct the comp_dir attribute to simplify the code below. */
7141 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7142 comp_dir->name = DW_AT_comp_dir;
7143 comp_dir->form = DW_FORM_string;
7144 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7145 DW_STRING (comp_dir) = stub_comp_dir;
7148 /* Set up for reading the DWO CU/TU. */
7149 cu->dwo_unit = dwo_unit;
7150 dwarf2_section_info *section = dwo_unit->section;
7151 dwarf2_read_section (objfile, section);
7152 abfd = get_section_bfd_owner (section);
7153 begin_info_ptr = info_ptr = (section->buffer
7154 + to_underlying (dwo_unit->sect_off));
7155 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7157 if (this_cu->is_debug_types)
7159 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7161 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7162 &cu->header, section,
7164 info_ptr, rcuh_kind::TYPE);
7165 /* This is not an assert because it can be caused by bad debug info. */
7166 if (sig_type->signature != cu->header.signature)
7168 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7169 " TU at offset %s [in module %s]"),
7170 hex_string (sig_type->signature),
7171 hex_string (cu->header.signature),
7172 sect_offset_str (dwo_unit->sect_off),
7173 bfd_get_filename (abfd));
7175 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7176 /* For DWOs coming from DWP files, we don't know the CU length
7177 nor the type's offset in the TU until now. */
7178 dwo_unit->length = get_cu_length (&cu->header);
7179 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7181 /* Establish the type offset that can be used to lookup the type.
7182 For DWO files, we don't know it until now. */
7183 sig_type->type_offset_in_section
7184 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7188 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7189 &cu->header, section,
7191 info_ptr, rcuh_kind::COMPILE);
7192 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7193 /* For DWOs coming from DWP files, we don't know the CU length
7195 dwo_unit->length = get_cu_length (&cu->header);
7198 *result_dwo_abbrev_table
7199 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7200 cu->header.abbrev_sect_off);
7201 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7202 result_dwo_abbrev_table->get ());
7204 /* Read in the die, but leave space to copy over the attributes
7205 from the stub. This has the benefit of simplifying the rest of
7206 the code - all the work to maintain the illusion of a single
7207 DW_TAG_{compile,type}_unit DIE is done here. */
7208 num_extra_attrs = ((stmt_list != NULL)
7212 + (comp_dir != NULL));
7213 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7214 result_has_children, num_extra_attrs);
7216 /* Copy over the attributes from the stub to the DIE we just read in. */
7217 comp_unit_die = *result_comp_unit_die;
7218 i = comp_unit_die->num_attrs;
7219 if (stmt_list != NULL)
7220 comp_unit_die->attrs[i++] = *stmt_list;
7222 comp_unit_die->attrs[i++] = *low_pc;
7223 if (high_pc != NULL)
7224 comp_unit_die->attrs[i++] = *high_pc;
7226 comp_unit_die->attrs[i++] = *ranges;
7227 if (comp_dir != NULL)
7228 comp_unit_die->attrs[i++] = *comp_dir;
7229 comp_unit_die->num_attrs += num_extra_attrs;
7231 if (dwarf_die_debug)
7233 fprintf_unfiltered (gdb_stdlog,
7234 "Read die from %s@0x%x of %s:\n",
7235 get_section_name (section),
7236 (unsigned) (begin_info_ptr - section->buffer),
7237 bfd_get_filename (abfd));
7238 dump_die (comp_unit_die, dwarf_die_debug);
7241 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7242 TUs by skipping the stub and going directly to the entry in the DWO file.
7243 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7244 to get it via circuitous means. Blech. */
7245 if (comp_dir != NULL)
7246 result_reader->comp_dir = DW_STRING (comp_dir);
7248 /* Skip dummy compilation units. */
7249 if (info_ptr >= begin_info_ptr + dwo_unit->length
7250 || peek_abbrev_code (abfd, info_ptr) == 0)
7253 *result_info_ptr = info_ptr;
7257 /* Subroutine of init_cutu_and_read_dies to simplify it.
7258 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7259 Returns NULL if the specified DWO unit cannot be found. */
7261 static struct dwo_unit *
7262 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7263 struct die_info *comp_unit_die)
7265 struct dwarf2_cu *cu = this_cu->cu;
7267 struct dwo_unit *dwo_unit;
7268 const char *comp_dir, *dwo_name;
7270 gdb_assert (cu != NULL);
7272 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7273 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7274 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7276 if (this_cu->is_debug_types)
7278 struct signatured_type *sig_type;
7280 /* Since this_cu is the first member of struct signatured_type,
7281 we can go from a pointer to one to a pointer to the other. */
7282 sig_type = (struct signatured_type *) this_cu;
7283 signature = sig_type->signature;
7284 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7288 struct attribute *attr;
7290 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7292 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7294 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7295 signature = DW_UNSND (attr);
7296 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7303 /* Subroutine of init_cutu_and_read_dies to simplify it.
7304 See it for a description of the parameters.
7305 Read a TU directly from a DWO file, bypassing the stub. */
7308 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7309 int use_existing_cu, int keep,
7310 die_reader_func_ftype *die_reader_func,
7313 std::unique_ptr<dwarf2_cu> new_cu;
7314 struct signatured_type *sig_type;
7315 struct die_reader_specs reader;
7316 const gdb_byte *info_ptr;
7317 struct die_info *comp_unit_die;
7319 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7321 /* Verify we can do the following downcast, and that we have the
7323 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7324 sig_type = (struct signatured_type *) this_cu;
7325 gdb_assert (sig_type->dwo_unit != NULL);
7327 if (use_existing_cu && this_cu->cu != NULL)
7329 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7330 /* There's no need to do the rereading_dwo_cu handling that
7331 init_cutu_and_read_dies does since we don't read the stub. */
7335 /* If !use_existing_cu, this_cu->cu must be NULL. */
7336 gdb_assert (this_cu->cu == NULL);
7337 new_cu.reset (new dwarf2_cu (this_cu));
7340 /* A future optimization, if needed, would be to use an existing
7341 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7342 could share abbrev tables. */
7344 /* The abbreviation table used by READER, this must live at least as long as
7346 abbrev_table_up dwo_abbrev_table;
7348 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7349 NULL /* stub_comp_unit_die */,
7350 sig_type->dwo_unit->dwo_file->comp_dir,
7352 &comp_unit_die, &has_children,
7353 &dwo_abbrev_table) == 0)
7359 /* All the "real" work is done here. */
7360 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7362 /* This duplicates the code in init_cutu_and_read_dies,
7363 but the alternative is making the latter more complex.
7364 This function is only for the special case of using DWO files directly:
7365 no point in overly complicating the general case just to handle this. */
7366 if (new_cu != NULL && keep)
7368 /* Link this CU into read_in_chain. */
7369 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7370 dwarf2_per_objfile->read_in_chain = this_cu;
7371 /* The chain owns it now. */
7376 /* Initialize a CU (or TU) and read its DIEs.
7377 If the CU defers to a DWO file, read the DWO file as well.
7379 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7380 Otherwise the table specified in the comp unit header is read in and used.
7381 This is an optimization for when we already have the abbrev table.
7383 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7384 Otherwise, a new CU is allocated with xmalloc.
7386 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7387 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7389 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7390 linker) then DIE_READER_FUNC will not get called. */
7393 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7394 struct abbrev_table *abbrev_table,
7395 int use_existing_cu, int keep,
7397 die_reader_func_ftype *die_reader_func,
7400 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7401 struct objfile *objfile = dwarf2_per_objfile->objfile;
7402 struct dwarf2_section_info *section = this_cu->section;
7403 bfd *abfd = get_section_bfd_owner (section);
7404 struct dwarf2_cu *cu;
7405 const gdb_byte *begin_info_ptr, *info_ptr;
7406 struct die_reader_specs reader;
7407 struct die_info *comp_unit_die;
7409 struct attribute *attr;
7410 struct signatured_type *sig_type = NULL;
7411 struct dwarf2_section_info *abbrev_section;
7412 /* Non-zero if CU currently points to a DWO file and we need to
7413 reread it. When this happens we need to reread the skeleton die
7414 before we can reread the DWO file (this only applies to CUs, not TUs). */
7415 int rereading_dwo_cu = 0;
7417 if (dwarf_die_debug)
7418 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7419 this_cu->is_debug_types ? "type" : "comp",
7420 sect_offset_str (this_cu->sect_off));
7422 if (use_existing_cu)
7425 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7426 file (instead of going through the stub), short-circuit all of this. */
7427 if (this_cu->reading_dwo_directly)
7429 /* Narrow down the scope of possibilities to have to understand. */
7430 gdb_assert (this_cu->is_debug_types);
7431 gdb_assert (abbrev_table == NULL);
7432 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7433 die_reader_func, data);
7437 /* This is cheap if the section is already read in. */
7438 dwarf2_read_section (objfile, section);
7440 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7442 abbrev_section = get_abbrev_section_for_cu (this_cu);
7444 std::unique_ptr<dwarf2_cu> new_cu;
7445 if (use_existing_cu && this_cu->cu != NULL)
7448 /* If this CU is from a DWO file we need to start over, we need to
7449 refetch the attributes from the skeleton CU.
7450 This could be optimized by retrieving those attributes from when we
7451 were here the first time: the previous comp_unit_die was stored in
7452 comp_unit_obstack. But there's no data yet that we need this
7454 if (cu->dwo_unit != NULL)
7455 rereading_dwo_cu = 1;
7459 /* If !use_existing_cu, this_cu->cu must be NULL. */
7460 gdb_assert (this_cu->cu == NULL);
7461 new_cu.reset (new dwarf2_cu (this_cu));
7465 /* Get the header. */
7466 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7468 /* We already have the header, there's no need to read it in again. */
7469 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7473 if (this_cu->is_debug_types)
7475 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7476 &cu->header, section,
7477 abbrev_section, info_ptr,
7480 /* Since per_cu is the first member of struct signatured_type,
7481 we can go from a pointer to one to a pointer to the other. */
7482 sig_type = (struct signatured_type *) this_cu;
7483 gdb_assert (sig_type->signature == cu->header.signature);
7484 gdb_assert (sig_type->type_offset_in_tu
7485 == cu->header.type_cu_offset_in_tu);
7486 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7488 /* LENGTH has not been set yet for type units if we're
7489 using .gdb_index. */
7490 this_cu->length = get_cu_length (&cu->header);
7492 /* Establish the type offset that can be used to lookup the type. */
7493 sig_type->type_offset_in_section =
7494 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7496 this_cu->dwarf_version = cu->header.version;
7500 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7501 &cu->header, section,
7504 rcuh_kind::COMPILE);
7506 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7507 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7508 this_cu->dwarf_version = cu->header.version;
7512 /* Skip dummy compilation units. */
7513 if (info_ptr >= begin_info_ptr + this_cu->length
7514 || peek_abbrev_code (abfd, info_ptr) == 0)
7517 /* If we don't have them yet, read the abbrevs for this compilation unit.
7518 And if we need to read them now, make sure they're freed when we're
7519 done (own the table through ABBREV_TABLE_HOLDER). */
7520 abbrev_table_up abbrev_table_holder;
7521 if (abbrev_table != NULL)
7522 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7526 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7527 cu->header.abbrev_sect_off);
7528 abbrev_table = abbrev_table_holder.get ();
7531 /* Read the top level CU/TU die. */
7532 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7533 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7535 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7538 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7539 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7540 table from the DWO file and pass the ownership over to us. It will be
7541 referenced from READER, so we must make sure to free it after we're done
7544 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7545 DWO CU, that this test will fail (the attribute will not be present). */
7546 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7547 abbrev_table_up dwo_abbrev_table;
7550 struct dwo_unit *dwo_unit;
7551 struct die_info *dwo_comp_unit_die;
7555 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7556 " has children (offset %s) [in module %s]"),
7557 sect_offset_str (this_cu->sect_off),
7558 bfd_get_filename (abfd));
7560 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7561 if (dwo_unit != NULL)
7563 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7564 comp_unit_die, NULL,
7566 &dwo_comp_unit_die, &has_children,
7567 &dwo_abbrev_table) == 0)
7572 comp_unit_die = dwo_comp_unit_die;
7576 /* Yikes, we couldn't find the rest of the DIE, we only have
7577 the stub. A complaint has already been logged. There's
7578 not much more we can do except pass on the stub DIE to
7579 die_reader_func. We don't want to throw an error on bad
7584 /* All of the above is setup for this call. Yikes. */
7585 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7587 /* Done, clean up. */
7588 if (new_cu != NULL && keep)
7590 /* Link this CU into read_in_chain. */
7591 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7592 dwarf2_per_objfile->read_in_chain = this_cu;
7593 /* The chain owns it now. */
7598 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7599 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7600 to have already done the lookup to find the DWO file).
7602 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7603 THIS_CU->is_debug_types, but nothing else.
7605 We fill in THIS_CU->length.
7607 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7608 linker) then DIE_READER_FUNC will not get called.
7610 THIS_CU->cu is always freed when done.
7611 This is done in order to not leave THIS_CU->cu in a state where we have
7612 to care whether it refers to the "main" CU or the DWO CU. */
7615 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7616 struct dwo_file *dwo_file,
7617 die_reader_func_ftype *die_reader_func,
7620 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7621 struct objfile *objfile = dwarf2_per_objfile->objfile;
7622 struct dwarf2_section_info *section = this_cu->section;
7623 bfd *abfd = get_section_bfd_owner (section);
7624 struct dwarf2_section_info *abbrev_section;
7625 const gdb_byte *begin_info_ptr, *info_ptr;
7626 struct die_reader_specs reader;
7627 struct die_info *comp_unit_die;
7630 if (dwarf_die_debug)
7631 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7632 this_cu->is_debug_types ? "type" : "comp",
7633 sect_offset_str (this_cu->sect_off));
7635 gdb_assert (this_cu->cu == NULL);
7637 abbrev_section = (dwo_file != NULL
7638 ? &dwo_file->sections.abbrev
7639 : get_abbrev_section_for_cu (this_cu));
7641 /* This is cheap if the section is already read in. */
7642 dwarf2_read_section (objfile, section);
7644 struct dwarf2_cu cu (this_cu);
7646 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7647 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7648 &cu.header, section,
7649 abbrev_section, info_ptr,
7650 (this_cu->is_debug_types
7652 : rcuh_kind::COMPILE));
7654 this_cu->length = get_cu_length (&cu.header);
7656 /* Skip dummy compilation units. */
7657 if (info_ptr >= begin_info_ptr + this_cu->length
7658 || peek_abbrev_code (abfd, info_ptr) == 0)
7661 abbrev_table_up abbrev_table
7662 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7663 cu.header.abbrev_sect_off);
7665 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
7666 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7668 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7671 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7672 does not lookup the specified DWO file.
7673 This cannot be used to read DWO files.
7675 THIS_CU->cu is always freed when done.
7676 This is done in order to not leave THIS_CU->cu in a state where we have
7677 to care whether it refers to the "main" CU or the DWO CU.
7678 We can revisit this if the data shows there's a performance issue. */
7681 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7682 die_reader_func_ftype *die_reader_func,
7685 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
7688 /* Type Unit Groups.
7690 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7691 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7692 so that all types coming from the same compilation (.o file) are grouped
7693 together. A future step could be to put the types in the same symtab as
7694 the CU the types ultimately came from. */
7697 hash_type_unit_group (const void *item)
7699 const struct type_unit_group *tu_group
7700 = (const struct type_unit_group *) item;
7702 return hash_stmt_list_entry (&tu_group->hash);
7706 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7708 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7709 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7711 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7714 /* Allocate a hash table for type unit groups. */
7717 allocate_type_unit_groups_table (struct objfile *objfile)
7719 return htab_create_alloc_ex (3,
7720 hash_type_unit_group,
7723 &objfile->objfile_obstack,
7724 hashtab_obstack_allocate,
7725 dummy_obstack_deallocate);
7728 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7729 partial symtabs. We combine several TUs per psymtab to not let the size
7730 of any one psymtab grow too big. */
7731 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7732 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7734 /* Helper routine for get_type_unit_group.
7735 Create the type_unit_group object used to hold one or more TUs. */
7737 static struct type_unit_group *
7738 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7740 struct dwarf2_per_objfile *dwarf2_per_objfile
7741 = cu->per_cu->dwarf2_per_objfile;
7742 struct objfile *objfile = dwarf2_per_objfile->objfile;
7743 struct dwarf2_per_cu_data *per_cu;
7744 struct type_unit_group *tu_group;
7746 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7747 struct type_unit_group);
7748 per_cu = &tu_group->per_cu;
7749 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7751 if (dwarf2_per_objfile->using_index)
7753 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7754 struct dwarf2_per_cu_quick_data);
7758 unsigned int line_offset = to_underlying (line_offset_struct);
7759 struct partial_symtab *pst;
7762 /* Give the symtab a useful name for debug purposes. */
7763 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7764 name = xstrprintf ("<type_units_%d>",
7765 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7767 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
7769 pst = create_partial_symtab (per_cu, name);
7775 tu_group->hash.dwo_unit = cu->dwo_unit;
7776 tu_group->hash.line_sect_off = line_offset_struct;
7781 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7782 STMT_LIST is a DW_AT_stmt_list attribute. */
7784 static struct type_unit_group *
7785 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7787 struct dwarf2_per_objfile *dwarf2_per_objfile
7788 = cu->per_cu->dwarf2_per_objfile;
7789 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7790 struct type_unit_group *tu_group;
7792 unsigned int line_offset;
7793 struct type_unit_group type_unit_group_for_lookup;
7795 if (dwarf2_per_objfile->type_unit_groups == NULL)
7797 dwarf2_per_objfile->type_unit_groups =
7798 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7801 /* Do we need to create a new group, or can we use an existing one? */
7805 line_offset = DW_UNSND (stmt_list);
7806 ++tu_stats->nr_symtab_sharers;
7810 /* Ugh, no stmt_list. Rare, but we have to handle it.
7811 We can do various things here like create one group per TU or
7812 spread them over multiple groups to split up the expansion work.
7813 To avoid worst case scenarios (too many groups or too large groups)
7814 we, umm, group them in bunches. */
7815 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7816 | (tu_stats->nr_stmt_less_type_units
7817 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7818 ++tu_stats->nr_stmt_less_type_units;
7821 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7822 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7823 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7824 &type_unit_group_for_lookup, INSERT);
7827 tu_group = (struct type_unit_group *) *slot;
7828 gdb_assert (tu_group != NULL);
7832 sect_offset line_offset_struct = (sect_offset) line_offset;
7833 tu_group = create_type_unit_group (cu, line_offset_struct);
7835 ++tu_stats->nr_symtabs;
7841 /* Partial symbol tables. */
7843 /* Create a psymtab named NAME and assign it to PER_CU.
7845 The caller must fill in the following details:
7846 dirname, textlow, texthigh. */
7848 static struct partial_symtab *
7849 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7851 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7852 struct partial_symtab *pst;
7854 pst = start_psymtab_common (objfile, name, 0,
7855 objfile->global_psymbols,
7856 objfile->static_psymbols);
7858 pst->psymtabs_addrmap_supported = 1;
7860 /* This is the glue that links PST into GDB's symbol API. */
7861 pst->read_symtab_private = per_cu;
7862 pst->read_symtab = dwarf2_read_symtab;
7863 per_cu->v.psymtab = pst;
7868 /* The DATA object passed to process_psymtab_comp_unit_reader has this
7871 struct process_psymtab_comp_unit_data
7873 /* True if we are reading a DW_TAG_partial_unit. */
7875 int want_partial_unit;
7877 /* The "pretend" language that is used if the CU doesn't declare a
7880 enum language pretend_language;
7883 /* die_reader_func for process_psymtab_comp_unit. */
7886 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7887 const gdb_byte *info_ptr,
7888 struct die_info *comp_unit_die,
7892 struct dwarf2_cu *cu = reader->cu;
7893 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7894 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7895 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7897 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7898 struct partial_symtab *pst;
7899 enum pc_bounds_kind cu_bounds_kind;
7900 const char *filename;
7901 struct process_psymtab_comp_unit_data *info
7902 = (struct process_psymtab_comp_unit_data *) data;
7904 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
7907 gdb_assert (! per_cu->is_debug_types);
7909 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
7911 /* Allocate a new partial symbol table structure. */
7912 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7913 if (filename == NULL)
7916 pst = create_partial_symtab (per_cu, filename);
7918 /* This must be done before calling dwarf2_build_include_psymtabs. */
7919 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7921 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7923 dwarf2_find_base_address (comp_unit_die, cu);
7925 /* Possibly set the default values of LOWPC and HIGHPC from
7927 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7928 &best_highpc, cu, pst);
7929 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7930 /* Store the contiguous range if it is not empty; it can be empty for
7931 CUs with no code. */
7932 addrmap_set_empty (objfile->psymtabs_addrmap,
7933 gdbarch_adjust_dwarf2_addr (gdbarch,
7934 best_lowpc + baseaddr),
7935 gdbarch_adjust_dwarf2_addr (gdbarch,
7936 best_highpc + baseaddr) - 1,
7939 /* Check if comp unit has_children.
7940 If so, read the rest of the partial symbols from this comp unit.
7941 If not, there's no more debug_info for this comp unit. */
7944 struct partial_die_info *first_die;
7945 CORE_ADDR lowpc, highpc;
7947 lowpc = ((CORE_ADDR) -1);
7948 highpc = ((CORE_ADDR) 0);
7950 first_die = load_partial_dies (reader, info_ptr, 1);
7952 scan_partial_symbols (first_die, &lowpc, &highpc,
7953 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7955 /* If we didn't find a lowpc, set it to highpc to avoid
7956 complaints from `maint check'. */
7957 if (lowpc == ((CORE_ADDR) -1))
7960 /* If the compilation unit didn't have an explicit address range,
7961 then use the information extracted from its child dies. */
7962 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7965 best_highpc = highpc;
7968 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
7969 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
7971 end_psymtab_common (objfile, pst);
7973 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
7976 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
7977 struct dwarf2_per_cu_data *iter;
7979 /* Fill in 'dependencies' here; we fill in 'users' in a
7981 pst->number_of_dependencies = len;
7983 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
7985 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7988 pst->dependencies[i] = iter->v.psymtab;
7990 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
7993 /* Get the list of files included in the current compilation unit,
7994 and build a psymtab for each of them. */
7995 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7997 if (dwarf_read_debug)
7999 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8001 fprintf_unfiltered (gdb_stdlog,
8002 "Psymtab for %s unit @%s: %s - %s"
8003 ", %d global, %d static syms\n",
8004 per_cu->is_debug_types ? "type" : "comp",
8005 sect_offset_str (per_cu->sect_off),
8006 paddress (gdbarch, pst->textlow),
8007 paddress (gdbarch, pst->texthigh),
8008 pst->n_global_syms, pst->n_static_syms);
8012 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8013 Process compilation unit THIS_CU for a psymtab. */
8016 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8017 int want_partial_unit,
8018 enum language pretend_language)
8020 /* If this compilation unit was already read in, free the
8021 cached copy in order to read it in again. This is
8022 necessary because we skipped some symbols when we first
8023 read in the compilation unit (see load_partial_dies).
8024 This problem could be avoided, but the benefit is unclear. */
8025 if (this_cu->cu != NULL)
8026 free_one_cached_comp_unit (this_cu);
8028 if (this_cu->is_debug_types)
8029 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8030 build_type_psymtabs_reader, NULL);
8033 process_psymtab_comp_unit_data info;
8034 info.want_partial_unit = want_partial_unit;
8035 info.pretend_language = pretend_language;
8036 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8037 process_psymtab_comp_unit_reader, &info);
8040 /* Age out any secondary CUs. */
8041 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8044 /* Reader function for build_type_psymtabs. */
8047 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8048 const gdb_byte *info_ptr,
8049 struct die_info *type_unit_die,
8053 struct dwarf2_per_objfile *dwarf2_per_objfile
8054 = reader->cu->per_cu->dwarf2_per_objfile;
8055 struct objfile *objfile = dwarf2_per_objfile->objfile;
8056 struct dwarf2_cu *cu = reader->cu;
8057 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8058 struct signatured_type *sig_type;
8059 struct type_unit_group *tu_group;
8060 struct attribute *attr;
8061 struct partial_die_info *first_die;
8062 CORE_ADDR lowpc, highpc;
8063 struct partial_symtab *pst;
8065 gdb_assert (data == NULL);
8066 gdb_assert (per_cu->is_debug_types);
8067 sig_type = (struct signatured_type *) per_cu;
8072 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8073 tu_group = get_type_unit_group (cu, attr);
8075 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8077 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8078 pst = create_partial_symtab (per_cu, "");
8081 first_die = load_partial_dies (reader, info_ptr, 1);
8083 lowpc = (CORE_ADDR) -1;
8084 highpc = (CORE_ADDR) 0;
8085 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8087 end_psymtab_common (objfile, pst);
8090 /* Struct used to sort TUs by their abbreviation table offset. */
8092 struct tu_abbrev_offset
8094 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8095 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8098 signatured_type *sig_type;
8099 sect_offset abbrev_offset;
8102 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8105 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8106 const struct tu_abbrev_offset &b)
8108 return a.abbrev_offset < b.abbrev_offset;
8111 /* Efficiently read all the type units.
8112 This does the bulk of the work for build_type_psymtabs.
8114 The efficiency is because we sort TUs by the abbrev table they use and
8115 only read each abbrev table once. In one program there are 200K TUs
8116 sharing 8K abbrev tables.
8118 The main purpose of this function is to support building the
8119 dwarf2_per_objfile->type_unit_groups table.
8120 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8121 can collapse the search space by grouping them by stmt_list.
8122 The savings can be significant, in the same program from above the 200K TUs
8123 share 8K stmt_list tables.
8125 FUNC is expected to call get_type_unit_group, which will create the
8126 struct type_unit_group if necessary and add it to
8127 dwarf2_per_objfile->type_unit_groups. */
8130 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8132 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8133 abbrev_table_up abbrev_table;
8134 sect_offset abbrev_offset;
8136 /* It's up to the caller to not call us multiple times. */
8137 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8139 if (dwarf2_per_objfile->all_type_units.empty ())
8142 /* TUs typically share abbrev tables, and there can be way more TUs than
8143 abbrev tables. Sort by abbrev table to reduce the number of times we
8144 read each abbrev table in.
8145 Alternatives are to punt or to maintain a cache of abbrev tables.
8146 This is simpler and efficient enough for now.
8148 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8149 symtab to use). Typically TUs with the same abbrev offset have the same
8150 stmt_list value too so in practice this should work well.
8152 The basic algorithm here is:
8154 sort TUs by abbrev table
8155 for each TU with same abbrev table:
8156 read abbrev table if first user
8157 read TU top level DIE
8158 [IWBN if DWO skeletons had DW_AT_stmt_list]
8161 if (dwarf_read_debug)
8162 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8164 /* Sort in a separate table to maintain the order of all_type_units
8165 for .gdb_index: TU indices directly index all_type_units. */
8166 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8167 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8169 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8170 sorted_by_abbrev.emplace_back
8171 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8172 sig_type->per_cu.section,
8173 sig_type->per_cu.sect_off));
8175 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8176 sort_tu_by_abbrev_offset);
8178 abbrev_offset = (sect_offset) ~(unsigned) 0;
8180 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
8182 /* Switch to the next abbrev table if necessary. */
8183 if (abbrev_table == NULL
8184 || tu.abbrev_offset != abbrev_offset)
8186 abbrev_offset = tu.abbrev_offset;
8188 abbrev_table_read_table (dwarf2_per_objfile,
8189 &dwarf2_per_objfile->abbrev,
8191 ++tu_stats->nr_uniq_abbrev_tables;
8194 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
8195 0, 0, false, build_type_psymtabs_reader, NULL);
8199 /* Print collected type unit statistics. */
8202 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8204 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8206 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8207 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8208 dwarf2_per_objfile->all_type_units.size ());
8209 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8210 tu_stats->nr_uniq_abbrev_tables);
8211 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8212 tu_stats->nr_symtabs);
8213 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8214 tu_stats->nr_symtab_sharers);
8215 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8216 tu_stats->nr_stmt_less_type_units);
8217 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8218 tu_stats->nr_all_type_units_reallocs);
8221 /* Traversal function for build_type_psymtabs. */
8224 build_type_psymtab_dependencies (void **slot, void *info)
8226 struct dwarf2_per_objfile *dwarf2_per_objfile
8227 = (struct dwarf2_per_objfile *) info;
8228 struct objfile *objfile = dwarf2_per_objfile->objfile;
8229 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8230 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8231 struct partial_symtab *pst = per_cu->v.psymtab;
8232 int len = VEC_length (sig_type_ptr, tu_group->tus);
8233 struct signatured_type *iter;
8236 gdb_assert (len > 0);
8237 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8239 pst->number_of_dependencies = len;
8241 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8243 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8246 gdb_assert (iter->per_cu.is_debug_types);
8247 pst->dependencies[i] = iter->per_cu.v.psymtab;
8248 iter->type_unit_group = tu_group;
8251 VEC_free (sig_type_ptr, tu_group->tus);
8256 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8257 Build partial symbol tables for the .debug_types comp-units. */
8260 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8262 if (! create_all_type_units (dwarf2_per_objfile))
8265 build_type_psymtabs_1 (dwarf2_per_objfile);
8268 /* Traversal function for process_skeletonless_type_unit.
8269 Read a TU in a DWO file and build partial symbols for it. */
8272 process_skeletonless_type_unit (void **slot, void *info)
8274 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8275 struct dwarf2_per_objfile *dwarf2_per_objfile
8276 = (struct dwarf2_per_objfile *) info;
8277 struct signatured_type find_entry, *entry;
8279 /* If this TU doesn't exist in the global table, add it and read it in. */
8281 if (dwarf2_per_objfile->signatured_types == NULL)
8283 dwarf2_per_objfile->signatured_types
8284 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8287 find_entry.signature = dwo_unit->signature;
8288 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8290 /* If we've already seen this type there's nothing to do. What's happening
8291 is we're doing our own version of comdat-folding here. */
8295 /* This does the job that create_all_type_units would have done for
8297 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8298 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8301 /* This does the job that build_type_psymtabs_1 would have done. */
8302 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
8303 build_type_psymtabs_reader, NULL);
8308 /* Traversal function for process_skeletonless_type_units. */
8311 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8313 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8315 if (dwo_file->tus != NULL)
8317 htab_traverse_noresize (dwo_file->tus,
8318 process_skeletonless_type_unit, info);
8324 /* Scan all TUs of DWO files, verifying we've processed them.
8325 This is needed in case a TU was emitted without its skeleton.
8326 Note: This can't be done until we know what all the DWO files are. */
8329 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8331 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8332 if (get_dwp_file (dwarf2_per_objfile) == NULL
8333 && dwarf2_per_objfile->dwo_files != NULL)
8335 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8336 process_dwo_file_for_skeletonless_type_units,
8337 dwarf2_per_objfile);
8341 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8344 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8346 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8348 struct partial_symtab *pst = per_cu->v.psymtab;
8353 for (int j = 0; j < pst->number_of_dependencies; ++j)
8355 /* Set the 'user' field only if it is not already set. */
8356 if (pst->dependencies[j]->user == NULL)
8357 pst->dependencies[j]->user = pst;
8362 /* Build the partial symbol table by doing a quick pass through the
8363 .debug_info and .debug_abbrev sections. */
8366 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8368 struct objfile *objfile = dwarf2_per_objfile->objfile;
8370 if (dwarf_read_debug)
8372 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8373 objfile_name (objfile));
8376 dwarf2_per_objfile->reading_partial_symbols = 1;
8378 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8380 /* Any cached compilation units will be linked by the per-objfile
8381 read_in_chain. Make sure to free them when we're done. */
8382 free_cached_comp_units freer (dwarf2_per_objfile);
8384 build_type_psymtabs (dwarf2_per_objfile);
8386 create_all_comp_units (dwarf2_per_objfile);
8388 /* Create a temporary address map on a temporary obstack. We later
8389 copy this to the final obstack. */
8390 auto_obstack temp_obstack;
8392 scoped_restore save_psymtabs_addrmap
8393 = make_scoped_restore (&objfile->psymtabs_addrmap,
8394 addrmap_create_mutable (&temp_obstack));
8396 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8397 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8399 /* This has to wait until we read the CUs, we need the list of DWOs. */
8400 process_skeletonless_type_units (dwarf2_per_objfile);
8402 /* Now that all TUs have been processed we can fill in the dependencies. */
8403 if (dwarf2_per_objfile->type_unit_groups != NULL)
8405 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8406 build_type_psymtab_dependencies, dwarf2_per_objfile);
8409 if (dwarf_read_debug)
8410 print_tu_stats (dwarf2_per_objfile);
8412 set_partial_user (dwarf2_per_objfile);
8414 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8415 &objfile->objfile_obstack);
8416 /* At this point we want to keep the address map. */
8417 save_psymtabs_addrmap.release ();
8419 if (dwarf_read_debug)
8420 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8421 objfile_name (objfile));
8424 /* die_reader_func for load_partial_comp_unit. */
8427 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8428 const gdb_byte *info_ptr,
8429 struct die_info *comp_unit_die,
8433 struct dwarf2_cu *cu = reader->cu;
8435 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8437 /* Check if comp unit has_children.
8438 If so, read the rest of the partial symbols from this comp unit.
8439 If not, there's no more debug_info for this comp unit. */
8441 load_partial_dies (reader, info_ptr, 0);
8444 /* Load the partial DIEs for a secondary CU into memory.
8445 This is also used when rereading a primary CU with load_all_dies. */
8448 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8450 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
8451 load_partial_comp_unit_reader, NULL);
8455 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8456 struct dwarf2_section_info *section,
8457 struct dwarf2_section_info *abbrev_section,
8458 unsigned int is_dwz)
8460 const gdb_byte *info_ptr;
8461 struct objfile *objfile = dwarf2_per_objfile->objfile;
8463 if (dwarf_read_debug)
8464 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8465 get_section_name (section),
8466 get_section_file_name (section));
8468 dwarf2_read_section (objfile, section);
8470 info_ptr = section->buffer;
8472 while (info_ptr < section->buffer + section->size)
8474 struct dwarf2_per_cu_data *this_cu;
8476 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8478 comp_unit_head cu_header;
8479 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8480 abbrev_section, info_ptr,
8481 rcuh_kind::COMPILE);
8483 /* Save the compilation unit for later lookup. */
8484 if (cu_header.unit_type != DW_UT_type)
8486 this_cu = XOBNEW (&objfile->objfile_obstack,
8487 struct dwarf2_per_cu_data);
8488 memset (this_cu, 0, sizeof (*this_cu));
8492 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8493 struct signatured_type);
8494 memset (sig_type, 0, sizeof (*sig_type));
8495 sig_type->signature = cu_header.signature;
8496 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8497 this_cu = &sig_type->per_cu;
8499 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8500 this_cu->sect_off = sect_off;
8501 this_cu->length = cu_header.length + cu_header.initial_length_size;
8502 this_cu->is_dwz = is_dwz;
8503 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8504 this_cu->section = section;
8506 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8508 info_ptr = info_ptr + this_cu->length;
8512 /* Create a list of all compilation units in OBJFILE.
8513 This is only done for -readnow and building partial symtabs. */
8516 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8518 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8519 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8520 &dwarf2_per_objfile->abbrev, 0);
8522 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8524 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8528 /* Process all loaded DIEs for compilation unit CU, starting at
8529 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8530 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8531 DW_AT_ranges). See the comments of add_partial_subprogram on how
8532 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8535 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8536 CORE_ADDR *highpc, int set_addrmap,
8537 struct dwarf2_cu *cu)
8539 struct partial_die_info *pdi;
8541 /* Now, march along the PDI's, descending into ones which have
8542 interesting children but skipping the children of the other ones,
8543 until we reach the end of the compilation unit. */
8551 /* Anonymous namespaces or modules have no name but have interesting
8552 children, so we need to look at them. Ditto for anonymous
8555 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8556 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8557 || pdi->tag == DW_TAG_imported_unit
8558 || pdi->tag == DW_TAG_inlined_subroutine)
8562 case DW_TAG_subprogram:
8563 case DW_TAG_inlined_subroutine:
8564 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8566 case DW_TAG_constant:
8567 case DW_TAG_variable:
8568 case DW_TAG_typedef:
8569 case DW_TAG_union_type:
8570 if (!pdi->is_declaration)
8572 add_partial_symbol (pdi, cu);
8575 case DW_TAG_class_type:
8576 case DW_TAG_interface_type:
8577 case DW_TAG_structure_type:
8578 if (!pdi->is_declaration)
8580 add_partial_symbol (pdi, cu);
8582 if ((cu->language == language_rust
8583 || cu->language == language_cplus) && pdi->has_children)
8584 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8587 case DW_TAG_enumeration_type:
8588 if (!pdi->is_declaration)
8589 add_partial_enumeration (pdi, cu);
8591 case DW_TAG_base_type:
8592 case DW_TAG_subrange_type:
8593 /* File scope base type definitions are added to the partial
8595 add_partial_symbol (pdi, cu);
8597 case DW_TAG_namespace:
8598 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8601 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8603 case DW_TAG_imported_unit:
8605 struct dwarf2_per_cu_data *per_cu;
8607 /* For now we don't handle imported units in type units. */
8608 if (cu->per_cu->is_debug_types)
8610 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8611 " supported in type units [in module %s]"),
8612 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8615 per_cu = dwarf2_find_containing_comp_unit
8616 (pdi->d.sect_off, pdi->is_dwz,
8617 cu->per_cu->dwarf2_per_objfile);
8619 /* Go read the partial unit, if needed. */
8620 if (per_cu->v.psymtab == NULL)
8621 process_psymtab_comp_unit (per_cu, 1, cu->language);
8623 VEC_safe_push (dwarf2_per_cu_ptr,
8624 cu->per_cu->imported_symtabs, per_cu);
8627 case DW_TAG_imported_declaration:
8628 add_partial_symbol (pdi, cu);
8635 /* If the die has a sibling, skip to the sibling. */
8637 pdi = pdi->die_sibling;
8641 /* Functions used to compute the fully scoped name of a partial DIE.
8643 Normally, this is simple. For C++, the parent DIE's fully scoped
8644 name is concatenated with "::" and the partial DIE's name.
8645 Enumerators are an exception; they use the scope of their parent
8646 enumeration type, i.e. the name of the enumeration type is not
8647 prepended to the enumerator.
8649 There are two complexities. One is DW_AT_specification; in this
8650 case "parent" means the parent of the target of the specification,
8651 instead of the direct parent of the DIE. The other is compilers
8652 which do not emit DW_TAG_namespace; in this case we try to guess
8653 the fully qualified name of structure types from their members'
8654 linkage names. This must be done using the DIE's children rather
8655 than the children of any DW_AT_specification target. We only need
8656 to do this for structures at the top level, i.e. if the target of
8657 any DW_AT_specification (if any; otherwise the DIE itself) does not
8660 /* Compute the scope prefix associated with PDI's parent, in
8661 compilation unit CU. The result will be allocated on CU's
8662 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8663 field. NULL is returned if no prefix is necessary. */
8665 partial_die_parent_scope (struct partial_die_info *pdi,
8666 struct dwarf2_cu *cu)
8668 const char *grandparent_scope;
8669 struct partial_die_info *parent, *real_pdi;
8671 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8672 then this means the parent of the specification DIE. */
8675 while (real_pdi->has_specification)
8676 real_pdi = find_partial_die (real_pdi->spec_offset,
8677 real_pdi->spec_is_dwz, cu);
8679 parent = real_pdi->die_parent;
8683 if (parent->scope_set)
8684 return parent->scope;
8688 grandparent_scope = partial_die_parent_scope (parent, cu);
8690 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8691 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8692 Work around this problem here. */
8693 if (cu->language == language_cplus
8694 && parent->tag == DW_TAG_namespace
8695 && strcmp (parent->name, "::") == 0
8696 && grandparent_scope == NULL)
8698 parent->scope = NULL;
8699 parent->scope_set = 1;
8703 if (pdi->tag == DW_TAG_enumerator)
8704 /* Enumerators should not get the name of the enumeration as a prefix. */
8705 parent->scope = grandparent_scope;
8706 else if (parent->tag == DW_TAG_namespace
8707 || parent->tag == DW_TAG_module
8708 || parent->tag == DW_TAG_structure_type
8709 || parent->tag == DW_TAG_class_type
8710 || parent->tag == DW_TAG_interface_type
8711 || parent->tag == DW_TAG_union_type
8712 || parent->tag == DW_TAG_enumeration_type)
8714 if (grandparent_scope == NULL)
8715 parent->scope = parent->name;
8717 parent->scope = typename_concat (&cu->comp_unit_obstack,
8719 parent->name, 0, cu);
8723 /* FIXME drow/2004-04-01: What should we be doing with
8724 function-local names? For partial symbols, we should probably be
8726 complaint (_("unhandled containing DIE tag %d for DIE at %s"),
8727 parent->tag, sect_offset_str (pdi->sect_off));
8728 parent->scope = grandparent_scope;
8731 parent->scope_set = 1;
8732 return parent->scope;
8735 /* Return the fully scoped name associated with PDI, from compilation unit
8736 CU. The result will be allocated with malloc. */
8739 partial_die_full_name (struct partial_die_info *pdi,
8740 struct dwarf2_cu *cu)
8742 const char *parent_scope;
8744 /* If this is a template instantiation, we can not work out the
8745 template arguments from partial DIEs. So, unfortunately, we have
8746 to go through the full DIEs. At least any work we do building
8747 types here will be reused if full symbols are loaded later. */
8748 if (pdi->has_template_arguments)
8752 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8754 struct die_info *die;
8755 struct attribute attr;
8756 struct dwarf2_cu *ref_cu = cu;
8758 /* DW_FORM_ref_addr is using section offset. */
8759 attr.name = (enum dwarf_attribute) 0;
8760 attr.form = DW_FORM_ref_addr;
8761 attr.u.unsnd = to_underlying (pdi->sect_off);
8762 die = follow_die_ref (NULL, &attr, &ref_cu);
8764 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8768 parent_scope = partial_die_parent_scope (pdi, cu);
8769 if (parent_scope == NULL)
8772 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
8776 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8778 struct dwarf2_per_objfile *dwarf2_per_objfile
8779 = cu->per_cu->dwarf2_per_objfile;
8780 struct objfile *objfile = dwarf2_per_objfile->objfile;
8781 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8783 const char *actual_name = NULL;
8785 char *built_actual_name;
8787 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8789 built_actual_name = partial_die_full_name (pdi, cu);
8790 if (built_actual_name != NULL)
8791 actual_name = built_actual_name;
8793 if (actual_name == NULL)
8794 actual_name = pdi->name;
8798 case DW_TAG_inlined_subroutine:
8799 case DW_TAG_subprogram:
8800 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
8801 if (pdi->is_external || cu->language == language_ada)
8803 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8804 of the global scope. But in Ada, we want to be able to access
8805 nested procedures globally. So all Ada subprograms are stored
8806 in the global scope. */
8807 add_psymbol_to_list (actual_name, strlen (actual_name),
8808 built_actual_name != NULL,
8809 VAR_DOMAIN, LOC_BLOCK,
8810 &objfile->global_psymbols,
8811 addr, cu->language, objfile);
8815 add_psymbol_to_list (actual_name, strlen (actual_name),
8816 built_actual_name != NULL,
8817 VAR_DOMAIN, LOC_BLOCK,
8818 &objfile->static_psymbols,
8819 addr, cu->language, objfile);
8822 if (pdi->main_subprogram && actual_name != NULL)
8823 set_objfile_main_name (objfile, actual_name, cu->language);
8825 case DW_TAG_constant:
8827 std::vector<partial_symbol *> *list;
8829 if (pdi->is_external)
8830 list = &objfile->global_psymbols;
8832 list = &objfile->static_psymbols;
8833 add_psymbol_to_list (actual_name, strlen (actual_name),
8834 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8835 list, 0, cu->language, objfile);
8838 case DW_TAG_variable:
8840 addr = decode_locdesc (pdi->d.locdesc, cu);
8844 && !dwarf2_per_objfile->has_section_at_zero)
8846 /* A global or static variable may also have been stripped
8847 out by the linker if unused, in which case its address
8848 will be nullified; do not add such variables into partial
8849 symbol table then. */
8851 else if (pdi->is_external)
8854 Don't enter into the minimal symbol tables as there is
8855 a minimal symbol table entry from the ELF symbols already.
8856 Enter into partial symbol table if it has a location
8857 descriptor or a type.
8858 If the location descriptor is missing, new_symbol will create
8859 a LOC_UNRESOLVED symbol, the address of the variable will then
8860 be determined from the minimal symbol table whenever the variable
8862 The address for the partial symbol table entry is not
8863 used by GDB, but it comes in handy for debugging partial symbol
8866 if (pdi->d.locdesc || pdi->has_type)
8867 add_psymbol_to_list (actual_name, strlen (actual_name),
8868 built_actual_name != NULL,
8869 VAR_DOMAIN, LOC_STATIC,
8870 &objfile->global_psymbols,
8872 cu->language, objfile);
8876 int has_loc = pdi->d.locdesc != NULL;
8878 /* Static Variable. Skip symbols whose value we cannot know (those
8879 without location descriptors or constant values). */
8880 if (!has_loc && !pdi->has_const_value)
8882 xfree (built_actual_name);
8886 add_psymbol_to_list (actual_name, strlen (actual_name),
8887 built_actual_name != NULL,
8888 VAR_DOMAIN, LOC_STATIC,
8889 &objfile->static_psymbols,
8890 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
8891 cu->language, objfile);
8894 case DW_TAG_typedef:
8895 case DW_TAG_base_type:
8896 case DW_TAG_subrange_type:
8897 add_psymbol_to_list (actual_name, strlen (actual_name),
8898 built_actual_name != NULL,
8899 VAR_DOMAIN, LOC_TYPEDEF,
8900 &objfile->static_psymbols,
8901 0, cu->language, objfile);
8903 case DW_TAG_imported_declaration:
8904 case DW_TAG_namespace:
8905 add_psymbol_to_list (actual_name, strlen (actual_name),
8906 built_actual_name != NULL,
8907 VAR_DOMAIN, LOC_TYPEDEF,
8908 &objfile->global_psymbols,
8909 0, cu->language, objfile);
8912 add_psymbol_to_list (actual_name, strlen (actual_name),
8913 built_actual_name != NULL,
8914 MODULE_DOMAIN, LOC_TYPEDEF,
8915 &objfile->global_psymbols,
8916 0, cu->language, objfile);
8918 case DW_TAG_class_type:
8919 case DW_TAG_interface_type:
8920 case DW_TAG_structure_type:
8921 case DW_TAG_union_type:
8922 case DW_TAG_enumeration_type:
8923 /* Skip external references. The DWARF standard says in the section
8924 about "Structure, Union, and Class Type Entries": "An incomplete
8925 structure, union or class type is represented by a structure,
8926 union or class entry that does not have a byte size attribute
8927 and that has a DW_AT_declaration attribute." */
8928 if (!pdi->has_byte_size && pdi->is_declaration)
8930 xfree (built_actual_name);
8934 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8935 static vs. global. */
8936 add_psymbol_to_list (actual_name, strlen (actual_name),
8937 built_actual_name != NULL,
8938 STRUCT_DOMAIN, LOC_TYPEDEF,
8939 cu->language == language_cplus
8940 ? &objfile->global_psymbols
8941 : &objfile->static_psymbols,
8942 0, cu->language, objfile);
8945 case DW_TAG_enumerator:
8946 add_psymbol_to_list (actual_name, strlen (actual_name),
8947 built_actual_name != NULL,
8948 VAR_DOMAIN, LOC_CONST,
8949 cu->language == language_cplus
8950 ? &objfile->global_psymbols
8951 : &objfile->static_psymbols,
8952 0, cu->language, objfile);
8958 xfree (built_actual_name);
8961 /* Read a partial die corresponding to a namespace; also, add a symbol
8962 corresponding to that namespace to the symbol table. NAMESPACE is
8963 the name of the enclosing namespace. */
8966 add_partial_namespace (struct partial_die_info *pdi,
8967 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8968 int set_addrmap, struct dwarf2_cu *cu)
8970 /* Add a symbol for the namespace. */
8972 add_partial_symbol (pdi, cu);
8974 /* Now scan partial symbols in that namespace. */
8976 if (pdi->has_children)
8977 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8980 /* Read a partial die corresponding to a Fortran module. */
8983 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8984 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8986 /* Add a symbol for the namespace. */
8988 add_partial_symbol (pdi, cu);
8990 /* Now scan partial symbols in that module. */
8992 if (pdi->has_children)
8993 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8996 /* Read a partial die corresponding to a subprogram or an inlined
8997 subprogram and create a partial symbol for that subprogram.
8998 When the CU language allows it, this routine also defines a partial
8999 symbol for each nested subprogram that this subprogram contains.
9000 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9001 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9003 PDI may also be a lexical block, in which case we simply search
9004 recursively for subprograms defined inside that lexical block.
9005 Again, this is only performed when the CU language allows this
9006 type of definitions. */
9009 add_partial_subprogram (struct partial_die_info *pdi,
9010 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9011 int set_addrmap, struct dwarf2_cu *cu)
9013 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9015 if (pdi->has_pc_info)
9017 if (pdi->lowpc < *lowpc)
9018 *lowpc = pdi->lowpc;
9019 if (pdi->highpc > *highpc)
9020 *highpc = pdi->highpc;
9023 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9024 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9029 baseaddr = ANOFFSET (objfile->section_offsets,
9030 SECT_OFF_TEXT (objfile));
9031 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9032 pdi->lowpc + baseaddr);
9033 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9034 pdi->highpc + baseaddr);
9035 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9036 cu->per_cu->v.psymtab);
9040 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9042 if (!pdi->is_declaration)
9043 /* Ignore subprogram DIEs that do not have a name, they are
9044 illegal. Do not emit a complaint at this point, we will
9045 do so when we convert this psymtab into a symtab. */
9047 add_partial_symbol (pdi, cu);
9051 if (! pdi->has_children)
9054 if (cu->language == language_ada)
9056 pdi = pdi->die_child;
9060 if (pdi->tag == DW_TAG_subprogram
9061 || pdi->tag == DW_TAG_inlined_subroutine
9062 || pdi->tag == DW_TAG_lexical_block)
9063 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9064 pdi = pdi->die_sibling;
9069 /* Read a partial die corresponding to an enumeration type. */
9072 add_partial_enumeration (struct partial_die_info *enum_pdi,
9073 struct dwarf2_cu *cu)
9075 struct partial_die_info *pdi;
9077 if (enum_pdi->name != NULL)
9078 add_partial_symbol (enum_pdi, cu);
9080 pdi = enum_pdi->die_child;
9083 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9084 complaint (_("malformed enumerator DIE ignored"));
9086 add_partial_symbol (pdi, cu);
9087 pdi = pdi->die_sibling;
9091 /* Return the initial uleb128 in the die at INFO_PTR. */
9094 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9096 unsigned int bytes_read;
9098 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9101 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9102 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9104 Return the corresponding abbrev, or NULL if the number is zero (indicating
9105 an empty DIE). In either case *BYTES_READ will be set to the length of
9106 the initial number. */
9108 static struct abbrev_info *
9109 peek_die_abbrev (const die_reader_specs &reader,
9110 const gdb_byte *info_ptr, unsigned int *bytes_read)
9112 dwarf2_cu *cu = reader.cu;
9113 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9114 unsigned int abbrev_number
9115 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9117 if (abbrev_number == 0)
9120 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9123 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9124 " at offset %s [in module %s]"),
9125 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9126 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9132 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9133 Returns a pointer to the end of a series of DIEs, terminated by an empty
9134 DIE. Any children of the skipped DIEs will also be skipped. */
9136 static const gdb_byte *
9137 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9141 unsigned int bytes_read;
9142 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9145 return info_ptr + bytes_read;
9147 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9151 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9152 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9153 abbrev corresponding to that skipped uleb128 should be passed in
9154 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9157 static const gdb_byte *
9158 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9159 struct abbrev_info *abbrev)
9161 unsigned int bytes_read;
9162 struct attribute attr;
9163 bfd *abfd = reader->abfd;
9164 struct dwarf2_cu *cu = reader->cu;
9165 const gdb_byte *buffer = reader->buffer;
9166 const gdb_byte *buffer_end = reader->buffer_end;
9167 unsigned int form, i;
9169 for (i = 0; i < abbrev->num_attrs; i++)
9171 /* The only abbrev we care about is DW_AT_sibling. */
9172 if (abbrev->attrs[i].name == DW_AT_sibling)
9174 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9175 if (attr.form == DW_FORM_ref_addr)
9176 complaint (_("ignoring absolute DW_AT_sibling"));
9179 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9180 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9182 if (sibling_ptr < info_ptr)
9183 complaint (_("DW_AT_sibling points backwards"));
9184 else if (sibling_ptr > reader->buffer_end)
9185 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9191 /* If it isn't DW_AT_sibling, skip this attribute. */
9192 form = abbrev->attrs[i].form;
9196 case DW_FORM_ref_addr:
9197 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9198 and later it is offset sized. */
9199 if (cu->header.version == 2)
9200 info_ptr += cu->header.addr_size;
9202 info_ptr += cu->header.offset_size;
9204 case DW_FORM_GNU_ref_alt:
9205 info_ptr += cu->header.offset_size;
9208 info_ptr += cu->header.addr_size;
9215 case DW_FORM_flag_present:
9216 case DW_FORM_implicit_const:
9228 case DW_FORM_ref_sig8:
9231 case DW_FORM_data16:
9234 case DW_FORM_string:
9235 read_direct_string (abfd, info_ptr, &bytes_read);
9236 info_ptr += bytes_read;
9238 case DW_FORM_sec_offset:
9240 case DW_FORM_GNU_strp_alt:
9241 info_ptr += cu->header.offset_size;
9243 case DW_FORM_exprloc:
9245 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9246 info_ptr += bytes_read;
9248 case DW_FORM_block1:
9249 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9251 case DW_FORM_block2:
9252 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9254 case DW_FORM_block4:
9255 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9259 case DW_FORM_ref_udata:
9260 case DW_FORM_GNU_addr_index:
9261 case DW_FORM_GNU_str_index:
9262 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9264 case DW_FORM_indirect:
9265 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9266 info_ptr += bytes_read;
9267 /* We need to continue parsing from here, so just go back to
9269 goto skip_attribute;
9272 error (_("Dwarf Error: Cannot handle %s "
9273 "in DWARF reader [in module %s]"),
9274 dwarf_form_name (form),
9275 bfd_get_filename (abfd));
9279 if (abbrev->has_children)
9280 return skip_children (reader, info_ptr);
9285 /* Locate ORIG_PDI's sibling.
9286 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9288 static const gdb_byte *
9289 locate_pdi_sibling (const struct die_reader_specs *reader,
9290 struct partial_die_info *orig_pdi,
9291 const gdb_byte *info_ptr)
9293 /* Do we know the sibling already? */
9295 if (orig_pdi->sibling)
9296 return orig_pdi->sibling;
9298 /* Are there any children to deal with? */
9300 if (!orig_pdi->has_children)
9303 /* Skip the children the long way. */
9305 return skip_children (reader, info_ptr);
9308 /* Expand this partial symbol table into a full symbol table. SELF is
9312 dwarf2_read_symtab (struct partial_symtab *self,
9313 struct objfile *objfile)
9315 struct dwarf2_per_objfile *dwarf2_per_objfile
9316 = get_dwarf2_per_objfile (objfile);
9320 warning (_("bug: psymtab for %s is already read in."),
9327 printf_filtered (_("Reading in symbols for %s..."),
9329 gdb_flush (gdb_stdout);
9332 /* If this psymtab is constructed from a debug-only objfile, the
9333 has_section_at_zero flag will not necessarily be correct. We
9334 can get the correct value for this flag by looking at the data
9335 associated with the (presumably stripped) associated objfile. */
9336 if (objfile->separate_debug_objfile_backlink)
9338 struct dwarf2_per_objfile *dpo_backlink
9339 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9341 dwarf2_per_objfile->has_section_at_zero
9342 = dpo_backlink->has_section_at_zero;
9345 dwarf2_per_objfile->reading_partial_symbols = 0;
9347 psymtab_to_symtab_1 (self);
9349 /* Finish up the debug error message. */
9351 printf_filtered (_("done.\n"));
9354 process_cu_includes (dwarf2_per_objfile);
9357 /* Reading in full CUs. */
9359 /* Add PER_CU to the queue. */
9362 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9363 enum language pretend_language)
9365 struct dwarf2_queue_item *item;
9368 item = XNEW (struct dwarf2_queue_item);
9369 item->per_cu = per_cu;
9370 item->pretend_language = pretend_language;
9373 if (dwarf2_queue == NULL)
9374 dwarf2_queue = item;
9376 dwarf2_queue_tail->next = item;
9378 dwarf2_queue_tail = item;
9381 /* If PER_CU is not yet queued, add it to the queue.
9382 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9384 The result is non-zero if PER_CU was queued, otherwise the result is zero
9385 meaning either PER_CU is already queued or it is already loaded.
9387 N.B. There is an invariant here that if a CU is queued then it is loaded.
9388 The caller is required to load PER_CU if we return non-zero. */
9391 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9392 struct dwarf2_per_cu_data *per_cu,
9393 enum language pretend_language)
9395 /* We may arrive here during partial symbol reading, if we need full
9396 DIEs to process an unusual case (e.g. template arguments). Do
9397 not queue PER_CU, just tell our caller to load its DIEs. */
9398 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9400 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9405 /* Mark the dependence relation so that we don't flush PER_CU
9407 if (dependent_cu != NULL)
9408 dwarf2_add_dependence (dependent_cu, per_cu);
9410 /* If it's already on the queue, we have nothing to do. */
9414 /* If the compilation unit is already loaded, just mark it as
9416 if (per_cu->cu != NULL)
9418 per_cu->cu->last_used = 0;
9422 /* Add it to the queue. */
9423 queue_comp_unit (per_cu, pretend_language);
9428 /* Process the queue. */
9431 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9433 struct dwarf2_queue_item *item, *next_item;
9435 if (dwarf_read_debug)
9437 fprintf_unfiltered (gdb_stdlog,
9438 "Expanding one or more symtabs of objfile %s ...\n",
9439 objfile_name (dwarf2_per_objfile->objfile));
9442 /* The queue starts out with one item, but following a DIE reference
9443 may load a new CU, adding it to the end of the queue. */
9444 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9446 if ((dwarf2_per_objfile->using_index
9447 ? !item->per_cu->v.quick->compunit_symtab
9448 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9449 /* Skip dummy CUs. */
9450 && item->per_cu->cu != NULL)
9452 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9453 unsigned int debug_print_threshold;
9456 if (per_cu->is_debug_types)
9458 struct signatured_type *sig_type =
9459 (struct signatured_type *) per_cu;
9461 sprintf (buf, "TU %s at offset %s",
9462 hex_string (sig_type->signature),
9463 sect_offset_str (per_cu->sect_off));
9464 /* There can be 100s of TUs.
9465 Only print them in verbose mode. */
9466 debug_print_threshold = 2;
9470 sprintf (buf, "CU at offset %s",
9471 sect_offset_str (per_cu->sect_off));
9472 debug_print_threshold = 1;
9475 if (dwarf_read_debug >= debug_print_threshold)
9476 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9478 if (per_cu->is_debug_types)
9479 process_full_type_unit (per_cu, item->pretend_language);
9481 process_full_comp_unit (per_cu, item->pretend_language);
9483 if (dwarf_read_debug >= debug_print_threshold)
9484 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9487 item->per_cu->queued = 0;
9488 next_item = item->next;
9492 dwarf2_queue_tail = NULL;
9494 if (dwarf_read_debug)
9496 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9497 objfile_name (dwarf2_per_objfile->objfile));
9501 /* Read in full symbols for PST, and anything it depends on. */
9504 psymtab_to_symtab_1 (struct partial_symtab *pst)
9506 struct dwarf2_per_cu_data *per_cu;
9512 for (i = 0; i < pst->number_of_dependencies; i++)
9513 if (!pst->dependencies[i]->readin
9514 && pst->dependencies[i]->user == NULL)
9516 /* Inform about additional files that need to be read in. */
9519 /* FIXME: i18n: Need to make this a single string. */
9520 fputs_filtered (" ", gdb_stdout);
9522 fputs_filtered ("and ", gdb_stdout);
9524 printf_filtered ("%s...", pst->dependencies[i]->filename);
9525 wrap_here (""); /* Flush output. */
9526 gdb_flush (gdb_stdout);
9528 psymtab_to_symtab_1 (pst->dependencies[i]);
9531 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9535 /* It's an include file, no symbols to read for it.
9536 Everything is in the parent symtab. */
9541 dw2_do_instantiate_symtab (per_cu, false);
9544 /* Trivial hash function for die_info: the hash value of a DIE
9545 is its offset in .debug_info for this objfile. */
9548 die_hash (const void *item)
9550 const struct die_info *die = (const struct die_info *) item;
9552 return to_underlying (die->sect_off);
9555 /* Trivial comparison function for die_info structures: two DIEs
9556 are equal if they have the same offset. */
9559 die_eq (const void *item_lhs, const void *item_rhs)
9561 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9562 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9564 return die_lhs->sect_off == die_rhs->sect_off;
9567 /* die_reader_func for load_full_comp_unit.
9568 This is identical to read_signatured_type_reader,
9569 but is kept separate for now. */
9572 load_full_comp_unit_reader (const struct die_reader_specs *reader,
9573 const gdb_byte *info_ptr,
9574 struct die_info *comp_unit_die,
9578 struct dwarf2_cu *cu = reader->cu;
9579 enum language *language_ptr = (enum language *) data;
9581 gdb_assert (cu->die_hash == NULL);
9583 htab_create_alloc_ex (cu->header.length / 12,
9587 &cu->comp_unit_obstack,
9588 hashtab_obstack_allocate,
9589 dummy_obstack_deallocate);
9592 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9593 &info_ptr, comp_unit_die);
9594 cu->dies = comp_unit_die;
9595 /* comp_unit_die is not stored in die_hash, no need. */
9597 /* We try not to read any attributes in this function, because not
9598 all CUs needed for references have been loaded yet, and symbol
9599 table processing isn't initialized. But we have to set the CU language,
9600 or we won't be able to build types correctly.
9601 Similarly, if we do not read the producer, we can not apply
9602 producer-specific interpretation. */
9603 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
9606 /* Load the DIEs associated with PER_CU into memory. */
9609 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9611 enum language pretend_language)
9613 gdb_assert (! this_cu->is_debug_types);
9615 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
9616 load_full_comp_unit_reader, &pretend_language);
9619 /* Add a DIE to the delayed physname list. */
9622 add_to_method_list (struct type *type, int fnfield_index, int index,
9623 const char *name, struct die_info *die,
9624 struct dwarf2_cu *cu)
9626 struct delayed_method_info mi;
9628 mi.fnfield_index = fnfield_index;
9632 cu->method_list.push_back (mi);
9635 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9636 "const" / "volatile". If so, decrements LEN by the length of the
9637 modifier and return true. Otherwise return false. */
9641 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9643 size_t mod_len = sizeof (mod) - 1;
9644 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9652 /* Compute the physnames of any methods on the CU's method list.
9654 The computation of method physnames is delayed in order to avoid the
9655 (bad) condition that one of the method's formal parameters is of an as yet
9659 compute_delayed_physnames (struct dwarf2_cu *cu)
9661 /* Only C++ delays computing physnames. */
9662 if (cu->method_list.empty ())
9664 gdb_assert (cu->language == language_cplus);
9666 for (const delayed_method_info &mi : cu->method_list)
9668 const char *physname;
9669 struct fn_fieldlist *fn_flp
9670 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9671 physname = dwarf2_physname (mi.name, mi.die, cu);
9672 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9673 = physname ? physname : "";
9675 /* Since there's no tag to indicate whether a method is a
9676 const/volatile overload, extract that information out of the
9678 if (physname != NULL)
9680 size_t len = strlen (physname);
9684 if (physname[len] == ')') /* shortcut */
9686 else if (check_modifier (physname, len, " const"))
9687 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9688 else if (check_modifier (physname, len, " volatile"))
9689 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9696 /* The list is no longer needed. */
9697 cu->method_list.clear ();
9700 /* Go objects should be embedded in a DW_TAG_module DIE,
9701 and it's not clear if/how imported objects will appear.
9702 To keep Go support simple until that's worked out,
9703 go back through what we've read and create something usable.
9704 We could do this while processing each DIE, and feels kinda cleaner,
9705 but that way is more invasive.
9706 This is to, for example, allow the user to type "p var" or "b main"
9707 without having to specify the package name, and allow lookups
9708 of module.object to work in contexts that use the expression
9712 fixup_go_packaging (struct dwarf2_cu *cu)
9714 char *package_name = NULL;
9715 struct pending *list;
9718 for (list = *get_global_symbols (); list != NULL; list = list->next)
9720 for (i = 0; i < list->nsyms; ++i)
9722 struct symbol *sym = list->symbol[i];
9724 if (SYMBOL_LANGUAGE (sym) == language_go
9725 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9727 char *this_package_name = go_symbol_package_name (sym);
9729 if (this_package_name == NULL)
9731 if (package_name == NULL)
9732 package_name = this_package_name;
9735 struct objfile *objfile
9736 = cu->per_cu->dwarf2_per_objfile->objfile;
9737 if (strcmp (package_name, this_package_name) != 0)
9738 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9739 (symbol_symtab (sym) != NULL
9740 ? symtab_to_filename_for_display
9741 (symbol_symtab (sym))
9742 : objfile_name (objfile)),
9743 this_package_name, package_name);
9744 xfree (this_package_name);
9750 if (package_name != NULL)
9752 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9753 const char *saved_package_name
9754 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
9756 strlen (package_name));
9757 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9758 saved_package_name);
9761 sym = allocate_symbol (objfile);
9762 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
9763 SYMBOL_SET_NAMES (sym, saved_package_name,
9764 strlen (saved_package_name), 0, objfile);
9765 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9766 e.g., "main" finds the "main" module and not C's main(). */
9767 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9768 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9769 SYMBOL_TYPE (sym) = type;
9771 add_symbol_to_list (sym, get_global_symbols ());
9773 xfree (package_name);
9777 /* Allocate a fully-qualified name consisting of the two parts on the
9781 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9783 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9786 /* A helper that allocates a struct discriminant_info to attach to a
9789 static struct discriminant_info *
9790 alloc_discriminant_info (struct type *type, int discriminant_index,
9793 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9794 gdb_assert (discriminant_index == -1
9795 || (discriminant_index >= 0
9796 && discriminant_index < TYPE_NFIELDS (type)));
9797 gdb_assert (default_index == -1
9798 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9800 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9802 struct discriminant_info *disc
9803 = ((struct discriminant_info *)
9805 offsetof (struct discriminant_info, discriminants)
9806 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9807 disc->default_index = default_index;
9808 disc->discriminant_index = discriminant_index;
9810 struct dynamic_prop prop;
9811 prop.kind = PROP_UNDEFINED;
9812 prop.data.baton = disc;
9814 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9819 /* Some versions of rustc emitted enums in an unusual way.
9821 Ordinary enums were emitted as unions. The first element of each
9822 structure in the union was named "RUST$ENUM$DISR". This element
9823 held the discriminant.
9825 These versions of Rust also implemented the "non-zero"
9826 optimization. When the enum had two values, and one is empty and
9827 the other holds a pointer that cannot be zero, the pointer is used
9828 as the discriminant, with a zero value meaning the empty variant.
9829 Here, the union's first member is of the form
9830 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9831 where the fieldnos are the indices of the fields that should be
9832 traversed in order to find the field (which may be several fields deep)
9833 and the variantname is the name of the variant of the case when the
9836 This function recognizes whether TYPE is of one of these forms,
9837 and, if so, smashes it to be a variant type. */
9840 quirk_rust_enum (struct type *type, struct objfile *objfile)
9842 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9844 /* We don't need to deal with empty enums. */
9845 if (TYPE_NFIELDS (type) == 0)
9848 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9849 if (TYPE_NFIELDS (type) == 1
9850 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9852 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9854 /* Decode the field name to find the offset of the
9856 ULONGEST bit_offset = 0;
9857 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9858 while (name[0] >= '0' && name[0] <= '9')
9861 unsigned long index = strtoul (name, &tail, 10);
9864 || index >= TYPE_NFIELDS (field_type)
9865 || (TYPE_FIELD_LOC_KIND (field_type, index)
9866 != FIELD_LOC_KIND_BITPOS))
9868 complaint (_("Could not parse Rust enum encoding string \"%s\""
9870 TYPE_FIELD_NAME (type, 0),
9871 objfile_name (objfile));
9876 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9877 field_type = TYPE_FIELD_TYPE (field_type, index);
9880 /* Make a union to hold the variants. */
9881 struct type *union_type = alloc_type (objfile);
9882 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9883 TYPE_NFIELDS (union_type) = 3;
9884 TYPE_FIELDS (union_type)
9885 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9886 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9887 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9889 /* Put the discriminant must at index 0. */
9890 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9891 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9892 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9893 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9895 /* The order of fields doesn't really matter, so put the real
9896 field at index 1 and the data-less field at index 2. */
9897 struct discriminant_info *disc
9898 = alloc_discriminant_info (union_type, 0, 1);
9899 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9900 TYPE_FIELD_NAME (union_type, 1)
9901 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9902 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9903 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9904 TYPE_FIELD_NAME (union_type, 1));
9906 const char *dataless_name
9907 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9909 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9911 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9912 /* NAME points into the original discriminant name, which
9913 already has the correct lifetime. */
9914 TYPE_FIELD_NAME (union_type, 2) = name;
9915 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9916 disc->discriminants[2] = 0;
9918 /* Smash this type to be a structure type. We have to do this
9919 because the type has already been recorded. */
9920 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9921 TYPE_NFIELDS (type) = 1;
9923 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9925 /* Install the variant part. */
9926 TYPE_FIELD_TYPE (type, 0) = union_type;
9927 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9928 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9930 else if (TYPE_NFIELDS (type) == 1)
9932 /* We assume that a union with a single field is a univariant
9934 /* Smash this type to be a structure type. We have to do this
9935 because the type has already been recorded. */
9936 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9938 /* Make a union to hold the variants. */
9939 struct type *union_type = alloc_type (objfile);
9940 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9941 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9942 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9943 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9944 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9946 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9947 const char *variant_name
9948 = rust_last_path_segment (TYPE_NAME (field_type));
9949 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9950 TYPE_NAME (field_type)
9951 = rust_fully_qualify (&objfile->objfile_obstack,
9952 TYPE_NAME (type), variant_name);
9954 /* Install the union in the outer struct type. */
9955 TYPE_NFIELDS (type) = 1;
9957 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9958 TYPE_FIELD_TYPE (type, 0) = union_type;
9959 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9960 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9962 alloc_discriminant_info (union_type, -1, 0);
9966 struct type *disr_type = nullptr;
9967 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9969 disr_type = TYPE_FIELD_TYPE (type, i);
9971 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9973 /* All fields of a true enum will be structs. */
9976 else if (TYPE_NFIELDS (disr_type) == 0)
9978 /* Could be data-less variant, so keep going. */
9979 disr_type = nullptr;
9981 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9982 "RUST$ENUM$DISR") != 0)
9984 /* Not a Rust enum. */
9994 /* If we got here without a discriminant, then it's probably
9996 if (disr_type == nullptr)
9999 /* Smash this type to be a structure type. We have to do this
10000 because the type has already been recorded. */
10001 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10003 /* Make a union to hold the variants. */
10004 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10005 struct type *union_type = alloc_type (objfile);
10006 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10007 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10008 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10009 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10010 TYPE_FIELDS (union_type)
10011 = (struct field *) TYPE_ZALLOC (union_type,
10012 (TYPE_NFIELDS (union_type)
10013 * sizeof (struct field)));
10015 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10016 TYPE_NFIELDS (type) * sizeof (struct field));
10018 /* Install the discriminant at index 0 in the union. */
10019 TYPE_FIELD (union_type, 0) = *disr_field;
10020 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10021 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10023 /* Install the union in the outer struct type. */
10024 TYPE_FIELD_TYPE (type, 0) = union_type;
10025 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10026 TYPE_NFIELDS (type) = 1;
10028 /* Set the size and offset of the union type. */
10029 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10031 /* We need a way to find the correct discriminant given a
10032 variant name. For convenience we build a map here. */
10033 struct type *enum_type = FIELD_TYPE (*disr_field);
10034 std::unordered_map<std::string, ULONGEST> discriminant_map;
10035 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10037 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10040 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10041 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10045 int n_fields = TYPE_NFIELDS (union_type);
10046 struct discriminant_info *disc
10047 = alloc_discriminant_info (union_type, 0, -1);
10048 /* Skip the discriminant here. */
10049 for (int i = 1; i < n_fields; ++i)
10051 /* Find the final word in the name of this variant's type.
10052 That name can be used to look up the correct
10054 const char *variant_name
10055 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10058 auto iter = discriminant_map.find (variant_name);
10059 if (iter != discriminant_map.end ())
10060 disc->discriminants[i] = iter->second;
10062 /* Remove the discriminant field, if it exists. */
10063 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10064 if (TYPE_NFIELDS (sub_type) > 0)
10066 --TYPE_NFIELDS (sub_type);
10067 ++TYPE_FIELDS (sub_type);
10069 TYPE_FIELD_NAME (union_type, i) = variant_name;
10070 TYPE_NAME (sub_type)
10071 = rust_fully_qualify (&objfile->objfile_obstack,
10072 TYPE_NAME (type), variant_name);
10077 /* Rewrite some Rust unions to be structures with variants parts. */
10080 rust_union_quirks (struct dwarf2_cu *cu)
10082 gdb_assert (cu->language == language_rust);
10083 for (type *type_ : cu->rust_unions)
10084 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
10085 /* We don't need this any more. */
10086 cu->rust_unions.clear ();
10089 /* Return the symtab for PER_CU. This works properly regardless of
10090 whether we're using the index or psymtabs. */
10092 static struct compunit_symtab *
10093 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10095 return (per_cu->dwarf2_per_objfile->using_index
10096 ? per_cu->v.quick->compunit_symtab
10097 : per_cu->v.psymtab->compunit_symtab);
10100 /* A helper function for computing the list of all symbol tables
10101 included by PER_CU. */
10104 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
10105 htab_t all_children, htab_t all_type_symtabs,
10106 struct dwarf2_per_cu_data *per_cu,
10107 struct compunit_symtab *immediate_parent)
10111 struct compunit_symtab *cust;
10112 struct dwarf2_per_cu_data *iter;
10114 slot = htab_find_slot (all_children, per_cu, INSERT);
10117 /* This inclusion and its children have been processed. */
10122 /* Only add a CU if it has a symbol table. */
10123 cust = get_compunit_symtab (per_cu);
10126 /* If this is a type unit only add its symbol table if we haven't
10127 seen it yet (type unit per_cu's can share symtabs). */
10128 if (per_cu->is_debug_types)
10130 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10134 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10135 if (cust->user == NULL)
10136 cust->user = immediate_parent;
10141 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10142 if (cust->user == NULL)
10143 cust->user = immediate_parent;
10148 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10151 recursively_compute_inclusions (result, all_children,
10152 all_type_symtabs, iter, cust);
10156 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10160 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10162 gdb_assert (! per_cu->is_debug_types);
10164 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10167 struct dwarf2_per_cu_data *per_cu_iter;
10168 struct compunit_symtab *compunit_symtab_iter;
10169 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
10170 htab_t all_children, all_type_symtabs;
10171 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10173 /* If we don't have a symtab, we can just skip this case. */
10177 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10178 NULL, xcalloc, xfree);
10179 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10180 NULL, xcalloc, xfree);
10183 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10187 recursively_compute_inclusions (&result_symtabs, all_children,
10188 all_type_symtabs, per_cu_iter,
10192 /* Now we have a transitive closure of all the included symtabs. */
10193 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10195 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10196 struct compunit_symtab *, len + 1);
10198 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10199 compunit_symtab_iter);
10201 cust->includes[ix] = compunit_symtab_iter;
10202 cust->includes[len] = NULL;
10204 VEC_free (compunit_symtab_ptr, result_symtabs);
10205 htab_delete (all_children);
10206 htab_delete (all_type_symtabs);
10210 /* Compute the 'includes' field for the symtabs of all the CUs we just
10214 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10216 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
10218 if (! iter->is_debug_types)
10219 compute_compunit_symtab_includes (iter);
10222 dwarf2_per_objfile->just_read_cus.clear ();
10225 /* Generate full symbol information for PER_CU, whose DIEs have
10226 already been loaded into memory. */
10229 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10230 enum language pretend_language)
10232 struct dwarf2_cu *cu = per_cu->cu;
10233 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10234 struct objfile *objfile = dwarf2_per_objfile->objfile;
10235 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10236 CORE_ADDR lowpc, highpc;
10237 struct compunit_symtab *cust;
10238 CORE_ADDR baseaddr;
10239 struct block *static_block;
10242 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10245 scoped_free_pendings free_pending;
10247 /* Clear the list here in case something was left over. */
10248 cu->method_list.clear ();
10250 cu->language = pretend_language;
10251 cu->language_defn = language_def (cu->language);
10253 /* Do line number decoding in read_file_scope () */
10254 process_die (cu->dies, cu);
10256 /* For now fudge the Go package. */
10257 if (cu->language == language_go)
10258 fixup_go_packaging (cu);
10260 /* Now that we have processed all the DIEs in the CU, all the types
10261 should be complete, and it should now be safe to compute all of the
10263 compute_delayed_physnames (cu);
10265 if (cu->language == language_rust)
10266 rust_union_quirks (cu);
10268 /* Some compilers don't define a DW_AT_high_pc attribute for the
10269 compilation unit. If the DW_AT_high_pc is missing, synthesize
10270 it, by scanning the DIE's below the compilation unit. */
10271 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10273 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10274 static_block = end_symtab_get_static_block (addr, 0, 1);
10276 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10277 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10278 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10279 addrmap to help ensure it has an accurate map of pc values belonging to
10281 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10283 cust = end_symtab_from_static_block (static_block,
10284 SECT_OFF_TEXT (objfile), 0);
10288 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10290 /* Set symtab language to language from DW_AT_language. If the
10291 compilation is from a C file generated by language preprocessors, do
10292 not set the language if it was already deduced by start_subfile. */
10293 if (!(cu->language == language_c
10294 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10295 COMPUNIT_FILETABS (cust)->language = cu->language;
10297 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10298 produce DW_AT_location with location lists but it can be possibly
10299 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10300 there were bugs in prologue debug info, fixed later in GCC-4.5
10301 by "unwind info for epilogues" patch (which is not directly related).
10303 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10304 needed, it would be wrong due to missing DW_AT_producer there.
10306 Still one can confuse GDB by using non-standard GCC compilation
10307 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10309 if (cu->has_loclist && gcc_4_minor >= 5)
10310 cust->locations_valid = 1;
10312 if (gcc_4_minor >= 5)
10313 cust->epilogue_unwind_valid = 1;
10315 cust->call_site_htab = cu->call_site_htab;
10318 if (dwarf2_per_objfile->using_index)
10319 per_cu->v.quick->compunit_symtab = cust;
10322 struct partial_symtab *pst = per_cu->v.psymtab;
10323 pst->compunit_symtab = cust;
10327 /* Push it for inclusion processing later. */
10328 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10331 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10332 already been loaded into memory. */
10335 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10336 enum language pretend_language)
10338 struct dwarf2_cu *cu = per_cu->cu;
10339 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10340 struct objfile *objfile = dwarf2_per_objfile->objfile;
10341 struct compunit_symtab *cust;
10342 struct signatured_type *sig_type;
10344 gdb_assert (per_cu->is_debug_types);
10345 sig_type = (struct signatured_type *) per_cu;
10348 scoped_free_pendings free_pending;
10350 /* Clear the list here in case something was left over. */
10351 cu->method_list.clear ();
10353 cu->language = pretend_language;
10354 cu->language_defn = language_def (cu->language);
10356 /* The symbol tables are set up in read_type_unit_scope. */
10357 process_die (cu->dies, cu);
10359 /* For now fudge the Go package. */
10360 if (cu->language == language_go)
10361 fixup_go_packaging (cu);
10363 /* Now that we have processed all the DIEs in the CU, all the types
10364 should be complete, and it should now be safe to compute all of the
10366 compute_delayed_physnames (cu);
10368 if (cu->language == language_rust)
10369 rust_union_quirks (cu);
10371 /* TUs share symbol tables.
10372 If this is the first TU to use this symtab, complete the construction
10373 of it with end_expandable_symtab. Otherwise, complete the addition of
10374 this TU's symbols to the existing symtab. */
10375 if (sig_type->type_unit_group->compunit_symtab == NULL)
10377 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10378 sig_type->type_unit_group->compunit_symtab = cust;
10382 /* Set symtab language to language from DW_AT_language. If the
10383 compilation is from a C file generated by language preprocessors,
10384 do not set the language if it was already deduced by
10386 if (!(cu->language == language_c
10387 && COMPUNIT_FILETABS (cust)->language != language_c))
10388 COMPUNIT_FILETABS (cust)->language = cu->language;
10393 augment_type_symtab ();
10394 cust = sig_type->type_unit_group->compunit_symtab;
10397 if (dwarf2_per_objfile->using_index)
10398 per_cu->v.quick->compunit_symtab = cust;
10401 struct partial_symtab *pst = per_cu->v.psymtab;
10402 pst->compunit_symtab = cust;
10407 /* Process an imported unit DIE. */
10410 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10412 struct attribute *attr;
10414 /* For now we don't handle imported units in type units. */
10415 if (cu->per_cu->is_debug_types)
10417 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10418 " supported in type units [in module %s]"),
10419 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10422 attr = dwarf2_attr (die, DW_AT_import, cu);
10425 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10426 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10427 dwarf2_per_cu_data *per_cu
10428 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10429 cu->per_cu->dwarf2_per_objfile);
10431 /* If necessary, add it to the queue and load its DIEs. */
10432 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10433 load_full_comp_unit (per_cu, false, cu->language);
10435 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10440 /* RAII object that represents a process_die scope: i.e.,
10441 starts/finishes processing a DIE. */
10442 class process_die_scope
10445 process_die_scope (die_info *die, dwarf2_cu *cu)
10446 : m_die (die), m_cu (cu)
10448 /* We should only be processing DIEs not already in process. */
10449 gdb_assert (!m_die->in_process);
10450 m_die->in_process = true;
10453 ~process_die_scope ()
10455 m_die->in_process = false;
10457 /* If we're done processing the DIE for the CU that owns the line
10458 header, we don't need the line header anymore. */
10459 if (m_cu->line_header_die_owner == m_die)
10461 delete m_cu->line_header;
10462 m_cu->line_header = NULL;
10463 m_cu->line_header_die_owner = NULL;
10472 /* Process a die and its children. */
10475 process_die (struct die_info *die, struct dwarf2_cu *cu)
10477 process_die_scope scope (die, cu);
10481 case DW_TAG_padding:
10483 case DW_TAG_compile_unit:
10484 case DW_TAG_partial_unit:
10485 read_file_scope (die, cu);
10487 case DW_TAG_type_unit:
10488 read_type_unit_scope (die, cu);
10490 case DW_TAG_subprogram:
10491 case DW_TAG_inlined_subroutine:
10492 read_func_scope (die, cu);
10494 case DW_TAG_lexical_block:
10495 case DW_TAG_try_block:
10496 case DW_TAG_catch_block:
10497 read_lexical_block_scope (die, cu);
10499 case DW_TAG_call_site:
10500 case DW_TAG_GNU_call_site:
10501 read_call_site_scope (die, cu);
10503 case DW_TAG_class_type:
10504 case DW_TAG_interface_type:
10505 case DW_TAG_structure_type:
10506 case DW_TAG_union_type:
10507 process_structure_scope (die, cu);
10509 case DW_TAG_enumeration_type:
10510 process_enumeration_scope (die, cu);
10513 /* These dies have a type, but processing them does not create
10514 a symbol or recurse to process the children. Therefore we can
10515 read them on-demand through read_type_die. */
10516 case DW_TAG_subroutine_type:
10517 case DW_TAG_set_type:
10518 case DW_TAG_array_type:
10519 case DW_TAG_pointer_type:
10520 case DW_TAG_ptr_to_member_type:
10521 case DW_TAG_reference_type:
10522 case DW_TAG_rvalue_reference_type:
10523 case DW_TAG_string_type:
10526 case DW_TAG_base_type:
10527 case DW_TAG_subrange_type:
10528 case DW_TAG_typedef:
10529 /* Add a typedef symbol for the type definition, if it has a
10531 new_symbol (die, read_type_die (die, cu), cu);
10533 case DW_TAG_common_block:
10534 read_common_block (die, cu);
10536 case DW_TAG_common_inclusion:
10538 case DW_TAG_namespace:
10539 cu->processing_has_namespace_info = 1;
10540 read_namespace (die, cu);
10542 case DW_TAG_module:
10543 cu->processing_has_namespace_info = 1;
10544 read_module (die, cu);
10546 case DW_TAG_imported_declaration:
10547 cu->processing_has_namespace_info = 1;
10548 if (read_namespace_alias (die, cu))
10550 /* The declaration is not a global namespace alias. */
10551 /* Fall through. */
10552 case DW_TAG_imported_module:
10553 cu->processing_has_namespace_info = 1;
10554 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10555 || cu->language != language_fortran))
10556 complaint (_("Tag '%s' has unexpected children"),
10557 dwarf_tag_name (die->tag));
10558 read_import_statement (die, cu);
10561 case DW_TAG_imported_unit:
10562 process_imported_unit_die (die, cu);
10565 case DW_TAG_variable:
10566 read_variable (die, cu);
10570 new_symbol (die, NULL, cu);
10575 /* DWARF name computation. */
10577 /* A helper function for dwarf2_compute_name which determines whether DIE
10578 needs to have the name of the scope prepended to the name listed in the
10582 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10584 struct attribute *attr;
10588 case DW_TAG_namespace:
10589 case DW_TAG_typedef:
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 case DW_TAG_enumeration_type:
10595 case DW_TAG_enumerator:
10596 case DW_TAG_subprogram:
10597 case DW_TAG_inlined_subroutine:
10598 case DW_TAG_member:
10599 case DW_TAG_imported_declaration:
10602 case DW_TAG_variable:
10603 case DW_TAG_constant:
10604 /* We only need to prefix "globally" visible variables. These include
10605 any variable marked with DW_AT_external or any variable that
10606 lives in a namespace. [Variables in anonymous namespaces
10607 require prefixing, but they are not DW_AT_external.] */
10609 if (dwarf2_attr (die, DW_AT_specification, cu))
10611 struct dwarf2_cu *spec_cu = cu;
10613 return die_needs_namespace (die_specification (die, &spec_cu),
10617 attr = dwarf2_attr (die, DW_AT_external, cu);
10618 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10619 && die->parent->tag != DW_TAG_module)
10621 /* A variable in a lexical block of some kind does not need a
10622 namespace, even though in C++ such variables may be external
10623 and have a mangled name. */
10624 if (die->parent->tag == DW_TAG_lexical_block
10625 || die->parent->tag == DW_TAG_try_block
10626 || die->parent->tag == DW_TAG_catch_block
10627 || die->parent->tag == DW_TAG_subprogram)
10636 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10637 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10638 defined for the given DIE. */
10640 static struct attribute *
10641 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10643 struct attribute *attr;
10645 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10647 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10652 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10653 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10654 defined for the given DIE. */
10656 static const char *
10657 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10659 const char *linkage_name;
10661 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10662 if (linkage_name == NULL)
10663 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10665 return linkage_name;
10668 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10669 compute the physname for the object, which include a method's:
10670 - formal parameters (C++),
10671 - receiver type (Go),
10673 The term "physname" is a bit confusing.
10674 For C++, for example, it is the demangled name.
10675 For Go, for example, it's the mangled name.
10677 For Ada, return the DIE's linkage name rather than the fully qualified
10678 name. PHYSNAME is ignored..
10680 The result is allocated on the objfile_obstack and canonicalized. */
10682 static const char *
10683 dwarf2_compute_name (const char *name,
10684 struct die_info *die, struct dwarf2_cu *cu,
10687 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10690 name = dwarf2_name (die, cu);
10692 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10693 but otherwise compute it by typename_concat inside GDB.
10694 FIXME: Actually this is not really true, or at least not always true.
10695 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10696 Fortran names because there is no mangling standard. So new_symbol
10697 will set the demangled name to the result of dwarf2_full_name, and it is
10698 the demangled name that GDB uses if it exists. */
10699 if (cu->language == language_ada
10700 || (cu->language == language_fortran && physname))
10702 /* For Ada unit, we prefer the linkage name over the name, as
10703 the former contains the exported name, which the user expects
10704 to be able to reference. Ideally, we want the user to be able
10705 to reference this entity using either natural or linkage name,
10706 but we haven't started looking at this enhancement yet. */
10707 const char *linkage_name = dw2_linkage_name (die, cu);
10709 if (linkage_name != NULL)
10710 return linkage_name;
10713 /* These are the only languages we know how to qualify names in. */
10715 && (cu->language == language_cplus
10716 || cu->language == language_fortran || cu->language == language_d
10717 || cu->language == language_rust))
10719 if (die_needs_namespace (die, cu))
10721 const char *prefix;
10722 const char *canonical_name = NULL;
10726 prefix = determine_prefix (die, cu);
10727 if (*prefix != '\0')
10729 char *prefixed_name = typename_concat (NULL, prefix, name,
10732 buf.puts (prefixed_name);
10733 xfree (prefixed_name);
10738 /* Template parameters may be specified in the DIE's DW_AT_name, or
10739 as children with DW_TAG_template_type_param or
10740 DW_TAG_value_type_param. If the latter, add them to the name
10741 here. If the name already has template parameters, then
10742 skip this step; some versions of GCC emit both, and
10743 it is more efficient to use the pre-computed name.
10745 Something to keep in mind about this process: it is very
10746 unlikely, or in some cases downright impossible, to produce
10747 something that will match the mangled name of a function.
10748 If the definition of the function has the same debug info,
10749 we should be able to match up with it anyway. But fallbacks
10750 using the minimal symbol, for instance to find a method
10751 implemented in a stripped copy of libstdc++, will not work.
10752 If we do not have debug info for the definition, we will have to
10753 match them up some other way.
10755 When we do name matching there is a related problem with function
10756 templates; two instantiated function templates are allowed to
10757 differ only by their return types, which we do not add here. */
10759 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10761 struct attribute *attr;
10762 struct die_info *child;
10765 die->building_fullname = 1;
10767 for (child = die->child; child != NULL; child = child->sibling)
10771 const gdb_byte *bytes;
10772 struct dwarf2_locexpr_baton *baton;
10775 if (child->tag != DW_TAG_template_type_param
10776 && child->tag != DW_TAG_template_value_param)
10787 attr = dwarf2_attr (child, DW_AT_type, cu);
10790 complaint (_("template parameter missing DW_AT_type"));
10791 buf.puts ("UNKNOWN_TYPE");
10794 type = die_type (child, cu);
10796 if (child->tag == DW_TAG_template_type_param)
10798 c_print_type (type, "", &buf, -1, 0, cu->language,
10799 &type_print_raw_options);
10803 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10806 complaint (_("template parameter missing "
10807 "DW_AT_const_value"));
10808 buf.puts ("UNKNOWN_VALUE");
10812 dwarf2_const_value_attr (attr, type, name,
10813 &cu->comp_unit_obstack, cu,
10814 &value, &bytes, &baton);
10816 if (TYPE_NOSIGN (type))
10817 /* GDB prints characters as NUMBER 'CHAR'. If that's
10818 changed, this can use value_print instead. */
10819 c_printchar (value, type, &buf);
10822 struct value_print_options opts;
10825 v = dwarf2_evaluate_loc_desc (type, NULL,
10829 else if (bytes != NULL)
10831 v = allocate_value (type);
10832 memcpy (value_contents_writeable (v), bytes,
10833 TYPE_LENGTH (type));
10836 v = value_from_longest (type, value);
10838 /* Specify decimal so that we do not depend on
10840 get_formatted_print_options (&opts, 'd');
10842 value_print (v, &buf, &opts);
10847 die->building_fullname = 0;
10851 /* Close the argument list, with a space if necessary
10852 (nested templates). */
10853 if (!buf.empty () && buf.string ().back () == '>')
10860 /* For C++ methods, append formal parameter type
10861 information, if PHYSNAME. */
10863 if (physname && die->tag == DW_TAG_subprogram
10864 && cu->language == language_cplus)
10866 struct type *type = read_type_die (die, cu);
10868 c_type_print_args (type, &buf, 1, cu->language,
10869 &type_print_raw_options);
10871 if (cu->language == language_cplus)
10873 /* Assume that an artificial first parameter is
10874 "this", but do not crash if it is not. RealView
10875 marks unnamed (and thus unused) parameters as
10876 artificial; there is no way to differentiate
10878 if (TYPE_NFIELDS (type) > 0
10879 && TYPE_FIELD_ARTIFICIAL (type, 0)
10880 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10881 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10883 buf.puts (" const");
10887 const std::string &intermediate_name = buf.string ();
10889 if (cu->language == language_cplus)
10891 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10892 &objfile->per_bfd->storage_obstack);
10894 /* If we only computed INTERMEDIATE_NAME, or if
10895 INTERMEDIATE_NAME is already canonical, then we need to
10896 copy it to the appropriate obstack. */
10897 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10898 name = ((const char *)
10899 obstack_copy0 (&objfile->per_bfd->storage_obstack,
10900 intermediate_name.c_str (),
10901 intermediate_name.length ()));
10903 name = canonical_name;
10910 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10911 If scope qualifiers are appropriate they will be added. The result
10912 will be allocated on the storage_obstack, or NULL if the DIE does
10913 not have a name. NAME may either be from a previous call to
10914 dwarf2_name or NULL.
10916 The output string will be canonicalized (if C++). */
10918 static const char *
10919 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10921 return dwarf2_compute_name (name, die, cu, 0);
10924 /* Construct a physname for the given DIE in CU. NAME may either be
10925 from a previous call to dwarf2_name or NULL. The result will be
10926 allocated on the objfile_objstack or NULL if the DIE does not have a
10929 The output string will be canonicalized (if C++). */
10931 static const char *
10932 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10934 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10935 const char *retval, *mangled = NULL, *canon = NULL;
10938 /* In this case dwarf2_compute_name is just a shortcut not building anything
10940 if (!die_needs_namespace (die, cu))
10941 return dwarf2_compute_name (name, die, cu, 1);
10943 mangled = dw2_linkage_name (die, cu);
10945 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10946 See https://github.com/rust-lang/rust/issues/32925. */
10947 if (cu->language == language_rust && mangled != NULL
10948 && strchr (mangled, '{') != NULL)
10951 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10953 gdb::unique_xmalloc_ptr<char> demangled;
10954 if (mangled != NULL)
10957 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10959 /* Do nothing (do not demangle the symbol name). */
10961 else if (cu->language == language_go)
10963 /* This is a lie, but we already lie to the caller new_symbol.
10964 new_symbol assumes we return the mangled name.
10965 This just undoes that lie until things are cleaned up. */
10969 /* Use DMGL_RET_DROP for C++ template functions to suppress
10970 their return type. It is easier for GDB users to search
10971 for such functions as `name(params)' than `long name(params)'.
10972 In such case the minimal symbol names do not match the full
10973 symbol names but for template functions there is never a need
10974 to look up their definition from their declaration so
10975 the only disadvantage remains the minimal symbol variant
10976 `long name(params)' does not have the proper inferior type. */
10977 demangled.reset (gdb_demangle (mangled,
10978 (DMGL_PARAMS | DMGL_ANSI
10979 | DMGL_RET_DROP)));
10982 canon = demangled.get ();
10990 if (canon == NULL || check_physname)
10992 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10994 if (canon != NULL && strcmp (physname, canon) != 0)
10996 /* It may not mean a bug in GDB. The compiler could also
10997 compute DW_AT_linkage_name incorrectly. But in such case
10998 GDB would need to be bug-to-bug compatible. */
11000 complaint (_("Computed physname <%s> does not match demangled <%s> "
11001 "(from linkage <%s>) - DIE at %s [in module %s]"),
11002 physname, canon, mangled, sect_offset_str (die->sect_off),
11003 objfile_name (objfile));
11005 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11006 is available here - over computed PHYSNAME. It is safer
11007 against both buggy GDB and buggy compilers. */
11021 retval = ((const char *)
11022 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11023 retval, strlen (retval)));
11028 /* Inspect DIE in CU for a namespace alias. If one exists, record
11029 a new symbol for it.
11031 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11034 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11036 struct attribute *attr;
11038 /* If the die does not have a name, this is not a namespace
11040 attr = dwarf2_attr (die, DW_AT_name, cu);
11044 struct die_info *d = die;
11045 struct dwarf2_cu *imported_cu = cu;
11047 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11048 keep inspecting DIEs until we hit the underlying import. */
11049 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11050 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11052 attr = dwarf2_attr (d, DW_AT_import, cu);
11056 d = follow_die_ref (d, attr, &imported_cu);
11057 if (d->tag != DW_TAG_imported_declaration)
11061 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11063 complaint (_("DIE at %s has too many recursively imported "
11064 "declarations"), sect_offset_str (d->sect_off));
11071 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11073 type = get_die_type_at_offset (sect_off, cu->per_cu);
11074 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11076 /* This declaration is a global namespace alias. Add
11077 a symbol for it whose type is the aliased namespace. */
11078 new_symbol (die, type, cu);
11087 /* Return the using directives repository (global or local?) to use in the
11088 current context for LANGUAGE.
11090 For Ada, imported declarations can materialize renamings, which *may* be
11091 global. However it is impossible (for now?) in DWARF to distinguish
11092 "external" imported declarations and "static" ones. As all imported
11093 declarations seem to be static in all other languages, make them all CU-wide
11094 global only in Ada. */
11096 static struct using_direct **
11097 using_directives (enum language language)
11099 if (language == language_ada && outermost_context_p ())
11100 return get_global_using_directives ();
11102 return get_local_using_directives ();
11105 /* Read the import statement specified by the given die and record it. */
11108 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11110 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11111 struct attribute *import_attr;
11112 struct die_info *imported_die, *child_die;
11113 struct dwarf2_cu *imported_cu;
11114 const char *imported_name;
11115 const char *imported_name_prefix;
11116 const char *canonical_name;
11117 const char *import_alias;
11118 const char *imported_declaration = NULL;
11119 const char *import_prefix;
11120 std::vector<const char *> excludes;
11122 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11123 if (import_attr == NULL)
11125 complaint (_("Tag '%s' has no DW_AT_import"),
11126 dwarf_tag_name (die->tag));
11131 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11132 imported_name = dwarf2_name (imported_die, imported_cu);
11133 if (imported_name == NULL)
11135 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11137 The import in the following code:
11151 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11152 <52> DW_AT_decl_file : 1
11153 <53> DW_AT_decl_line : 6
11154 <54> DW_AT_import : <0x75>
11155 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11156 <59> DW_AT_name : B
11157 <5b> DW_AT_decl_file : 1
11158 <5c> DW_AT_decl_line : 2
11159 <5d> DW_AT_type : <0x6e>
11161 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11162 <76> DW_AT_byte_size : 4
11163 <77> DW_AT_encoding : 5 (signed)
11165 imports the wrong die ( 0x75 instead of 0x58 ).
11166 This case will be ignored until the gcc bug is fixed. */
11170 /* Figure out the local name after import. */
11171 import_alias = dwarf2_name (die, cu);
11173 /* Figure out where the statement is being imported to. */
11174 import_prefix = determine_prefix (die, cu);
11176 /* Figure out what the scope of the imported die is and prepend it
11177 to the name of the imported die. */
11178 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11180 if (imported_die->tag != DW_TAG_namespace
11181 && imported_die->tag != DW_TAG_module)
11183 imported_declaration = imported_name;
11184 canonical_name = imported_name_prefix;
11186 else if (strlen (imported_name_prefix) > 0)
11187 canonical_name = obconcat (&objfile->objfile_obstack,
11188 imported_name_prefix,
11189 (cu->language == language_d ? "." : "::"),
11190 imported_name, (char *) NULL);
11192 canonical_name = imported_name;
11194 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11195 for (child_die = die->child; child_die && child_die->tag;
11196 child_die = sibling_die (child_die))
11198 /* DWARF-4: A Fortran use statement with a “rename list” may be
11199 represented by an imported module entry with an import attribute
11200 referring to the module and owned entries corresponding to those
11201 entities that are renamed as part of being imported. */
11203 if (child_die->tag != DW_TAG_imported_declaration)
11205 complaint (_("child DW_TAG_imported_declaration expected "
11206 "- DIE at %s [in module %s]"),
11207 sect_offset_str (child_die->sect_off),
11208 objfile_name (objfile));
11212 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11213 if (import_attr == NULL)
11215 complaint (_("Tag '%s' has no DW_AT_import"),
11216 dwarf_tag_name (child_die->tag));
11221 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11223 imported_name = dwarf2_name (imported_die, imported_cu);
11224 if (imported_name == NULL)
11226 complaint (_("child DW_TAG_imported_declaration has unknown "
11227 "imported name - DIE at %s [in module %s]"),
11228 sect_offset_str (child_die->sect_off),
11229 objfile_name (objfile));
11233 excludes.push_back (imported_name);
11235 process_die (child_die, cu);
11238 add_using_directive (using_directives (cu->language),
11242 imported_declaration,
11245 &objfile->objfile_obstack);
11248 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11249 types, but gives them a size of zero. Starting with version 14,
11250 ICC is compatible with GCC. */
11253 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11255 if (!cu->checked_producer)
11256 check_producer (cu);
11258 return cu->producer_is_icc_lt_14;
11261 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11262 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11263 this, it was first present in GCC release 4.3.0. */
11266 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11268 if (!cu->checked_producer)
11269 check_producer (cu);
11271 return cu->producer_is_gcc_lt_4_3;
11274 static file_and_directory
11275 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11277 file_and_directory res;
11279 /* Find the filename. Do not use dwarf2_name here, since the filename
11280 is not a source language identifier. */
11281 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11282 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11284 if (res.comp_dir == NULL
11285 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11286 && IS_ABSOLUTE_PATH (res.name))
11288 res.comp_dir_storage = ldirname (res.name);
11289 if (!res.comp_dir_storage.empty ())
11290 res.comp_dir = res.comp_dir_storage.c_str ();
11292 if (res.comp_dir != NULL)
11294 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11295 directory, get rid of it. */
11296 const char *cp = strchr (res.comp_dir, ':');
11298 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11299 res.comp_dir = cp + 1;
11302 if (res.name == NULL)
11303 res.name = "<unknown>";
11308 /* Handle DW_AT_stmt_list for a compilation unit.
11309 DIE is the DW_TAG_compile_unit die for CU.
11310 COMP_DIR is the compilation directory. LOWPC is passed to
11311 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11314 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11315 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11317 struct dwarf2_per_objfile *dwarf2_per_objfile
11318 = cu->per_cu->dwarf2_per_objfile;
11319 struct objfile *objfile = dwarf2_per_objfile->objfile;
11320 struct attribute *attr;
11321 struct line_header line_header_local;
11322 hashval_t line_header_local_hash;
11324 int decode_mapping;
11326 gdb_assert (! cu->per_cu->is_debug_types);
11328 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11332 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11334 /* The line header hash table is only created if needed (it exists to
11335 prevent redundant reading of the line table for partial_units).
11336 If we're given a partial_unit, we'll need it. If we're given a
11337 compile_unit, then use the line header hash table if it's already
11338 created, but don't create one just yet. */
11340 if (dwarf2_per_objfile->line_header_hash == NULL
11341 && die->tag == DW_TAG_partial_unit)
11343 dwarf2_per_objfile->line_header_hash
11344 = htab_create_alloc_ex (127, line_header_hash_voidp,
11345 line_header_eq_voidp,
11346 free_line_header_voidp,
11347 &objfile->objfile_obstack,
11348 hashtab_obstack_allocate,
11349 dummy_obstack_deallocate);
11352 line_header_local.sect_off = line_offset;
11353 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11354 line_header_local_hash = line_header_hash (&line_header_local);
11355 if (dwarf2_per_objfile->line_header_hash != NULL)
11357 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11358 &line_header_local,
11359 line_header_local_hash, NO_INSERT);
11361 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11362 is not present in *SLOT (since if there is something in *SLOT then
11363 it will be for a partial_unit). */
11364 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11366 gdb_assert (*slot != NULL);
11367 cu->line_header = (struct line_header *) *slot;
11372 /* dwarf_decode_line_header does not yet provide sufficient information.
11373 We always have to call also dwarf_decode_lines for it. */
11374 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11378 cu->line_header = lh.release ();
11379 cu->line_header_die_owner = die;
11381 if (dwarf2_per_objfile->line_header_hash == NULL)
11385 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11386 &line_header_local,
11387 line_header_local_hash, INSERT);
11388 gdb_assert (slot != NULL);
11390 if (slot != NULL && *slot == NULL)
11392 /* This newly decoded line number information unit will be owned
11393 by line_header_hash hash table. */
11394 *slot = cu->line_header;
11395 cu->line_header_die_owner = NULL;
11399 /* We cannot free any current entry in (*slot) as that struct line_header
11400 may be already used by multiple CUs. Create only temporary decoded
11401 line_header for this CU - it may happen at most once for each line
11402 number information unit. And if we're not using line_header_hash
11403 then this is what we want as well. */
11404 gdb_assert (die->tag != DW_TAG_partial_unit);
11406 decode_mapping = (die->tag != DW_TAG_partial_unit);
11407 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11412 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11415 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11417 struct dwarf2_per_objfile *dwarf2_per_objfile
11418 = cu->per_cu->dwarf2_per_objfile;
11419 struct objfile *objfile = dwarf2_per_objfile->objfile;
11420 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11421 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11422 CORE_ADDR highpc = ((CORE_ADDR) 0);
11423 struct attribute *attr;
11424 struct die_info *child_die;
11425 CORE_ADDR baseaddr;
11427 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11429 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11431 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11432 from finish_block. */
11433 if (lowpc == ((CORE_ADDR) -1))
11435 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11437 file_and_directory fnd = find_file_and_directory (die, cu);
11439 prepare_one_comp_unit (cu, die, cu->language);
11441 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11442 standardised yet. As a workaround for the language detection we fall
11443 back to the DW_AT_producer string. */
11444 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11445 cu->language = language_opencl;
11447 /* Similar hack for Go. */
11448 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11449 set_cu_language (DW_LANG_Go, cu);
11451 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
11453 /* Decode line number information if present. We do this before
11454 processing child DIEs, so that the line header table is available
11455 for DW_AT_decl_file. */
11456 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11458 /* Process all dies in compilation unit. */
11459 if (die->child != NULL)
11461 child_die = die->child;
11462 while (child_die && child_die->tag)
11464 process_die (child_die, cu);
11465 child_die = sibling_die (child_die);
11469 /* Decode macro information, if present. Dwarf 2 macro information
11470 refers to information in the line number info statement program
11471 header, so we can only read it if we've read the header
11473 attr = dwarf2_attr (die, DW_AT_macros, cu);
11475 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11476 if (attr && cu->line_header)
11478 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11479 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11481 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11485 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11486 if (attr && cu->line_header)
11488 unsigned int macro_offset = DW_UNSND (attr);
11490 dwarf_decode_macros (cu, macro_offset, 0);
11495 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11496 Create the set of symtabs used by this TU, or if this TU is sharing
11497 symtabs with another TU and the symtabs have already been created
11498 then restore those symtabs in the line header.
11499 We don't need the pc/line-number mapping for type units. */
11502 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
11504 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11505 struct type_unit_group *tu_group;
11507 struct attribute *attr;
11509 struct signatured_type *sig_type;
11511 gdb_assert (per_cu->is_debug_types);
11512 sig_type = (struct signatured_type *) per_cu;
11514 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11516 /* If we're using .gdb_index (includes -readnow) then
11517 per_cu->type_unit_group may not have been set up yet. */
11518 if (sig_type->type_unit_group == NULL)
11519 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11520 tu_group = sig_type->type_unit_group;
11522 /* If we've already processed this stmt_list there's no real need to
11523 do it again, we could fake it and just recreate the part we need
11524 (file name,index -> symtab mapping). If data shows this optimization
11525 is useful we can do it then. */
11526 first_time = tu_group->compunit_symtab == NULL;
11528 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11533 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11534 lh = dwarf_decode_line_header (line_offset, cu);
11539 dwarf2_start_symtab (cu, "", NULL, 0);
11542 gdb_assert (tu_group->symtabs == NULL);
11543 restart_symtab (tu_group->compunit_symtab, "", 0);
11548 cu->line_header = lh.release ();
11549 cu->line_header_die_owner = die;
11553 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
11555 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11556 still initializing it, and our caller (a few levels up)
11557 process_full_type_unit still needs to know if this is the first
11560 tu_group->num_symtabs = cu->line_header->file_names.size ();
11561 tu_group->symtabs = XNEWVEC (struct symtab *,
11562 cu->line_header->file_names.size ());
11564 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11566 file_entry &fe = cu->line_header->file_names[i];
11568 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
11570 if (get_current_subfile ()->symtab == NULL)
11572 /* NOTE: start_subfile will recognize when it's been
11573 passed a file it has already seen. So we can't
11574 assume there's a simple mapping from
11575 cu->line_header->file_names to subfiles, plus
11576 cu->line_header->file_names may contain dups. */
11577 get_current_subfile ()->symtab
11578 = allocate_symtab (cust, get_current_subfile ()->name);
11581 fe.symtab = get_current_subfile ()->symtab;
11582 tu_group->symtabs[i] = fe.symtab;
11587 restart_symtab (tu_group->compunit_symtab, "", 0);
11589 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11591 file_entry &fe = cu->line_header->file_names[i];
11593 fe.symtab = tu_group->symtabs[i];
11597 /* The main symtab is allocated last. Type units don't have DW_AT_name
11598 so they don't have a "real" (so to speak) symtab anyway.
11599 There is later code that will assign the main symtab to all symbols
11600 that don't have one. We need to handle the case of a symbol with a
11601 missing symtab (DW_AT_decl_file) anyway. */
11604 /* Process DW_TAG_type_unit.
11605 For TUs we want to skip the first top level sibling if it's not the
11606 actual type being defined by this TU. In this case the first top
11607 level sibling is there to provide context only. */
11610 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11612 struct die_info *child_die;
11614 prepare_one_comp_unit (cu, die, language_minimal);
11616 /* Initialize (or reinitialize) the machinery for building symtabs.
11617 We do this before processing child DIEs, so that the line header table
11618 is available for DW_AT_decl_file. */
11619 setup_type_unit_groups (die, cu);
11621 if (die->child != NULL)
11623 child_die = die->child;
11624 while (child_die && child_die->tag)
11626 process_die (child_die, cu);
11627 child_die = sibling_die (child_die);
11634 http://gcc.gnu.org/wiki/DebugFission
11635 http://gcc.gnu.org/wiki/DebugFissionDWP
11637 To simplify handling of both DWO files ("object" files with the DWARF info)
11638 and DWP files (a file with the DWOs packaged up into one file), we treat
11639 DWP files as having a collection of virtual DWO files. */
11642 hash_dwo_file (const void *item)
11644 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11647 hash = htab_hash_string (dwo_file->dwo_name);
11648 if (dwo_file->comp_dir != NULL)
11649 hash += htab_hash_string (dwo_file->comp_dir);
11654 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11656 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11657 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11659 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11661 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11662 return lhs->comp_dir == rhs->comp_dir;
11663 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11666 /* Allocate a hash table for DWO files. */
11669 allocate_dwo_file_hash_table (struct objfile *objfile)
11671 return htab_create_alloc_ex (41,
11675 &objfile->objfile_obstack,
11676 hashtab_obstack_allocate,
11677 dummy_obstack_deallocate);
11680 /* Lookup DWO file DWO_NAME. */
11683 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11684 const char *dwo_name,
11685 const char *comp_dir)
11687 struct dwo_file find_entry;
11690 if (dwarf2_per_objfile->dwo_files == NULL)
11691 dwarf2_per_objfile->dwo_files
11692 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11694 memset (&find_entry, 0, sizeof (find_entry));
11695 find_entry.dwo_name = dwo_name;
11696 find_entry.comp_dir = comp_dir;
11697 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11703 hash_dwo_unit (const void *item)
11705 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11707 /* This drops the top 32 bits of the id, but is ok for a hash. */
11708 return dwo_unit->signature;
11712 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11714 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11715 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11717 /* The signature is assumed to be unique within the DWO file.
11718 So while object file CU dwo_id's always have the value zero,
11719 that's OK, assuming each object file DWO file has only one CU,
11720 and that's the rule for now. */
11721 return lhs->signature == rhs->signature;
11724 /* Allocate a hash table for DWO CUs,TUs.
11725 There is one of these tables for each of CUs,TUs for each DWO file. */
11728 allocate_dwo_unit_table (struct objfile *objfile)
11730 /* Start out with a pretty small number.
11731 Generally DWO files contain only one CU and maybe some TUs. */
11732 return htab_create_alloc_ex (3,
11736 &objfile->objfile_obstack,
11737 hashtab_obstack_allocate,
11738 dummy_obstack_deallocate);
11741 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11743 struct create_dwo_cu_data
11745 struct dwo_file *dwo_file;
11746 struct dwo_unit dwo_unit;
11749 /* die_reader_func for create_dwo_cu. */
11752 create_dwo_cu_reader (const struct die_reader_specs *reader,
11753 const gdb_byte *info_ptr,
11754 struct die_info *comp_unit_die,
11758 struct dwarf2_cu *cu = reader->cu;
11759 sect_offset sect_off = cu->per_cu->sect_off;
11760 struct dwarf2_section_info *section = cu->per_cu->section;
11761 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11762 struct dwo_file *dwo_file = data->dwo_file;
11763 struct dwo_unit *dwo_unit = &data->dwo_unit;
11764 struct attribute *attr;
11766 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11769 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11770 " its dwo_id [in module %s]"),
11771 sect_offset_str (sect_off), dwo_file->dwo_name);
11775 dwo_unit->dwo_file = dwo_file;
11776 dwo_unit->signature = DW_UNSND (attr);
11777 dwo_unit->section = section;
11778 dwo_unit->sect_off = sect_off;
11779 dwo_unit->length = cu->per_cu->length;
11781 if (dwarf_read_debug)
11782 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11783 sect_offset_str (sect_off),
11784 hex_string (dwo_unit->signature));
11787 /* Create the dwo_units for the CUs in a DWO_FILE.
11788 Note: This function processes DWO files only, not DWP files. */
11791 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11792 struct dwo_file &dwo_file, dwarf2_section_info §ion,
11795 struct objfile *objfile = dwarf2_per_objfile->objfile;
11796 const gdb_byte *info_ptr, *end_ptr;
11798 dwarf2_read_section (objfile, §ion);
11799 info_ptr = section.buffer;
11801 if (info_ptr == NULL)
11804 if (dwarf_read_debug)
11806 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11807 get_section_name (§ion),
11808 get_section_file_name (§ion));
11811 end_ptr = info_ptr + section.size;
11812 while (info_ptr < end_ptr)
11814 struct dwarf2_per_cu_data per_cu;
11815 struct create_dwo_cu_data create_dwo_cu_data;
11816 struct dwo_unit *dwo_unit;
11818 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11820 memset (&create_dwo_cu_data.dwo_unit, 0,
11821 sizeof (create_dwo_cu_data.dwo_unit));
11822 memset (&per_cu, 0, sizeof (per_cu));
11823 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11824 per_cu.is_debug_types = 0;
11825 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11826 per_cu.section = §ion;
11827 create_dwo_cu_data.dwo_file = &dwo_file;
11829 init_cutu_and_read_dies_no_follow (
11830 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11831 info_ptr += per_cu.length;
11833 // If the unit could not be parsed, skip it.
11834 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11837 if (cus_htab == NULL)
11838 cus_htab = allocate_dwo_unit_table (objfile);
11840 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11841 *dwo_unit = create_dwo_cu_data.dwo_unit;
11842 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11843 gdb_assert (slot != NULL);
11846 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11847 sect_offset dup_sect_off = dup_cu->sect_off;
11849 complaint (_("debug cu entry at offset %s is duplicate to"
11850 " the entry at offset %s, signature %s"),
11851 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11852 hex_string (dwo_unit->signature));
11854 *slot = (void *)dwo_unit;
11858 /* DWP file .debug_{cu,tu}_index section format:
11859 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11863 Both index sections have the same format, and serve to map a 64-bit
11864 signature to a set of section numbers. Each section begins with a header,
11865 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11866 indexes, and a pool of 32-bit section numbers. The index sections will be
11867 aligned at 8-byte boundaries in the file.
11869 The index section header consists of:
11871 V, 32 bit version number
11873 N, 32 bit number of compilation units or type units in the index
11874 M, 32 bit number of slots in the hash table
11876 Numbers are recorded using the byte order of the application binary.
11878 The hash table begins at offset 16 in the section, and consists of an array
11879 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11880 order of the application binary). Unused slots in the hash table are 0.
11881 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11883 The parallel table begins immediately after the hash table
11884 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11885 array of 32-bit indexes (using the byte order of the application binary),
11886 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11887 table contains a 32-bit index into the pool of section numbers. For unused
11888 hash table slots, the corresponding entry in the parallel table will be 0.
11890 The pool of section numbers begins immediately following the hash table
11891 (at offset 16 + 12 * M from the beginning of the section). The pool of
11892 section numbers consists of an array of 32-bit words (using the byte order
11893 of the application binary). Each item in the array is indexed starting
11894 from 0. The hash table entry provides the index of the first section
11895 number in the set. Additional section numbers in the set follow, and the
11896 set is terminated by a 0 entry (section number 0 is not used in ELF).
11898 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11899 section must be the first entry in the set, and the .debug_abbrev.dwo must
11900 be the second entry. Other members of the set may follow in any order.
11906 DWP Version 2 combines all the .debug_info, etc. sections into one,
11907 and the entries in the index tables are now offsets into these sections.
11908 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11911 Index Section Contents:
11913 Hash Table of Signatures dwp_hash_table.hash_table
11914 Parallel Table of Indices dwp_hash_table.unit_table
11915 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11916 Table of Section Sizes dwp_hash_table.v2.sizes
11918 The index section header consists of:
11920 V, 32 bit version number
11921 L, 32 bit number of columns in the table of section offsets
11922 N, 32 bit number of compilation units or type units in the index
11923 M, 32 bit number of slots in the hash table
11925 Numbers are recorded using the byte order of the application binary.
11927 The hash table has the same format as version 1.
11928 The parallel table of indices has the same format as version 1,
11929 except that the entries are origin-1 indices into the table of sections
11930 offsets and the table of section sizes.
11932 The table of offsets begins immediately following the parallel table
11933 (at offset 16 + 12 * M from the beginning of the section). The table is
11934 a two-dimensional array of 32-bit words (using the byte order of the
11935 application binary), with L columns and N+1 rows, in row-major order.
11936 Each row in the array is indexed starting from 0. The first row provides
11937 a key to the remaining rows: each column in this row provides an identifier
11938 for a debug section, and the offsets in the same column of subsequent rows
11939 refer to that section. The section identifiers are:
11941 DW_SECT_INFO 1 .debug_info.dwo
11942 DW_SECT_TYPES 2 .debug_types.dwo
11943 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11944 DW_SECT_LINE 4 .debug_line.dwo
11945 DW_SECT_LOC 5 .debug_loc.dwo
11946 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11947 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11948 DW_SECT_MACRO 8 .debug_macro.dwo
11950 The offsets provided by the CU and TU index sections are the base offsets
11951 for the contributions made by each CU or TU to the corresponding section
11952 in the package file. Each CU and TU header contains an abbrev_offset
11953 field, used to find the abbreviations table for that CU or TU within the
11954 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11955 be interpreted as relative to the base offset given in the index section.
11956 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11957 should be interpreted as relative to the base offset for .debug_line.dwo,
11958 and offsets into other debug sections obtained from DWARF attributes should
11959 also be interpreted as relative to the corresponding base offset.
11961 The table of sizes begins immediately following the table of offsets.
11962 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11963 with L columns and N rows, in row-major order. Each row in the array is
11964 indexed starting from 1 (row 0 is shared by the two tables).
11968 Hash table lookup is handled the same in version 1 and 2:
11970 We assume that N and M will not exceed 2^32 - 1.
11971 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11973 Given a 64-bit compilation unit signature or a type signature S, an entry
11974 in the hash table is located as follows:
11976 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11977 the low-order k bits all set to 1.
11979 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11981 3) If the hash table entry at index H matches the signature, use that
11982 entry. If the hash table entry at index H is unused (all zeroes),
11983 terminate the search: the signature is not present in the table.
11985 4) Let H = (H + H') modulo M. Repeat at Step 3.
11987 Because M > N and H' and M are relatively prime, the search is guaranteed
11988 to stop at an unused slot or find the match. */
11990 /* Create a hash table to map DWO IDs to their CU/TU entry in
11991 .debug_{info,types}.dwo in DWP_FILE.
11992 Returns NULL if there isn't one.
11993 Note: This function processes DWP files only, not DWO files. */
11995 static struct dwp_hash_table *
11996 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11997 struct dwp_file *dwp_file, int is_debug_types)
11999 struct objfile *objfile = dwarf2_per_objfile->objfile;
12000 bfd *dbfd = dwp_file->dbfd.get ();
12001 const gdb_byte *index_ptr, *index_end;
12002 struct dwarf2_section_info *index;
12003 uint32_t version, nr_columns, nr_units, nr_slots;
12004 struct dwp_hash_table *htab;
12006 if (is_debug_types)
12007 index = &dwp_file->sections.tu_index;
12009 index = &dwp_file->sections.cu_index;
12011 if (dwarf2_section_empty_p (index))
12013 dwarf2_read_section (objfile, index);
12015 index_ptr = index->buffer;
12016 index_end = index_ptr + index->size;
12018 version = read_4_bytes (dbfd, index_ptr);
12021 nr_columns = read_4_bytes (dbfd, index_ptr);
12025 nr_units = read_4_bytes (dbfd, index_ptr);
12027 nr_slots = read_4_bytes (dbfd, index_ptr);
12030 if (version != 1 && version != 2)
12032 error (_("Dwarf Error: unsupported DWP file version (%s)"
12033 " [in module %s]"),
12034 pulongest (version), dwp_file->name);
12036 if (nr_slots != (nr_slots & -nr_slots))
12038 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12039 " is not power of 2 [in module %s]"),
12040 pulongest (nr_slots), dwp_file->name);
12043 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12044 htab->version = version;
12045 htab->nr_columns = nr_columns;
12046 htab->nr_units = nr_units;
12047 htab->nr_slots = nr_slots;
12048 htab->hash_table = index_ptr;
12049 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12051 /* Exit early if the table is empty. */
12052 if (nr_slots == 0 || nr_units == 0
12053 || (version == 2 && nr_columns == 0))
12055 /* All must be zero. */
12056 if (nr_slots != 0 || nr_units != 0
12057 || (version == 2 && nr_columns != 0))
12059 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
12060 " all zero [in modules %s]"),
12068 htab->section_pool.v1.indices =
12069 htab->unit_table + sizeof (uint32_t) * nr_slots;
12070 /* It's harder to decide whether the section is too small in v1.
12071 V1 is deprecated anyway so we punt. */
12075 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12076 int *ids = htab->section_pool.v2.section_ids;
12077 /* Reverse map for error checking. */
12078 int ids_seen[DW_SECT_MAX + 1];
12081 if (nr_columns < 2)
12083 error (_("Dwarf Error: bad DWP hash table, too few columns"
12084 " in section table [in module %s]"),
12087 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12089 error (_("Dwarf Error: bad DWP hash table, too many columns"
12090 " in section table [in module %s]"),
12093 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12094 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12095 for (i = 0; i < nr_columns; ++i)
12097 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12099 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12101 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12102 " in section table [in module %s]"),
12103 id, dwp_file->name);
12105 if (ids_seen[id] != -1)
12107 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12108 " id %d in section table [in module %s]"),
12109 id, dwp_file->name);
12114 /* Must have exactly one info or types section. */
12115 if (((ids_seen[DW_SECT_INFO] != -1)
12116 + (ids_seen[DW_SECT_TYPES] != -1))
12119 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12120 " DWO info/types section [in module %s]"),
12123 /* Must have an abbrev section. */
12124 if (ids_seen[DW_SECT_ABBREV] == -1)
12126 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12127 " section [in module %s]"),
12130 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12131 htab->section_pool.v2.sizes =
12132 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12133 * nr_units * nr_columns);
12134 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12135 * nr_units * nr_columns))
12138 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12139 " [in module %s]"),
12147 /* Update SECTIONS with the data from SECTP.
12149 This function is like the other "locate" section routines that are
12150 passed to bfd_map_over_sections, but in this context the sections to
12151 read comes from the DWP V1 hash table, not the full ELF section table.
12153 The result is non-zero for success, or zero if an error was found. */
12156 locate_v1_virtual_dwo_sections (asection *sectp,
12157 struct virtual_v1_dwo_sections *sections)
12159 const struct dwop_section_names *names = &dwop_section_names;
12161 if (section_is_p (sectp->name, &names->abbrev_dwo))
12163 /* There can be only one. */
12164 if (sections->abbrev.s.section != NULL)
12166 sections->abbrev.s.section = sectp;
12167 sections->abbrev.size = bfd_get_section_size (sectp);
12169 else if (section_is_p (sectp->name, &names->info_dwo)
12170 || section_is_p (sectp->name, &names->types_dwo))
12172 /* There can be only one. */
12173 if (sections->info_or_types.s.section != NULL)
12175 sections->info_or_types.s.section = sectp;
12176 sections->info_or_types.size = bfd_get_section_size (sectp);
12178 else if (section_is_p (sectp->name, &names->line_dwo))
12180 /* There can be only one. */
12181 if (sections->line.s.section != NULL)
12183 sections->line.s.section = sectp;
12184 sections->line.size = bfd_get_section_size (sectp);
12186 else if (section_is_p (sectp->name, &names->loc_dwo))
12188 /* There can be only one. */
12189 if (sections->loc.s.section != NULL)
12191 sections->loc.s.section = sectp;
12192 sections->loc.size = bfd_get_section_size (sectp);
12194 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12196 /* There can be only one. */
12197 if (sections->macinfo.s.section != NULL)
12199 sections->macinfo.s.section = sectp;
12200 sections->macinfo.size = bfd_get_section_size (sectp);
12202 else if (section_is_p (sectp->name, &names->macro_dwo))
12204 /* There can be only one. */
12205 if (sections->macro.s.section != NULL)
12207 sections->macro.s.section = sectp;
12208 sections->macro.size = bfd_get_section_size (sectp);
12210 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12212 /* There can be only one. */
12213 if (sections->str_offsets.s.section != NULL)
12215 sections->str_offsets.s.section = sectp;
12216 sections->str_offsets.size = bfd_get_section_size (sectp);
12220 /* No other kind of section is valid. */
12227 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12228 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12229 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12230 This is for DWP version 1 files. */
12232 static struct dwo_unit *
12233 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12234 struct dwp_file *dwp_file,
12235 uint32_t unit_index,
12236 const char *comp_dir,
12237 ULONGEST signature, int is_debug_types)
12239 struct objfile *objfile = dwarf2_per_objfile->objfile;
12240 const struct dwp_hash_table *dwp_htab =
12241 is_debug_types ? dwp_file->tus : dwp_file->cus;
12242 bfd *dbfd = dwp_file->dbfd.get ();
12243 const char *kind = is_debug_types ? "TU" : "CU";
12244 struct dwo_file *dwo_file;
12245 struct dwo_unit *dwo_unit;
12246 struct virtual_v1_dwo_sections sections;
12247 void **dwo_file_slot;
12250 gdb_assert (dwp_file->version == 1);
12252 if (dwarf_read_debug)
12254 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12256 pulongest (unit_index), hex_string (signature),
12260 /* Fetch the sections of this DWO unit.
12261 Put a limit on the number of sections we look for so that bad data
12262 doesn't cause us to loop forever. */
12264 #define MAX_NR_V1_DWO_SECTIONS \
12265 (1 /* .debug_info or .debug_types */ \
12266 + 1 /* .debug_abbrev */ \
12267 + 1 /* .debug_line */ \
12268 + 1 /* .debug_loc */ \
12269 + 1 /* .debug_str_offsets */ \
12270 + 1 /* .debug_macro or .debug_macinfo */ \
12271 + 1 /* trailing zero */)
12273 memset (§ions, 0, sizeof (sections));
12275 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12278 uint32_t section_nr =
12279 read_4_bytes (dbfd,
12280 dwp_htab->section_pool.v1.indices
12281 + (unit_index + i) * sizeof (uint32_t));
12283 if (section_nr == 0)
12285 if (section_nr >= dwp_file->num_sections)
12287 error (_("Dwarf Error: bad DWP hash table, section number too large"
12288 " [in module %s]"),
12292 sectp = dwp_file->elf_sections[section_nr];
12293 if (! locate_v1_virtual_dwo_sections (sectp, §ions))
12295 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12296 " [in module %s]"),
12302 || dwarf2_section_empty_p (§ions.info_or_types)
12303 || dwarf2_section_empty_p (§ions.abbrev))
12305 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12306 " [in module %s]"),
12309 if (i == MAX_NR_V1_DWO_SECTIONS)
12311 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12312 " [in module %s]"),
12316 /* It's easier for the rest of the code if we fake a struct dwo_file and
12317 have dwo_unit "live" in that. At least for now.
12319 The DWP file can be made up of a random collection of CUs and TUs.
12320 However, for each CU + set of TUs that came from the same original DWO
12321 file, we can combine them back into a virtual DWO file to save space
12322 (fewer struct dwo_file objects to allocate). Remember that for really
12323 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12325 std::string virtual_dwo_name =
12326 string_printf ("virtual-dwo/%d-%d-%d-%d",
12327 get_section_id (§ions.abbrev),
12328 get_section_id (§ions.line),
12329 get_section_id (§ions.loc),
12330 get_section_id (§ions.str_offsets));
12331 /* Can we use an existing virtual DWO file? */
12332 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12333 virtual_dwo_name.c_str (),
12335 /* Create one if necessary. */
12336 if (*dwo_file_slot == NULL)
12338 if (dwarf_read_debug)
12340 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12341 virtual_dwo_name.c_str ());
12343 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12345 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12346 virtual_dwo_name.c_str (),
12347 virtual_dwo_name.size ());
12348 dwo_file->comp_dir = comp_dir;
12349 dwo_file->sections.abbrev = sections.abbrev;
12350 dwo_file->sections.line = sections.line;
12351 dwo_file->sections.loc = sections.loc;
12352 dwo_file->sections.macinfo = sections.macinfo;
12353 dwo_file->sections.macro = sections.macro;
12354 dwo_file->sections.str_offsets = sections.str_offsets;
12355 /* The "str" section is global to the entire DWP file. */
12356 dwo_file->sections.str = dwp_file->sections.str;
12357 /* The info or types section is assigned below to dwo_unit,
12358 there's no need to record it in dwo_file.
12359 Also, we can't simply record type sections in dwo_file because
12360 we record a pointer into the vector in dwo_unit. As we collect more
12361 types we'll grow the vector and eventually have to reallocate space
12362 for it, invalidating all copies of pointers into the previous
12364 *dwo_file_slot = dwo_file;
12368 if (dwarf_read_debug)
12370 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12371 virtual_dwo_name.c_str ());
12373 dwo_file = (struct dwo_file *) *dwo_file_slot;
12376 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12377 dwo_unit->dwo_file = dwo_file;
12378 dwo_unit->signature = signature;
12379 dwo_unit->section =
12380 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12381 *dwo_unit->section = sections.info_or_types;
12382 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12387 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12388 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12389 piece within that section used by a TU/CU, return a virtual section
12390 of just that piece. */
12392 static struct dwarf2_section_info
12393 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12394 struct dwarf2_section_info *section,
12395 bfd_size_type offset, bfd_size_type size)
12397 struct dwarf2_section_info result;
12400 gdb_assert (section != NULL);
12401 gdb_assert (!section->is_virtual);
12403 memset (&result, 0, sizeof (result));
12404 result.s.containing_section = section;
12405 result.is_virtual = 1;
12410 sectp = get_section_bfd_section (section);
12412 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12413 bounds of the real section. This is a pretty-rare event, so just
12414 flag an error (easier) instead of a warning and trying to cope. */
12416 || offset + size > bfd_get_section_size (sectp))
12418 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12419 " in section %s [in module %s]"),
12420 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12421 objfile_name (dwarf2_per_objfile->objfile));
12424 result.virtual_offset = offset;
12425 result.size = size;
12429 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12430 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12431 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12432 This is for DWP version 2 files. */
12434 static struct dwo_unit *
12435 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12436 struct dwp_file *dwp_file,
12437 uint32_t unit_index,
12438 const char *comp_dir,
12439 ULONGEST signature, int is_debug_types)
12441 struct objfile *objfile = dwarf2_per_objfile->objfile;
12442 const struct dwp_hash_table *dwp_htab =
12443 is_debug_types ? dwp_file->tus : dwp_file->cus;
12444 bfd *dbfd = dwp_file->dbfd.get ();
12445 const char *kind = is_debug_types ? "TU" : "CU";
12446 struct dwo_file *dwo_file;
12447 struct dwo_unit *dwo_unit;
12448 struct virtual_v2_dwo_sections sections;
12449 void **dwo_file_slot;
12452 gdb_assert (dwp_file->version == 2);
12454 if (dwarf_read_debug)
12456 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12458 pulongest (unit_index), hex_string (signature),
12462 /* Fetch the section offsets of this DWO unit. */
12464 memset (§ions, 0, sizeof (sections));
12466 for (i = 0; i < dwp_htab->nr_columns; ++i)
12468 uint32_t offset = read_4_bytes (dbfd,
12469 dwp_htab->section_pool.v2.offsets
12470 + (((unit_index - 1) * dwp_htab->nr_columns
12472 * sizeof (uint32_t)));
12473 uint32_t size = read_4_bytes (dbfd,
12474 dwp_htab->section_pool.v2.sizes
12475 + (((unit_index - 1) * dwp_htab->nr_columns
12477 * sizeof (uint32_t)));
12479 switch (dwp_htab->section_pool.v2.section_ids[i])
12482 case DW_SECT_TYPES:
12483 sections.info_or_types_offset = offset;
12484 sections.info_or_types_size = size;
12486 case DW_SECT_ABBREV:
12487 sections.abbrev_offset = offset;
12488 sections.abbrev_size = size;
12491 sections.line_offset = offset;
12492 sections.line_size = size;
12495 sections.loc_offset = offset;
12496 sections.loc_size = size;
12498 case DW_SECT_STR_OFFSETS:
12499 sections.str_offsets_offset = offset;
12500 sections.str_offsets_size = size;
12502 case DW_SECT_MACINFO:
12503 sections.macinfo_offset = offset;
12504 sections.macinfo_size = size;
12506 case DW_SECT_MACRO:
12507 sections.macro_offset = offset;
12508 sections.macro_size = size;
12513 /* It's easier for the rest of the code if we fake a struct dwo_file and
12514 have dwo_unit "live" in that. At least for now.
12516 The DWP file can be made up of a random collection of CUs and TUs.
12517 However, for each CU + set of TUs that came from the same original DWO
12518 file, we can combine them back into a virtual DWO file to save space
12519 (fewer struct dwo_file objects to allocate). Remember that for really
12520 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12522 std::string virtual_dwo_name =
12523 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12524 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12525 (long) (sections.line_size ? sections.line_offset : 0),
12526 (long) (sections.loc_size ? sections.loc_offset : 0),
12527 (long) (sections.str_offsets_size
12528 ? sections.str_offsets_offset : 0));
12529 /* Can we use an existing virtual DWO file? */
12530 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12531 virtual_dwo_name.c_str (),
12533 /* Create one if necessary. */
12534 if (*dwo_file_slot == NULL)
12536 if (dwarf_read_debug)
12538 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12539 virtual_dwo_name.c_str ());
12541 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12543 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12544 virtual_dwo_name.c_str (),
12545 virtual_dwo_name.size ());
12546 dwo_file->comp_dir = comp_dir;
12547 dwo_file->sections.abbrev =
12548 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12549 sections.abbrev_offset, sections.abbrev_size);
12550 dwo_file->sections.line =
12551 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12552 sections.line_offset, sections.line_size);
12553 dwo_file->sections.loc =
12554 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12555 sections.loc_offset, sections.loc_size);
12556 dwo_file->sections.macinfo =
12557 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12558 sections.macinfo_offset, sections.macinfo_size);
12559 dwo_file->sections.macro =
12560 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12561 sections.macro_offset, sections.macro_size);
12562 dwo_file->sections.str_offsets =
12563 create_dwp_v2_section (dwarf2_per_objfile,
12564 &dwp_file->sections.str_offsets,
12565 sections.str_offsets_offset,
12566 sections.str_offsets_size);
12567 /* The "str" section is global to the entire DWP file. */
12568 dwo_file->sections.str = dwp_file->sections.str;
12569 /* The info or types section is assigned below to dwo_unit,
12570 there's no need to record it in dwo_file.
12571 Also, we can't simply record type sections in dwo_file because
12572 we record a pointer into the vector in dwo_unit. As we collect more
12573 types we'll grow the vector and eventually have to reallocate space
12574 for it, invalidating all copies of pointers into the previous
12576 *dwo_file_slot = dwo_file;
12580 if (dwarf_read_debug)
12582 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12583 virtual_dwo_name.c_str ());
12585 dwo_file = (struct dwo_file *) *dwo_file_slot;
12588 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12589 dwo_unit->dwo_file = dwo_file;
12590 dwo_unit->signature = signature;
12591 dwo_unit->section =
12592 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12593 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12595 ? &dwp_file->sections.types
12596 : &dwp_file->sections.info,
12597 sections.info_or_types_offset,
12598 sections.info_or_types_size);
12599 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12604 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12605 Returns NULL if the signature isn't found. */
12607 static struct dwo_unit *
12608 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12609 struct dwp_file *dwp_file, const char *comp_dir,
12610 ULONGEST signature, int is_debug_types)
12612 const struct dwp_hash_table *dwp_htab =
12613 is_debug_types ? dwp_file->tus : dwp_file->cus;
12614 bfd *dbfd = dwp_file->dbfd.get ();
12615 uint32_t mask = dwp_htab->nr_slots - 1;
12616 uint32_t hash = signature & mask;
12617 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12620 struct dwo_unit find_dwo_cu;
12622 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12623 find_dwo_cu.signature = signature;
12624 slot = htab_find_slot (is_debug_types
12625 ? dwp_file->loaded_tus
12626 : dwp_file->loaded_cus,
12627 &find_dwo_cu, INSERT);
12630 return (struct dwo_unit *) *slot;
12632 /* Use a for loop so that we don't loop forever on bad debug info. */
12633 for (i = 0; i < dwp_htab->nr_slots; ++i)
12635 ULONGEST signature_in_table;
12637 signature_in_table =
12638 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12639 if (signature_in_table == signature)
12641 uint32_t unit_index =
12642 read_4_bytes (dbfd,
12643 dwp_htab->unit_table + hash * sizeof (uint32_t));
12645 if (dwp_file->version == 1)
12647 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12648 dwp_file, unit_index,
12649 comp_dir, signature,
12654 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12655 dwp_file, unit_index,
12656 comp_dir, signature,
12659 return (struct dwo_unit *) *slot;
12661 if (signature_in_table == 0)
12663 hash = (hash + hash2) & mask;
12666 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12667 " [in module %s]"),
12671 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12672 Open the file specified by FILE_NAME and hand it off to BFD for
12673 preliminary analysis. Return a newly initialized bfd *, which
12674 includes a canonicalized copy of FILE_NAME.
12675 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12676 SEARCH_CWD is true if the current directory is to be searched.
12677 It will be searched before debug-file-directory.
12678 If successful, the file is added to the bfd include table of the
12679 objfile's bfd (see gdb_bfd_record_inclusion).
12680 If unable to find/open the file, return NULL.
12681 NOTE: This function is derived from symfile_bfd_open. */
12683 static gdb_bfd_ref_ptr
12684 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12685 const char *file_name, int is_dwp, int search_cwd)
12688 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12689 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12690 to debug_file_directory. */
12691 const char *search_path;
12692 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12694 gdb::unique_xmalloc_ptr<char> search_path_holder;
12697 if (*debug_file_directory != '\0')
12699 search_path_holder.reset (concat (".", dirname_separator_string,
12700 debug_file_directory,
12702 search_path = search_path_holder.get ();
12708 search_path = debug_file_directory;
12710 openp_flags flags = OPF_RETURN_REALPATH;
12712 flags |= OPF_SEARCH_IN_PATH;
12714 gdb::unique_xmalloc_ptr<char> absolute_name;
12715 desc = openp (search_path, flags, file_name,
12716 O_RDONLY | O_BINARY, &absolute_name);
12720 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12722 if (sym_bfd == NULL)
12724 bfd_set_cacheable (sym_bfd.get (), 1);
12726 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12729 /* Success. Record the bfd as having been included by the objfile's bfd.
12730 This is important because things like demangled_names_hash lives in the
12731 objfile's per_bfd space and may have references to things like symbol
12732 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12733 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12738 /* Try to open DWO file FILE_NAME.
12739 COMP_DIR is the DW_AT_comp_dir attribute.
12740 The result is the bfd handle of the file.
12741 If there is a problem finding or opening the file, return NULL.
12742 Upon success, the canonicalized path of the file is stored in the bfd,
12743 same as symfile_bfd_open. */
12745 static gdb_bfd_ref_ptr
12746 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12747 const char *file_name, const char *comp_dir)
12749 if (IS_ABSOLUTE_PATH (file_name))
12750 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12751 0 /*is_dwp*/, 0 /*search_cwd*/);
12753 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12755 if (comp_dir != NULL)
12757 char *path_to_try = concat (comp_dir, SLASH_STRING,
12758 file_name, (char *) NULL);
12760 /* NOTE: If comp_dir is a relative path, this will also try the
12761 search path, which seems useful. */
12762 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12765 1 /*search_cwd*/));
12766 xfree (path_to_try);
12771 /* That didn't work, try debug-file-directory, which, despite its name,
12772 is a list of paths. */
12774 if (*debug_file_directory == '\0')
12777 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12778 0 /*is_dwp*/, 1 /*search_cwd*/);
12781 /* This function is mapped across the sections and remembers the offset and
12782 size of each of the DWO debugging sections we are interested in. */
12785 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12787 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12788 const struct dwop_section_names *names = &dwop_section_names;
12790 if (section_is_p (sectp->name, &names->abbrev_dwo))
12792 dwo_sections->abbrev.s.section = sectp;
12793 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12795 else if (section_is_p (sectp->name, &names->info_dwo))
12797 dwo_sections->info.s.section = sectp;
12798 dwo_sections->info.size = bfd_get_section_size (sectp);
12800 else if (section_is_p (sectp->name, &names->line_dwo))
12802 dwo_sections->line.s.section = sectp;
12803 dwo_sections->line.size = bfd_get_section_size (sectp);
12805 else if (section_is_p (sectp->name, &names->loc_dwo))
12807 dwo_sections->loc.s.section = sectp;
12808 dwo_sections->loc.size = bfd_get_section_size (sectp);
12810 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12812 dwo_sections->macinfo.s.section = sectp;
12813 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12815 else if (section_is_p (sectp->name, &names->macro_dwo))
12817 dwo_sections->macro.s.section = sectp;
12818 dwo_sections->macro.size = bfd_get_section_size (sectp);
12820 else if (section_is_p (sectp->name, &names->str_dwo))
12822 dwo_sections->str.s.section = sectp;
12823 dwo_sections->str.size = bfd_get_section_size (sectp);
12825 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12827 dwo_sections->str_offsets.s.section = sectp;
12828 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12830 else if (section_is_p (sectp->name, &names->types_dwo))
12832 struct dwarf2_section_info type_section;
12834 memset (&type_section, 0, sizeof (type_section));
12835 type_section.s.section = sectp;
12836 type_section.size = bfd_get_section_size (sectp);
12837 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
12842 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12843 by PER_CU. This is for the non-DWP case.
12844 The result is NULL if DWO_NAME can't be found. */
12846 static struct dwo_file *
12847 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12848 const char *dwo_name, const char *comp_dir)
12850 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12851 struct objfile *objfile = dwarf2_per_objfile->objfile;
12853 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
12856 if (dwarf_read_debug)
12857 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12861 /* We use a unique pointer here, despite the obstack allocation,
12862 because a dwo_file needs some cleanup if it is abandoned. */
12863 dwo_file_up dwo_file (OBSTACK_ZALLOC (&objfile->objfile_obstack,
12865 dwo_file->dwo_name = dwo_name;
12866 dwo_file->comp_dir = comp_dir;
12867 dwo_file->dbfd = dbfd.release ();
12869 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
12870 &dwo_file->sections);
12872 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
12875 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12876 dwo_file->sections.types, dwo_file->tus);
12878 if (dwarf_read_debug)
12879 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12881 return dwo_file.release ();
12884 /* This function is mapped across the sections and remembers the offset and
12885 size of each of the DWP debugging sections common to version 1 and 2 that
12886 we are interested in. */
12889 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12890 void *dwp_file_ptr)
12892 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12893 const struct dwop_section_names *names = &dwop_section_names;
12894 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12896 /* Record the ELF section number for later lookup: this is what the
12897 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12898 gdb_assert (elf_section_nr < dwp_file->num_sections);
12899 dwp_file->elf_sections[elf_section_nr] = sectp;
12901 /* Look for specific sections that we need. */
12902 if (section_is_p (sectp->name, &names->str_dwo))
12904 dwp_file->sections.str.s.section = sectp;
12905 dwp_file->sections.str.size = bfd_get_section_size (sectp);
12907 else if (section_is_p (sectp->name, &names->cu_index))
12909 dwp_file->sections.cu_index.s.section = sectp;
12910 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
12912 else if (section_is_p (sectp->name, &names->tu_index))
12914 dwp_file->sections.tu_index.s.section = sectp;
12915 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
12919 /* This function is mapped across the sections and remembers the offset and
12920 size of each of the DWP version 2 debugging sections that we are interested
12921 in. This is split into a separate function because we don't know if we
12922 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12925 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12927 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12928 const struct dwop_section_names *names = &dwop_section_names;
12929 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12931 /* Record the ELF section number for later lookup: this is what the
12932 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12933 gdb_assert (elf_section_nr < dwp_file->num_sections);
12934 dwp_file->elf_sections[elf_section_nr] = sectp;
12936 /* Look for specific sections that we need. */
12937 if (section_is_p (sectp->name, &names->abbrev_dwo))
12939 dwp_file->sections.abbrev.s.section = sectp;
12940 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
12942 else if (section_is_p (sectp->name, &names->info_dwo))
12944 dwp_file->sections.info.s.section = sectp;
12945 dwp_file->sections.info.size = bfd_get_section_size (sectp);
12947 else if (section_is_p (sectp->name, &names->line_dwo))
12949 dwp_file->sections.line.s.section = sectp;
12950 dwp_file->sections.line.size = bfd_get_section_size (sectp);
12952 else if (section_is_p (sectp->name, &names->loc_dwo))
12954 dwp_file->sections.loc.s.section = sectp;
12955 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
12957 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12959 dwp_file->sections.macinfo.s.section = sectp;
12960 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
12962 else if (section_is_p (sectp->name, &names->macro_dwo))
12964 dwp_file->sections.macro.s.section = sectp;
12965 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
12967 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12969 dwp_file->sections.str_offsets.s.section = sectp;
12970 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
12972 else if (section_is_p (sectp->name, &names->types_dwo))
12974 dwp_file->sections.types.s.section = sectp;
12975 dwp_file->sections.types.size = bfd_get_section_size (sectp);
12979 /* Hash function for dwp_file loaded CUs/TUs. */
12982 hash_dwp_loaded_cutus (const void *item)
12984 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12986 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12987 return dwo_unit->signature;
12990 /* Equality function for dwp_file loaded CUs/TUs. */
12993 eq_dwp_loaded_cutus (const void *a, const void *b)
12995 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12996 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12998 return dua->signature == dub->signature;
13001 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13004 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13006 return htab_create_alloc_ex (3,
13007 hash_dwp_loaded_cutus,
13008 eq_dwp_loaded_cutus,
13010 &objfile->objfile_obstack,
13011 hashtab_obstack_allocate,
13012 dummy_obstack_deallocate);
13015 /* Try to open DWP file FILE_NAME.
13016 The result is the bfd handle of the file.
13017 If there is a problem finding or opening the file, return NULL.
13018 Upon success, the canonicalized path of the file is stored in the bfd,
13019 same as symfile_bfd_open. */
13021 static gdb_bfd_ref_ptr
13022 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13023 const char *file_name)
13025 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13027 1 /*search_cwd*/));
13031 /* Work around upstream bug 15652.
13032 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13033 [Whether that's a "bug" is debatable, but it is getting in our way.]
13034 We have no real idea where the dwp file is, because gdb's realpath-ing
13035 of the executable's path may have discarded the needed info.
13036 [IWBN if the dwp file name was recorded in the executable, akin to
13037 .gnu_debuglink, but that doesn't exist yet.]
13038 Strip the directory from FILE_NAME and search again. */
13039 if (*debug_file_directory != '\0')
13041 /* Don't implicitly search the current directory here.
13042 If the user wants to search "." to handle this case,
13043 it must be added to debug-file-directory. */
13044 return try_open_dwop_file (dwarf2_per_objfile,
13045 lbasename (file_name), 1 /*is_dwp*/,
13052 /* Initialize the use of the DWP file for the current objfile.
13053 By convention the name of the DWP file is ${objfile}.dwp.
13054 The result is NULL if it can't be found. */
13056 static std::unique_ptr<struct dwp_file>
13057 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13059 struct objfile *objfile = dwarf2_per_objfile->objfile;
13061 /* Try to find first .dwp for the binary file before any symbolic links
13064 /* If the objfile is a debug file, find the name of the real binary
13065 file and get the name of dwp file from there. */
13066 std::string dwp_name;
13067 if (objfile->separate_debug_objfile_backlink != NULL)
13069 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13070 const char *backlink_basename = lbasename (backlink->original_name);
13072 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13075 dwp_name = objfile->original_name;
13077 dwp_name += ".dwp";
13079 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13081 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13083 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13084 dwp_name = objfile_name (objfile);
13085 dwp_name += ".dwp";
13086 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13091 if (dwarf_read_debug)
13092 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13093 return std::unique_ptr<dwp_file> ();
13096 const char *name = bfd_get_filename (dbfd.get ());
13097 std::unique_ptr<struct dwp_file> dwp_file
13098 (new struct dwp_file (name, std::move (dbfd)));
13100 /* +1: section 0 is unused */
13101 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
13102 dwp_file->elf_sections =
13103 OBSTACK_CALLOC (&objfile->objfile_obstack,
13104 dwp_file->num_sections, asection *);
13106 bfd_map_over_sections (dwp_file->dbfd.get (),
13107 dwarf2_locate_common_dwp_sections,
13110 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13113 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13116 /* The DWP file version is stored in the hash table. Oh well. */
13117 if (dwp_file->cus && dwp_file->tus
13118 && dwp_file->cus->version != dwp_file->tus->version)
13120 /* Technically speaking, we should try to limp along, but this is
13121 pretty bizarre. We use pulongest here because that's the established
13122 portability solution (e.g, we cannot use %u for uint32_t). */
13123 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13124 " TU version %s [in DWP file %s]"),
13125 pulongest (dwp_file->cus->version),
13126 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13130 dwp_file->version = dwp_file->cus->version;
13131 else if (dwp_file->tus)
13132 dwp_file->version = dwp_file->tus->version;
13134 dwp_file->version = 2;
13136 if (dwp_file->version == 2)
13137 bfd_map_over_sections (dwp_file->dbfd.get (),
13138 dwarf2_locate_v2_dwp_sections,
13141 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13142 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13144 if (dwarf_read_debug)
13146 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13147 fprintf_unfiltered (gdb_stdlog,
13148 " %s CUs, %s TUs\n",
13149 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13150 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13156 /* Wrapper around open_and_init_dwp_file, only open it once. */
13158 static struct dwp_file *
13159 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13161 if (! dwarf2_per_objfile->dwp_checked)
13163 dwarf2_per_objfile->dwp_file
13164 = open_and_init_dwp_file (dwarf2_per_objfile);
13165 dwarf2_per_objfile->dwp_checked = 1;
13167 return dwarf2_per_objfile->dwp_file.get ();
13170 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13171 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13172 or in the DWP file for the objfile, referenced by THIS_UNIT.
13173 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13174 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13176 This is called, for example, when wanting to read a variable with a
13177 complex location. Therefore we don't want to do file i/o for every call.
13178 Therefore we don't want to look for a DWO file on every call.
13179 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13180 then we check if we've already seen DWO_NAME, and only THEN do we check
13183 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13184 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13186 static struct dwo_unit *
13187 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13188 const char *dwo_name, const char *comp_dir,
13189 ULONGEST signature, int is_debug_types)
13191 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13192 struct objfile *objfile = dwarf2_per_objfile->objfile;
13193 const char *kind = is_debug_types ? "TU" : "CU";
13194 void **dwo_file_slot;
13195 struct dwo_file *dwo_file;
13196 struct dwp_file *dwp_file;
13198 /* First see if there's a DWP file.
13199 If we have a DWP file but didn't find the DWO inside it, don't
13200 look for the original DWO file. It makes gdb behave differently
13201 depending on whether one is debugging in the build tree. */
13203 dwp_file = get_dwp_file (dwarf2_per_objfile);
13204 if (dwp_file != NULL)
13206 const struct dwp_hash_table *dwp_htab =
13207 is_debug_types ? dwp_file->tus : dwp_file->cus;
13209 if (dwp_htab != NULL)
13211 struct dwo_unit *dwo_cutu =
13212 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13213 signature, is_debug_types);
13215 if (dwo_cutu != NULL)
13217 if (dwarf_read_debug)
13219 fprintf_unfiltered (gdb_stdlog,
13220 "Virtual DWO %s %s found: @%s\n",
13221 kind, hex_string (signature),
13222 host_address_to_string (dwo_cutu));
13230 /* No DWP file, look for the DWO file. */
13232 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13233 dwo_name, comp_dir);
13234 if (*dwo_file_slot == NULL)
13236 /* Read in the file and build a table of the CUs/TUs it contains. */
13237 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13239 /* NOTE: This will be NULL if unable to open the file. */
13240 dwo_file = (struct dwo_file *) *dwo_file_slot;
13242 if (dwo_file != NULL)
13244 struct dwo_unit *dwo_cutu = NULL;
13246 if (is_debug_types && dwo_file->tus)
13248 struct dwo_unit find_dwo_cutu;
13250 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13251 find_dwo_cutu.signature = signature;
13253 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13255 else if (!is_debug_types && dwo_file->cus)
13257 struct dwo_unit find_dwo_cutu;
13259 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13260 find_dwo_cutu.signature = signature;
13261 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13265 if (dwo_cutu != NULL)
13267 if (dwarf_read_debug)
13269 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13270 kind, dwo_name, hex_string (signature),
13271 host_address_to_string (dwo_cutu));
13278 /* We didn't find it. This could mean a dwo_id mismatch, or
13279 someone deleted the DWO/DWP file, or the search path isn't set up
13280 correctly to find the file. */
13282 if (dwarf_read_debug)
13284 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13285 kind, dwo_name, hex_string (signature));
13288 /* This is a warning and not a complaint because it can be caused by
13289 pilot error (e.g., user accidentally deleting the DWO). */
13291 /* Print the name of the DWP file if we looked there, helps the user
13292 better diagnose the problem. */
13293 std::string dwp_text;
13295 if (dwp_file != NULL)
13296 dwp_text = string_printf (" [in DWP file %s]",
13297 lbasename (dwp_file->name));
13299 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13300 " [in module %s]"),
13301 kind, dwo_name, hex_string (signature),
13303 this_unit->is_debug_types ? "TU" : "CU",
13304 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13309 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13310 See lookup_dwo_cutu_unit for details. */
13312 static struct dwo_unit *
13313 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13314 const char *dwo_name, const char *comp_dir,
13315 ULONGEST signature)
13317 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13320 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13321 See lookup_dwo_cutu_unit for details. */
13323 static struct dwo_unit *
13324 lookup_dwo_type_unit (struct signatured_type *this_tu,
13325 const char *dwo_name, const char *comp_dir)
13327 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13330 /* Traversal function for queue_and_load_all_dwo_tus. */
13333 queue_and_load_dwo_tu (void **slot, void *info)
13335 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13336 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13337 ULONGEST signature = dwo_unit->signature;
13338 struct signatured_type *sig_type =
13339 lookup_dwo_signatured_type (per_cu->cu, signature);
13341 if (sig_type != NULL)
13343 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13345 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13346 a real dependency of PER_CU on SIG_TYPE. That is detected later
13347 while processing PER_CU. */
13348 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13349 load_full_type_unit (sig_cu);
13350 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13356 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13357 The DWO may have the only definition of the type, though it may not be
13358 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13359 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13362 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13364 struct dwo_unit *dwo_unit;
13365 struct dwo_file *dwo_file;
13367 gdb_assert (!per_cu->is_debug_types);
13368 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13369 gdb_assert (per_cu->cu != NULL);
13371 dwo_unit = per_cu->cu->dwo_unit;
13372 gdb_assert (dwo_unit != NULL);
13374 dwo_file = dwo_unit->dwo_file;
13375 if (dwo_file->tus != NULL)
13376 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13379 /* Free all resources associated with DWO_FILE.
13380 Close the DWO file and munmap the sections. */
13383 free_dwo_file (struct dwo_file *dwo_file)
13385 /* Note: dbfd is NULL for virtual DWO files. */
13386 gdb_bfd_unref (dwo_file->dbfd);
13388 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13391 /* Traversal function for free_dwo_files. */
13394 free_dwo_file_from_slot (void **slot, void *info)
13396 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13398 free_dwo_file (dwo_file);
13403 /* Free all resources associated with DWO_FILES. */
13406 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13408 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
13411 /* Read in various DIEs. */
13413 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13414 Inherit only the children of the DW_AT_abstract_origin DIE not being
13415 already referenced by DW_AT_abstract_origin from the children of the
13419 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13421 struct die_info *child_die;
13422 sect_offset *offsetp;
13423 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13424 struct die_info *origin_die;
13425 /* Iterator of the ORIGIN_DIE children. */
13426 struct die_info *origin_child_die;
13427 struct attribute *attr;
13428 struct dwarf2_cu *origin_cu;
13429 struct pending **origin_previous_list_in_scope;
13431 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13435 /* Note that following die references may follow to a die in a
13439 origin_die = follow_die_ref (die, attr, &origin_cu);
13441 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13443 origin_previous_list_in_scope = origin_cu->list_in_scope;
13444 origin_cu->list_in_scope = cu->list_in_scope;
13446 if (die->tag != origin_die->tag
13447 && !(die->tag == DW_TAG_inlined_subroutine
13448 && origin_die->tag == DW_TAG_subprogram))
13449 complaint (_("DIE %s and its abstract origin %s have different tags"),
13450 sect_offset_str (die->sect_off),
13451 sect_offset_str (origin_die->sect_off));
13453 std::vector<sect_offset> offsets;
13455 for (child_die = die->child;
13456 child_die && child_die->tag;
13457 child_die = sibling_die (child_die))
13459 struct die_info *child_origin_die;
13460 struct dwarf2_cu *child_origin_cu;
13462 /* We are trying to process concrete instance entries:
13463 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13464 it's not relevant to our analysis here. i.e. detecting DIEs that are
13465 present in the abstract instance but not referenced in the concrete
13467 if (child_die->tag == DW_TAG_call_site
13468 || child_die->tag == DW_TAG_GNU_call_site)
13471 /* For each CHILD_DIE, find the corresponding child of
13472 ORIGIN_DIE. If there is more than one layer of
13473 DW_AT_abstract_origin, follow them all; there shouldn't be,
13474 but GCC versions at least through 4.4 generate this (GCC PR
13476 child_origin_die = child_die;
13477 child_origin_cu = cu;
13480 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13484 child_origin_die = follow_die_ref (child_origin_die, attr,
13488 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13489 counterpart may exist. */
13490 if (child_origin_die != child_die)
13492 if (child_die->tag != child_origin_die->tag
13493 && !(child_die->tag == DW_TAG_inlined_subroutine
13494 && child_origin_die->tag == DW_TAG_subprogram))
13495 complaint (_("Child DIE %s and its abstract origin %s have "
13497 sect_offset_str (child_die->sect_off),
13498 sect_offset_str (child_origin_die->sect_off));
13499 if (child_origin_die->parent != origin_die)
13500 complaint (_("Child DIE %s and its abstract origin %s have "
13501 "different parents"),
13502 sect_offset_str (child_die->sect_off),
13503 sect_offset_str (child_origin_die->sect_off));
13505 offsets.push_back (child_origin_die->sect_off);
13508 std::sort (offsets.begin (), offsets.end ());
13509 sect_offset *offsets_end = offsets.data () + offsets.size ();
13510 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13511 if (offsetp[-1] == *offsetp)
13512 complaint (_("Multiple children of DIE %s refer "
13513 "to DIE %s as their abstract origin"),
13514 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13516 offsetp = offsets.data ();
13517 origin_child_die = origin_die->child;
13518 while (origin_child_die && origin_child_die->tag)
13520 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13521 while (offsetp < offsets_end
13522 && *offsetp < origin_child_die->sect_off)
13524 if (offsetp >= offsets_end
13525 || *offsetp > origin_child_die->sect_off)
13527 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13528 Check whether we're already processing ORIGIN_CHILD_DIE.
13529 This can happen with mutually referenced abstract_origins.
13531 if (!origin_child_die->in_process)
13532 process_die (origin_child_die, origin_cu);
13534 origin_child_die = sibling_die (origin_child_die);
13536 origin_cu->list_in_scope = origin_previous_list_in_scope;
13540 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13542 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13543 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13544 struct context_stack *newobj;
13547 struct die_info *child_die;
13548 struct attribute *attr, *call_line, *call_file;
13550 CORE_ADDR baseaddr;
13551 struct block *block;
13552 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13553 std::vector<struct symbol *> template_args;
13554 struct template_symbol *templ_func = NULL;
13558 /* If we do not have call site information, we can't show the
13559 caller of this inlined function. That's too confusing, so
13560 only use the scope for local variables. */
13561 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13562 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13563 if (call_line == NULL || call_file == NULL)
13565 read_lexical_block_scope (die, cu);
13570 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13572 name = dwarf2_name (die, cu);
13574 /* Ignore functions with missing or empty names. These are actually
13575 illegal according to the DWARF standard. */
13578 complaint (_("missing name for subprogram DIE at %s"),
13579 sect_offset_str (die->sect_off));
13583 /* Ignore functions with missing or invalid low and high pc attributes. */
13584 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13585 <= PC_BOUNDS_INVALID)
13587 attr = dwarf2_attr (die, DW_AT_external, cu);
13588 if (!attr || !DW_UNSND (attr))
13589 complaint (_("cannot get low and high bounds "
13590 "for subprogram DIE at %s"),
13591 sect_offset_str (die->sect_off));
13595 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13596 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13598 /* If we have any template arguments, then we must allocate a
13599 different sort of symbol. */
13600 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13602 if (child_die->tag == DW_TAG_template_type_param
13603 || child_die->tag == DW_TAG_template_value_param)
13605 templ_func = allocate_template_symbol (objfile);
13606 templ_func->subclass = SYMBOL_TEMPLATE;
13611 newobj = push_context (0, lowpc);
13612 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13613 (struct symbol *) templ_func);
13615 /* If there is a location expression for DW_AT_frame_base, record
13617 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13619 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13621 /* If there is a location for the static link, record it. */
13622 newobj->static_link = NULL;
13623 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13626 newobj->static_link
13627 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13628 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13631 cu->list_in_scope = get_local_symbols ();
13633 if (die->child != NULL)
13635 child_die = die->child;
13636 while (child_die && child_die->tag)
13638 if (child_die->tag == DW_TAG_template_type_param
13639 || child_die->tag == DW_TAG_template_value_param)
13641 struct symbol *arg = new_symbol (child_die, NULL, cu);
13644 template_args.push_back (arg);
13647 process_die (child_die, cu);
13648 child_die = sibling_die (child_die);
13652 inherit_abstract_dies (die, cu);
13654 /* If we have a DW_AT_specification, we might need to import using
13655 directives from the context of the specification DIE. See the
13656 comment in determine_prefix. */
13657 if (cu->language == language_cplus
13658 && dwarf2_attr (die, DW_AT_specification, cu))
13660 struct dwarf2_cu *spec_cu = cu;
13661 struct die_info *spec_die = die_specification (die, &spec_cu);
13665 child_die = spec_die->child;
13666 while (child_die && child_die->tag)
13668 if (child_die->tag == DW_TAG_imported_module)
13669 process_die (child_die, spec_cu);
13670 child_die = sibling_die (child_die);
13673 /* In some cases, GCC generates specification DIEs that
13674 themselves contain DW_AT_specification attributes. */
13675 spec_die = die_specification (spec_die, &spec_cu);
13679 struct context_stack cstk = pop_context ();
13680 /* Make a block for the local symbols within. */
13681 block = finish_block (cstk.name, cstk.old_blocks,
13682 cstk.static_link, lowpc, highpc);
13684 /* For C++, set the block's scope. */
13685 if ((cu->language == language_cplus
13686 || cu->language == language_fortran
13687 || cu->language == language_d
13688 || cu->language == language_rust)
13689 && cu->processing_has_namespace_info)
13690 block_set_scope (block, determine_prefix (die, cu),
13691 &objfile->objfile_obstack);
13693 /* If we have address ranges, record them. */
13694 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13696 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13698 /* Attach template arguments to function. */
13699 if (!template_args.empty ())
13701 gdb_assert (templ_func != NULL);
13703 templ_func->n_template_arguments = template_args.size ();
13704 templ_func->template_arguments
13705 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13706 templ_func->n_template_arguments);
13707 memcpy (templ_func->template_arguments,
13708 template_args.data (),
13709 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13712 /* In C++, we can have functions nested inside functions (e.g., when
13713 a function declares a class that has methods). This means that
13714 when we finish processing a function scope, we may need to go
13715 back to building a containing block's symbol lists. */
13716 *get_local_symbols () = cstk.locals;
13717 set_local_using_directives (cstk.local_using_directives);
13719 /* If we've finished processing a top-level function, subsequent
13720 symbols go in the file symbol list. */
13721 if (outermost_context_p ())
13722 cu->list_in_scope = get_file_symbols ();
13725 /* Process all the DIES contained within a lexical block scope. Start
13726 a new scope, process the dies, and then close the scope. */
13729 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13731 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13732 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13733 CORE_ADDR lowpc, highpc;
13734 struct die_info *child_die;
13735 CORE_ADDR baseaddr;
13737 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13739 /* Ignore blocks with missing or invalid low and high pc attributes. */
13740 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13741 as multiple lexical blocks? Handling children in a sane way would
13742 be nasty. Might be easier to properly extend generic blocks to
13743 describe ranges. */
13744 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13746 case PC_BOUNDS_NOT_PRESENT:
13747 /* DW_TAG_lexical_block has no attributes, process its children as if
13748 there was no wrapping by that DW_TAG_lexical_block.
13749 GCC does no longer produces such DWARF since GCC r224161. */
13750 for (child_die = die->child;
13751 child_die != NULL && child_die->tag;
13752 child_die = sibling_die (child_die))
13753 process_die (child_die, cu);
13755 case PC_BOUNDS_INVALID:
13758 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13759 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13761 push_context (0, lowpc);
13762 if (die->child != NULL)
13764 child_die = die->child;
13765 while (child_die && child_die->tag)
13767 process_die (child_die, cu);
13768 child_die = sibling_die (child_die);
13771 inherit_abstract_dies (die, cu);
13772 struct context_stack cstk = pop_context ();
13774 if (*get_local_symbols () != NULL || (*get_local_using_directives ()) != NULL)
13776 struct block *block
13777 = finish_block (0, cstk.old_blocks, NULL,
13778 cstk.start_addr, highpc);
13780 /* Note that recording ranges after traversing children, as we
13781 do here, means that recording a parent's ranges entails
13782 walking across all its children's ranges as they appear in
13783 the address map, which is quadratic behavior.
13785 It would be nicer to record the parent's ranges before
13786 traversing its children, simply overriding whatever you find
13787 there. But since we don't even decide whether to create a
13788 block until after we've traversed its children, that's hard
13790 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13792 *get_local_symbols () = cstk.locals;
13793 set_local_using_directives (cstk.local_using_directives);
13796 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13799 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13801 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13802 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13803 CORE_ADDR pc, baseaddr;
13804 struct attribute *attr;
13805 struct call_site *call_site, call_site_local;
13808 struct die_info *child_die;
13810 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13812 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13815 /* This was a pre-DWARF-5 GNU extension alias
13816 for DW_AT_call_return_pc. */
13817 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13821 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13822 "DIE %s [in module %s]"),
13823 sect_offset_str (die->sect_off), objfile_name (objfile));
13826 pc = attr_value_as_address (attr) + baseaddr;
13827 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13829 if (cu->call_site_htab == NULL)
13830 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13831 NULL, &objfile->objfile_obstack,
13832 hashtab_obstack_allocate, NULL);
13833 call_site_local.pc = pc;
13834 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13837 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13838 "DIE %s [in module %s]"),
13839 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13840 objfile_name (objfile));
13844 /* Count parameters at the caller. */
13847 for (child_die = die->child; child_die && child_die->tag;
13848 child_die = sibling_die (child_die))
13850 if (child_die->tag != DW_TAG_call_site_parameter
13851 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13853 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13854 "DW_TAG_call_site child DIE %s [in module %s]"),
13855 child_die->tag, sect_offset_str (child_die->sect_off),
13856 objfile_name (objfile));
13864 = ((struct call_site *)
13865 obstack_alloc (&objfile->objfile_obstack,
13866 sizeof (*call_site)
13867 + (sizeof (*call_site->parameter) * (nparams - 1))));
13869 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13870 call_site->pc = pc;
13872 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13873 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13875 struct die_info *func_die;
13877 /* Skip also over DW_TAG_inlined_subroutine. */
13878 for (func_die = die->parent;
13879 func_die && func_die->tag != DW_TAG_subprogram
13880 && func_die->tag != DW_TAG_subroutine_type;
13881 func_die = func_die->parent);
13883 /* DW_AT_call_all_calls is a superset
13884 of DW_AT_call_all_tail_calls. */
13886 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13887 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13888 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13889 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13891 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13892 not complete. But keep CALL_SITE for look ups via call_site_htab,
13893 both the initial caller containing the real return address PC and
13894 the final callee containing the current PC of a chain of tail
13895 calls do not need to have the tail call list complete. But any
13896 function candidate for a virtual tail call frame searched via
13897 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13898 determined unambiguously. */
13902 struct type *func_type = NULL;
13905 func_type = get_die_type (func_die, cu);
13906 if (func_type != NULL)
13908 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13910 /* Enlist this call site to the function. */
13911 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13912 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13915 complaint (_("Cannot find function owning DW_TAG_call_site "
13916 "DIE %s [in module %s]"),
13917 sect_offset_str (die->sect_off), objfile_name (objfile));
13921 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13923 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13925 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13928 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13929 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13931 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13932 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
13933 /* Keep NULL DWARF_BLOCK. */;
13934 else if (attr_form_is_block (attr))
13936 struct dwarf2_locexpr_baton *dlbaton;
13938 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13939 dlbaton->data = DW_BLOCK (attr)->data;
13940 dlbaton->size = DW_BLOCK (attr)->size;
13941 dlbaton->per_cu = cu->per_cu;
13943 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13945 else if (attr_form_is_ref (attr))
13947 struct dwarf2_cu *target_cu = cu;
13948 struct die_info *target_die;
13950 target_die = follow_die_ref (die, attr, &target_cu);
13951 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13952 if (die_is_declaration (target_die, target_cu))
13954 const char *target_physname;
13956 /* Prefer the mangled name; otherwise compute the demangled one. */
13957 target_physname = dw2_linkage_name (target_die, target_cu);
13958 if (target_physname == NULL)
13959 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13960 if (target_physname == NULL)
13961 complaint (_("DW_AT_call_target target DIE has invalid "
13962 "physname, for referencing DIE %s [in module %s]"),
13963 sect_offset_str (die->sect_off), objfile_name (objfile));
13965 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13971 /* DW_AT_entry_pc should be preferred. */
13972 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13973 <= PC_BOUNDS_INVALID)
13974 complaint (_("DW_AT_call_target target DIE has invalid "
13975 "low pc, for referencing DIE %s [in module %s]"),
13976 sect_offset_str (die->sect_off), objfile_name (objfile));
13979 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13980 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13985 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13986 "block nor reference, for DIE %s [in module %s]"),
13987 sect_offset_str (die->sect_off), objfile_name (objfile));
13989 call_site->per_cu = cu->per_cu;
13991 for (child_die = die->child;
13992 child_die && child_die->tag;
13993 child_die = sibling_die (child_die))
13995 struct call_site_parameter *parameter;
13996 struct attribute *loc, *origin;
13998 if (child_die->tag != DW_TAG_call_site_parameter
13999 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14001 /* Already printed the complaint above. */
14005 gdb_assert (call_site->parameter_count < nparams);
14006 parameter = &call_site->parameter[call_site->parameter_count];
14008 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14009 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14010 register is contained in DW_AT_call_value. */
14012 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14013 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14014 if (origin == NULL)
14016 /* This was a pre-DWARF-5 GNU extension alias
14017 for DW_AT_call_parameter. */
14018 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14020 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14022 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14024 sect_offset sect_off
14025 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14026 if (!offset_in_cu_p (&cu->header, sect_off))
14028 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14029 binding can be done only inside one CU. Such referenced DIE
14030 therefore cannot be even moved to DW_TAG_partial_unit. */
14031 complaint (_("DW_AT_call_parameter offset is not in CU for "
14032 "DW_TAG_call_site child DIE %s [in module %s]"),
14033 sect_offset_str (child_die->sect_off),
14034 objfile_name (objfile));
14037 parameter->u.param_cu_off
14038 = (cu_offset) (sect_off - cu->header.sect_off);
14040 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14042 complaint (_("No DW_FORM_block* DW_AT_location for "
14043 "DW_TAG_call_site child DIE %s [in module %s]"),
14044 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14049 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14050 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14051 if (parameter->u.dwarf_reg != -1)
14052 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14053 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14054 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14055 ¶meter->u.fb_offset))
14056 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14059 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14060 "for DW_FORM_block* DW_AT_location is supported for "
14061 "DW_TAG_call_site child DIE %s "
14063 sect_offset_str (child_die->sect_off),
14064 objfile_name (objfile));
14069 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14071 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14072 if (!attr_form_is_block (attr))
14074 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14075 "DW_TAG_call_site child DIE %s [in module %s]"),
14076 sect_offset_str (child_die->sect_off),
14077 objfile_name (objfile));
14080 parameter->value = DW_BLOCK (attr)->data;
14081 parameter->value_size = DW_BLOCK (attr)->size;
14083 /* Parameters are not pre-cleared by memset above. */
14084 parameter->data_value = NULL;
14085 parameter->data_value_size = 0;
14086 call_site->parameter_count++;
14088 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14090 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14093 if (!attr_form_is_block (attr))
14094 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14095 "DW_TAG_call_site child DIE %s [in module %s]"),
14096 sect_offset_str (child_die->sect_off),
14097 objfile_name (objfile));
14100 parameter->data_value = DW_BLOCK (attr)->data;
14101 parameter->data_value_size = DW_BLOCK (attr)->size;
14107 /* Helper function for read_variable. If DIE represents a virtual
14108 table, then return the type of the concrete object that is
14109 associated with the virtual table. Otherwise, return NULL. */
14111 static struct type *
14112 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14114 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14118 /* Find the type DIE. */
14119 struct die_info *type_die = NULL;
14120 struct dwarf2_cu *type_cu = cu;
14122 if (attr_form_is_ref (attr))
14123 type_die = follow_die_ref (die, attr, &type_cu);
14124 if (type_die == NULL)
14127 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14129 return die_containing_type (type_die, type_cu);
14132 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14135 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14137 struct rust_vtable_symbol *storage = NULL;
14139 if (cu->language == language_rust)
14141 struct type *containing_type = rust_containing_type (die, cu);
14143 if (containing_type != NULL)
14145 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14147 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14148 struct rust_vtable_symbol);
14149 initialize_objfile_symbol (storage);
14150 storage->concrete_type = containing_type;
14151 storage->subclass = SYMBOL_RUST_VTABLE;
14155 new_symbol (die, NULL, cu, storage);
14158 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14159 reading .debug_rnglists.
14160 Callback's type should be:
14161 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14162 Return true if the attributes are present and valid, otherwise,
14165 template <typename Callback>
14167 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14168 Callback &&callback)
14170 struct dwarf2_per_objfile *dwarf2_per_objfile
14171 = cu->per_cu->dwarf2_per_objfile;
14172 struct objfile *objfile = dwarf2_per_objfile->objfile;
14173 bfd *obfd = objfile->obfd;
14174 /* Base address selection entry. */
14177 const gdb_byte *buffer;
14178 CORE_ADDR baseaddr;
14179 bool overflow = false;
14181 found_base = cu->base_known;
14182 base = cu->base_address;
14184 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14185 if (offset >= dwarf2_per_objfile->rnglists.size)
14187 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14191 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14193 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14197 /* Initialize it due to a false compiler warning. */
14198 CORE_ADDR range_beginning = 0, range_end = 0;
14199 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14200 + dwarf2_per_objfile->rnglists.size);
14201 unsigned int bytes_read;
14203 if (buffer == buf_end)
14208 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14211 case DW_RLE_end_of_list:
14213 case DW_RLE_base_address:
14214 if (buffer + cu->header.addr_size > buf_end)
14219 base = read_address (obfd, buffer, cu, &bytes_read);
14221 buffer += bytes_read;
14223 case DW_RLE_start_length:
14224 if (buffer + cu->header.addr_size > buf_end)
14229 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14230 buffer += bytes_read;
14231 range_end = (range_beginning
14232 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14233 buffer += bytes_read;
14234 if (buffer > buf_end)
14240 case DW_RLE_offset_pair:
14241 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14242 buffer += bytes_read;
14243 if (buffer > buf_end)
14248 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14249 buffer += bytes_read;
14250 if (buffer > buf_end)
14256 case DW_RLE_start_end:
14257 if (buffer + 2 * cu->header.addr_size > buf_end)
14262 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14263 buffer += bytes_read;
14264 range_end = read_address (obfd, buffer, cu, &bytes_read);
14265 buffer += bytes_read;
14268 complaint (_("Invalid .debug_rnglists data (no base address)"));
14271 if (rlet == DW_RLE_end_of_list || overflow)
14273 if (rlet == DW_RLE_base_address)
14278 /* We have no valid base address for the ranges
14280 complaint (_("Invalid .debug_rnglists data (no base address)"));
14284 if (range_beginning > range_end)
14286 /* Inverted range entries are invalid. */
14287 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14291 /* Empty range entries have no effect. */
14292 if (range_beginning == range_end)
14295 range_beginning += base;
14298 /* A not-uncommon case of bad debug info.
14299 Don't pollute the addrmap with bad data. */
14300 if (range_beginning + baseaddr == 0
14301 && !dwarf2_per_objfile->has_section_at_zero)
14303 complaint (_(".debug_rnglists entry has start address of zero"
14304 " [in module %s]"), objfile_name (objfile));
14308 callback (range_beginning, range_end);
14313 complaint (_("Offset %d is not terminated "
14314 "for DW_AT_ranges attribute"),
14322 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14323 Callback's type should be:
14324 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14325 Return 1 if the attributes are present and valid, otherwise, return 0. */
14327 template <typename Callback>
14329 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14330 Callback &&callback)
14332 struct dwarf2_per_objfile *dwarf2_per_objfile
14333 = cu->per_cu->dwarf2_per_objfile;
14334 struct objfile *objfile = dwarf2_per_objfile->objfile;
14335 struct comp_unit_head *cu_header = &cu->header;
14336 bfd *obfd = objfile->obfd;
14337 unsigned int addr_size = cu_header->addr_size;
14338 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14339 /* Base address selection entry. */
14342 unsigned int dummy;
14343 const gdb_byte *buffer;
14344 CORE_ADDR baseaddr;
14346 if (cu_header->version >= 5)
14347 return dwarf2_rnglists_process (offset, cu, callback);
14349 found_base = cu->base_known;
14350 base = cu->base_address;
14352 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14353 if (offset >= dwarf2_per_objfile->ranges.size)
14355 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14359 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14361 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14365 CORE_ADDR range_beginning, range_end;
14367 range_beginning = read_address (obfd, buffer, cu, &dummy);
14368 buffer += addr_size;
14369 range_end = read_address (obfd, buffer, cu, &dummy);
14370 buffer += addr_size;
14371 offset += 2 * addr_size;
14373 /* An end of list marker is a pair of zero addresses. */
14374 if (range_beginning == 0 && range_end == 0)
14375 /* Found the end of list entry. */
14378 /* Each base address selection entry is a pair of 2 values.
14379 The first is the largest possible address, the second is
14380 the base address. Check for a base address here. */
14381 if ((range_beginning & mask) == mask)
14383 /* If we found the largest possible address, then we already
14384 have the base address in range_end. */
14392 /* We have no valid base address for the ranges
14394 complaint (_("Invalid .debug_ranges data (no base address)"));
14398 if (range_beginning > range_end)
14400 /* Inverted range entries are invalid. */
14401 complaint (_("Invalid .debug_ranges data (inverted range)"));
14405 /* Empty range entries have no effect. */
14406 if (range_beginning == range_end)
14409 range_beginning += base;
14412 /* A not-uncommon case of bad debug info.
14413 Don't pollute the addrmap with bad data. */
14414 if (range_beginning + baseaddr == 0
14415 && !dwarf2_per_objfile->has_section_at_zero)
14417 complaint (_(".debug_ranges entry has start address of zero"
14418 " [in module %s]"), objfile_name (objfile));
14422 callback (range_beginning, range_end);
14428 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14429 Return 1 if the attributes are present and valid, otherwise, return 0.
14430 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14433 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14434 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14435 struct partial_symtab *ranges_pst)
14437 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14438 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14439 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14440 SECT_OFF_TEXT (objfile));
14443 CORE_ADDR high = 0;
14446 retval = dwarf2_ranges_process (offset, cu,
14447 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14449 if (ranges_pst != NULL)
14454 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14455 range_beginning + baseaddr);
14456 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14457 range_end + baseaddr);
14458 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14462 /* FIXME: This is recording everything as a low-high
14463 segment of consecutive addresses. We should have a
14464 data structure for discontiguous block ranges
14468 low = range_beginning;
14474 if (range_beginning < low)
14475 low = range_beginning;
14476 if (range_end > high)
14484 /* If the first entry is an end-of-list marker, the range
14485 describes an empty scope, i.e. no instructions. */
14491 *high_return = high;
14495 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14496 definition for the return value. *LOWPC and *HIGHPC are set iff
14497 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14499 static enum pc_bounds_kind
14500 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14501 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14502 struct partial_symtab *pst)
14504 struct dwarf2_per_objfile *dwarf2_per_objfile
14505 = cu->per_cu->dwarf2_per_objfile;
14506 struct attribute *attr;
14507 struct attribute *attr_high;
14509 CORE_ADDR high = 0;
14510 enum pc_bounds_kind ret;
14512 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14515 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14518 low = attr_value_as_address (attr);
14519 high = attr_value_as_address (attr_high);
14520 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14524 /* Found high w/o low attribute. */
14525 return PC_BOUNDS_INVALID;
14527 /* Found consecutive range of addresses. */
14528 ret = PC_BOUNDS_HIGH_LOW;
14532 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14535 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14536 We take advantage of the fact that DW_AT_ranges does not appear
14537 in DW_TAG_compile_unit of DWO files. */
14538 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14539 unsigned int ranges_offset = (DW_UNSND (attr)
14540 + (need_ranges_base
14544 /* Value of the DW_AT_ranges attribute is the offset in the
14545 .debug_ranges section. */
14546 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14547 return PC_BOUNDS_INVALID;
14548 /* Found discontinuous range of addresses. */
14549 ret = PC_BOUNDS_RANGES;
14552 return PC_BOUNDS_NOT_PRESENT;
14555 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14557 return PC_BOUNDS_INVALID;
14559 /* When using the GNU linker, .gnu.linkonce. sections are used to
14560 eliminate duplicate copies of functions and vtables and such.
14561 The linker will arbitrarily choose one and discard the others.
14562 The AT_*_pc values for such functions refer to local labels in
14563 these sections. If the section from that file was discarded, the
14564 labels are not in the output, so the relocs get a value of 0.
14565 If this is a discarded function, mark the pc bounds as invalid,
14566 so that GDB will ignore it. */
14567 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14568 return PC_BOUNDS_INVALID;
14576 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14577 its low and high PC addresses. Do nothing if these addresses could not
14578 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14579 and HIGHPC to the high address if greater than HIGHPC. */
14582 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14583 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14584 struct dwarf2_cu *cu)
14586 CORE_ADDR low, high;
14587 struct die_info *child = die->child;
14589 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14591 *lowpc = std::min (*lowpc, low);
14592 *highpc = std::max (*highpc, high);
14595 /* If the language does not allow nested subprograms (either inside
14596 subprograms or lexical blocks), we're done. */
14597 if (cu->language != language_ada)
14600 /* Check all the children of the given DIE. If it contains nested
14601 subprograms, then check their pc bounds. Likewise, we need to
14602 check lexical blocks as well, as they may also contain subprogram
14604 while (child && child->tag)
14606 if (child->tag == DW_TAG_subprogram
14607 || child->tag == DW_TAG_lexical_block)
14608 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14609 child = sibling_die (child);
14613 /* Get the low and high pc's represented by the scope DIE, and store
14614 them in *LOWPC and *HIGHPC. If the correct values can't be
14615 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14618 get_scope_pc_bounds (struct die_info *die,
14619 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14620 struct dwarf2_cu *cu)
14622 CORE_ADDR best_low = (CORE_ADDR) -1;
14623 CORE_ADDR best_high = (CORE_ADDR) 0;
14624 CORE_ADDR current_low, current_high;
14626 if (dwarf2_get_pc_bounds (die, ¤t_low, ¤t_high, cu, NULL)
14627 >= PC_BOUNDS_RANGES)
14629 best_low = current_low;
14630 best_high = current_high;
14634 struct die_info *child = die->child;
14636 while (child && child->tag)
14638 switch (child->tag) {
14639 case DW_TAG_subprogram:
14640 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14642 case DW_TAG_namespace:
14643 case DW_TAG_module:
14644 /* FIXME: carlton/2004-01-16: Should we do this for
14645 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14646 that current GCC's always emit the DIEs corresponding
14647 to definitions of methods of classes as children of a
14648 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14649 the DIEs giving the declarations, which could be
14650 anywhere). But I don't see any reason why the
14651 standards says that they have to be there. */
14652 get_scope_pc_bounds (child, ¤t_low, ¤t_high, cu);
14654 if (current_low != ((CORE_ADDR) -1))
14656 best_low = std::min (best_low, current_low);
14657 best_high = std::max (best_high, current_high);
14665 child = sibling_die (child);
14670 *highpc = best_high;
14673 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14677 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14678 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14680 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14681 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14682 struct attribute *attr;
14683 struct attribute *attr_high;
14685 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14688 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14691 CORE_ADDR low = attr_value_as_address (attr);
14692 CORE_ADDR high = attr_value_as_address (attr_high);
14694 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14697 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14698 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14699 record_block_range (block, low, high - 1);
14703 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14706 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14707 We take advantage of the fact that DW_AT_ranges does not appear
14708 in DW_TAG_compile_unit of DWO files. */
14709 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14711 /* The value of the DW_AT_ranges attribute is the offset of the
14712 address range list in the .debug_ranges section. */
14713 unsigned long offset = (DW_UNSND (attr)
14714 + (need_ranges_base ? cu->ranges_base : 0));
14716 dwarf2_ranges_process (offset, cu,
14717 [&] (CORE_ADDR start, CORE_ADDR end)
14721 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14722 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14723 record_block_range (block, start, end - 1);
14728 /* Check whether the producer field indicates either of GCC < 4.6, or the
14729 Intel C/C++ compiler, and cache the result in CU. */
14732 check_producer (struct dwarf2_cu *cu)
14736 if (cu->producer == NULL)
14738 /* For unknown compilers expect their behavior is DWARF version
14741 GCC started to support .debug_types sections by -gdwarf-4 since
14742 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14743 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14744 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14745 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14747 else if (producer_is_gcc (cu->producer, &major, &minor))
14749 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14750 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14752 else if (producer_is_icc (cu->producer, &major, &minor))
14753 cu->producer_is_icc_lt_14 = major < 14;
14756 /* For other non-GCC compilers, expect their behavior is DWARF version
14760 cu->checked_producer = 1;
14763 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14764 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14765 during 4.6.0 experimental. */
14768 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14770 if (!cu->checked_producer)
14771 check_producer (cu);
14773 return cu->producer_is_gxx_lt_4_6;
14776 /* Return the default accessibility type if it is not overriden by
14777 DW_AT_accessibility. */
14779 static enum dwarf_access_attribute
14780 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14782 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14784 /* The default DWARF 2 accessibility for members is public, the default
14785 accessibility for inheritance is private. */
14787 if (die->tag != DW_TAG_inheritance)
14788 return DW_ACCESS_public;
14790 return DW_ACCESS_private;
14794 /* DWARF 3+ defines the default accessibility a different way. The same
14795 rules apply now for DW_TAG_inheritance as for the members and it only
14796 depends on the container kind. */
14798 if (die->parent->tag == DW_TAG_class_type)
14799 return DW_ACCESS_private;
14801 return DW_ACCESS_public;
14805 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14806 offset. If the attribute was not found return 0, otherwise return
14807 1. If it was found but could not properly be handled, set *OFFSET
14811 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14814 struct attribute *attr;
14816 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14821 /* Note that we do not check for a section offset first here.
14822 This is because DW_AT_data_member_location is new in DWARF 4,
14823 so if we see it, we can assume that a constant form is really
14824 a constant and not a section offset. */
14825 if (attr_form_is_constant (attr))
14826 *offset = dwarf2_get_attr_constant_value (attr, 0);
14827 else if (attr_form_is_section_offset (attr))
14828 dwarf2_complex_location_expr_complaint ();
14829 else if (attr_form_is_block (attr))
14830 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14832 dwarf2_complex_location_expr_complaint ();
14840 /* Add an aggregate field to the field list. */
14843 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14844 struct dwarf2_cu *cu)
14846 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14847 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14848 struct nextfield *new_field;
14849 struct attribute *attr;
14851 const char *fieldname = "";
14853 if (die->tag == DW_TAG_inheritance)
14855 fip->baseclasses.emplace_back ();
14856 new_field = &fip->baseclasses.back ();
14860 fip->fields.emplace_back ();
14861 new_field = &fip->fields.back ();
14866 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14868 new_field->accessibility = DW_UNSND (attr);
14870 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14871 if (new_field->accessibility != DW_ACCESS_public)
14872 fip->non_public_fields = 1;
14874 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14876 new_field->virtuality = DW_UNSND (attr);
14878 new_field->virtuality = DW_VIRTUALITY_none;
14880 fp = &new_field->field;
14882 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14886 /* Data member other than a C++ static data member. */
14888 /* Get type of field. */
14889 fp->type = die_type (die, cu);
14891 SET_FIELD_BITPOS (*fp, 0);
14893 /* Get bit size of field (zero if none). */
14894 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14897 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14901 FIELD_BITSIZE (*fp) = 0;
14904 /* Get bit offset of field. */
14905 if (handle_data_member_location (die, cu, &offset))
14906 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14907 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14910 if (gdbarch_bits_big_endian (gdbarch))
14912 /* For big endian bits, the DW_AT_bit_offset gives the
14913 additional bit offset from the MSB of the containing
14914 anonymous object to the MSB of the field. We don't
14915 have to do anything special since we don't need to
14916 know the size of the anonymous object. */
14917 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14921 /* For little endian bits, compute the bit offset to the
14922 MSB of the anonymous object, subtract off the number of
14923 bits from the MSB of the field to the MSB of the
14924 object, and then subtract off the number of bits of
14925 the field itself. The result is the bit offset of
14926 the LSB of the field. */
14927 int anonymous_size;
14928 int bit_offset = DW_UNSND (attr);
14930 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14933 /* The size of the anonymous object containing
14934 the bit field is explicit, so use the
14935 indicated size (in bytes). */
14936 anonymous_size = DW_UNSND (attr);
14940 /* The size of the anonymous object containing
14941 the bit field must be inferred from the type
14942 attribute of the data member containing the
14944 anonymous_size = TYPE_LENGTH (fp->type);
14946 SET_FIELD_BITPOS (*fp,
14947 (FIELD_BITPOS (*fp)
14948 + anonymous_size * bits_per_byte
14949 - bit_offset - FIELD_BITSIZE (*fp)));
14952 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14954 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14955 + dwarf2_get_attr_constant_value (attr, 0)));
14957 /* Get name of field. */
14958 fieldname = dwarf2_name (die, cu);
14959 if (fieldname == NULL)
14962 /* The name is already allocated along with this objfile, so we don't
14963 need to duplicate it for the type. */
14964 fp->name = fieldname;
14966 /* Change accessibility for artificial fields (e.g. virtual table
14967 pointer or virtual base class pointer) to private. */
14968 if (dwarf2_attr (die, DW_AT_artificial, cu))
14970 FIELD_ARTIFICIAL (*fp) = 1;
14971 new_field->accessibility = DW_ACCESS_private;
14972 fip->non_public_fields = 1;
14975 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14977 /* C++ static member. */
14979 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14980 is a declaration, but all versions of G++ as of this writing
14981 (so through at least 3.2.1) incorrectly generate
14982 DW_TAG_variable tags. */
14984 const char *physname;
14986 /* Get name of field. */
14987 fieldname = dwarf2_name (die, cu);
14988 if (fieldname == NULL)
14991 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14993 /* Only create a symbol if this is an external value.
14994 new_symbol checks this and puts the value in the global symbol
14995 table, which we want. If it is not external, new_symbol
14996 will try to put the value in cu->list_in_scope which is wrong. */
14997 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14999 /* A static const member, not much different than an enum as far as
15000 we're concerned, except that we can support more types. */
15001 new_symbol (die, NULL, cu);
15004 /* Get physical name. */
15005 physname = dwarf2_physname (fieldname, die, cu);
15007 /* The name is already allocated along with this objfile, so we don't
15008 need to duplicate it for the type. */
15009 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15010 FIELD_TYPE (*fp) = die_type (die, cu);
15011 FIELD_NAME (*fp) = fieldname;
15013 else if (die->tag == DW_TAG_inheritance)
15017 /* C++ base class field. */
15018 if (handle_data_member_location (die, cu, &offset))
15019 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15020 FIELD_BITSIZE (*fp) = 0;
15021 FIELD_TYPE (*fp) = die_type (die, cu);
15022 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
15024 else if (die->tag == DW_TAG_variant_part)
15026 /* process_structure_scope will treat this DIE as a union. */
15027 process_structure_scope (die, cu);
15029 /* The variant part is relative to the start of the enclosing
15031 SET_FIELD_BITPOS (*fp, 0);
15032 fp->type = get_die_type (die, cu);
15033 fp->artificial = 1;
15034 fp->name = "<<variant>>";
15037 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15040 /* Can the type given by DIE define another type? */
15043 type_can_define_types (const struct die_info *die)
15047 case DW_TAG_typedef:
15048 case DW_TAG_class_type:
15049 case DW_TAG_structure_type:
15050 case DW_TAG_union_type:
15051 case DW_TAG_enumeration_type:
15059 /* Add a type definition defined in the scope of the FIP's class. */
15062 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15063 struct dwarf2_cu *cu)
15065 struct decl_field fp;
15066 memset (&fp, 0, sizeof (fp));
15068 gdb_assert (type_can_define_types (die));
15070 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15071 fp.name = dwarf2_name (die, cu);
15072 fp.type = read_type_die (die, cu);
15074 /* Save accessibility. */
15075 enum dwarf_access_attribute accessibility;
15076 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15078 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15080 accessibility = dwarf2_default_access_attribute (die, cu);
15081 switch (accessibility)
15083 case DW_ACCESS_public:
15084 /* The assumed value if neither private nor protected. */
15086 case DW_ACCESS_private:
15089 case DW_ACCESS_protected:
15090 fp.is_protected = 1;
15093 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15096 if (die->tag == DW_TAG_typedef)
15097 fip->typedef_field_list.push_back (fp);
15099 fip->nested_types_list.push_back (fp);
15102 /* Create the vector of fields, and attach it to the type. */
15105 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15106 struct dwarf2_cu *cu)
15108 int nfields = fip->nfields;
15110 /* Record the field count, allocate space for the array of fields,
15111 and create blank accessibility bitfields if necessary. */
15112 TYPE_NFIELDS (type) = nfields;
15113 TYPE_FIELDS (type) = (struct field *)
15114 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15116 if (fip->non_public_fields && cu->language != language_ada)
15118 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15120 TYPE_FIELD_PRIVATE_BITS (type) =
15121 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15122 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15124 TYPE_FIELD_PROTECTED_BITS (type) =
15125 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15126 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15128 TYPE_FIELD_IGNORE_BITS (type) =
15129 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15130 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15133 /* If the type has baseclasses, allocate and clear a bit vector for
15134 TYPE_FIELD_VIRTUAL_BITS. */
15135 if (!fip->baseclasses.empty () && cu->language != language_ada)
15137 int num_bytes = B_BYTES (fip->baseclasses.size ());
15138 unsigned char *pointer;
15140 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15141 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15142 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15143 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15144 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15147 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15149 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15151 for (int index = 0; index < nfields; ++index)
15153 struct nextfield &field = fip->fields[index];
15155 if (field.variant.is_discriminant)
15156 di->discriminant_index = index;
15157 else if (field.variant.default_branch)
15158 di->default_index = index;
15160 di->discriminants[index] = field.variant.discriminant_value;
15164 /* Copy the saved-up fields into the field vector. */
15165 for (int i = 0; i < nfields; ++i)
15167 struct nextfield &field
15168 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15169 : fip->fields[i - fip->baseclasses.size ()]);
15171 TYPE_FIELD (type, i) = field.field;
15172 switch (field.accessibility)
15174 case DW_ACCESS_private:
15175 if (cu->language != language_ada)
15176 SET_TYPE_FIELD_PRIVATE (type, i);
15179 case DW_ACCESS_protected:
15180 if (cu->language != language_ada)
15181 SET_TYPE_FIELD_PROTECTED (type, i);
15184 case DW_ACCESS_public:
15188 /* Unknown accessibility. Complain and treat it as public. */
15190 complaint (_("unsupported accessibility %d"),
15191 field.accessibility);
15195 if (i < fip->baseclasses.size ())
15197 switch (field.virtuality)
15199 case DW_VIRTUALITY_virtual:
15200 case DW_VIRTUALITY_pure_virtual:
15201 if (cu->language == language_ada)
15202 error (_("unexpected virtuality in component of Ada type"));
15203 SET_TYPE_FIELD_VIRTUAL (type, i);
15210 /* Return true if this member function is a constructor, false
15214 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15216 const char *fieldname;
15217 const char *type_name;
15220 if (die->parent == NULL)
15223 if (die->parent->tag != DW_TAG_structure_type
15224 && die->parent->tag != DW_TAG_union_type
15225 && die->parent->tag != DW_TAG_class_type)
15228 fieldname = dwarf2_name (die, cu);
15229 type_name = dwarf2_name (die->parent, cu);
15230 if (fieldname == NULL || type_name == NULL)
15233 len = strlen (fieldname);
15234 return (strncmp (fieldname, type_name, len) == 0
15235 && (type_name[len] == '\0' || type_name[len] == '<'));
15238 /* Add a member function to the proper fieldlist. */
15241 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15242 struct type *type, struct dwarf2_cu *cu)
15244 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15245 struct attribute *attr;
15247 struct fnfieldlist *flp = nullptr;
15248 struct fn_field *fnp;
15249 const char *fieldname;
15250 struct type *this_type;
15251 enum dwarf_access_attribute accessibility;
15253 if (cu->language == language_ada)
15254 error (_("unexpected member function in Ada type"));
15256 /* Get name of member function. */
15257 fieldname = dwarf2_name (die, cu);
15258 if (fieldname == NULL)
15261 /* Look up member function name in fieldlist. */
15262 for (i = 0; i < fip->fnfieldlists.size (); i++)
15264 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15266 flp = &fip->fnfieldlists[i];
15271 /* Create a new fnfieldlist if necessary. */
15272 if (flp == nullptr)
15274 fip->fnfieldlists.emplace_back ();
15275 flp = &fip->fnfieldlists.back ();
15276 flp->name = fieldname;
15277 i = fip->fnfieldlists.size () - 1;
15280 /* Create a new member function field and add it to the vector of
15282 flp->fnfields.emplace_back ();
15283 fnp = &flp->fnfields.back ();
15285 /* Delay processing of the physname until later. */
15286 if (cu->language == language_cplus)
15287 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15291 const char *physname = dwarf2_physname (fieldname, die, cu);
15292 fnp->physname = physname ? physname : "";
15295 fnp->type = alloc_type (objfile);
15296 this_type = read_type_die (die, cu);
15297 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15299 int nparams = TYPE_NFIELDS (this_type);
15301 /* TYPE is the domain of this method, and THIS_TYPE is the type
15302 of the method itself (TYPE_CODE_METHOD). */
15303 smash_to_method_type (fnp->type, type,
15304 TYPE_TARGET_TYPE (this_type),
15305 TYPE_FIELDS (this_type),
15306 TYPE_NFIELDS (this_type),
15307 TYPE_VARARGS (this_type));
15309 /* Handle static member functions.
15310 Dwarf2 has no clean way to discern C++ static and non-static
15311 member functions. G++ helps GDB by marking the first
15312 parameter for non-static member functions (which is the this
15313 pointer) as artificial. We obtain this information from
15314 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15315 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15316 fnp->voffset = VOFFSET_STATIC;
15319 complaint (_("member function type missing for '%s'"),
15320 dwarf2_full_name (fieldname, die, cu));
15322 /* Get fcontext from DW_AT_containing_type if present. */
15323 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15324 fnp->fcontext = die_containing_type (die, cu);
15326 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15327 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15329 /* Get accessibility. */
15330 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15332 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15334 accessibility = dwarf2_default_access_attribute (die, cu);
15335 switch (accessibility)
15337 case DW_ACCESS_private:
15338 fnp->is_private = 1;
15340 case DW_ACCESS_protected:
15341 fnp->is_protected = 1;
15345 /* Check for artificial methods. */
15346 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15347 if (attr && DW_UNSND (attr) != 0)
15348 fnp->is_artificial = 1;
15350 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15352 /* Get index in virtual function table if it is a virtual member
15353 function. For older versions of GCC, this is an offset in the
15354 appropriate virtual table, as specified by DW_AT_containing_type.
15355 For everyone else, it is an expression to be evaluated relative
15356 to the object address. */
15358 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15361 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15363 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15365 /* Old-style GCC. */
15366 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15368 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15369 || (DW_BLOCK (attr)->size > 1
15370 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15371 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15373 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15374 if ((fnp->voffset % cu->header.addr_size) != 0)
15375 dwarf2_complex_location_expr_complaint ();
15377 fnp->voffset /= cu->header.addr_size;
15381 dwarf2_complex_location_expr_complaint ();
15383 if (!fnp->fcontext)
15385 /* If there is no `this' field and no DW_AT_containing_type,
15386 we cannot actually find a base class context for the
15388 if (TYPE_NFIELDS (this_type) == 0
15389 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15391 complaint (_("cannot determine context for virtual member "
15392 "function \"%s\" (offset %s)"),
15393 fieldname, sect_offset_str (die->sect_off));
15398 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15402 else if (attr_form_is_section_offset (attr))
15404 dwarf2_complex_location_expr_complaint ();
15408 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15414 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15415 if (attr && DW_UNSND (attr))
15417 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15418 complaint (_("Member function \"%s\" (offset %s) is virtual "
15419 "but the vtable offset is not specified"),
15420 fieldname, sect_offset_str (die->sect_off));
15421 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15422 TYPE_CPLUS_DYNAMIC (type) = 1;
15427 /* Create the vector of member function fields, and attach it to the type. */
15430 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15431 struct dwarf2_cu *cu)
15433 if (cu->language == language_ada)
15434 error (_("unexpected member functions in Ada type"));
15436 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15437 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15439 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15441 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15443 struct fnfieldlist &nf = fip->fnfieldlists[i];
15444 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15446 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15447 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15448 fn_flp->fn_fields = (struct fn_field *)
15449 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15451 for (int k = 0; k < nf.fnfields.size (); ++k)
15452 fn_flp->fn_fields[k] = nf.fnfields[k];
15455 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15458 /* Returns non-zero if NAME is the name of a vtable member in CU's
15459 language, zero otherwise. */
15461 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15463 static const char vptr[] = "_vptr";
15465 /* Look for the C++ form of the vtable. */
15466 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15472 /* GCC outputs unnamed structures that are really pointers to member
15473 functions, with the ABI-specified layout. If TYPE describes
15474 such a structure, smash it into a member function type.
15476 GCC shouldn't do this; it should just output pointer to member DIEs.
15477 This is GCC PR debug/28767. */
15480 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15482 struct type *pfn_type, *self_type, *new_type;
15484 /* Check for a structure with no name and two children. */
15485 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15488 /* Check for __pfn and __delta members. */
15489 if (TYPE_FIELD_NAME (type, 0) == NULL
15490 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15491 || TYPE_FIELD_NAME (type, 1) == NULL
15492 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15495 /* Find the type of the method. */
15496 pfn_type = TYPE_FIELD_TYPE (type, 0);
15497 if (pfn_type == NULL
15498 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15499 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15502 /* Look for the "this" argument. */
15503 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15504 if (TYPE_NFIELDS (pfn_type) == 0
15505 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15506 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15509 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15510 new_type = alloc_type (objfile);
15511 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15512 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15513 TYPE_VARARGS (pfn_type));
15514 smash_to_methodptr_type (type, new_type);
15517 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15518 appropriate error checking and issuing complaints if there is a
15522 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15524 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15526 if (attr == nullptr)
15529 if (!attr_form_is_constant (attr))
15531 complaint (_("DW_AT_alignment must have constant form"
15532 " - DIE at %s [in module %s]"),
15533 sect_offset_str (die->sect_off),
15534 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15539 if (attr->form == DW_FORM_sdata)
15541 LONGEST val = DW_SND (attr);
15544 complaint (_("DW_AT_alignment value must not be negative"
15545 " - DIE at %s [in module %s]"),
15546 sect_offset_str (die->sect_off),
15547 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15553 align = DW_UNSND (attr);
15557 complaint (_("DW_AT_alignment value must not be zero"
15558 " - DIE at %s [in module %s]"),
15559 sect_offset_str (die->sect_off),
15560 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15563 if ((align & (align - 1)) != 0)
15565 complaint (_("DW_AT_alignment value must be a power of 2"
15566 " - DIE at %s [in module %s]"),
15567 sect_offset_str (die->sect_off),
15568 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15575 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15576 the alignment for TYPE. */
15579 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15582 if (!set_type_align (type, get_alignment (cu, die)))
15583 complaint (_("DW_AT_alignment value too large"
15584 " - DIE at %s [in module %s]"),
15585 sect_offset_str (die->sect_off),
15586 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15589 /* Called when we find the DIE that starts a structure or union scope
15590 (definition) to create a type for the structure or union. Fill in
15591 the type's name and general properties; the members will not be
15592 processed until process_structure_scope. A symbol table entry for
15593 the type will also not be done until process_structure_scope (assuming
15594 the type has a name).
15596 NOTE: we need to call these functions regardless of whether or not the
15597 DIE has a DW_AT_name attribute, since it might be an anonymous
15598 structure or union. This gets the type entered into our set of
15599 user defined types. */
15601 static struct type *
15602 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15604 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15606 struct attribute *attr;
15609 /* If the definition of this type lives in .debug_types, read that type.
15610 Don't follow DW_AT_specification though, that will take us back up
15611 the chain and we want to go down. */
15612 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15615 type = get_DW_AT_signature_type (die, attr, cu);
15617 /* The type's CU may not be the same as CU.
15618 Ensure TYPE is recorded with CU in die_type_hash. */
15619 return set_die_type (die, type, cu);
15622 type = alloc_type (objfile);
15623 INIT_CPLUS_SPECIFIC (type);
15625 name = dwarf2_name (die, cu);
15628 if (cu->language == language_cplus
15629 || cu->language == language_d
15630 || cu->language == language_rust)
15632 const char *full_name = dwarf2_full_name (name, die, cu);
15634 /* dwarf2_full_name might have already finished building the DIE's
15635 type. If so, there is no need to continue. */
15636 if (get_die_type (die, cu) != NULL)
15637 return get_die_type (die, cu);
15639 TYPE_NAME (type) = full_name;
15643 /* The name is already allocated along with this objfile, so
15644 we don't need to duplicate it for the type. */
15645 TYPE_NAME (type) = name;
15649 if (die->tag == DW_TAG_structure_type)
15651 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15653 else if (die->tag == DW_TAG_union_type)
15655 TYPE_CODE (type) = TYPE_CODE_UNION;
15657 else if (die->tag == DW_TAG_variant_part)
15659 TYPE_CODE (type) = TYPE_CODE_UNION;
15660 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15664 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15667 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15668 TYPE_DECLARED_CLASS (type) = 1;
15670 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15673 if (attr_form_is_constant (attr))
15674 TYPE_LENGTH (type) = DW_UNSND (attr);
15677 /* For the moment, dynamic type sizes are not supported
15678 by GDB's struct type. The actual size is determined
15679 on-demand when resolving the type of a given object,
15680 so set the type's length to zero for now. Otherwise,
15681 we record an expression as the length, and that expression
15682 could lead to a very large value, which could eventually
15683 lead to us trying to allocate that much memory when creating
15684 a value of that type. */
15685 TYPE_LENGTH (type) = 0;
15690 TYPE_LENGTH (type) = 0;
15693 maybe_set_alignment (cu, die, type);
15695 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15697 /* ICC<14 does not output the required DW_AT_declaration on
15698 incomplete types, but gives them a size of zero. */
15699 TYPE_STUB (type) = 1;
15702 TYPE_STUB_SUPPORTED (type) = 1;
15704 if (die_is_declaration (die, cu))
15705 TYPE_STUB (type) = 1;
15706 else if (attr == NULL && die->child == NULL
15707 && producer_is_realview (cu->producer))
15708 /* RealView does not output the required DW_AT_declaration
15709 on incomplete types. */
15710 TYPE_STUB (type) = 1;
15712 /* We need to add the type field to the die immediately so we don't
15713 infinitely recurse when dealing with pointers to the structure
15714 type within the structure itself. */
15715 set_die_type (die, type, cu);
15717 /* set_die_type should be already done. */
15718 set_descriptive_type (type, die, cu);
15723 /* A helper for process_structure_scope that handles a single member
15727 handle_struct_member_die (struct die_info *child_die, struct type *type,
15728 struct field_info *fi,
15729 std::vector<struct symbol *> *template_args,
15730 struct dwarf2_cu *cu)
15732 if (child_die->tag == DW_TAG_member
15733 || child_die->tag == DW_TAG_variable
15734 || child_die->tag == DW_TAG_variant_part)
15736 /* NOTE: carlton/2002-11-05: A C++ static data member
15737 should be a DW_TAG_member that is a declaration, but
15738 all versions of G++ as of this writing (so through at
15739 least 3.2.1) incorrectly generate DW_TAG_variable
15740 tags for them instead. */
15741 dwarf2_add_field (fi, child_die, cu);
15743 else if (child_die->tag == DW_TAG_subprogram)
15745 /* Rust doesn't have member functions in the C++ sense.
15746 However, it does emit ordinary functions as children
15747 of a struct DIE. */
15748 if (cu->language == language_rust)
15749 read_func_scope (child_die, cu);
15752 /* C++ member function. */
15753 dwarf2_add_member_fn (fi, child_die, type, cu);
15756 else if (child_die->tag == DW_TAG_inheritance)
15758 /* C++ base class field. */
15759 dwarf2_add_field (fi, child_die, cu);
15761 else if (type_can_define_types (child_die))
15762 dwarf2_add_type_defn (fi, child_die, cu);
15763 else if (child_die->tag == DW_TAG_template_type_param
15764 || child_die->tag == DW_TAG_template_value_param)
15766 struct symbol *arg = new_symbol (child_die, NULL, cu);
15769 template_args->push_back (arg);
15771 else if (child_die->tag == DW_TAG_variant)
15773 /* In a variant we want to get the discriminant and also add a
15774 field for our sole member child. */
15775 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15777 for (struct die_info *variant_child = child_die->child;
15778 variant_child != NULL;
15779 variant_child = sibling_die (variant_child))
15781 if (variant_child->tag == DW_TAG_member)
15783 handle_struct_member_die (variant_child, type, fi,
15784 template_args, cu);
15785 /* Only handle the one. */
15790 /* We don't handle this but we might as well report it if we see
15792 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15793 complaint (_("DW_AT_discr_list is not supported yet"
15794 " - DIE at %s [in module %s]"),
15795 sect_offset_str (child_die->sect_off),
15796 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15798 /* The first field was just added, so we can stash the
15799 discriminant there. */
15800 gdb_assert (!fi->fields.empty ());
15802 fi->fields.back ().variant.default_branch = true;
15804 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15808 /* Finish creating a structure or union type, including filling in
15809 its members and creating a symbol for it. */
15812 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15814 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15815 struct die_info *child_die;
15818 type = get_die_type (die, cu);
15820 type = read_structure_type (die, cu);
15822 /* When reading a DW_TAG_variant_part, we need to notice when we
15823 read the discriminant member, so we can record it later in the
15824 discriminant_info. */
15825 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15826 sect_offset discr_offset;
15828 if (is_variant_part)
15830 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15833 /* Maybe it's a univariant form, an extension we support.
15834 In this case arrange not to check the offset. */
15835 is_variant_part = false;
15837 else if (attr_form_is_ref (discr))
15839 struct dwarf2_cu *target_cu = cu;
15840 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15842 discr_offset = target_die->sect_off;
15846 complaint (_("DW_AT_discr does not have DIE reference form"
15847 " - DIE at %s [in module %s]"),
15848 sect_offset_str (die->sect_off),
15849 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15850 is_variant_part = false;
15854 if (die->child != NULL && ! die_is_declaration (die, cu))
15856 struct field_info fi;
15857 std::vector<struct symbol *> template_args;
15859 child_die = die->child;
15861 while (child_die && child_die->tag)
15863 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15865 if (is_variant_part && discr_offset == child_die->sect_off)
15866 fi.fields.back ().variant.is_discriminant = true;
15868 child_die = sibling_die (child_die);
15871 /* Attach template arguments to type. */
15872 if (!template_args.empty ())
15874 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15875 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15876 TYPE_TEMPLATE_ARGUMENTS (type)
15877 = XOBNEWVEC (&objfile->objfile_obstack,
15879 TYPE_N_TEMPLATE_ARGUMENTS (type));
15880 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15881 template_args.data (),
15882 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15883 * sizeof (struct symbol *)));
15886 /* Attach fields and member functions to the type. */
15888 dwarf2_attach_fields_to_type (&fi, type, cu);
15889 if (!fi.fnfieldlists.empty ())
15891 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15893 /* Get the type which refers to the base class (possibly this
15894 class itself) which contains the vtable pointer for the current
15895 class from the DW_AT_containing_type attribute. This use of
15896 DW_AT_containing_type is a GNU extension. */
15898 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15900 struct type *t = die_containing_type (die, cu);
15902 set_type_vptr_basetype (type, t);
15907 /* Our own class provides vtbl ptr. */
15908 for (i = TYPE_NFIELDS (t) - 1;
15909 i >= TYPE_N_BASECLASSES (t);
15912 const char *fieldname = TYPE_FIELD_NAME (t, i);
15914 if (is_vtable_name (fieldname, cu))
15916 set_type_vptr_fieldno (type, i);
15921 /* Complain if virtual function table field not found. */
15922 if (i < TYPE_N_BASECLASSES (t))
15923 complaint (_("virtual function table pointer "
15924 "not found when defining class '%s'"),
15925 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15929 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15932 else if (cu->producer
15933 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15935 /* The IBM XLC compiler does not provide direct indication
15936 of the containing type, but the vtable pointer is
15937 always named __vfp. */
15941 for (i = TYPE_NFIELDS (type) - 1;
15942 i >= TYPE_N_BASECLASSES (type);
15945 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15947 set_type_vptr_fieldno (type, i);
15948 set_type_vptr_basetype (type, type);
15955 /* Copy fi.typedef_field_list linked list elements content into the
15956 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15957 if (!fi.typedef_field_list.empty ())
15959 int count = fi.typedef_field_list.size ();
15961 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15962 TYPE_TYPEDEF_FIELD_ARRAY (type)
15963 = ((struct decl_field *)
15965 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15966 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15968 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15969 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15972 /* Copy fi.nested_types_list linked list elements content into the
15973 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15974 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15976 int count = fi.nested_types_list.size ();
15978 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15979 TYPE_NESTED_TYPES_ARRAY (type)
15980 = ((struct decl_field *)
15981 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15982 TYPE_NESTED_TYPES_COUNT (type) = count;
15984 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15985 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15989 quirk_gcc_member_function_pointer (type, objfile);
15990 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15991 cu->rust_unions.push_back (type);
15993 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15994 snapshots) has been known to create a die giving a declaration
15995 for a class that has, as a child, a die giving a definition for a
15996 nested class. So we have to process our children even if the
15997 current die is a declaration. Normally, of course, a declaration
15998 won't have any children at all. */
16000 child_die = die->child;
16002 while (child_die != NULL && child_die->tag)
16004 if (child_die->tag == DW_TAG_member
16005 || child_die->tag == DW_TAG_variable
16006 || child_die->tag == DW_TAG_inheritance
16007 || child_die->tag == DW_TAG_template_value_param
16008 || child_die->tag == DW_TAG_template_type_param)
16013 process_die (child_die, cu);
16015 child_die = sibling_die (child_die);
16018 /* Do not consider external references. According to the DWARF standard,
16019 these DIEs are identified by the fact that they have no byte_size
16020 attribute, and a declaration attribute. */
16021 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16022 || !die_is_declaration (die, cu))
16023 new_symbol (die, type, cu);
16026 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16027 update TYPE using some information only available in DIE's children. */
16030 update_enumeration_type_from_children (struct die_info *die,
16032 struct dwarf2_cu *cu)
16034 struct die_info *child_die;
16035 int unsigned_enum = 1;
16039 auto_obstack obstack;
16041 for (child_die = die->child;
16042 child_die != NULL && child_die->tag;
16043 child_die = sibling_die (child_die))
16045 struct attribute *attr;
16047 const gdb_byte *bytes;
16048 struct dwarf2_locexpr_baton *baton;
16051 if (child_die->tag != DW_TAG_enumerator)
16054 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16058 name = dwarf2_name (child_die, cu);
16060 name = "<anonymous enumerator>";
16062 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16063 &value, &bytes, &baton);
16069 else if ((mask & value) != 0)
16074 /* If we already know that the enum type is neither unsigned, nor
16075 a flag type, no need to look at the rest of the enumerates. */
16076 if (!unsigned_enum && !flag_enum)
16081 TYPE_UNSIGNED (type) = 1;
16083 TYPE_FLAG_ENUM (type) = 1;
16086 /* Given a DW_AT_enumeration_type die, set its type. We do not
16087 complete the type's fields yet, or create any symbols. */
16089 static struct type *
16090 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16092 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16094 struct attribute *attr;
16097 /* If the definition of this type lives in .debug_types, read that type.
16098 Don't follow DW_AT_specification though, that will take us back up
16099 the chain and we want to go down. */
16100 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16103 type = get_DW_AT_signature_type (die, attr, cu);
16105 /* The type's CU may not be the same as CU.
16106 Ensure TYPE is recorded with CU in die_type_hash. */
16107 return set_die_type (die, type, cu);
16110 type = alloc_type (objfile);
16112 TYPE_CODE (type) = TYPE_CODE_ENUM;
16113 name = dwarf2_full_name (NULL, die, cu);
16115 TYPE_NAME (type) = name;
16117 attr = dwarf2_attr (die, DW_AT_type, cu);
16120 struct type *underlying_type = die_type (die, cu);
16122 TYPE_TARGET_TYPE (type) = underlying_type;
16125 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16128 TYPE_LENGTH (type) = DW_UNSND (attr);
16132 TYPE_LENGTH (type) = 0;
16135 maybe_set_alignment (cu, die, type);
16137 /* The enumeration DIE can be incomplete. In Ada, any type can be
16138 declared as private in the package spec, and then defined only
16139 inside the package body. Such types are known as Taft Amendment
16140 Types. When another package uses such a type, an incomplete DIE
16141 may be generated by the compiler. */
16142 if (die_is_declaration (die, cu))
16143 TYPE_STUB (type) = 1;
16145 /* Finish the creation of this type by using the enum's children.
16146 We must call this even when the underlying type has been provided
16147 so that we can determine if we're looking at a "flag" enum. */
16148 update_enumeration_type_from_children (die, type, cu);
16150 /* If this type has an underlying type that is not a stub, then we
16151 may use its attributes. We always use the "unsigned" attribute
16152 in this situation, because ordinarily we guess whether the type
16153 is unsigned -- but the guess can be wrong and the underlying type
16154 can tell us the reality. However, we defer to a local size
16155 attribute if one exists, because this lets the compiler override
16156 the underlying type if needed. */
16157 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16159 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16160 if (TYPE_LENGTH (type) == 0)
16161 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16162 if (TYPE_RAW_ALIGN (type) == 0
16163 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16164 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16167 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16169 return set_die_type (die, type, cu);
16172 /* Given a pointer to a die which begins an enumeration, process all
16173 the dies that define the members of the enumeration, and create the
16174 symbol for the enumeration type.
16176 NOTE: We reverse the order of the element list. */
16179 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16181 struct type *this_type;
16183 this_type = get_die_type (die, cu);
16184 if (this_type == NULL)
16185 this_type = read_enumeration_type (die, cu);
16187 if (die->child != NULL)
16189 struct die_info *child_die;
16190 struct symbol *sym;
16191 struct field *fields = NULL;
16192 int num_fields = 0;
16195 child_die = die->child;
16196 while (child_die && child_die->tag)
16198 if (child_die->tag != DW_TAG_enumerator)
16200 process_die (child_die, cu);
16204 name = dwarf2_name (child_die, cu);
16207 sym = new_symbol (child_die, this_type, cu);
16209 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16211 fields = (struct field *)
16213 (num_fields + DW_FIELD_ALLOC_CHUNK)
16214 * sizeof (struct field));
16217 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16218 FIELD_TYPE (fields[num_fields]) = NULL;
16219 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16220 FIELD_BITSIZE (fields[num_fields]) = 0;
16226 child_die = sibling_die (child_die);
16231 TYPE_NFIELDS (this_type) = num_fields;
16232 TYPE_FIELDS (this_type) = (struct field *)
16233 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16234 memcpy (TYPE_FIELDS (this_type), fields,
16235 sizeof (struct field) * num_fields);
16240 /* If we are reading an enum from a .debug_types unit, and the enum
16241 is a declaration, and the enum is not the signatured type in the
16242 unit, then we do not want to add a symbol for it. Adding a
16243 symbol would in some cases obscure the true definition of the
16244 enum, giving users an incomplete type when the definition is
16245 actually available. Note that we do not want to do this for all
16246 enums which are just declarations, because C++0x allows forward
16247 enum declarations. */
16248 if (cu->per_cu->is_debug_types
16249 && die_is_declaration (die, cu))
16251 struct signatured_type *sig_type;
16253 sig_type = (struct signatured_type *) cu->per_cu;
16254 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16255 if (sig_type->type_offset_in_section != die->sect_off)
16259 new_symbol (die, this_type, cu);
16262 /* Extract all information from a DW_TAG_array_type DIE and put it in
16263 the DIE's type field. For now, this only handles one dimensional
16266 static struct type *
16267 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16269 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16270 struct die_info *child_die;
16272 struct type *element_type, *range_type, *index_type;
16273 struct attribute *attr;
16275 struct dynamic_prop *byte_stride_prop = NULL;
16276 unsigned int bit_stride = 0;
16278 element_type = die_type (die, cu);
16280 /* The die_type call above may have already set the type for this DIE. */
16281 type = get_die_type (die, cu);
16285 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16291 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16292 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16295 complaint (_("unable to read array DW_AT_byte_stride "
16296 " - DIE at %s [in module %s]"),
16297 sect_offset_str (die->sect_off),
16298 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16299 /* Ignore this attribute. We will likely not be able to print
16300 arrays of this type correctly, but there is little we can do
16301 to help if we cannot read the attribute's value. */
16302 byte_stride_prop = NULL;
16306 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16308 bit_stride = DW_UNSND (attr);
16310 /* Irix 6.2 native cc creates array types without children for
16311 arrays with unspecified length. */
16312 if (die->child == NULL)
16314 index_type = objfile_type (objfile)->builtin_int;
16315 range_type = create_static_range_type (NULL, index_type, 0, -1);
16316 type = create_array_type_with_stride (NULL, element_type, range_type,
16317 byte_stride_prop, bit_stride);
16318 return set_die_type (die, type, cu);
16321 std::vector<struct type *> range_types;
16322 child_die = die->child;
16323 while (child_die && child_die->tag)
16325 if (child_die->tag == DW_TAG_subrange_type)
16327 struct type *child_type = read_type_die (child_die, cu);
16329 if (child_type != NULL)
16331 /* The range type was succesfully read. Save it for the
16332 array type creation. */
16333 range_types.push_back (child_type);
16336 child_die = sibling_die (child_die);
16339 /* Dwarf2 dimensions are output from left to right, create the
16340 necessary array types in backwards order. */
16342 type = element_type;
16344 if (read_array_order (die, cu) == DW_ORD_col_major)
16348 while (i < range_types.size ())
16349 type = create_array_type_with_stride (NULL, type, range_types[i++],
16350 byte_stride_prop, bit_stride);
16354 size_t ndim = range_types.size ();
16356 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16357 byte_stride_prop, bit_stride);
16360 /* Understand Dwarf2 support for vector types (like they occur on
16361 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16362 array type. This is not part of the Dwarf2/3 standard yet, but a
16363 custom vendor extension. The main difference between a regular
16364 array and the vector variant is that vectors are passed by value
16366 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16368 make_vector_type (type);
16370 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16371 implementation may choose to implement triple vectors using this
16373 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16376 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16377 TYPE_LENGTH (type) = DW_UNSND (attr);
16379 complaint (_("DW_AT_byte_size for array type smaller "
16380 "than the total size of elements"));
16383 name = dwarf2_name (die, cu);
16385 TYPE_NAME (type) = name;
16387 maybe_set_alignment (cu, die, type);
16389 /* Install the type in the die. */
16390 set_die_type (die, type, cu);
16392 /* set_die_type should be already done. */
16393 set_descriptive_type (type, die, cu);
16398 static enum dwarf_array_dim_ordering
16399 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16401 struct attribute *attr;
16403 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16406 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16408 /* GNU F77 is a special case, as at 08/2004 array type info is the
16409 opposite order to the dwarf2 specification, but data is still
16410 laid out as per normal fortran.
16412 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16413 version checking. */
16415 if (cu->language == language_fortran
16416 && cu->producer && strstr (cu->producer, "GNU F77"))
16418 return DW_ORD_row_major;
16421 switch (cu->language_defn->la_array_ordering)
16423 case array_column_major:
16424 return DW_ORD_col_major;
16425 case array_row_major:
16427 return DW_ORD_row_major;
16431 /* Extract all information from a DW_TAG_set_type DIE and put it in
16432 the DIE's type field. */
16434 static struct type *
16435 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16437 struct type *domain_type, *set_type;
16438 struct attribute *attr;
16440 domain_type = die_type (die, cu);
16442 /* The die_type call above may have already set the type for this DIE. */
16443 set_type = get_die_type (die, cu);
16447 set_type = create_set_type (NULL, domain_type);
16449 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16451 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16453 maybe_set_alignment (cu, die, set_type);
16455 return set_die_type (die, set_type, cu);
16458 /* A helper for read_common_block that creates a locexpr baton.
16459 SYM is the symbol which we are marking as computed.
16460 COMMON_DIE is the DIE for the common block.
16461 COMMON_LOC is the location expression attribute for the common
16463 MEMBER_LOC is the location expression attribute for the particular
16464 member of the common block that we are processing.
16465 CU is the CU from which the above come. */
16468 mark_common_block_symbol_computed (struct symbol *sym,
16469 struct die_info *common_die,
16470 struct attribute *common_loc,
16471 struct attribute *member_loc,
16472 struct dwarf2_cu *cu)
16474 struct dwarf2_per_objfile *dwarf2_per_objfile
16475 = cu->per_cu->dwarf2_per_objfile;
16476 struct objfile *objfile = dwarf2_per_objfile->objfile;
16477 struct dwarf2_locexpr_baton *baton;
16479 unsigned int cu_off;
16480 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16481 LONGEST offset = 0;
16483 gdb_assert (common_loc && member_loc);
16484 gdb_assert (attr_form_is_block (common_loc));
16485 gdb_assert (attr_form_is_block (member_loc)
16486 || attr_form_is_constant (member_loc));
16488 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16489 baton->per_cu = cu->per_cu;
16490 gdb_assert (baton->per_cu);
16492 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16494 if (attr_form_is_constant (member_loc))
16496 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16497 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16500 baton->size += DW_BLOCK (member_loc)->size;
16502 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16505 *ptr++ = DW_OP_call4;
16506 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16507 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16510 if (attr_form_is_constant (member_loc))
16512 *ptr++ = DW_OP_addr;
16513 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16514 ptr += cu->header.addr_size;
16518 /* We have to copy the data here, because DW_OP_call4 will only
16519 use a DW_AT_location attribute. */
16520 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16521 ptr += DW_BLOCK (member_loc)->size;
16524 *ptr++ = DW_OP_plus;
16525 gdb_assert (ptr - baton->data == baton->size);
16527 SYMBOL_LOCATION_BATON (sym) = baton;
16528 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16531 /* Create appropriate locally-scoped variables for all the
16532 DW_TAG_common_block entries. Also create a struct common_block
16533 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16534 is used to sepate the common blocks name namespace from regular
16538 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16540 struct attribute *attr;
16542 attr = dwarf2_attr (die, DW_AT_location, cu);
16545 /* Support the .debug_loc offsets. */
16546 if (attr_form_is_block (attr))
16550 else if (attr_form_is_section_offset (attr))
16552 dwarf2_complex_location_expr_complaint ();
16557 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16558 "common block member");
16563 if (die->child != NULL)
16565 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16566 struct die_info *child_die;
16567 size_t n_entries = 0, size;
16568 struct common_block *common_block;
16569 struct symbol *sym;
16571 for (child_die = die->child;
16572 child_die && child_die->tag;
16573 child_die = sibling_die (child_die))
16576 size = (sizeof (struct common_block)
16577 + (n_entries - 1) * sizeof (struct symbol *));
16579 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16581 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16582 common_block->n_entries = 0;
16584 for (child_die = die->child;
16585 child_die && child_die->tag;
16586 child_die = sibling_die (child_die))
16588 /* Create the symbol in the DW_TAG_common_block block in the current
16590 sym = new_symbol (child_die, NULL, cu);
16593 struct attribute *member_loc;
16595 common_block->contents[common_block->n_entries++] = sym;
16597 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16601 /* GDB has handled this for a long time, but it is
16602 not specified by DWARF. It seems to have been
16603 emitted by gfortran at least as recently as:
16604 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16605 complaint (_("Variable in common block has "
16606 "DW_AT_data_member_location "
16607 "- DIE at %s [in module %s]"),
16608 sect_offset_str (child_die->sect_off),
16609 objfile_name (objfile));
16611 if (attr_form_is_section_offset (member_loc))
16612 dwarf2_complex_location_expr_complaint ();
16613 else if (attr_form_is_constant (member_loc)
16614 || attr_form_is_block (member_loc))
16617 mark_common_block_symbol_computed (sym, die, attr,
16621 dwarf2_complex_location_expr_complaint ();
16626 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16627 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16631 /* Create a type for a C++ namespace. */
16633 static struct type *
16634 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16636 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16637 const char *previous_prefix, *name;
16641 /* For extensions, reuse the type of the original namespace. */
16642 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16644 struct die_info *ext_die;
16645 struct dwarf2_cu *ext_cu = cu;
16647 ext_die = dwarf2_extension (die, &ext_cu);
16648 type = read_type_die (ext_die, ext_cu);
16650 /* EXT_CU may not be the same as CU.
16651 Ensure TYPE is recorded with CU in die_type_hash. */
16652 return set_die_type (die, type, cu);
16655 name = namespace_name (die, &is_anonymous, cu);
16657 /* Now build the name of the current namespace. */
16659 previous_prefix = determine_prefix (die, cu);
16660 if (previous_prefix[0] != '\0')
16661 name = typename_concat (&objfile->objfile_obstack,
16662 previous_prefix, name, 0, cu);
16664 /* Create the type. */
16665 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16667 return set_die_type (die, type, cu);
16670 /* Read a namespace scope. */
16673 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16675 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16678 /* Add a symbol associated to this if we haven't seen the namespace
16679 before. Also, add a using directive if it's an anonymous
16682 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16686 type = read_type_die (die, cu);
16687 new_symbol (die, type, cu);
16689 namespace_name (die, &is_anonymous, cu);
16692 const char *previous_prefix = determine_prefix (die, cu);
16694 std::vector<const char *> excludes;
16695 add_using_directive (using_directives (cu->language),
16696 previous_prefix, TYPE_NAME (type), NULL,
16697 NULL, excludes, 0, &objfile->objfile_obstack);
16701 if (die->child != NULL)
16703 struct die_info *child_die = die->child;
16705 while (child_die && child_die->tag)
16707 process_die (child_die, cu);
16708 child_die = sibling_die (child_die);
16713 /* Read a Fortran module as type. This DIE can be only a declaration used for
16714 imported module. Still we need that type as local Fortran "use ... only"
16715 declaration imports depend on the created type in determine_prefix. */
16717 static struct type *
16718 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16720 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16721 const char *module_name;
16724 module_name = dwarf2_name (die, cu);
16726 complaint (_("DW_TAG_module has no name, offset %s"),
16727 sect_offset_str (die->sect_off));
16728 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16730 return set_die_type (die, type, cu);
16733 /* Read a Fortran module. */
16736 read_module (struct die_info *die, struct dwarf2_cu *cu)
16738 struct die_info *child_die = die->child;
16741 type = read_type_die (die, cu);
16742 new_symbol (die, type, cu);
16744 while (child_die && child_die->tag)
16746 process_die (child_die, cu);
16747 child_die = sibling_die (child_die);
16751 /* Return the name of the namespace represented by DIE. Set
16752 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16755 static const char *
16756 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16758 struct die_info *current_die;
16759 const char *name = NULL;
16761 /* Loop through the extensions until we find a name. */
16763 for (current_die = die;
16764 current_die != NULL;
16765 current_die = dwarf2_extension (die, &cu))
16767 /* We don't use dwarf2_name here so that we can detect the absence
16768 of a name -> anonymous namespace. */
16769 name = dwarf2_string_attr (die, DW_AT_name, cu);
16775 /* Is it an anonymous namespace? */
16777 *is_anonymous = (name == NULL);
16779 name = CP_ANONYMOUS_NAMESPACE_STR;
16784 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16785 the user defined type vector. */
16787 static struct type *
16788 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16790 struct gdbarch *gdbarch
16791 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16792 struct comp_unit_head *cu_header = &cu->header;
16794 struct attribute *attr_byte_size;
16795 struct attribute *attr_address_class;
16796 int byte_size, addr_class;
16797 struct type *target_type;
16799 target_type = die_type (die, cu);
16801 /* The die_type call above may have already set the type for this DIE. */
16802 type = get_die_type (die, cu);
16806 type = lookup_pointer_type (target_type);
16808 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16809 if (attr_byte_size)
16810 byte_size = DW_UNSND (attr_byte_size);
16812 byte_size = cu_header->addr_size;
16814 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16815 if (attr_address_class)
16816 addr_class = DW_UNSND (attr_address_class);
16818 addr_class = DW_ADDR_none;
16820 ULONGEST alignment = get_alignment (cu, die);
16822 /* If the pointer size, alignment, or address class is different
16823 than the default, create a type variant marked as such and set
16824 the length accordingly. */
16825 if (TYPE_LENGTH (type) != byte_size
16826 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16827 && alignment != TYPE_RAW_ALIGN (type))
16828 || addr_class != DW_ADDR_none)
16830 if (gdbarch_address_class_type_flags_p (gdbarch))
16834 type_flags = gdbarch_address_class_type_flags
16835 (gdbarch, byte_size, addr_class);
16836 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16838 type = make_type_with_address_space (type, type_flags);
16840 else if (TYPE_LENGTH (type) != byte_size)
16842 complaint (_("invalid pointer size %d"), byte_size);
16844 else if (TYPE_RAW_ALIGN (type) != alignment)
16846 complaint (_("Invalid DW_AT_alignment"
16847 " - DIE at %s [in module %s]"),
16848 sect_offset_str (die->sect_off),
16849 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16853 /* Should we also complain about unhandled address classes? */
16857 TYPE_LENGTH (type) = byte_size;
16858 set_type_align (type, alignment);
16859 return set_die_type (die, type, cu);
16862 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16863 the user defined type vector. */
16865 static struct type *
16866 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16869 struct type *to_type;
16870 struct type *domain;
16872 to_type = die_type (die, cu);
16873 domain = die_containing_type (die, cu);
16875 /* The calls above may have already set the type for this DIE. */
16876 type = get_die_type (die, cu);
16880 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16881 type = lookup_methodptr_type (to_type);
16882 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16884 struct type *new_type
16885 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16887 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16888 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16889 TYPE_VARARGS (to_type));
16890 type = lookup_methodptr_type (new_type);
16893 type = lookup_memberptr_type (to_type, domain);
16895 return set_die_type (die, type, cu);
16898 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16899 the user defined type vector. */
16901 static struct type *
16902 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16903 enum type_code refcode)
16905 struct comp_unit_head *cu_header = &cu->header;
16906 struct type *type, *target_type;
16907 struct attribute *attr;
16909 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16911 target_type = die_type (die, cu);
16913 /* The die_type call above may have already set the type for this DIE. */
16914 type = get_die_type (die, cu);
16918 type = lookup_reference_type (target_type, refcode);
16919 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16922 TYPE_LENGTH (type) = DW_UNSND (attr);
16926 TYPE_LENGTH (type) = cu_header->addr_size;
16928 maybe_set_alignment (cu, die, type);
16929 return set_die_type (die, type, cu);
16932 /* Add the given cv-qualifiers to the element type of the array. GCC
16933 outputs DWARF type qualifiers that apply to an array, not the
16934 element type. But GDB relies on the array element type to carry
16935 the cv-qualifiers. This mimics section 6.7.3 of the C99
16938 static struct type *
16939 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16940 struct type *base_type, int cnst, int voltl)
16942 struct type *el_type, *inner_array;
16944 base_type = copy_type (base_type);
16945 inner_array = base_type;
16947 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16949 TYPE_TARGET_TYPE (inner_array) =
16950 copy_type (TYPE_TARGET_TYPE (inner_array));
16951 inner_array = TYPE_TARGET_TYPE (inner_array);
16954 el_type = TYPE_TARGET_TYPE (inner_array);
16955 cnst |= TYPE_CONST (el_type);
16956 voltl |= TYPE_VOLATILE (el_type);
16957 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16959 return set_die_type (die, base_type, cu);
16962 static struct type *
16963 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16965 struct type *base_type, *cv_type;
16967 base_type = die_type (die, cu);
16969 /* The die_type call above may have already set the type for this DIE. */
16970 cv_type = get_die_type (die, cu);
16974 /* In case the const qualifier is applied to an array type, the element type
16975 is so qualified, not the array type (section 6.7.3 of C99). */
16976 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16977 return add_array_cv_type (die, cu, base_type, 1, 0);
16979 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16980 return set_die_type (die, cv_type, cu);
16983 static struct type *
16984 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16986 struct type *base_type, *cv_type;
16988 base_type = die_type (die, cu);
16990 /* The die_type call above may have already set the type for this DIE. */
16991 cv_type = get_die_type (die, cu);
16995 /* In case the volatile qualifier is applied to an array type, the
16996 element type is so qualified, not the array type (section 6.7.3
16998 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16999 return add_array_cv_type (die, cu, base_type, 0, 1);
17001 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17002 return set_die_type (die, cv_type, cu);
17005 /* Handle DW_TAG_restrict_type. */
17007 static struct type *
17008 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17010 struct type *base_type, *cv_type;
17012 base_type = die_type (die, cu);
17014 /* The die_type call above may have already set the type for this DIE. */
17015 cv_type = get_die_type (die, cu);
17019 cv_type = make_restrict_type (base_type);
17020 return set_die_type (die, cv_type, cu);
17023 /* Handle DW_TAG_atomic_type. */
17025 static struct type *
17026 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17028 struct type *base_type, *cv_type;
17030 base_type = die_type (die, cu);
17032 /* The die_type call above may have already set the type for this DIE. */
17033 cv_type = get_die_type (die, cu);
17037 cv_type = make_atomic_type (base_type);
17038 return set_die_type (die, cv_type, cu);
17041 /* Extract all information from a DW_TAG_string_type DIE and add to
17042 the user defined type vector. It isn't really a user defined type,
17043 but it behaves like one, with other DIE's using an AT_user_def_type
17044 attribute to reference it. */
17046 static struct type *
17047 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17049 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17050 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17051 struct type *type, *range_type, *index_type, *char_type;
17052 struct attribute *attr;
17053 unsigned int length;
17055 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17058 length = DW_UNSND (attr);
17062 /* Check for the DW_AT_byte_size attribute. */
17063 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17066 length = DW_UNSND (attr);
17074 index_type = objfile_type (objfile)->builtin_int;
17075 range_type = create_static_range_type (NULL, index_type, 1, length);
17076 char_type = language_string_char_type (cu->language_defn, gdbarch);
17077 type = create_string_type (NULL, char_type, range_type);
17079 return set_die_type (die, type, cu);
17082 /* Assuming that DIE corresponds to a function, returns nonzero
17083 if the function is prototyped. */
17086 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17088 struct attribute *attr;
17090 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17091 if (attr && (DW_UNSND (attr) != 0))
17094 /* The DWARF standard implies that the DW_AT_prototyped attribute
17095 is only meaninful for C, but the concept also extends to other
17096 languages that allow unprototyped functions (Eg: Objective C).
17097 For all other languages, assume that functions are always
17099 if (cu->language != language_c
17100 && cu->language != language_objc
17101 && cu->language != language_opencl)
17104 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17105 prototyped and unprototyped functions; default to prototyped,
17106 since that is more common in modern code (and RealView warns
17107 about unprototyped functions). */
17108 if (producer_is_realview (cu->producer))
17114 /* Handle DIES due to C code like:
17118 int (*funcp)(int a, long l);
17122 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17124 static struct type *
17125 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17127 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17128 struct type *type; /* Type that this function returns. */
17129 struct type *ftype; /* Function that returns above type. */
17130 struct attribute *attr;
17132 type = die_type (die, cu);
17134 /* The die_type call above may have already set the type for this DIE. */
17135 ftype = get_die_type (die, cu);
17139 ftype = lookup_function_type (type);
17141 if (prototyped_function_p (die, cu))
17142 TYPE_PROTOTYPED (ftype) = 1;
17144 /* Store the calling convention in the type if it's available in
17145 the subroutine die. Otherwise set the calling convention to
17146 the default value DW_CC_normal. */
17147 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17149 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17150 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17151 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17153 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17155 /* Record whether the function returns normally to its caller or not
17156 if the DWARF producer set that information. */
17157 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17158 if (attr && (DW_UNSND (attr) != 0))
17159 TYPE_NO_RETURN (ftype) = 1;
17161 /* We need to add the subroutine type to the die immediately so
17162 we don't infinitely recurse when dealing with parameters
17163 declared as the same subroutine type. */
17164 set_die_type (die, ftype, cu);
17166 if (die->child != NULL)
17168 struct type *void_type = objfile_type (objfile)->builtin_void;
17169 struct die_info *child_die;
17170 int nparams, iparams;
17172 /* Count the number of parameters.
17173 FIXME: GDB currently ignores vararg functions, but knows about
17174 vararg member functions. */
17176 child_die = die->child;
17177 while (child_die && child_die->tag)
17179 if (child_die->tag == DW_TAG_formal_parameter)
17181 else if (child_die->tag == DW_TAG_unspecified_parameters)
17182 TYPE_VARARGS (ftype) = 1;
17183 child_die = sibling_die (child_die);
17186 /* Allocate storage for parameters and fill them in. */
17187 TYPE_NFIELDS (ftype) = nparams;
17188 TYPE_FIELDS (ftype) = (struct field *)
17189 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17191 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17192 even if we error out during the parameters reading below. */
17193 for (iparams = 0; iparams < nparams; iparams++)
17194 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17197 child_die = die->child;
17198 while (child_die && child_die->tag)
17200 if (child_die->tag == DW_TAG_formal_parameter)
17202 struct type *arg_type;
17204 /* DWARF version 2 has no clean way to discern C++
17205 static and non-static member functions. G++ helps
17206 GDB by marking the first parameter for non-static
17207 member functions (which is the this pointer) as
17208 artificial. We pass this information to
17209 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17211 DWARF version 3 added DW_AT_object_pointer, which GCC
17212 4.5 does not yet generate. */
17213 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17215 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17217 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17218 arg_type = die_type (child_die, cu);
17220 /* RealView does not mark THIS as const, which the testsuite
17221 expects. GCC marks THIS as const in method definitions,
17222 but not in the class specifications (GCC PR 43053). */
17223 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17224 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17227 struct dwarf2_cu *arg_cu = cu;
17228 const char *name = dwarf2_name (child_die, cu);
17230 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17233 /* If the compiler emits this, use it. */
17234 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17237 else if (name && strcmp (name, "this") == 0)
17238 /* Function definitions will have the argument names. */
17240 else if (name == NULL && iparams == 0)
17241 /* Declarations may not have the names, so like
17242 elsewhere in GDB, assume an artificial first
17243 argument is "this". */
17247 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17251 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17254 child_die = sibling_die (child_die);
17261 static struct type *
17262 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17264 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17265 const char *name = NULL;
17266 struct type *this_type, *target_type;
17268 name = dwarf2_full_name (NULL, die, cu);
17269 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17270 TYPE_TARGET_STUB (this_type) = 1;
17271 set_die_type (die, this_type, cu);
17272 target_type = die_type (die, cu);
17273 if (target_type != this_type)
17274 TYPE_TARGET_TYPE (this_type) = target_type;
17277 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17278 spec and cause infinite loops in GDB. */
17279 complaint (_("Self-referential DW_TAG_typedef "
17280 "- DIE at %s [in module %s]"),
17281 sect_offset_str (die->sect_off), objfile_name (objfile));
17282 TYPE_TARGET_TYPE (this_type) = NULL;
17287 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17288 (which may be different from NAME) to the architecture back-end to allow
17289 it to guess the correct format if necessary. */
17291 static struct type *
17292 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17293 const char *name_hint)
17295 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17296 const struct floatformat **format;
17299 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17301 type = init_float_type (objfile, bits, name, format);
17303 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17308 /* Find a representation of a given base type and install
17309 it in the TYPE field of the die. */
17311 static struct type *
17312 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17314 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17316 struct attribute *attr;
17317 int encoding = 0, bits = 0;
17320 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17323 encoding = DW_UNSND (attr);
17325 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17328 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17330 name = dwarf2_name (die, cu);
17333 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17338 case DW_ATE_address:
17339 /* Turn DW_ATE_address into a void * pointer. */
17340 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17341 type = init_pointer_type (objfile, bits, name, type);
17343 case DW_ATE_boolean:
17344 type = init_boolean_type (objfile, bits, 1, name);
17346 case DW_ATE_complex_float:
17347 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
17348 type = init_complex_type (objfile, name, type);
17350 case DW_ATE_decimal_float:
17351 type = init_decfloat_type (objfile, bits, name);
17354 type = dwarf2_init_float_type (objfile, bits, name, name);
17356 case DW_ATE_signed:
17357 type = init_integer_type (objfile, bits, 0, name);
17359 case DW_ATE_unsigned:
17360 if (cu->language == language_fortran
17362 && startswith (name, "character("))
17363 type = init_character_type (objfile, bits, 1, name);
17365 type = init_integer_type (objfile, bits, 1, name);
17367 case DW_ATE_signed_char:
17368 if (cu->language == language_ada || cu->language == language_m2
17369 || cu->language == language_pascal
17370 || cu->language == language_fortran)
17371 type = init_character_type (objfile, bits, 0, name);
17373 type = init_integer_type (objfile, bits, 0, name);
17375 case DW_ATE_unsigned_char:
17376 if (cu->language == language_ada || cu->language == language_m2
17377 || cu->language == language_pascal
17378 || cu->language == language_fortran
17379 || cu->language == language_rust)
17380 type = init_character_type (objfile, bits, 1, name);
17382 type = init_integer_type (objfile, bits, 1, name);
17386 gdbarch *arch = get_objfile_arch (objfile);
17389 type = builtin_type (arch)->builtin_char16;
17390 else if (bits == 32)
17391 type = builtin_type (arch)->builtin_char32;
17394 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17396 type = init_integer_type (objfile, bits, 1, name);
17398 return set_die_type (die, type, cu);
17403 complaint (_("unsupported DW_AT_encoding: '%s'"),
17404 dwarf_type_encoding_name (encoding));
17405 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17409 if (name && strcmp (name, "char") == 0)
17410 TYPE_NOSIGN (type) = 1;
17412 maybe_set_alignment (cu, die, type);
17414 return set_die_type (die, type, cu);
17417 /* Parse dwarf attribute if it's a block, reference or constant and put the
17418 resulting value of the attribute into struct bound_prop.
17419 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17422 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17423 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17425 struct dwarf2_property_baton *baton;
17426 struct obstack *obstack
17427 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17429 if (attr == NULL || prop == NULL)
17432 if (attr_form_is_block (attr))
17434 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17435 baton->referenced_type = NULL;
17436 baton->locexpr.per_cu = cu->per_cu;
17437 baton->locexpr.size = DW_BLOCK (attr)->size;
17438 baton->locexpr.data = DW_BLOCK (attr)->data;
17439 prop->data.baton = baton;
17440 prop->kind = PROP_LOCEXPR;
17441 gdb_assert (prop->data.baton != NULL);
17443 else if (attr_form_is_ref (attr))
17445 struct dwarf2_cu *target_cu = cu;
17446 struct die_info *target_die;
17447 struct attribute *target_attr;
17449 target_die = follow_die_ref (die, attr, &target_cu);
17450 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17451 if (target_attr == NULL)
17452 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17454 if (target_attr == NULL)
17457 switch (target_attr->name)
17459 case DW_AT_location:
17460 if (attr_form_is_section_offset (target_attr))
17462 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17463 baton->referenced_type = die_type (target_die, target_cu);
17464 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17465 prop->data.baton = baton;
17466 prop->kind = PROP_LOCLIST;
17467 gdb_assert (prop->data.baton != NULL);
17469 else if (attr_form_is_block (target_attr))
17471 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17472 baton->referenced_type = die_type (target_die, target_cu);
17473 baton->locexpr.per_cu = cu->per_cu;
17474 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17475 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17476 prop->data.baton = baton;
17477 prop->kind = PROP_LOCEXPR;
17478 gdb_assert (prop->data.baton != NULL);
17482 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17483 "dynamic property");
17487 case DW_AT_data_member_location:
17491 if (!handle_data_member_location (target_die, target_cu,
17495 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17496 baton->referenced_type = read_type_die (target_die->parent,
17498 baton->offset_info.offset = offset;
17499 baton->offset_info.type = die_type (target_die, target_cu);
17500 prop->data.baton = baton;
17501 prop->kind = PROP_ADDR_OFFSET;
17506 else if (attr_form_is_constant (attr))
17508 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17509 prop->kind = PROP_CONST;
17513 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17514 dwarf2_name (die, cu));
17521 /* Read the given DW_AT_subrange DIE. */
17523 static struct type *
17524 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17526 struct type *base_type, *orig_base_type;
17527 struct type *range_type;
17528 struct attribute *attr;
17529 struct dynamic_prop low, high;
17530 int low_default_is_valid;
17531 int high_bound_is_count = 0;
17533 LONGEST negative_mask;
17535 orig_base_type = die_type (die, cu);
17536 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17537 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17538 creating the range type, but we use the result of check_typedef
17539 when examining properties of the type. */
17540 base_type = check_typedef (orig_base_type);
17542 /* The die_type call above may have already set the type for this DIE. */
17543 range_type = get_die_type (die, cu);
17547 low.kind = PROP_CONST;
17548 high.kind = PROP_CONST;
17549 high.data.const_val = 0;
17551 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17552 omitting DW_AT_lower_bound. */
17553 switch (cu->language)
17556 case language_cplus:
17557 low.data.const_val = 0;
17558 low_default_is_valid = 1;
17560 case language_fortran:
17561 low.data.const_val = 1;
17562 low_default_is_valid = 1;
17565 case language_objc:
17566 case language_rust:
17567 low.data.const_val = 0;
17568 low_default_is_valid = (cu->header.version >= 4);
17572 case language_pascal:
17573 low.data.const_val = 1;
17574 low_default_is_valid = (cu->header.version >= 4);
17577 low.data.const_val = 0;
17578 low_default_is_valid = 0;
17582 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17584 attr_to_dynamic_prop (attr, die, cu, &low);
17585 else if (!low_default_is_valid)
17586 complaint (_("Missing DW_AT_lower_bound "
17587 "- DIE at %s [in module %s]"),
17588 sect_offset_str (die->sect_off),
17589 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17591 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
17592 if (!attr_to_dynamic_prop (attr, die, cu, &high))
17594 attr = dwarf2_attr (die, DW_AT_count, cu);
17595 if (attr_to_dynamic_prop (attr, die, cu, &high))
17597 /* If bounds are constant do the final calculation here. */
17598 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17599 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17601 high_bound_is_count = 1;
17605 /* Dwarf-2 specifications explicitly allows to create subrange types
17606 without specifying a base type.
17607 In that case, the base type must be set to the type of
17608 the lower bound, upper bound or count, in that order, if any of these
17609 three attributes references an object that has a type.
17610 If no base type is found, the Dwarf-2 specifications say that
17611 a signed integer type of size equal to the size of an address should
17613 For the following C code: `extern char gdb_int [];'
17614 GCC produces an empty range DIE.
17615 FIXME: muller/2010-05-28: Possible references to object for low bound,
17616 high bound or count are not yet handled by this code. */
17617 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17619 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17620 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17621 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17622 struct type *int_type = objfile_type (objfile)->builtin_int;
17624 /* Test "int", "long int", and "long long int" objfile types,
17625 and select the first one having a size above or equal to the
17626 architecture address size. */
17627 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17628 base_type = int_type;
17631 int_type = objfile_type (objfile)->builtin_long;
17632 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17633 base_type = int_type;
17636 int_type = objfile_type (objfile)->builtin_long_long;
17637 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17638 base_type = int_type;
17643 /* Normally, the DWARF producers are expected to use a signed
17644 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17645 But this is unfortunately not always the case, as witnessed
17646 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17647 is used instead. To work around that ambiguity, we treat
17648 the bounds as signed, and thus sign-extend their values, when
17649 the base type is signed. */
17651 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17652 if (low.kind == PROP_CONST
17653 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17654 low.data.const_val |= negative_mask;
17655 if (high.kind == PROP_CONST
17656 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17657 high.data.const_val |= negative_mask;
17659 range_type = create_range_type (NULL, orig_base_type, &low, &high);
17661 if (high_bound_is_count)
17662 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17664 /* Ada expects an empty array on no boundary attributes. */
17665 if (attr == NULL && cu->language != language_ada)
17666 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17668 name = dwarf2_name (die, cu);
17670 TYPE_NAME (range_type) = name;
17672 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17674 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17676 maybe_set_alignment (cu, die, range_type);
17678 set_die_type (die, range_type, cu);
17680 /* set_die_type should be already done. */
17681 set_descriptive_type (range_type, die, cu);
17686 static struct type *
17687 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17691 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17693 TYPE_NAME (type) = dwarf2_name (die, cu);
17695 /* In Ada, an unspecified type is typically used when the description
17696 of the type is defered to a different unit. When encountering
17697 such a type, we treat it as a stub, and try to resolve it later on,
17699 if (cu->language == language_ada)
17700 TYPE_STUB (type) = 1;
17702 return set_die_type (die, type, cu);
17705 /* Read a single die and all its descendents. Set the die's sibling
17706 field to NULL; set other fields in the die correctly, and set all
17707 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17708 location of the info_ptr after reading all of those dies. PARENT
17709 is the parent of the die in question. */
17711 static struct die_info *
17712 read_die_and_children (const struct die_reader_specs *reader,
17713 const gdb_byte *info_ptr,
17714 const gdb_byte **new_info_ptr,
17715 struct die_info *parent)
17717 struct die_info *die;
17718 const gdb_byte *cur_ptr;
17721 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
17724 *new_info_ptr = cur_ptr;
17727 store_in_ref_table (die, reader->cu);
17730 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17734 *new_info_ptr = cur_ptr;
17737 die->sibling = NULL;
17738 die->parent = parent;
17742 /* Read a die, all of its descendents, and all of its siblings; set
17743 all of the fields of all of the dies correctly. Arguments are as
17744 in read_die_and_children. */
17746 static struct die_info *
17747 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17748 const gdb_byte *info_ptr,
17749 const gdb_byte **new_info_ptr,
17750 struct die_info *parent)
17752 struct die_info *first_die, *last_sibling;
17753 const gdb_byte *cur_ptr;
17755 cur_ptr = info_ptr;
17756 first_die = last_sibling = NULL;
17760 struct die_info *die
17761 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17765 *new_info_ptr = cur_ptr;
17772 last_sibling->sibling = die;
17774 last_sibling = die;
17778 /* Read a die, all of its descendents, and all of its siblings; set
17779 all of the fields of all of the dies correctly. Arguments are as
17780 in read_die_and_children.
17781 This the main entry point for reading a DIE and all its children. */
17783 static struct die_info *
17784 read_die_and_siblings (const struct die_reader_specs *reader,
17785 const gdb_byte *info_ptr,
17786 const gdb_byte **new_info_ptr,
17787 struct die_info *parent)
17789 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17790 new_info_ptr, parent);
17792 if (dwarf_die_debug)
17794 fprintf_unfiltered (gdb_stdlog,
17795 "Read die from %s@0x%x of %s:\n",
17796 get_section_name (reader->die_section),
17797 (unsigned) (info_ptr - reader->die_section->buffer),
17798 bfd_get_filename (reader->abfd));
17799 dump_die (die, dwarf_die_debug);
17805 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17807 The caller is responsible for filling in the extra attributes
17808 and updating (*DIEP)->num_attrs.
17809 Set DIEP to point to a newly allocated die with its information,
17810 except for its child, sibling, and parent fields.
17811 Set HAS_CHILDREN to tell whether the die has children or not. */
17813 static const gdb_byte *
17814 read_full_die_1 (const struct die_reader_specs *reader,
17815 struct die_info **diep, const gdb_byte *info_ptr,
17816 int *has_children, int num_extra_attrs)
17818 unsigned int abbrev_number, bytes_read, i;
17819 struct abbrev_info *abbrev;
17820 struct die_info *die;
17821 struct dwarf2_cu *cu = reader->cu;
17822 bfd *abfd = reader->abfd;
17824 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17825 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17826 info_ptr += bytes_read;
17827 if (!abbrev_number)
17834 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17836 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17838 bfd_get_filename (abfd));
17840 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17841 die->sect_off = sect_off;
17842 die->tag = abbrev->tag;
17843 die->abbrev = abbrev_number;
17845 /* Make the result usable.
17846 The caller needs to update num_attrs after adding the extra
17848 die->num_attrs = abbrev->num_attrs;
17850 for (i = 0; i < abbrev->num_attrs; ++i)
17851 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17855 *has_children = abbrev->has_children;
17859 /* Read a die and all its attributes.
17860 Set DIEP to point to a newly allocated die with its information,
17861 except for its child, sibling, and parent fields.
17862 Set HAS_CHILDREN to tell whether the die has children or not. */
17864 static const gdb_byte *
17865 read_full_die (const struct die_reader_specs *reader,
17866 struct die_info **diep, const gdb_byte *info_ptr,
17869 const gdb_byte *result;
17871 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
17873 if (dwarf_die_debug)
17875 fprintf_unfiltered (gdb_stdlog,
17876 "Read die from %s@0x%x of %s:\n",
17877 get_section_name (reader->die_section),
17878 (unsigned) (info_ptr - reader->die_section->buffer),
17879 bfd_get_filename (reader->abfd));
17880 dump_die (*diep, dwarf_die_debug);
17886 /* Abbreviation tables.
17888 In DWARF version 2, the description of the debugging information is
17889 stored in a separate .debug_abbrev section. Before we read any
17890 dies from a section we read in all abbreviations and install them
17891 in a hash table. */
17893 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
17895 struct abbrev_info *
17896 abbrev_table::alloc_abbrev ()
17898 struct abbrev_info *abbrev;
17900 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
17901 memset (abbrev, 0, sizeof (struct abbrev_info));
17906 /* Add an abbreviation to the table. */
17909 abbrev_table::add_abbrev (unsigned int abbrev_number,
17910 struct abbrev_info *abbrev)
17912 unsigned int hash_number;
17914 hash_number = abbrev_number % ABBREV_HASH_SIZE;
17915 abbrev->next = m_abbrevs[hash_number];
17916 m_abbrevs[hash_number] = abbrev;
17919 /* Look up an abbrev in the table.
17920 Returns NULL if the abbrev is not found. */
17922 struct abbrev_info *
17923 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
17925 unsigned int hash_number;
17926 struct abbrev_info *abbrev;
17928 hash_number = abbrev_number % ABBREV_HASH_SIZE;
17929 abbrev = m_abbrevs[hash_number];
17933 if (abbrev->number == abbrev_number)
17935 abbrev = abbrev->next;
17940 /* Read in an abbrev table. */
17942 static abbrev_table_up
17943 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
17944 struct dwarf2_section_info *section,
17945 sect_offset sect_off)
17947 struct objfile *objfile = dwarf2_per_objfile->objfile;
17948 bfd *abfd = get_section_bfd_owner (section);
17949 const gdb_byte *abbrev_ptr;
17950 struct abbrev_info *cur_abbrev;
17951 unsigned int abbrev_number, bytes_read, abbrev_name;
17952 unsigned int abbrev_form;
17953 struct attr_abbrev *cur_attrs;
17954 unsigned int allocated_attrs;
17956 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
17958 dwarf2_read_section (objfile, section);
17959 abbrev_ptr = section->buffer + to_underlying (sect_off);
17960 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17961 abbrev_ptr += bytes_read;
17963 allocated_attrs = ATTR_ALLOC_CHUNK;
17964 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
17966 /* Loop until we reach an abbrev number of 0. */
17967 while (abbrev_number)
17969 cur_abbrev = abbrev_table->alloc_abbrev ();
17971 /* read in abbrev header */
17972 cur_abbrev->number = abbrev_number;
17974 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17975 abbrev_ptr += bytes_read;
17976 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
17979 /* now read in declarations */
17982 LONGEST implicit_const;
17984 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17985 abbrev_ptr += bytes_read;
17986 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17987 abbrev_ptr += bytes_read;
17988 if (abbrev_form == DW_FORM_implicit_const)
17990 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
17992 abbrev_ptr += bytes_read;
17996 /* Initialize it due to a false compiler warning. */
17997 implicit_const = -1;
18000 if (abbrev_name == 0)
18003 if (cur_abbrev->num_attrs == allocated_attrs)
18005 allocated_attrs += ATTR_ALLOC_CHUNK;
18007 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18010 cur_attrs[cur_abbrev->num_attrs].name
18011 = (enum dwarf_attribute) abbrev_name;
18012 cur_attrs[cur_abbrev->num_attrs].form
18013 = (enum dwarf_form) abbrev_form;
18014 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18015 ++cur_abbrev->num_attrs;
18018 cur_abbrev->attrs =
18019 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18020 cur_abbrev->num_attrs);
18021 memcpy (cur_abbrev->attrs, cur_attrs,
18022 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18024 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18026 /* Get next abbreviation.
18027 Under Irix6 the abbreviations for a compilation unit are not
18028 always properly terminated with an abbrev number of 0.
18029 Exit loop if we encounter an abbreviation which we have
18030 already read (which means we are about to read the abbreviations
18031 for the next compile unit) or if the end of the abbreviation
18032 table is reached. */
18033 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18035 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18036 abbrev_ptr += bytes_read;
18037 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18042 return abbrev_table;
18045 /* Returns nonzero if TAG represents a type that we might generate a partial
18049 is_type_tag_for_partial (int tag)
18054 /* Some types that would be reasonable to generate partial symbols for,
18055 that we don't at present. */
18056 case DW_TAG_array_type:
18057 case DW_TAG_file_type:
18058 case DW_TAG_ptr_to_member_type:
18059 case DW_TAG_set_type:
18060 case DW_TAG_string_type:
18061 case DW_TAG_subroutine_type:
18063 case DW_TAG_base_type:
18064 case DW_TAG_class_type:
18065 case DW_TAG_interface_type:
18066 case DW_TAG_enumeration_type:
18067 case DW_TAG_structure_type:
18068 case DW_TAG_subrange_type:
18069 case DW_TAG_typedef:
18070 case DW_TAG_union_type:
18077 /* Load all DIEs that are interesting for partial symbols into memory. */
18079 static struct partial_die_info *
18080 load_partial_dies (const struct die_reader_specs *reader,
18081 const gdb_byte *info_ptr, int building_psymtab)
18083 struct dwarf2_cu *cu = reader->cu;
18084 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18085 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18086 unsigned int bytes_read;
18087 unsigned int load_all = 0;
18088 int nesting_level = 1;
18093 gdb_assert (cu->per_cu != NULL);
18094 if (cu->per_cu->load_all_dies)
18098 = htab_create_alloc_ex (cu->header.length / 12,
18102 &cu->comp_unit_obstack,
18103 hashtab_obstack_allocate,
18104 dummy_obstack_deallocate);
18108 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18110 /* A NULL abbrev means the end of a series of children. */
18111 if (abbrev == NULL)
18113 if (--nesting_level == 0)
18116 info_ptr += bytes_read;
18117 last_die = parent_die;
18118 parent_die = parent_die->die_parent;
18122 /* Check for template arguments. We never save these; if
18123 they're seen, we just mark the parent, and go on our way. */
18124 if (parent_die != NULL
18125 && cu->language == language_cplus
18126 && (abbrev->tag == DW_TAG_template_type_param
18127 || abbrev->tag == DW_TAG_template_value_param))
18129 parent_die->has_template_arguments = 1;
18133 /* We don't need a partial DIE for the template argument. */
18134 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18139 /* We only recurse into c++ subprograms looking for template arguments.
18140 Skip their other children. */
18142 && cu->language == language_cplus
18143 && parent_die != NULL
18144 && parent_die->tag == DW_TAG_subprogram)
18146 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18150 /* Check whether this DIE is interesting enough to save. Normally
18151 we would not be interested in members here, but there may be
18152 later variables referencing them via DW_AT_specification (for
18153 static members). */
18155 && !is_type_tag_for_partial (abbrev->tag)
18156 && abbrev->tag != DW_TAG_constant
18157 && abbrev->tag != DW_TAG_enumerator
18158 && abbrev->tag != DW_TAG_subprogram
18159 && abbrev->tag != DW_TAG_inlined_subroutine
18160 && abbrev->tag != DW_TAG_lexical_block
18161 && abbrev->tag != DW_TAG_variable
18162 && abbrev->tag != DW_TAG_namespace
18163 && abbrev->tag != DW_TAG_module
18164 && abbrev->tag != DW_TAG_member
18165 && abbrev->tag != DW_TAG_imported_unit
18166 && abbrev->tag != DW_TAG_imported_declaration)
18168 /* Otherwise we skip to the next sibling, if any. */
18169 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18173 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18176 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18178 /* This two-pass algorithm for processing partial symbols has a
18179 high cost in cache pressure. Thus, handle some simple cases
18180 here which cover the majority of C partial symbols. DIEs
18181 which neither have specification tags in them, nor could have
18182 specification tags elsewhere pointing at them, can simply be
18183 processed and discarded.
18185 This segment is also optional; scan_partial_symbols and
18186 add_partial_symbol will handle these DIEs if we chain
18187 them in normally. When compilers which do not emit large
18188 quantities of duplicate debug information are more common,
18189 this code can probably be removed. */
18191 /* Any complete simple types at the top level (pretty much all
18192 of them, for a language without namespaces), can be processed
18194 if (parent_die == NULL
18195 && pdi.has_specification == 0
18196 && pdi.is_declaration == 0
18197 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18198 || pdi.tag == DW_TAG_base_type
18199 || pdi.tag == DW_TAG_subrange_type))
18201 if (building_psymtab && pdi.name != NULL)
18202 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18203 VAR_DOMAIN, LOC_TYPEDEF,
18204 &objfile->static_psymbols,
18205 0, cu->language, objfile);
18206 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18210 /* The exception for DW_TAG_typedef with has_children above is
18211 a workaround of GCC PR debug/47510. In the case of this complaint
18212 type_name_or_error will error on such types later.
18214 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18215 it could not find the child DIEs referenced later, this is checked
18216 above. In correct DWARF DW_TAG_typedef should have no children. */
18218 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18219 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18220 "- DIE at %s [in module %s]"),
18221 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18223 /* If we're at the second level, and we're an enumerator, and
18224 our parent has no specification (meaning possibly lives in a
18225 namespace elsewhere), then we can add the partial symbol now
18226 instead of queueing it. */
18227 if (pdi.tag == DW_TAG_enumerator
18228 && parent_die != NULL
18229 && parent_die->die_parent == NULL
18230 && parent_die->tag == DW_TAG_enumeration_type
18231 && parent_die->has_specification == 0)
18233 if (pdi.name == NULL)
18234 complaint (_("malformed enumerator DIE ignored"));
18235 else if (building_psymtab)
18236 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18237 VAR_DOMAIN, LOC_CONST,
18238 cu->language == language_cplus
18239 ? &objfile->global_psymbols
18240 : &objfile->static_psymbols,
18241 0, cu->language, objfile);
18243 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18247 struct partial_die_info *part_die
18248 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18250 /* We'll save this DIE so link it in. */
18251 part_die->die_parent = parent_die;
18252 part_die->die_sibling = NULL;
18253 part_die->die_child = NULL;
18255 if (last_die && last_die == parent_die)
18256 last_die->die_child = part_die;
18258 last_die->die_sibling = part_die;
18260 last_die = part_die;
18262 if (first_die == NULL)
18263 first_die = part_die;
18265 /* Maybe add the DIE to the hash table. Not all DIEs that we
18266 find interesting need to be in the hash table, because we
18267 also have the parent/sibling/child chains; only those that we
18268 might refer to by offset later during partial symbol reading.
18270 For now this means things that might have be the target of a
18271 DW_AT_specification, DW_AT_abstract_origin, or
18272 DW_AT_extension. DW_AT_extension will refer only to
18273 namespaces; DW_AT_abstract_origin refers to functions (and
18274 many things under the function DIE, but we do not recurse
18275 into function DIEs during partial symbol reading) and
18276 possibly variables as well; DW_AT_specification refers to
18277 declarations. Declarations ought to have the DW_AT_declaration
18278 flag. It happens that GCC forgets to put it in sometimes, but
18279 only for functions, not for types.
18281 Adding more things than necessary to the hash table is harmless
18282 except for the performance cost. Adding too few will result in
18283 wasted time in find_partial_die, when we reread the compilation
18284 unit with load_all_dies set. */
18287 || abbrev->tag == DW_TAG_constant
18288 || abbrev->tag == DW_TAG_subprogram
18289 || abbrev->tag == DW_TAG_variable
18290 || abbrev->tag == DW_TAG_namespace
18291 || part_die->is_declaration)
18295 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18296 to_underlying (part_die->sect_off),
18301 /* For some DIEs we want to follow their children (if any). For C
18302 we have no reason to follow the children of structures; for other
18303 languages we have to, so that we can get at method physnames
18304 to infer fully qualified class names, for DW_AT_specification,
18305 and for C++ template arguments. For C++, we also look one level
18306 inside functions to find template arguments (if the name of the
18307 function does not already contain the template arguments).
18309 For Ada, we need to scan the children of subprograms and lexical
18310 blocks as well because Ada allows the definition of nested
18311 entities that could be interesting for the debugger, such as
18312 nested subprograms for instance. */
18313 if (last_die->has_children
18315 || last_die->tag == DW_TAG_namespace
18316 || last_die->tag == DW_TAG_module
18317 || last_die->tag == DW_TAG_enumeration_type
18318 || (cu->language == language_cplus
18319 && last_die->tag == DW_TAG_subprogram
18320 && (last_die->name == NULL
18321 || strchr (last_die->name, '<') == NULL))
18322 || (cu->language != language_c
18323 && (last_die->tag == DW_TAG_class_type
18324 || last_die->tag == DW_TAG_interface_type
18325 || last_die->tag == DW_TAG_structure_type
18326 || last_die->tag == DW_TAG_union_type))
18327 || (cu->language == language_ada
18328 && (last_die->tag == DW_TAG_subprogram
18329 || last_die->tag == DW_TAG_lexical_block))))
18332 parent_die = last_die;
18336 /* Otherwise we skip to the next sibling, if any. */
18337 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18339 /* Back to the top, do it again. */
18343 partial_die_info::partial_die_info (sect_offset sect_off_,
18344 struct abbrev_info *abbrev)
18345 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18349 /* Read a minimal amount of information into the minimal die structure.
18350 INFO_PTR should point just after the initial uleb128 of a DIE. */
18353 partial_die_info::read (const struct die_reader_specs *reader,
18354 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18356 struct dwarf2_cu *cu = reader->cu;
18357 struct dwarf2_per_objfile *dwarf2_per_objfile
18358 = cu->per_cu->dwarf2_per_objfile;
18360 int has_low_pc_attr = 0;
18361 int has_high_pc_attr = 0;
18362 int high_pc_relative = 0;
18364 for (i = 0; i < abbrev.num_attrs; ++i)
18366 struct attribute attr;
18368 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18370 /* Store the data if it is of an attribute we want to keep in a
18371 partial symbol table. */
18377 case DW_TAG_compile_unit:
18378 case DW_TAG_partial_unit:
18379 case DW_TAG_type_unit:
18380 /* Compilation units have a DW_AT_name that is a filename, not
18381 a source language identifier. */
18382 case DW_TAG_enumeration_type:
18383 case DW_TAG_enumerator:
18384 /* These tags always have simple identifiers already; no need
18385 to canonicalize them. */
18386 name = DW_STRING (&attr);
18390 struct objfile *objfile = dwarf2_per_objfile->objfile;
18393 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18394 &objfile->per_bfd->storage_obstack);
18399 case DW_AT_linkage_name:
18400 case DW_AT_MIPS_linkage_name:
18401 /* Note that both forms of linkage name might appear. We
18402 assume they will be the same, and we only store the last
18404 if (cu->language == language_ada)
18405 name = DW_STRING (&attr);
18406 linkage_name = DW_STRING (&attr);
18409 has_low_pc_attr = 1;
18410 lowpc = attr_value_as_address (&attr);
18412 case DW_AT_high_pc:
18413 has_high_pc_attr = 1;
18414 highpc = attr_value_as_address (&attr);
18415 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18416 high_pc_relative = 1;
18418 case DW_AT_location:
18419 /* Support the .debug_loc offsets. */
18420 if (attr_form_is_block (&attr))
18422 d.locdesc = DW_BLOCK (&attr);
18424 else if (attr_form_is_section_offset (&attr))
18426 dwarf2_complex_location_expr_complaint ();
18430 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18431 "partial symbol information");
18434 case DW_AT_external:
18435 is_external = DW_UNSND (&attr);
18437 case DW_AT_declaration:
18438 is_declaration = DW_UNSND (&attr);
18443 case DW_AT_abstract_origin:
18444 case DW_AT_specification:
18445 case DW_AT_extension:
18446 has_specification = 1;
18447 spec_offset = dwarf2_get_ref_die_offset (&attr);
18448 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18449 || cu->per_cu->is_dwz);
18451 case DW_AT_sibling:
18452 /* Ignore absolute siblings, they might point outside of
18453 the current compile unit. */
18454 if (attr.form == DW_FORM_ref_addr)
18455 complaint (_("ignoring absolute DW_AT_sibling"));
18458 const gdb_byte *buffer = reader->buffer;
18459 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18460 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18462 if (sibling_ptr < info_ptr)
18463 complaint (_("DW_AT_sibling points backwards"));
18464 else if (sibling_ptr > reader->buffer_end)
18465 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18467 sibling = sibling_ptr;
18470 case DW_AT_byte_size:
18473 case DW_AT_const_value:
18474 has_const_value = 1;
18476 case DW_AT_calling_convention:
18477 /* DWARF doesn't provide a way to identify a program's source-level
18478 entry point. DW_AT_calling_convention attributes are only meant
18479 to describe functions' calling conventions.
18481 However, because it's a necessary piece of information in
18482 Fortran, and before DWARF 4 DW_CC_program was the only
18483 piece of debugging information whose definition refers to
18484 a 'main program' at all, several compilers marked Fortran
18485 main programs with DW_CC_program --- even when those
18486 functions use the standard calling conventions.
18488 Although DWARF now specifies a way to provide this
18489 information, we support this practice for backward
18491 if (DW_UNSND (&attr) == DW_CC_program
18492 && cu->language == language_fortran)
18493 main_subprogram = 1;
18496 if (DW_UNSND (&attr) == DW_INL_inlined
18497 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18498 may_be_inlined = 1;
18502 if (tag == DW_TAG_imported_unit)
18504 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18505 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18506 || cu->per_cu->is_dwz);
18510 case DW_AT_main_subprogram:
18511 main_subprogram = DW_UNSND (&attr);
18519 if (high_pc_relative)
18522 if (has_low_pc_attr && has_high_pc_attr)
18524 /* When using the GNU linker, .gnu.linkonce. sections are used to
18525 eliminate duplicate copies of functions and vtables and such.
18526 The linker will arbitrarily choose one and discard the others.
18527 The AT_*_pc values for such functions refer to local labels in
18528 these sections. If the section from that file was discarded, the
18529 labels are not in the output, so the relocs get a value of 0.
18530 If this is a discarded function, mark the pc bounds as invalid,
18531 so that GDB will ignore it. */
18532 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18534 struct objfile *objfile = dwarf2_per_objfile->objfile;
18535 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18537 complaint (_("DW_AT_low_pc %s is zero "
18538 "for DIE at %s [in module %s]"),
18539 paddress (gdbarch, lowpc),
18540 sect_offset_str (sect_off),
18541 objfile_name (objfile));
18543 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18544 else if (lowpc >= highpc)
18546 struct objfile *objfile = dwarf2_per_objfile->objfile;
18547 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18549 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18550 "for DIE at %s [in module %s]"),
18551 paddress (gdbarch, lowpc),
18552 paddress (gdbarch, highpc),
18553 sect_offset_str (sect_off),
18554 objfile_name (objfile));
18563 /* Find a cached partial DIE at OFFSET in CU. */
18565 struct partial_die_info *
18566 dwarf2_cu::find_partial_die (sect_offset sect_off)
18568 struct partial_die_info *lookup_die = NULL;
18569 struct partial_die_info part_die (sect_off);
18571 lookup_die = ((struct partial_die_info *)
18572 htab_find_with_hash (partial_dies, &part_die,
18573 to_underlying (sect_off)));
18578 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18579 except in the case of .debug_types DIEs which do not reference
18580 outside their CU (they do however referencing other types via
18581 DW_FORM_ref_sig8). */
18583 static struct partial_die_info *
18584 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18586 struct dwarf2_per_objfile *dwarf2_per_objfile
18587 = cu->per_cu->dwarf2_per_objfile;
18588 struct objfile *objfile = dwarf2_per_objfile->objfile;
18589 struct dwarf2_per_cu_data *per_cu = NULL;
18590 struct partial_die_info *pd = NULL;
18592 if (offset_in_dwz == cu->per_cu->is_dwz
18593 && offset_in_cu_p (&cu->header, sect_off))
18595 pd = cu->find_partial_die (sect_off);
18598 /* We missed recording what we needed.
18599 Load all dies and try again. */
18600 per_cu = cu->per_cu;
18604 /* TUs don't reference other CUs/TUs (except via type signatures). */
18605 if (cu->per_cu->is_debug_types)
18607 error (_("Dwarf Error: Type Unit at offset %s contains"
18608 " external reference to offset %s [in module %s].\n"),
18609 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18610 bfd_get_filename (objfile->obfd));
18612 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18613 dwarf2_per_objfile);
18615 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18616 load_partial_comp_unit (per_cu);
18618 per_cu->cu->last_used = 0;
18619 pd = per_cu->cu->find_partial_die (sect_off);
18622 /* If we didn't find it, and not all dies have been loaded,
18623 load them all and try again. */
18625 if (pd == NULL && per_cu->load_all_dies == 0)
18627 per_cu->load_all_dies = 1;
18629 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18630 THIS_CU->cu may already be in use. So we can't just free it and
18631 replace its DIEs with the ones we read in. Instead, we leave those
18632 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18633 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18635 load_partial_comp_unit (per_cu);
18637 pd = per_cu->cu->find_partial_die (sect_off);
18641 internal_error (__FILE__, __LINE__,
18642 _("could not find partial DIE %s "
18643 "in cache [from module %s]\n"),
18644 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18648 /* See if we can figure out if the class lives in a namespace. We do
18649 this by looking for a member function; its demangled name will
18650 contain namespace info, if there is any. */
18653 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18654 struct dwarf2_cu *cu)
18656 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18657 what template types look like, because the demangler
18658 frequently doesn't give the same name as the debug info. We
18659 could fix this by only using the demangled name to get the
18660 prefix (but see comment in read_structure_type). */
18662 struct partial_die_info *real_pdi;
18663 struct partial_die_info *child_pdi;
18665 /* If this DIE (this DIE's specification, if any) has a parent, then
18666 we should not do this. We'll prepend the parent's fully qualified
18667 name when we create the partial symbol. */
18669 real_pdi = struct_pdi;
18670 while (real_pdi->has_specification)
18671 real_pdi = find_partial_die (real_pdi->spec_offset,
18672 real_pdi->spec_is_dwz, cu);
18674 if (real_pdi->die_parent != NULL)
18677 for (child_pdi = struct_pdi->die_child;
18679 child_pdi = child_pdi->die_sibling)
18681 if (child_pdi->tag == DW_TAG_subprogram
18682 && child_pdi->linkage_name != NULL)
18684 char *actual_class_name
18685 = language_class_name_from_physname (cu->language_defn,
18686 child_pdi->linkage_name);
18687 if (actual_class_name != NULL)
18689 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18692 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18694 strlen (actual_class_name)));
18695 xfree (actual_class_name);
18703 partial_die_info::fixup (struct dwarf2_cu *cu)
18705 /* Once we've fixed up a die, there's no point in doing so again.
18706 This also avoids a memory leak if we were to call
18707 guess_partial_die_structure_name multiple times. */
18711 /* If we found a reference attribute and the DIE has no name, try
18712 to find a name in the referred to DIE. */
18714 if (name == NULL && has_specification)
18716 struct partial_die_info *spec_die;
18718 spec_die = find_partial_die (spec_offset, spec_is_dwz, cu);
18720 spec_die->fixup (cu);
18722 if (spec_die->name)
18724 name = spec_die->name;
18726 /* Copy DW_AT_external attribute if it is set. */
18727 if (spec_die->is_external)
18728 is_external = spec_die->is_external;
18732 /* Set default names for some unnamed DIEs. */
18734 if (name == NULL && tag == DW_TAG_namespace)
18735 name = CP_ANONYMOUS_NAMESPACE_STR;
18737 /* If there is no parent die to provide a namespace, and there are
18738 children, see if we can determine the namespace from their linkage
18740 if (cu->language == language_cplus
18741 && !VEC_empty (dwarf2_section_info_def,
18742 cu->per_cu->dwarf2_per_objfile->types)
18743 && die_parent == NULL
18745 && (tag == DW_TAG_class_type
18746 || tag == DW_TAG_structure_type
18747 || tag == DW_TAG_union_type))
18748 guess_partial_die_structure_name (this, cu);
18750 /* GCC might emit a nameless struct or union that has a linkage
18751 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18753 && (tag == DW_TAG_class_type
18754 || tag == DW_TAG_interface_type
18755 || tag == DW_TAG_structure_type
18756 || tag == DW_TAG_union_type)
18757 && linkage_name != NULL)
18761 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
18766 /* Strip any leading namespaces/classes, keep only the base name.
18767 DW_AT_name for named DIEs does not contain the prefixes. */
18768 base = strrchr (demangled, ':');
18769 if (base && base > demangled && base[-1] == ':')
18774 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18777 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18778 base, strlen (base)));
18786 /* Read an attribute value described by an attribute form. */
18788 static const gdb_byte *
18789 read_attribute_value (const struct die_reader_specs *reader,
18790 struct attribute *attr, unsigned form,
18791 LONGEST implicit_const, const gdb_byte *info_ptr)
18793 struct dwarf2_cu *cu = reader->cu;
18794 struct dwarf2_per_objfile *dwarf2_per_objfile
18795 = cu->per_cu->dwarf2_per_objfile;
18796 struct objfile *objfile = dwarf2_per_objfile->objfile;
18797 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18798 bfd *abfd = reader->abfd;
18799 struct comp_unit_head *cu_header = &cu->header;
18800 unsigned int bytes_read;
18801 struct dwarf_block *blk;
18803 attr->form = (enum dwarf_form) form;
18806 case DW_FORM_ref_addr:
18807 if (cu->header.version == 2)
18808 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18810 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18811 &cu->header, &bytes_read);
18812 info_ptr += bytes_read;
18814 case DW_FORM_GNU_ref_alt:
18815 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18816 info_ptr += bytes_read;
18819 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18820 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18821 info_ptr += bytes_read;
18823 case DW_FORM_block2:
18824 blk = dwarf_alloc_block (cu);
18825 blk->size = read_2_bytes (abfd, info_ptr);
18827 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18828 info_ptr += blk->size;
18829 DW_BLOCK (attr) = blk;
18831 case DW_FORM_block4:
18832 blk = dwarf_alloc_block (cu);
18833 blk->size = read_4_bytes (abfd, info_ptr);
18835 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18836 info_ptr += blk->size;
18837 DW_BLOCK (attr) = blk;
18839 case DW_FORM_data2:
18840 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18843 case DW_FORM_data4:
18844 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18847 case DW_FORM_data8:
18848 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18851 case DW_FORM_data16:
18852 blk = dwarf_alloc_block (cu);
18854 blk->data = read_n_bytes (abfd, info_ptr, 16);
18856 DW_BLOCK (attr) = blk;
18858 case DW_FORM_sec_offset:
18859 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18860 info_ptr += bytes_read;
18862 case DW_FORM_string:
18863 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18864 DW_STRING_IS_CANONICAL (attr) = 0;
18865 info_ptr += bytes_read;
18868 if (!cu->per_cu->is_dwz)
18870 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18871 abfd, info_ptr, cu_header,
18873 DW_STRING_IS_CANONICAL (attr) = 0;
18874 info_ptr += bytes_read;
18878 case DW_FORM_line_strp:
18879 if (!cu->per_cu->is_dwz)
18881 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18883 cu_header, &bytes_read);
18884 DW_STRING_IS_CANONICAL (attr) = 0;
18885 info_ptr += bytes_read;
18889 case DW_FORM_GNU_strp_alt:
18891 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18892 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
18895 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18897 DW_STRING_IS_CANONICAL (attr) = 0;
18898 info_ptr += bytes_read;
18901 case DW_FORM_exprloc:
18902 case DW_FORM_block:
18903 blk = dwarf_alloc_block (cu);
18904 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18905 info_ptr += bytes_read;
18906 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18907 info_ptr += blk->size;
18908 DW_BLOCK (attr) = blk;
18910 case DW_FORM_block1:
18911 blk = dwarf_alloc_block (cu);
18912 blk->size = read_1_byte (abfd, info_ptr);
18914 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18915 info_ptr += blk->size;
18916 DW_BLOCK (attr) = blk;
18918 case DW_FORM_data1:
18919 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18923 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18926 case DW_FORM_flag_present:
18927 DW_UNSND (attr) = 1;
18929 case DW_FORM_sdata:
18930 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18931 info_ptr += bytes_read;
18933 case DW_FORM_udata:
18934 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18935 info_ptr += bytes_read;
18938 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18939 + read_1_byte (abfd, info_ptr));
18943 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18944 + read_2_bytes (abfd, info_ptr));
18948 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18949 + read_4_bytes (abfd, info_ptr));
18953 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18954 + read_8_bytes (abfd, info_ptr));
18957 case DW_FORM_ref_sig8:
18958 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18961 case DW_FORM_ref_udata:
18962 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18963 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18964 info_ptr += bytes_read;
18966 case DW_FORM_indirect:
18967 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18968 info_ptr += bytes_read;
18969 if (form == DW_FORM_implicit_const)
18971 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18972 info_ptr += bytes_read;
18974 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18977 case DW_FORM_implicit_const:
18978 DW_SND (attr) = implicit_const;
18980 case DW_FORM_GNU_addr_index:
18981 if (reader->dwo_file == NULL)
18983 /* For now flag a hard error.
18984 Later we can turn this into a complaint. */
18985 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
18986 dwarf_form_name (form),
18987 bfd_get_filename (abfd));
18989 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
18990 info_ptr += bytes_read;
18992 case DW_FORM_GNU_str_index:
18993 if (reader->dwo_file == NULL)
18995 /* For now flag a hard error.
18996 Later we can turn this into a complaint if warranted. */
18997 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
18998 dwarf_form_name (form),
18999 bfd_get_filename (abfd));
19002 ULONGEST str_index =
19003 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19005 DW_STRING (attr) = read_str_index (reader, str_index);
19006 DW_STRING_IS_CANONICAL (attr) = 0;
19007 info_ptr += bytes_read;
19011 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19012 dwarf_form_name (form),
19013 bfd_get_filename (abfd));
19017 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19018 attr->form = DW_FORM_GNU_ref_alt;
19020 /* We have seen instances where the compiler tried to emit a byte
19021 size attribute of -1 which ended up being encoded as an unsigned
19022 0xffffffff. Although 0xffffffff is technically a valid size value,
19023 an object of this size seems pretty unlikely so we can relatively
19024 safely treat these cases as if the size attribute was invalid and
19025 treat them as zero by default. */
19026 if (attr->name == DW_AT_byte_size
19027 && form == DW_FORM_data4
19028 && DW_UNSND (attr) >= 0xffffffff)
19031 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19032 hex_string (DW_UNSND (attr)));
19033 DW_UNSND (attr) = 0;
19039 /* Read an attribute described by an abbreviated attribute. */
19041 static const gdb_byte *
19042 read_attribute (const struct die_reader_specs *reader,
19043 struct attribute *attr, struct attr_abbrev *abbrev,
19044 const gdb_byte *info_ptr)
19046 attr->name = abbrev->name;
19047 return read_attribute_value (reader, attr, abbrev->form,
19048 abbrev->implicit_const, info_ptr);
19051 /* Read dwarf information from a buffer. */
19053 static unsigned int
19054 read_1_byte (bfd *abfd, const gdb_byte *buf)
19056 return bfd_get_8 (abfd, buf);
19060 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19062 return bfd_get_signed_8 (abfd, buf);
19065 static unsigned int
19066 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19068 return bfd_get_16 (abfd, buf);
19072 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19074 return bfd_get_signed_16 (abfd, buf);
19077 static unsigned int
19078 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19080 return bfd_get_32 (abfd, buf);
19084 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19086 return bfd_get_signed_32 (abfd, buf);
19090 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19092 return bfd_get_64 (abfd, buf);
19096 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19097 unsigned int *bytes_read)
19099 struct comp_unit_head *cu_header = &cu->header;
19100 CORE_ADDR retval = 0;
19102 if (cu_header->signed_addr_p)
19104 switch (cu_header->addr_size)
19107 retval = bfd_get_signed_16 (abfd, buf);
19110 retval = bfd_get_signed_32 (abfd, buf);
19113 retval = bfd_get_signed_64 (abfd, buf);
19116 internal_error (__FILE__, __LINE__,
19117 _("read_address: bad switch, signed [in module %s]"),
19118 bfd_get_filename (abfd));
19123 switch (cu_header->addr_size)
19126 retval = bfd_get_16 (abfd, buf);
19129 retval = bfd_get_32 (abfd, buf);
19132 retval = bfd_get_64 (abfd, buf);
19135 internal_error (__FILE__, __LINE__,
19136 _("read_address: bad switch, "
19137 "unsigned [in module %s]"),
19138 bfd_get_filename (abfd));
19142 *bytes_read = cu_header->addr_size;
19146 /* Read the initial length from a section. The (draft) DWARF 3
19147 specification allows the initial length to take up either 4 bytes
19148 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19149 bytes describe the length and all offsets will be 8 bytes in length
19152 An older, non-standard 64-bit format is also handled by this
19153 function. The older format in question stores the initial length
19154 as an 8-byte quantity without an escape value. Lengths greater
19155 than 2^32 aren't very common which means that the initial 4 bytes
19156 is almost always zero. Since a length value of zero doesn't make
19157 sense for the 32-bit format, this initial zero can be considered to
19158 be an escape value which indicates the presence of the older 64-bit
19159 format. As written, the code can't detect (old format) lengths
19160 greater than 4GB. If it becomes necessary to handle lengths
19161 somewhat larger than 4GB, we could allow other small values (such
19162 as the non-sensical values of 1, 2, and 3) to also be used as
19163 escape values indicating the presence of the old format.
19165 The value returned via bytes_read should be used to increment the
19166 relevant pointer after calling read_initial_length().
19168 [ Note: read_initial_length() and read_offset() are based on the
19169 document entitled "DWARF Debugging Information Format", revision
19170 3, draft 8, dated November 19, 2001. This document was obtained
19173 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19175 This document is only a draft and is subject to change. (So beware.)
19177 Details regarding the older, non-standard 64-bit format were
19178 determined empirically by examining 64-bit ELF files produced by
19179 the SGI toolchain on an IRIX 6.5 machine.
19181 - Kevin, July 16, 2002
19185 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19187 LONGEST length = bfd_get_32 (abfd, buf);
19189 if (length == 0xffffffff)
19191 length = bfd_get_64 (abfd, buf + 4);
19194 else if (length == 0)
19196 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19197 length = bfd_get_64 (abfd, buf);
19208 /* Cover function for read_initial_length.
19209 Returns the length of the object at BUF, and stores the size of the
19210 initial length in *BYTES_READ and stores the size that offsets will be in
19212 If the initial length size is not equivalent to that specified in
19213 CU_HEADER then issue a complaint.
19214 This is useful when reading non-comp-unit headers. */
19217 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19218 const struct comp_unit_head *cu_header,
19219 unsigned int *bytes_read,
19220 unsigned int *offset_size)
19222 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19224 gdb_assert (cu_header->initial_length_size == 4
19225 || cu_header->initial_length_size == 8
19226 || cu_header->initial_length_size == 12);
19228 if (cu_header->initial_length_size != *bytes_read)
19229 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19231 *offset_size = (*bytes_read == 4) ? 4 : 8;
19235 /* Read an offset from the data stream. The size of the offset is
19236 given by cu_header->offset_size. */
19239 read_offset (bfd *abfd, const gdb_byte *buf,
19240 const struct comp_unit_head *cu_header,
19241 unsigned int *bytes_read)
19243 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19245 *bytes_read = cu_header->offset_size;
19249 /* Read an offset from the data stream. */
19252 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19254 LONGEST retval = 0;
19256 switch (offset_size)
19259 retval = bfd_get_32 (abfd, buf);
19262 retval = bfd_get_64 (abfd, buf);
19265 internal_error (__FILE__, __LINE__,
19266 _("read_offset_1: bad switch [in module %s]"),
19267 bfd_get_filename (abfd));
19273 static const gdb_byte *
19274 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19276 /* If the size of a host char is 8 bits, we can return a pointer
19277 to the buffer, otherwise we have to copy the data to a buffer
19278 allocated on the temporary obstack. */
19279 gdb_assert (HOST_CHAR_BIT == 8);
19283 static const char *
19284 read_direct_string (bfd *abfd, const gdb_byte *buf,
19285 unsigned int *bytes_read_ptr)
19287 /* If the size of a host char is 8 bits, we can return a pointer
19288 to the string, otherwise we have to copy the string to a buffer
19289 allocated on the temporary obstack. */
19290 gdb_assert (HOST_CHAR_BIT == 8);
19293 *bytes_read_ptr = 1;
19296 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19297 return (const char *) buf;
19300 /* Return pointer to string at section SECT offset STR_OFFSET with error
19301 reporting strings FORM_NAME and SECT_NAME. */
19303 static const char *
19304 read_indirect_string_at_offset_from (struct objfile *objfile,
19305 bfd *abfd, LONGEST str_offset,
19306 struct dwarf2_section_info *sect,
19307 const char *form_name,
19308 const char *sect_name)
19310 dwarf2_read_section (objfile, sect);
19311 if (sect->buffer == NULL)
19312 error (_("%s used without %s section [in module %s]"),
19313 form_name, sect_name, bfd_get_filename (abfd));
19314 if (str_offset >= sect->size)
19315 error (_("%s pointing outside of %s section [in module %s]"),
19316 form_name, sect_name, bfd_get_filename (abfd));
19317 gdb_assert (HOST_CHAR_BIT == 8);
19318 if (sect->buffer[str_offset] == '\0')
19320 return (const char *) (sect->buffer + str_offset);
19323 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19325 static const char *
19326 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19327 bfd *abfd, LONGEST str_offset)
19329 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19331 &dwarf2_per_objfile->str,
19332 "DW_FORM_strp", ".debug_str");
19335 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19337 static const char *
19338 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19339 bfd *abfd, LONGEST str_offset)
19341 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19343 &dwarf2_per_objfile->line_str,
19344 "DW_FORM_line_strp",
19345 ".debug_line_str");
19348 /* Read a string at offset STR_OFFSET in the .debug_str section from
19349 the .dwz file DWZ. Throw an error if the offset is too large. If
19350 the string consists of a single NUL byte, return NULL; otherwise
19351 return a pointer to the string. */
19353 static const char *
19354 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19355 LONGEST str_offset)
19357 dwarf2_read_section (objfile, &dwz->str);
19359 if (dwz->str.buffer == NULL)
19360 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19361 "section [in module %s]"),
19362 bfd_get_filename (dwz->dwz_bfd));
19363 if (str_offset >= dwz->str.size)
19364 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19365 ".debug_str section [in module %s]"),
19366 bfd_get_filename (dwz->dwz_bfd));
19367 gdb_assert (HOST_CHAR_BIT == 8);
19368 if (dwz->str.buffer[str_offset] == '\0')
19370 return (const char *) (dwz->str.buffer + str_offset);
19373 /* Return pointer to string at .debug_str offset as read from BUF.
19374 BUF is assumed to be in a compilation unit described by CU_HEADER.
19375 Return *BYTES_READ_PTR count of bytes read from BUF. */
19377 static const char *
19378 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19379 const gdb_byte *buf,
19380 const struct comp_unit_head *cu_header,
19381 unsigned int *bytes_read_ptr)
19383 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19385 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19388 /* Return pointer to string at .debug_line_str offset as read from BUF.
19389 BUF is assumed to be in a compilation unit described by CU_HEADER.
19390 Return *BYTES_READ_PTR count of bytes read from BUF. */
19392 static const char *
19393 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19394 bfd *abfd, const gdb_byte *buf,
19395 const struct comp_unit_head *cu_header,
19396 unsigned int *bytes_read_ptr)
19398 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19400 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19405 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19406 unsigned int *bytes_read_ptr)
19409 unsigned int num_read;
19411 unsigned char byte;
19418 byte = bfd_get_8 (abfd, buf);
19421 result |= ((ULONGEST) (byte & 127) << shift);
19422 if ((byte & 128) == 0)
19428 *bytes_read_ptr = num_read;
19433 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19434 unsigned int *bytes_read_ptr)
19437 int shift, num_read;
19438 unsigned char byte;
19445 byte = bfd_get_8 (abfd, buf);
19448 result |= ((LONGEST) (byte & 127) << shift);
19450 if ((byte & 128) == 0)
19455 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19456 result |= -(((LONGEST) 1) << shift);
19457 *bytes_read_ptr = num_read;
19461 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19462 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19463 ADDR_SIZE is the size of addresses from the CU header. */
19466 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19467 unsigned int addr_index, ULONGEST addr_base, int addr_size)
19469 struct objfile *objfile = dwarf2_per_objfile->objfile;
19470 bfd *abfd = objfile->obfd;
19471 const gdb_byte *info_ptr;
19473 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19474 if (dwarf2_per_objfile->addr.buffer == NULL)
19475 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19476 objfile_name (objfile));
19477 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19478 error (_("DW_FORM_addr_index pointing outside of "
19479 ".debug_addr section [in module %s]"),
19480 objfile_name (objfile));
19481 info_ptr = (dwarf2_per_objfile->addr.buffer
19482 + addr_base + addr_index * addr_size);
19483 if (addr_size == 4)
19484 return bfd_get_32 (abfd, info_ptr);
19486 return bfd_get_64 (abfd, info_ptr);
19489 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19492 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19494 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19495 cu->addr_base, cu->header.addr_size);
19498 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19501 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19502 unsigned int *bytes_read)
19504 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19505 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19507 return read_addr_index (cu, addr_index);
19510 /* Data structure to pass results from dwarf2_read_addr_index_reader
19511 back to dwarf2_read_addr_index. */
19513 struct dwarf2_read_addr_index_data
19515 ULONGEST addr_base;
19519 /* die_reader_func for dwarf2_read_addr_index. */
19522 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19523 const gdb_byte *info_ptr,
19524 struct die_info *comp_unit_die,
19528 struct dwarf2_cu *cu = reader->cu;
19529 struct dwarf2_read_addr_index_data *aidata =
19530 (struct dwarf2_read_addr_index_data *) data;
19532 aidata->addr_base = cu->addr_base;
19533 aidata->addr_size = cu->header.addr_size;
19536 /* Given an index in .debug_addr, fetch the value.
19537 NOTE: This can be called during dwarf expression evaluation,
19538 long after the debug information has been read, and thus per_cu->cu
19539 may no longer exist. */
19542 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19543 unsigned int addr_index)
19545 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19546 struct dwarf2_cu *cu = per_cu->cu;
19547 ULONGEST addr_base;
19550 /* We need addr_base and addr_size.
19551 If we don't have PER_CU->cu, we have to get it.
19552 Nasty, but the alternative is storing the needed info in PER_CU,
19553 which at this point doesn't seem justified: it's not clear how frequently
19554 it would get used and it would increase the size of every PER_CU.
19555 Entry points like dwarf2_per_cu_addr_size do a similar thing
19556 so we're not in uncharted territory here.
19557 Alas we need to be a bit more complicated as addr_base is contained
19560 We don't need to read the entire CU(/TU).
19561 We just need the header and top level die.
19563 IWBN to use the aging mechanism to let us lazily later discard the CU.
19564 For now we skip this optimization. */
19568 addr_base = cu->addr_base;
19569 addr_size = cu->header.addr_size;
19573 struct dwarf2_read_addr_index_data aidata;
19575 /* Note: We can't use init_cutu_and_read_dies_simple here,
19576 we need addr_base. */
19577 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
19578 dwarf2_read_addr_index_reader, &aidata);
19579 addr_base = aidata.addr_base;
19580 addr_size = aidata.addr_size;
19583 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19587 /* Given a DW_FORM_GNU_str_index, fetch the string.
19588 This is only used by the Fission support. */
19590 static const char *
19591 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19593 struct dwarf2_cu *cu = reader->cu;
19594 struct dwarf2_per_objfile *dwarf2_per_objfile
19595 = cu->per_cu->dwarf2_per_objfile;
19596 struct objfile *objfile = dwarf2_per_objfile->objfile;
19597 const char *objf_name = objfile_name (objfile);
19598 bfd *abfd = objfile->obfd;
19599 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19600 struct dwarf2_section_info *str_offsets_section =
19601 &reader->dwo_file->sections.str_offsets;
19602 const gdb_byte *info_ptr;
19603 ULONGEST str_offset;
19604 static const char form_name[] = "DW_FORM_GNU_str_index";
19606 dwarf2_read_section (objfile, str_section);
19607 dwarf2_read_section (objfile, str_offsets_section);
19608 if (str_section->buffer == NULL)
19609 error (_("%s used without .debug_str.dwo section"
19610 " in CU at offset %s [in module %s]"),
19611 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19612 if (str_offsets_section->buffer == NULL)
19613 error (_("%s used without .debug_str_offsets.dwo section"
19614 " in CU at offset %s [in module %s]"),
19615 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19616 if (str_index * cu->header.offset_size >= str_offsets_section->size)
19617 error (_("%s pointing outside of .debug_str_offsets.dwo"
19618 " section in CU at offset %s [in module %s]"),
19619 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19620 info_ptr = (str_offsets_section->buffer
19621 + str_index * cu->header.offset_size);
19622 if (cu->header.offset_size == 4)
19623 str_offset = bfd_get_32 (abfd, info_ptr);
19625 str_offset = bfd_get_64 (abfd, info_ptr);
19626 if (str_offset >= str_section->size)
19627 error (_("Offset from %s pointing outside of"
19628 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19629 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19630 return (const char *) (str_section->buffer + str_offset);
19633 /* Return the length of an LEB128 number in BUF. */
19636 leb128_size (const gdb_byte *buf)
19638 const gdb_byte *begin = buf;
19644 if ((byte & 128) == 0)
19645 return buf - begin;
19650 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19659 cu->language = language_c;
19662 case DW_LANG_C_plus_plus:
19663 case DW_LANG_C_plus_plus_11:
19664 case DW_LANG_C_plus_plus_14:
19665 cu->language = language_cplus;
19668 cu->language = language_d;
19670 case DW_LANG_Fortran77:
19671 case DW_LANG_Fortran90:
19672 case DW_LANG_Fortran95:
19673 case DW_LANG_Fortran03:
19674 case DW_LANG_Fortran08:
19675 cu->language = language_fortran;
19678 cu->language = language_go;
19680 case DW_LANG_Mips_Assembler:
19681 cu->language = language_asm;
19683 case DW_LANG_Ada83:
19684 case DW_LANG_Ada95:
19685 cu->language = language_ada;
19687 case DW_LANG_Modula2:
19688 cu->language = language_m2;
19690 case DW_LANG_Pascal83:
19691 cu->language = language_pascal;
19694 cu->language = language_objc;
19697 case DW_LANG_Rust_old:
19698 cu->language = language_rust;
19700 case DW_LANG_Cobol74:
19701 case DW_LANG_Cobol85:
19703 cu->language = language_minimal;
19706 cu->language_defn = language_def (cu->language);
19709 /* Return the named attribute or NULL if not there. */
19711 static struct attribute *
19712 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19717 struct attribute *spec = NULL;
19719 for (i = 0; i < die->num_attrs; ++i)
19721 if (die->attrs[i].name == name)
19722 return &die->attrs[i];
19723 if (die->attrs[i].name == DW_AT_specification
19724 || die->attrs[i].name == DW_AT_abstract_origin)
19725 spec = &die->attrs[i];
19731 die = follow_die_ref (die, spec, &cu);
19737 /* Return the named attribute or NULL if not there,
19738 but do not follow DW_AT_specification, etc.
19739 This is for use in contexts where we're reading .debug_types dies.
19740 Following DW_AT_specification, DW_AT_abstract_origin will take us
19741 back up the chain, and we want to go down. */
19743 static struct attribute *
19744 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19748 for (i = 0; i < die->num_attrs; ++i)
19749 if (die->attrs[i].name == name)
19750 return &die->attrs[i];
19755 /* Return the string associated with a string-typed attribute, or NULL if it
19756 is either not found or is of an incorrect type. */
19758 static const char *
19759 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19761 struct attribute *attr;
19762 const char *str = NULL;
19764 attr = dwarf2_attr (die, name, cu);
19768 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19769 || attr->form == DW_FORM_string
19770 || attr->form == DW_FORM_GNU_str_index
19771 || attr->form == DW_FORM_GNU_strp_alt)
19772 str = DW_STRING (attr);
19774 complaint (_("string type expected for attribute %s for "
19775 "DIE at %s in module %s"),
19776 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19777 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19783 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19784 and holds a non-zero value. This function should only be used for
19785 DW_FORM_flag or DW_FORM_flag_present attributes. */
19788 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19790 struct attribute *attr = dwarf2_attr (die, name, cu);
19792 return (attr && DW_UNSND (attr));
19796 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19798 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19799 which value is non-zero. However, we have to be careful with
19800 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19801 (via dwarf2_flag_true_p) follows this attribute. So we may
19802 end up accidently finding a declaration attribute that belongs
19803 to a different DIE referenced by the specification attribute,
19804 even though the given DIE does not have a declaration attribute. */
19805 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19806 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19809 /* Return the die giving the specification for DIE, if there is
19810 one. *SPEC_CU is the CU containing DIE on input, and the CU
19811 containing the return value on output. If there is no
19812 specification, but there is an abstract origin, that is
19815 static struct die_info *
19816 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19818 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19821 if (spec_attr == NULL)
19822 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19824 if (spec_attr == NULL)
19827 return follow_die_ref (die, spec_attr, spec_cu);
19830 /* Stub for free_line_header to match void * callback types. */
19833 free_line_header_voidp (void *arg)
19835 struct line_header *lh = (struct line_header *) arg;
19841 line_header::add_include_dir (const char *include_dir)
19843 if (dwarf_line_debug >= 2)
19844 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19845 include_dirs.size () + 1, include_dir);
19847 include_dirs.push_back (include_dir);
19851 line_header::add_file_name (const char *name,
19853 unsigned int mod_time,
19854 unsigned int length)
19856 if (dwarf_line_debug >= 2)
19857 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
19858 (unsigned) file_names.size () + 1, name);
19860 file_names.emplace_back (name, d_index, mod_time, length);
19863 /* A convenience function to find the proper .debug_line section for a CU. */
19865 static struct dwarf2_section_info *
19866 get_debug_line_section (struct dwarf2_cu *cu)
19868 struct dwarf2_section_info *section;
19869 struct dwarf2_per_objfile *dwarf2_per_objfile
19870 = cu->per_cu->dwarf2_per_objfile;
19872 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19874 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19875 section = &cu->dwo_unit->dwo_file->sections.line;
19876 else if (cu->per_cu->is_dwz)
19878 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19880 section = &dwz->line;
19883 section = &dwarf2_per_objfile->line;
19888 /* Read directory or file name entry format, starting with byte of
19889 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19890 entries count and the entries themselves in the described entry
19894 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19895 bfd *abfd, const gdb_byte **bufp,
19896 struct line_header *lh,
19897 const struct comp_unit_head *cu_header,
19898 void (*callback) (struct line_header *lh,
19901 unsigned int mod_time,
19902 unsigned int length))
19904 gdb_byte format_count, formati;
19905 ULONGEST data_count, datai;
19906 const gdb_byte *buf = *bufp;
19907 const gdb_byte *format_header_data;
19908 unsigned int bytes_read;
19910 format_count = read_1_byte (abfd, buf);
19912 format_header_data = buf;
19913 for (formati = 0; formati < format_count; formati++)
19915 read_unsigned_leb128 (abfd, buf, &bytes_read);
19917 read_unsigned_leb128 (abfd, buf, &bytes_read);
19921 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19923 for (datai = 0; datai < data_count; datai++)
19925 const gdb_byte *format = format_header_data;
19926 struct file_entry fe;
19928 for (formati = 0; formati < format_count; formati++)
19930 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
19931 format += bytes_read;
19933 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
19934 format += bytes_read;
19936 gdb::optional<const char *> string;
19937 gdb::optional<unsigned int> uint;
19941 case DW_FORM_string:
19942 string.emplace (read_direct_string (abfd, buf, &bytes_read));
19946 case DW_FORM_line_strp:
19947 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
19954 case DW_FORM_data1:
19955 uint.emplace (read_1_byte (abfd, buf));
19959 case DW_FORM_data2:
19960 uint.emplace (read_2_bytes (abfd, buf));
19964 case DW_FORM_data4:
19965 uint.emplace (read_4_bytes (abfd, buf));
19969 case DW_FORM_data8:
19970 uint.emplace (read_8_bytes (abfd, buf));
19974 case DW_FORM_udata:
19975 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
19979 case DW_FORM_block:
19980 /* It is valid only for DW_LNCT_timestamp which is ignored by
19985 switch (content_type)
19988 if (string.has_value ())
19991 case DW_LNCT_directory_index:
19992 if (uint.has_value ())
19993 fe.d_index = (dir_index) *uint;
19995 case DW_LNCT_timestamp:
19996 if (uint.has_value ())
19997 fe.mod_time = *uint;
20000 if (uint.has_value ())
20006 complaint (_("Unknown format content type %s"),
20007 pulongest (content_type));
20011 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20017 /* Read the statement program header starting at OFFSET in
20018 .debug_line, or .debug_line.dwo. Return a pointer
20019 to a struct line_header, allocated using xmalloc.
20020 Returns NULL if there is a problem reading the header, e.g., if it
20021 has a version we don't understand.
20023 NOTE: the strings in the include directory and file name tables of
20024 the returned object point into the dwarf line section buffer,
20025 and must not be freed. */
20027 static line_header_up
20028 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20030 const gdb_byte *line_ptr;
20031 unsigned int bytes_read, offset_size;
20033 const char *cur_dir, *cur_file;
20034 struct dwarf2_section_info *section;
20036 struct dwarf2_per_objfile *dwarf2_per_objfile
20037 = cu->per_cu->dwarf2_per_objfile;
20039 section = get_debug_line_section (cu);
20040 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20041 if (section->buffer == NULL)
20043 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20044 complaint (_("missing .debug_line.dwo section"));
20046 complaint (_("missing .debug_line section"));
20050 /* We can't do this until we know the section is non-empty.
20051 Only then do we know we have such a section. */
20052 abfd = get_section_bfd_owner (section);
20054 /* Make sure that at least there's room for the total_length field.
20055 That could be 12 bytes long, but we're just going to fudge that. */
20056 if (to_underlying (sect_off) + 4 >= section->size)
20058 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20062 line_header_up lh (new line_header ());
20064 lh->sect_off = sect_off;
20065 lh->offset_in_dwz = cu->per_cu->is_dwz;
20067 line_ptr = section->buffer + to_underlying (sect_off);
20069 /* Read in the header. */
20071 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20072 &bytes_read, &offset_size);
20073 line_ptr += bytes_read;
20074 if (line_ptr + lh->total_length > (section->buffer + section->size))
20076 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20079 lh->statement_program_end = line_ptr + lh->total_length;
20080 lh->version = read_2_bytes (abfd, line_ptr);
20082 if (lh->version > 5)
20084 /* This is a version we don't understand. The format could have
20085 changed in ways we don't handle properly so just punt. */
20086 complaint (_("unsupported version in .debug_line section"));
20089 if (lh->version >= 5)
20091 gdb_byte segment_selector_size;
20093 /* Skip address size. */
20094 read_1_byte (abfd, line_ptr);
20097 segment_selector_size = read_1_byte (abfd, line_ptr);
20099 if (segment_selector_size != 0)
20101 complaint (_("unsupported segment selector size %u "
20102 "in .debug_line section"),
20103 segment_selector_size);
20107 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20108 line_ptr += offset_size;
20109 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20111 if (lh->version >= 4)
20113 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20117 lh->maximum_ops_per_instruction = 1;
20119 if (lh->maximum_ops_per_instruction == 0)
20121 lh->maximum_ops_per_instruction = 1;
20122 complaint (_("invalid maximum_ops_per_instruction "
20123 "in `.debug_line' section"));
20126 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20128 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20130 lh->line_range = read_1_byte (abfd, line_ptr);
20132 lh->opcode_base = read_1_byte (abfd, line_ptr);
20134 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20136 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20137 for (i = 1; i < lh->opcode_base; ++i)
20139 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20143 if (lh->version >= 5)
20145 /* Read directory table. */
20146 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20148 [] (struct line_header *lh, const char *name,
20149 dir_index d_index, unsigned int mod_time,
20150 unsigned int length)
20152 lh->add_include_dir (name);
20155 /* Read file name table. */
20156 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20158 [] (struct line_header *lh, const char *name,
20159 dir_index d_index, unsigned int mod_time,
20160 unsigned int length)
20162 lh->add_file_name (name, d_index, mod_time, length);
20167 /* Read directory table. */
20168 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20170 line_ptr += bytes_read;
20171 lh->add_include_dir (cur_dir);
20173 line_ptr += bytes_read;
20175 /* Read file name table. */
20176 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20178 unsigned int mod_time, length;
20181 line_ptr += bytes_read;
20182 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20183 line_ptr += bytes_read;
20184 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20185 line_ptr += bytes_read;
20186 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20187 line_ptr += bytes_read;
20189 lh->add_file_name (cur_file, d_index, mod_time, length);
20191 line_ptr += bytes_read;
20193 lh->statement_program_start = line_ptr;
20195 if (line_ptr > (section->buffer + section->size))
20196 complaint (_("line number info header doesn't "
20197 "fit in `.debug_line' section"));
20202 /* Subroutine of dwarf_decode_lines to simplify it.
20203 Return the file name of the psymtab for included file FILE_INDEX
20204 in line header LH of PST.
20205 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20206 If space for the result is malloc'd, *NAME_HOLDER will be set.
20207 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20209 static const char *
20210 psymtab_include_file_name (const struct line_header *lh, int file_index,
20211 const struct partial_symtab *pst,
20212 const char *comp_dir,
20213 gdb::unique_xmalloc_ptr<char> *name_holder)
20215 const file_entry &fe = lh->file_names[file_index];
20216 const char *include_name = fe.name;
20217 const char *include_name_to_compare = include_name;
20218 const char *pst_filename;
20221 const char *dir_name = fe.include_dir (lh);
20223 gdb::unique_xmalloc_ptr<char> hold_compare;
20224 if (!IS_ABSOLUTE_PATH (include_name)
20225 && (dir_name != NULL || comp_dir != NULL))
20227 /* Avoid creating a duplicate psymtab for PST.
20228 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20229 Before we do the comparison, however, we need to account
20230 for DIR_NAME and COMP_DIR.
20231 First prepend dir_name (if non-NULL). If we still don't
20232 have an absolute path prepend comp_dir (if non-NULL).
20233 However, the directory we record in the include-file's
20234 psymtab does not contain COMP_DIR (to match the
20235 corresponding symtab(s)).
20240 bash$ gcc -g ./hello.c
20241 include_name = "hello.c"
20243 DW_AT_comp_dir = comp_dir = "/tmp"
20244 DW_AT_name = "./hello.c"
20248 if (dir_name != NULL)
20250 name_holder->reset (concat (dir_name, SLASH_STRING,
20251 include_name, (char *) NULL));
20252 include_name = name_holder->get ();
20253 include_name_to_compare = include_name;
20255 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20257 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20258 include_name, (char *) NULL));
20259 include_name_to_compare = hold_compare.get ();
20263 pst_filename = pst->filename;
20264 gdb::unique_xmalloc_ptr<char> copied_name;
20265 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20267 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20268 pst_filename, (char *) NULL));
20269 pst_filename = copied_name.get ();
20272 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20276 return include_name;
20279 /* State machine to track the state of the line number program. */
20281 class lnp_state_machine
20284 /* Initialize a machine state for the start of a line number
20286 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20288 file_entry *current_file ()
20290 /* lh->file_names is 0-based, but the file name numbers in the
20291 statement program are 1-based. */
20292 return m_line_header->file_name_at (m_file);
20295 /* Record the line in the state machine. END_SEQUENCE is true if
20296 we're processing the end of a sequence. */
20297 void record_line (bool end_sequence);
20299 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20300 nop-out rest of the lines in this sequence. */
20301 void check_line_address (struct dwarf2_cu *cu,
20302 const gdb_byte *line_ptr,
20303 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20305 void handle_set_discriminator (unsigned int discriminator)
20307 m_discriminator = discriminator;
20308 m_line_has_non_zero_discriminator |= discriminator != 0;
20311 /* Handle DW_LNE_set_address. */
20312 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20315 address += baseaddr;
20316 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20319 /* Handle DW_LNS_advance_pc. */
20320 void handle_advance_pc (CORE_ADDR adjust);
20322 /* Handle a special opcode. */
20323 void handle_special_opcode (unsigned char op_code);
20325 /* Handle DW_LNS_advance_line. */
20326 void handle_advance_line (int line_delta)
20328 advance_line (line_delta);
20331 /* Handle DW_LNS_set_file. */
20332 void handle_set_file (file_name_index file);
20334 /* Handle DW_LNS_negate_stmt. */
20335 void handle_negate_stmt ()
20337 m_is_stmt = !m_is_stmt;
20340 /* Handle DW_LNS_const_add_pc. */
20341 void handle_const_add_pc ();
20343 /* Handle DW_LNS_fixed_advance_pc. */
20344 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20346 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20350 /* Handle DW_LNS_copy. */
20351 void handle_copy ()
20353 record_line (false);
20354 m_discriminator = 0;
20357 /* Handle DW_LNE_end_sequence. */
20358 void handle_end_sequence ()
20360 m_record_line_callback = ::record_line;
20364 /* Advance the line by LINE_DELTA. */
20365 void advance_line (int line_delta)
20367 m_line += line_delta;
20369 if (line_delta != 0)
20370 m_line_has_non_zero_discriminator = m_discriminator != 0;
20373 gdbarch *m_gdbarch;
20375 /* True if we're recording lines.
20376 Otherwise we're building partial symtabs and are just interested in
20377 finding include files mentioned by the line number program. */
20378 bool m_record_lines_p;
20380 /* The line number header. */
20381 line_header *m_line_header;
20383 /* These are part of the standard DWARF line number state machine,
20384 and initialized according to the DWARF spec. */
20386 unsigned char m_op_index = 0;
20387 /* The line table index (1-based) of the current file. */
20388 file_name_index m_file = (file_name_index) 1;
20389 unsigned int m_line = 1;
20391 /* These are initialized in the constructor. */
20393 CORE_ADDR m_address;
20395 unsigned int m_discriminator;
20397 /* Additional bits of state we need to track. */
20399 /* The last file that we called dwarf2_start_subfile for.
20400 This is only used for TLLs. */
20401 unsigned int m_last_file = 0;
20402 /* The last file a line number was recorded for. */
20403 struct subfile *m_last_subfile = NULL;
20405 /* The function to call to record a line. */
20406 record_line_ftype *m_record_line_callback = NULL;
20408 /* The last line number that was recorded, used to coalesce
20409 consecutive entries for the same line. This can happen, for
20410 example, when discriminators are present. PR 17276. */
20411 unsigned int m_last_line = 0;
20412 bool m_line_has_non_zero_discriminator = false;
20416 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20418 CORE_ADDR addr_adj = (((m_op_index + adjust)
20419 / m_line_header->maximum_ops_per_instruction)
20420 * m_line_header->minimum_instruction_length);
20421 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20422 m_op_index = ((m_op_index + adjust)
20423 % m_line_header->maximum_ops_per_instruction);
20427 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20429 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20430 CORE_ADDR addr_adj = (((m_op_index
20431 + (adj_opcode / m_line_header->line_range))
20432 / m_line_header->maximum_ops_per_instruction)
20433 * m_line_header->minimum_instruction_length);
20434 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20435 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20436 % m_line_header->maximum_ops_per_instruction);
20438 int line_delta = (m_line_header->line_base
20439 + (adj_opcode % m_line_header->line_range));
20440 advance_line (line_delta);
20441 record_line (false);
20442 m_discriminator = 0;
20446 lnp_state_machine::handle_set_file (file_name_index file)
20450 const file_entry *fe = current_file ();
20452 dwarf2_debug_line_missing_file_complaint ();
20453 else if (m_record_lines_p)
20455 const char *dir = fe->include_dir (m_line_header);
20457 m_last_subfile = get_current_subfile ();
20458 m_line_has_non_zero_discriminator = m_discriminator != 0;
20459 dwarf2_start_subfile (fe->name, dir);
20464 lnp_state_machine::handle_const_add_pc ()
20467 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20470 = (((m_op_index + adjust)
20471 / m_line_header->maximum_ops_per_instruction)
20472 * m_line_header->minimum_instruction_length);
20474 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20475 m_op_index = ((m_op_index + adjust)
20476 % m_line_header->maximum_ops_per_instruction);
20479 /* Ignore this record_line request. */
20482 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20487 /* Return non-zero if we should add LINE to the line number table.
20488 LINE is the line to add, LAST_LINE is the last line that was added,
20489 LAST_SUBFILE is the subfile for LAST_LINE.
20490 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20491 had a non-zero discriminator.
20493 We have to be careful in the presence of discriminators.
20494 E.g., for this line:
20496 for (i = 0; i < 100000; i++);
20498 clang can emit four line number entries for that one line,
20499 each with a different discriminator.
20500 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20502 However, we want gdb to coalesce all four entries into one.
20503 Otherwise the user could stepi into the middle of the line and
20504 gdb would get confused about whether the pc really was in the
20505 middle of the line.
20507 Things are further complicated by the fact that two consecutive
20508 line number entries for the same line is a heuristic used by gcc
20509 to denote the end of the prologue. So we can't just discard duplicate
20510 entries, we have to be selective about it. The heuristic we use is
20511 that we only collapse consecutive entries for the same line if at least
20512 one of those entries has a non-zero discriminator. PR 17276.
20514 Note: Addresses in the line number state machine can never go backwards
20515 within one sequence, thus this coalescing is ok. */
20518 dwarf_record_line_p (unsigned int line, unsigned int last_line,
20519 int line_has_non_zero_discriminator,
20520 struct subfile *last_subfile)
20522 if (get_current_subfile () != last_subfile)
20524 if (line != last_line)
20526 /* Same line for the same file that we've seen already.
20527 As a last check, for pr 17276, only record the line if the line
20528 has never had a non-zero discriminator. */
20529 if (!line_has_non_zero_discriminator)
20534 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20535 in the line table of subfile SUBFILE. */
20538 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20539 unsigned int line, CORE_ADDR address,
20540 record_line_ftype p_record_line)
20542 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20544 if (dwarf_line_debug)
20546 fprintf_unfiltered (gdb_stdlog,
20547 "Recording line %u, file %s, address %s\n",
20548 line, lbasename (subfile->name),
20549 paddress (gdbarch, address));
20552 (*p_record_line) (subfile, line, addr);
20555 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20556 Mark the end of a set of line number records.
20557 The arguments are the same as for dwarf_record_line_1.
20558 If SUBFILE is NULL the request is ignored. */
20561 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20562 CORE_ADDR address, record_line_ftype p_record_line)
20564 if (subfile == NULL)
20567 if (dwarf_line_debug)
20569 fprintf_unfiltered (gdb_stdlog,
20570 "Finishing current line, file %s, address %s\n",
20571 lbasename (subfile->name),
20572 paddress (gdbarch, address));
20575 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20579 lnp_state_machine::record_line (bool end_sequence)
20581 if (dwarf_line_debug)
20583 fprintf_unfiltered (gdb_stdlog,
20584 "Processing actual line %u: file %u,"
20585 " address %s, is_stmt %u, discrim %u\n",
20586 m_line, to_underlying (m_file),
20587 paddress (m_gdbarch, m_address),
20588 m_is_stmt, m_discriminator);
20591 file_entry *fe = current_file ();
20594 dwarf2_debug_line_missing_file_complaint ();
20595 /* For now we ignore lines not starting on an instruction boundary.
20596 But not when processing end_sequence for compatibility with the
20597 previous version of the code. */
20598 else if (m_op_index == 0 || end_sequence)
20600 fe->included_p = 1;
20601 if (m_record_lines_p && m_is_stmt)
20603 if (m_last_subfile != get_current_subfile () || end_sequence)
20605 dwarf_finish_line (m_gdbarch, m_last_subfile,
20606 m_address, m_record_line_callback);
20611 if (dwarf_record_line_p (m_line, m_last_line,
20612 m_line_has_non_zero_discriminator,
20615 dwarf_record_line_1 (m_gdbarch, get_current_subfile (),
20617 m_record_line_callback);
20619 m_last_subfile = get_current_subfile ();
20620 m_last_line = m_line;
20626 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20627 bool record_lines_p)
20630 m_record_lines_p = record_lines_p;
20631 m_line_header = lh;
20633 m_record_line_callback = ::record_line;
20635 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20636 was a line entry for it so that the backend has a chance to adjust it
20637 and also record it in case it needs it. This is currently used by MIPS
20638 code, cf. `mips_adjust_dwarf2_line'. */
20639 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20640 m_is_stmt = lh->default_is_stmt;
20641 m_discriminator = 0;
20645 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20646 const gdb_byte *line_ptr,
20647 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20649 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20650 the pc range of the CU. However, we restrict the test to only ADDRESS
20651 values of zero to preserve GDB's previous behaviour which is to handle
20652 the specific case of a function being GC'd by the linker. */
20654 if (address == 0 && address < unrelocated_lowpc)
20656 /* This line table is for a function which has been
20657 GCd by the linker. Ignore it. PR gdb/12528 */
20659 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20660 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20662 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20663 line_offset, objfile_name (objfile));
20664 m_record_line_callback = noop_record_line;
20665 /* Note: record_line_callback is left as noop_record_line until
20666 we see DW_LNE_end_sequence. */
20670 /* Subroutine of dwarf_decode_lines to simplify it.
20671 Process the line number information in LH.
20672 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20673 program in order to set included_p for every referenced header. */
20676 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20677 const int decode_for_pst_p, CORE_ADDR lowpc)
20679 const gdb_byte *line_ptr, *extended_end;
20680 const gdb_byte *line_end;
20681 unsigned int bytes_read, extended_len;
20682 unsigned char op_code, extended_op;
20683 CORE_ADDR baseaddr;
20684 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20685 bfd *abfd = objfile->obfd;
20686 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20687 /* True if we're recording line info (as opposed to building partial
20688 symtabs and just interested in finding include files mentioned by
20689 the line number program). */
20690 bool record_lines_p = !decode_for_pst_p;
20692 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20694 line_ptr = lh->statement_program_start;
20695 line_end = lh->statement_program_end;
20697 /* Read the statement sequences until there's nothing left. */
20698 while (line_ptr < line_end)
20700 /* The DWARF line number program state machine. Reset the state
20701 machine at the start of each sequence. */
20702 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20703 bool end_sequence = false;
20705 if (record_lines_p)
20707 /* Start a subfile for the current file of the state
20709 const file_entry *fe = state_machine.current_file ();
20712 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
20715 /* Decode the table. */
20716 while (line_ptr < line_end && !end_sequence)
20718 op_code = read_1_byte (abfd, line_ptr);
20721 if (op_code >= lh->opcode_base)
20723 /* Special opcode. */
20724 state_machine.handle_special_opcode (op_code);
20726 else switch (op_code)
20728 case DW_LNS_extended_op:
20729 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20731 line_ptr += bytes_read;
20732 extended_end = line_ptr + extended_len;
20733 extended_op = read_1_byte (abfd, line_ptr);
20735 switch (extended_op)
20737 case DW_LNE_end_sequence:
20738 state_machine.handle_end_sequence ();
20739 end_sequence = true;
20741 case DW_LNE_set_address:
20744 = read_address (abfd, line_ptr, cu, &bytes_read);
20745 line_ptr += bytes_read;
20747 state_machine.check_line_address (cu, line_ptr,
20748 lowpc - baseaddr, address);
20749 state_machine.handle_set_address (baseaddr, address);
20752 case DW_LNE_define_file:
20754 const char *cur_file;
20755 unsigned int mod_time, length;
20758 cur_file = read_direct_string (abfd, line_ptr,
20760 line_ptr += bytes_read;
20761 dindex = (dir_index)
20762 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20763 line_ptr += bytes_read;
20765 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20766 line_ptr += bytes_read;
20768 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20769 line_ptr += bytes_read;
20770 lh->add_file_name (cur_file, dindex, mod_time, length);
20773 case DW_LNE_set_discriminator:
20775 /* The discriminator is not interesting to the
20776 debugger; just ignore it. We still need to
20777 check its value though:
20778 if there are consecutive entries for the same
20779 (non-prologue) line we want to coalesce them.
20782 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20783 line_ptr += bytes_read;
20785 state_machine.handle_set_discriminator (discr);
20789 complaint (_("mangled .debug_line section"));
20792 /* Make sure that we parsed the extended op correctly. If e.g.
20793 we expected a different address size than the producer used,
20794 we may have read the wrong number of bytes. */
20795 if (line_ptr != extended_end)
20797 complaint (_("mangled .debug_line section"));
20802 state_machine.handle_copy ();
20804 case DW_LNS_advance_pc:
20807 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20808 line_ptr += bytes_read;
20810 state_machine.handle_advance_pc (adjust);
20813 case DW_LNS_advance_line:
20816 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20817 line_ptr += bytes_read;
20819 state_machine.handle_advance_line (line_delta);
20822 case DW_LNS_set_file:
20824 file_name_index file
20825 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20827 line_ptr += bytes_read;
20829 state_machine.handle_set_file (file);
20832 case DW_LNS_set_column:
20833 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20834 line_ptr += bytes_read;
20836 case DW_LNS_negate_stmt:
20837 state_machine.handle_negate_stmt ();
20839 case DW_LNS_set_basic_block:
20841 /* Add to the address register of the state machine the
20842 address increment value corresponding to special opcode
20843 255. I.e., this value is scaled by the minimum
20844 instruction length since special opcode 255 would have
20845 scaled the increment. */
20846 case DW_LNS_const_add_pc:
20847 state_machine.handle_const_add_pc ();
20849 case DW_LNS_fixed_advance_pc:
20851 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20854 state_machine.handle_fixed_advance_pc (addr_adj);
20859 /* Unknown standard opcode, ignore it. */
20862 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20864 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20865 line_ptr += bytes_read;
20872 dwarf2_debug_line_missing_end_sequence_complaint ();
20874 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20875 in which case we still finish recording the last line). */
20876 state_machine.record_line (true);
20880 /* Decode the Line Number Program (LNP) for the given line_header
20881 structure and CU. The actual information extracted and the type
20882 of structures created from the LNP depends on the value of PST.
20884 1. If PST is NULL, then this procedure uses the data from the program
20885 to create all necessary symbol tables, and their linetables.
20887 2. If PST is not NULL, this procedure reads the program to determine
20888 the list of files included by the unit represented by PST, and
20889 builds all the associated partial symbol tables.
20891 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20892 It is used for relative paths in the line table.
20893 NOTE: When processing partial symtabs (pst != NULL),
20894 comp_dir == pst->dirname.
20896 NOTE: It is important that psymtabs have the same file name (via strcmp)
20897 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20898 symtab we don't use it in the name of the psymtabs we create.
20899 E.g. expand_line_sal requires this when finding psymtabs to expand.
20900 A good testcase for this is mb-inline.exp.
20902 LOWPC is the lowest address in CU (or 0 if not known).
20904 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20905 for its PC<->lines mapping information. Otherwise only the filename
20906 table is read in. */
20909 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20910 struct dwarf2_cu *cu, struct partial_symtab *pst,
20911 CORE_ADDR lowpc, int decode_mapping)
20913 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20914 const int decode_for_pst_p = (pst != NULL);
20916 if (decode_mapping)
20917 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20919 if (decode_for_pst_p)
20923 /* Now that we're done scanning the Line Header Program, we can
20924 create the psymtab of each included file. */
20925 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
20926 if (lh->file_names[file_index].included_p == 1)
20928 gdb::unique_xmalloc_ptr<char> name_holder;
20929 const char *include_name =
20930 psymtab_include_file_name (lh, file_index, pst, comp_dir,
20932 if (include_name != NULL)
20933 dwarf2_create_include_psymtab (include_name, pst, objfile);
20938 /* Make sure a symtab is created for every file, even files
20939 which contain only variables (i.e. no code with associated
20941 struct compunit_symtab *cust = buildsym_compunit_symtab ();
20944 for (i = 0; i < lh->file_names.size (); i++)
20946 file_entry &fe = lh->file_names[i];
20948 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
20950 if (get_current_subfile ()->symtab == NULL)
20952 get_current_subfile ()->symtab
20953 = allocate_symtab (cust, get_current_subfile ()->name);
20955 fe.symtab = get_current_subfile ()->symtab;
20960 /* Start a subfile for DWARF. FILENAME is the name of the file and
20961 DIRNAME the name of the source directory which contains FILENAME
20962 or NULL if not known.
20963 This routine tries to keep line numbers from identical absolute and
20964 relative file names in a common subfile.
20966 Using the `list' example from the GDB testsuite, which resides in
20967 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20968 of /srcdir/list0.c yields the following debugging information for list0.c:
20970 DW_AT_name: /srcdir/list0.c
20971 DW_AT_comp_dir: /compdir
20972 files.files[0].name: list0.h
20973 files.files[0].dir: /srcdir
20974 files.files[1].name: list0.c
20975 files.files[1].dir: /srcdir
20977 The line number information for list0.c has to end up in a single
20978 subfile, so that `break /srcdir/list0.c:1' works as expected.
20979 start_subfile will ensure that this happens provided that we pass the
20980 concatenation of files.files[1].dir and files.files[1].name as the
20984 dwarf2_start_subfile (const char *filename, const char *dirname)
20988 /* In order not to lose the line information directory,
20989 we concatenate it to the filename when it makes sense.
20990 Note that the Dwarf3 standard says (speaking of filenames in line
20991 information): ``The directory index is ignored for file names
20992 that represent full path names''. Thus ignoring dirname in the
20993 `else' branch below isn't an issue. */
20995 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20997 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21001 start_subfile (filename);
21007 /* Start a symtab for DWARF.
21008 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21010 static struct compunit_symtab *
21011 dwarf2_start_symtab (struct dwarf2_cu *cu,
21012 const char *name, const char *comp_dir, CORE_ADDR low_pc)
21014 struct compunit_symtab *cust
21015 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21016 low_pc, cu->language);
21018 cu->list_in_scope = get_file_symbols ();
21020 record_debugformat ("DWARF 2");
21021 record_producer (cu->producer);
21023 cu->processing_has_namespace_info = 0;
21029 var_decode_location (struct attribute *attr, struct symbol *sym,
21030 struct dwarf2_cu *cu)
21032 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21033 struct comp_unit_head *cu_header = &cu->header;
21035 /* NOTE drow/2003-01-30: There used to be a comment and some special
21036 code here to turn a symbol with DW_AT_external and a
21037 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21038 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21039 with some versions of binutils) where shared libraries could have
21040 relocations against symbols in their debug information - the
21041 minimal symbol would have the right address, but the debug info
21042 would not. It's no longer necessary, because we will explicitly
21043 apply relocations when we read in the debug information now. */
21045 /* A DW_AT_location attribute with no contents indicates that a
21046 variable has been optimized away. */
21047 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21049 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21053 /* Handle one degenerate form of location expression specially, to
21054 preserve GDB's previous behavior when section offsets are
21055 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21056 then mark this symbol as LOC_STATIC. */
21058 if (attr_form_is_block (attr)
21059 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21060 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21061 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21062 && (DW_BLOCK (attr)->size
21063 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21065 unsigned int dummy;
21067 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21068 SYMBOL_VALUE_ADDRESS (sym) =
21069 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21071 SYMBOL_VALUE_ADDRESS (sym) =
21072 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21073 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21074 fixup_symbol_section (sym, objfile);
21075 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21076 SYMBOL_SECTION (sym));
21080 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21081 expression evaluator, and use LOC_COMPUTED only when necessary
21082 (i.e. when the value of a register or memory location is
21083 referenced, or a thread-local block, etc.). Then again, it might
21084 not be worthwhile. I'm assuming that it isn't unless performance
21085 or memory numbers show me otherwise. */
21087 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21089 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21090 cu->has_loclist = 1;
21093 /* Given a pointer to a DWARF information entry, figure out if we need
21094 to make a symbol table entry for it, and if so, create a new entry
21095 and return a pointer to it.
21096 If TYPE is NULL, determine symbol type from the die, otherwise
21097 used the passed type.
21098 If SPACE is not NULL, use it to hold the new symbol. If it is
21099 NULL, allocate a new symbol on the objfile's obstack. */
21101 static struct symbol *
21102 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21103 struct symbol *space)
21105 struct dwarf2_per_objfile *dwarf2_per_objfile
21106 = cu->per_cu->dwarf2_per_objfile;
21107 struct objfile *objfile = dwarf2_per_objfile->objfile;
21108 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21109 struct symbol *sym = NULL;
21111 struct attribute *attr = NULL;
21112 struct attribute *attr2 = NULL;
21113 CORE_ADDR baseaddr;
21114 struct pending **list_to_add = NULL;
21116 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21118 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21120 name = dwarf2_name (die, cu);
21123 const char *linkagename;
21124 int suppress_add = 0;
21129 sym = allocate_symbol (objfile);
21130 OBJSTAT (objfile, n_syms++);
21132 /* Cache this symbol's name and the name's demangled form (if any). */
21133 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21134 linkagename = dwarf2_physname (name, die, cu);
21135 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21137 /* Fortran does not have mangling standard and the mangling does differ
21138 between gfortran, iFort etc. */
21139 if (cu->language == language_fortran
21140 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21141 symbol_set_demangled_name (&(sym->ginfo),
21142 dwarf2_full_name (name, die, cu),
21145 /* Default assumptions.
21146 Use the passed type or decode it from the die. */
21147 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21148 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21150 SYMBOL_TYPE (sym) = type;
21152 SYMBOL_TYPE (sym) = die_type (die, cu);
21153 attr = dwarf2_attr (die,
21154 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21158 SYMBOL_LINE (sym) = DW_UNSND (attr);
21161 attr = dwarf2_attr (die,
21162 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21166 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21167 struct file_entry *fe;
21169 if (cu->line_header != NULL)
21170 fe = cu->line_header->file_name_at (file_index);
21175 complaint (_("file index out of range"));
21177 symbol_set_symtab (sym, fe->symtab);
21183 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21188 addr = attr_value_as_address (attr);
21189 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21190 SYMBOL_VALUE_ADDRESS (sym) = addr;
21192 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21193 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21194 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21195 add_symbol_to_list (sym, cu->list_in_scope);
21197 case DW_TAG_subprogram:
21198 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21200 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21201 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21202 if ((attr2 && (DW_UNSND (attr2) != 0))
21203 || cu->language == language_ada)
21205 /* Subprograms marked external are stored as a global symbol.
21206 Ada subprograms, whether marked external or not, are always
21207 stored as a global symbol, because we want to be able to
21208 access them globally. For instance, we want to be able
21209 to break on a nested subprogram without having to
21210 specify the context. */
21211 list_to_add = get_global_symbols ();
21215 list_to_add = cu->list_in_scope;
21218 case DW_TAG_inlined_subroutine:
21219 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21221 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21222 SYMBOL_INLINED (sym) = 1;
21223 list_to_add = cu->list_in_scope;
21225 case DW_TAG_template_value_param:
21227 /* Fall through. */
21228 case DW_TAG_constant:
21229 case DW_TAG_variable:
21230 case DW_TAG_member:
21231 /* Compilation with minimal debug info may result in
21232 variables with missing type entries. Change the
21233 misleading `void' type to something sensible. */
21234 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21235 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21237 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21238 /* In the case of DW_TAG_member, we should only be called for
21239 static const members. */
21240 if (die->tag == DW_TAG_member)
21242 /* dwarf2_add_field uses die_is_declaration,
21243 so we do the same. */
21244 gdb_assert (die_is_declaration (die, cu));
21249 dwarf2_const_value (attr, sym, cu);
21250 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21253 if (attr2 && (DW_UNSND (attr2) != 0))
21254 list_to_add = get_global_symbols ();
21256 list_to_add = cu->list_in_scope;
21260 attr = dwarf2_attr (die, DW_AT_location, cu);
21263 var_decode_location (attr, sym, cu);
21264 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21266 /* Fortran explicitly imports any global symbols to the local
21267 scope by DW_TAG_common_block. */
21268 if (cu->language == language_fortran && die->parent
21269 && die->parent->tag == DW_TAG_common_block)
21272 if (SYMBOL_CLASS (sym) == LOC_STATIC
21273 && SYMBOL_VALUE_ADDRESS (sym) == 0
21274 && !dwarf2_per_objfile->has_section_at_zero)
21276 /* When a static variable is eliminated by the linker,
21277 the corresponding debug information is not stripped
21278 out, but the variable address is set to null;
21279 do not add such variables into symbol table. */
21281 else if (attr2 && (DW_UNSND (attr2) != 0))
21283 /* Workaround gfortran PR debug/40040 - it uses
21284 DW_AT_location for variables in -fPIC libraries which may
21285 get overriden by other libraries/executable and get
21286 a different address. Resolve it by the minimal symbol
21287 which may come from inferior's executable using copy
21288 relocation. Make this workaround only for gfortran as for
21289 other compilers GDB cannot guess the minimal symbol
21290 Fortran mangling kind. */
21291 if (cu->language == language_fortran && die->parent
21292 && die->parent->tag == DW_TAG_module
21294 && startswith (cu->producer, "GNU Fortran"))
21295 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21297 /* A variable with DW_AT_external is never static,
21298 but it may be block-scoped. */
21299 list_to_add = (cu->list_in_scope == get_file_symbols ()
21300 ? get_global_symbols () : cu->list_in_scope);
21303 list_to_add = cu->list_in_scope;
21307 /* We do not know the address of this symbol.
21308 If it is an external symbol and we have type information
21309 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21310 The address of the variable will then be determined from
21311 the minimal symbol table whenever the variable is
21313 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21315 /* Fortran explicitly imports any global symbols to the local
21316 scope by DW_TAG_common_block. */
21317 if (cu->language == language_fortran && die->parent
21318 && die->parent->tag == DW_TAG_common_block)
21320 /* SYMBOL_CLASS doesn't matter here because
21321 read_common_block is going to reset it. */
21323 list_to_add = cu->list_in_scope;
21325 else if (attr2 && (DW_UNSND (attr2) != 0)
21326 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21328 /* A variable with DW_AT_external is never static, but it
21329 may be block-scoped. */
21330 list_to_add = (cu->list_in_scope == get_file_symbols ()
21331 ? get_global_symbols () : cu->list_in_scope);
21333 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21335 else if (!die_is_declaration (die, cu))
21337 /* Use the default LOC_OPTIMIZED_OUT class. */
21338 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21340 list_to_add = cu->list_in_scope;
21344 case DW_TAG_formal_parameter:
21346 /* If we are inside a function, mark this as an argument. If
21347 not, we might be looking at an argument to an inlined function
21348 when we do not have enough information to show inlined frames;
21349 pretend it's a local variable in that case so that the user can
21351 struct context_stack *curr = get_current_context_stack ();
21352 if (curr != nullptr && curr->name != nullptr)
21353 SYMBOL_IS_ARGUMENT (sym) = 1;
21354 attr = dwarf2_attr (die, DW_AT_location, cu);
21357 var_decode_location (attr, sym, cu);
21359 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21362 dwarf2_const_value (attr, sym, cu);
21365 list_to_add = cu->list_in_scope;
21368 case DW_TAG_unspecified_parameters:
21369 /* From varargs functions; gdb doesn't seem to have any
21370 interest in this information, so just ignore it for now.
21373 case DW_TAG_template_type_param:
21375 /* Fall through. */
21376 case DW_TAG_class_type:
21377 case DW_TAG_interface_type:
21378 case DW_TAG_structure_type:
21379 case DW_TAG_union_type:
21380 case DW_TAG_set_type:
21381 case DW_TAG_enumeration_type:
21382 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21383 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21386 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21387 really ever be static objects: otherwise, if you try
21388 to, say, break of a class's method and you're in a file
21389 which doesn't mention that class, it won't work unless
21390 the check for all static symbols in lookup_symbol_aux
21391 saves you. See the OtherFileClass tests in
21392 gdb.c++/namespace.exp. */
21396 list_to_add = (cu->list_in_scope == get_file_symbols ()
21397 && cu->language == language_cplus
21398 ? get_global_symbols () : cu->list_in_scope);
21400 /* The semantics of C++ state that "struct foo {
21401 ... }" also defines a typedef for "foo". */
21402 if (cu->language == language_cplus
21403 || cu->language == language_ada
21404 || cu->language == language_d
21405 || cu->language == language_rust)
21407 /* The symbol's name is already allocated along
21408 with this objfile, so we don't need to
21409 duplicate it for the type. */
21410 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21411 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21416 case DW_TAG_typedef:
21417 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21418 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21419 list_to_add = cu->list_in_scope;
21421 case DW_TAG_base_type:
21422 case DW_TAG_subrange_type:
21423 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21424 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21425 list_to_add = cu->list_in_scope;
21427 case DW_TAG_enumerator:
21428 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21431 dwarf2_const_value (attr, sym, cu);
21434 /* NOTE: carlton/2003-11-10: See comment above in the
21435 DW_TAG_class_type, etc. block. */
21437 list_to_add = (cu->list_in_scope == get_file_symbols ()
21438 && cu->language == language_cplus
21439 ? get_global_symbols () : cu->list_in_scope);
21442 case DW_TAG_imported_declaration:
21443 case DW_TAG_namespace:
21444 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21445 list_to_add = get_global_symbols ();
21447 case DW_TAG_module:
21448 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21449 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21450 list_to_add = get_global_symbols ();
21452 case DW_TAG_common_block:
21453 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21454 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21455 add_symbol_to_list (sym, cu->list_in_scope);
21458 /* Not a tag we recognize. Hopefully we aren't processing
21459 trash data, but since we must specifically ignore things
21460 we don't recognize, there is nothing else we should do at
21462 complaint (_("unsupported tag: '%s'"),
21463 dwarf_tag_name (die->tag));
21469 sym->hash_next = objfile->template_symbols;
21470 objfile->template_symbols = sym;
21471 list_to_add = NULL;
21474 if (list_to_add != NULL)
21475 add_symbol_to_list (sym, list_to_add);
21477 /* For the benefit of old versions of GCC, check for anonymous
21478 namespaces based on the demangled name. */
21479 if (!cu->processing_has_namespace_info
21480 && cu->language == language_cplus)
21481 cp_scan_for_anonymous_namespaces (sym, objfile);
21486 /* Given an attr with a DW_FORM_dataN value in host byte order,
21487 zero-extend it as appropriate for the symbol's type. The DWARF
21488 standard (v4) is not entirely clear about the meaning of using
21489 DW_FORM_dataN for a constant with a signed type, where the type is
21490 wider than the data. The conclusion of a discussion on the DWARF
21491 list was that this is unspecified. We choose to always zero-extend
21492 because that is the interpretation long in use by GCC. */
21495 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21496 struct dwarf2_cu *cu, LONGEST *value, int bits)
21498 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21499 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21500 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21501 LONGEST l = DW_UNSND (attr);
21503 if (bits < sizeof (*value) * 8)
21505 l &= ((LONGEST) 1 << bits) - 1;
21508 else if (bits == sizeof (*value) * 8)
21512 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21513 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21520 /* Read a constant value from an attribute. Either set *VALUE, or if
21521 the value does not fit in *VALUE, set *BYTES - either already
21522 allocated on the objfile obstack, or newly allocated on OBSTACK,
21523 or, set *BATON, if we translated the constant to a location
21527 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21528 const char *name, struct obstack *obstack,
21529 struct dwarf2_cu *cu,
21530 LONGEST *value, const gdb_byte **bytes,
21531 struct dwarf2_locexpr_baton **baton)
21533 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21534 struct comp_unit_head *cu_header = &cu->header;
21535 struct dwarf_block *blk;
21536 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21537 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21543 switch (attr->form)
21546 case DW_FORM_GNU_addr_index:
21550 if (TYPE_LENGTH (type) != cu_header->addr_size)
21551 dwarf2_const_value_length_mismatch_complaint (name,
21552 cu_header->addr_size,
21553 TYPE_LENGTH (type));
21554 /* Symbols of this form are reasonably rare, so we just
21555 piggyback on the existing location code rather than writing
21556 a new implementation of symbol_computed_ops. */
21557 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21558 (*baton)->per_cu = cu->per_cu;
21559 gdb_assert ((*baton)->per_cu);
21561 (*baton)->size = 2 + cu_header->addr_size;
21562 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21563 (*baton)->data = data;
21565 data[0] = DW_OP_addr;
21566 store_unsigned_integer (&data[1], cu_header->addr_size,
21567 byte_order, DW_ADDR (attr));
21568 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21571 case DW_FORM_string:
21573 case DW_FORM_GNU_str_index:
21574 case DW_FORM_GNU_strp_alt:
21575 /* DW_STRING is already allocated on the objfile obstack, point
21577 *bytes = (const gdb_byte *) DW_STRING (attr);
21579 case DW_FORM_block1:
21580 case DW_FORM_block2:
21581 case DW_FORM_block4:
21582 case DW_FORM_block:
21583 case DW_FORM_exprloc:
21584 case DW_FORM_data16:
21585 blk = DW_BLOCK (attr);
21586 if (TYPE_LENGTH (type) != blk->size)
21587 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21588 TYPE_LENGTH (type));
21589 *bytes = blk->data;
21592 /* The DW_AT_const_value attributes are supposed to carry the
21593 symbol's value "represented as it would be on the target
21594 architecture." By the time we get here, it's already been
21595 converted to host endianness, so we just need to sign- or
21596 zero-extend it as appropriate. */
21597 case DW_FORM_data1:
21598 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21600 case DW_FORM_data2:
21601 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21603 case DW_FORM_data4:
21604 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21606 case DW_FORM_data8:
21607 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21610 case DW_FORM_sdata:
21611 case DW_FORM_implicit_const:
21612 *value = DW_SND (attr);
21615 case DW_FORM_udata:
21616 *value = DW_UNSND (attr);
21620 complaint (_("unsupported const value attribute form: '%s'"),
21621 dwarf_form_name (attr->form));
21628 /* Copy constant value from an attribute to a symbol. */
21631 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21632 struct dwarf2_cu *cu)
21634 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21636 const gdb_byte *bytes;
21637 struct dwarf2_locexpr_baton *baton;
21639 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21640 SYMBOL_PRINT_NAME (sym),
21641 &objfile->objfile_obstack, cu,
21642 &value, &bytes, &baton);
21646 SYMBOL_LOCATION_BATON (sym) = baton;
21647 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21649 else if (bytes != NULL)
21651 SYMBOL_VALUE_BYTES (sym) = bytes;
21652 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21656 SYMBOL_VALUE (sym) = value;
21657 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21661 /* Return the type of the die in question using its DW_AT_type attribute. */
21663 static struct type *
21664 die_type (struct die_info *die, struct dwarf2_cu *cu)
21666 struct attribute *type_attr;
21668 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21671 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21672 /* A missing DW_AT_type represents a void type. */
21673 return objfile_type (objfile)->builtin_void;
21676 return lookup_die_type (die, type_attr, cu);
21679 /* True iff CU's producer generates GNAT Ada auxiliary information
21680 that allows to find parallel types through that information instead
21681 of having to do expensive parallel lookups by type name. */
21684 need_gnat_info (struct dwarf2_cu *cu)
21686 /* Assume that the Ada compiler was GNAT, which always produces
21687 the auxiliary information. */
21688 return (cu->language == language_ada);
21691 /* Return the auxiliary type of the die in question using its
21692 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21693 attribute is not present. */
21695 static struct type *
21696 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21698 struct attribute *type_attr;
21700 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21704 return lookup_die_type (die, type_attr, cu);
21707 /* If DIE has a descriptive_type attribute, then set the TYPE's
21708 descriptive type accordingly. */
21711 set_descriptive_type (struct type *type, struct die_info *die,
21712 struct dwarf2_cu *cu)
21714 struct type *descriptive_type = die_descriptive_type (die, cu);
21716 if (descriptive_type)
21718 ALLOCATE_GNAT_AUX_TYPE (type);
21719 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21723 /* Return the containing type of the die in question using its
21724 DW_AT_containing_type attribute. */
21726 static struct type *
21727 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21729 struct attribute *type_attr;
21730 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21732 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21734 error (_("Dwarf Error: Problem turning containing type into gdb type "
21735 "[in module %s]"), objfile_name (objfile));
21737 return lookup_die_type (die, type_attr, cu);
21740 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21742 static struct type *
21743 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21745 struct dwarf2_per_objfile *dwarf2_per_objfile
21746 = cu->per_cu->dwarf2_per_objfile;
21747 struct objfile *objfile = dwarf2_per_objfile->objfile;
21748 char *message, *saved;
21750 message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
21751 objfile_name (objfile),
21752 sect_offset_str (cu->header.sect_off),
21753 sect_offset_str (die->sect_off));
21754 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21755 message, strlen (message));
21758 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21761 /* Look up the type of DIE in CU using its type attribute ATTR.
21762 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21763 DW_AT_containing_type.
21764 If there is no type substitute an error marker. */
21766 static struct type *
21767 lookup_die_type (struct die_info *die, const struct attribute *attr,
21768 struct dwarf2_cu *cu)
21770 struct dwarf2_per_objfile *dwarf2_per_objfile
21771 = cu->per_cu->dwarf2_per_objfile;
21772 struct objfile *objfile = dwarf2_per_objfile->objfile;
21773 struct type *this_type;
21775 gdb_assert (attr->name == DW_AT_type
21776 || attr->name == DW_AT_GNAT_descriptive_type
21777 || attr->name == DW_AT_containing_type);
21779 /* First see if we have it cached. */
21781 if (attr->form == DW_FORM_GNU_ref_alt)
21783 struct dwarf2_per_cu_data *per_cu;
21784 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21786 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21787 dwarf2_per_objfile);
21788 this_type = get_die_type_at_offset (sect_off, per_cu);
21790 else if (attr_form_is_ref (attr))
21792 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21794 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21796 else if (attr->form == DW_FORM_ref_sig8)
21798 ULONGEST signature = DW_SIGNATURE (attr);
21800 return get_signatured_type (die, signature, cu);
21804 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21805 " at %s [in module %s]"),
21806 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21807 objfile_name (objfile));
21808 return build_error_marker_type (cu, die);
21811 /* If not cached we need to read it in. */
21813 if (this_type == NULL)
21815 struct die_info *type_die = NULL;
21816 struct dwarf2_cu *type_cu = cu;
21818 if (attr_form_is_ref (attr))
21819 type_die = follow_die_ref (die, attr, &type_cu);
21820 if (type_die == NULL)
21821 return build_error_marker_type (cu, die);
21822 /* If we find the type now, it's probably because the type came
21823 from an inter-CU reference and the type's CU got expanded before
21825 this_type = read_type_die (type_die, type_cu);
21828 /* If we still don't have a type use an error marker. */
21830 if (this_type == NULL)
21831 return build_error_marker_type (cu, die);
21836 /* Return the type in DIE, CU.
21837 Returns NULL for invalid types.
21839 This first does a lookup in die_type_hash,
21840 and only reads the die in if necessary.
21842 NOTE: This can be called when reading in partial or full symbols. */
21844 static struct type *
21845 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21847 struct type *this_type;
21849 this_type = get_die_type (die, cu);
21853 return read_type_die_1 (die, cu);
21856 /* Read the type in DIE, CU.
21857 Returns NULL for invalid types. */
21859 static struct type *
21860 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21862 struct type *this_type = NULL;
21866 case DW_TAG_class_type:
21867 case DW_TAG_interface_type:
21868 case DW_TAG_structure_type:
21869 case DW_TAG_union_type:
21870 this_type = read_structure_type (die, cu);
21872 case DW_TAG_enumeration_type:
21873 this_type = read_enumeration_type (die, cu);
21875 case DW_TAG_subprogram:
21876 case DW_TAG_subroutine_type:
21877 case DW_TAG_inlined_subroutine:
21878 this_type = read_subroutine_type (die, cu);
21880 case DW_TAG_array_type:
21881 this_type = read_array_type (die, cu);
21883 case DW_TAG_set_type:
21884 this_type = read_set_type (die, cu);
21886 case DW_TAG_pointer_type:
21887 this_type = read_tag_pointer_type (die, cu);
21889 case DW_TAG_ptr_to_member_type:
21890 this_type = read_tag_ptr_to_member_type (die, cu);
21892 case DW_TAG_reference_type:
21893 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21895 case DW_TAG_rvalue_reference_type:
21896 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21898 case DW_TAG_const_type:
21899 this_type = read_tag_const_type (die, cu);
21901 case DW_TAG_volatile_type:
21902 this_type = read_tag_volatile_type (die, cu);
21904 case DW_TAG_restrict_type:
21905 this_type = read_tag_restrict_type (die, cu);
21907 case DW_TAG_string_type:
21908 this_type = read_tag_string_type (die, cu);
21910 case DW_TAG_typedef:
21911 this_type = read_typedef (die, cu);
21913 case DW_TAG_subrange_type:
21914 this_type = read_subrange_type (die, cu);
21916 case DW_TAG_base_type:
21917 this_type = read_base_type (die, cu);
21919 case DW_TAG_unspecified_type:
21920 this_type = read_unspecified_type (die, cu);
21922 case DW_TAG_namespace:
21923 this_type = read_namespace_type (die, cu);
21925 case DW_TAG_module:
21926 this_type = read_module_type (die, cu);
21928 case DW_TAG_atomic_type:
21929 this_type = read_tag_atomic_type (die, cu);
21932 complaint (_("unexpected tag in read_type_die: '%s'"),
21933 dwarf_tag_name (die->tag));
21940 /* See if we can figure out if the class lives in a namespace. We do
21941 this by looking for a member function; its demangled name will
21942 contain namespace info, if there is any.
21943 Return the computed name or NULL.
21944 Space for the result is allocated on the objfile's obstack.
21945 This is the full-die version of guess_partial_die_structure_name.
21946 In this case we know DIE has no useful parent. */
21949 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21951 struct die_info *spec_die;
21952 struct dwarf2_cu *spec_cu;
21953 struct die_info *child;
21954 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21957 spec_die = die_specification (die, &spec_cu);
21958 if (spec_die != NULL)
21964 for (child = die->child;
21966 child = child->sibling)
21968 if (child->tag == DW_TAG_subprogram)
21970 const char *linkage_name = dw2_linkage_name (child, cu);
21972 if (linkage_name != NULL)
21975 = language_class_name_from_physname (cu->language_defn,
21979 if (actual_name != NULL)
21981 const char *die_name = dwarf2_name (die, cu);
21983 if (die_name != NULL
21984 && strcmp (die_name, actual_name) != 0)
21986 /* Strip off the class name from the full name.
21987 We want the prefix. */
21988 int die_name_len = strlen (die_name);
21989 int actual_name_len = strlen (actual_name);
21991 /* Test for '::' as a sanity check. */
21992 if (actual_name_len > die_name_len + 2
21993 && actual_name[actual_name_len
21994 - die_name_len - 1] == ':')
21995 name = (char *) obstack_copy0 (
21996 &objfile->per_bfd->storage_obstack,
21997 actual_name, actual_name_len - die_name_len - 2);
22000 xfree (actual_name);
22009 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22010 prefix part in such case. See
22011 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22013 static const char *
22014 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22016 struct attribute *attr;
22019 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22020 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22023 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22026 attr = dw2_linkage_name_attr (die, cu);
22027 if (attr == NULL || DW_STRING (attr) == NULL)
22030 /* dwarf2_name had to be already called. */
22031 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22033 /* Strip the base name, keep any leading namespaces/classes. */
22034 base = strrchr (DW_STRING (attr), ':');
22035 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22038 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22039 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
22041 &base[-1] - DW_STRING (attr));
22044 /* Return the name of the namespace/class that DIE is defined within,
22045 or "" if we can't tell. The caller should not xfree the result.
22047 For example, if we're within the method foo() in the following
22057 then determine_prefix on foo's die will return "N::C". */
22059 static const char *
22060 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22062 struct dwarf2_per_objfile *dwarf2_per_objfile
22063 = cu->per_cu->dwarf2_per_objfile;
22064 struct die_info *parent, *spec_die;
22065 struct dwarf2_cu *spec_cu;
22066 struct type *parent_type;
22067 const char *retval;
22069 if (cu->language != language_cplus
22070 && cu->language != language_fortran && cu->language != language_d
22071 && cu->language != language_rust)
22074 retval = anonymous_struct_prefix (die, cu);
22078 /* We have to be careful in the presence of DW_AT_specification.
22079 For example, with GCC 3.4, given the code
22083 // Definition of N::foo.
22087 then we'll have a tree of DIEs like this:
22089 1: DW_TAG_compile_unit
22090 2: DW_TAG_namespace // N
22091 3: DW_TAG_subprogram // declaration of N::foo
22092 4: DW_TAG_subprogram // definition of N::foo
22093 DW_AT_specification // refers to die #3
22095 Thus, when processing die #4, we have to pretend that we're in
22096 the context of its DW_AT_specification, namely the contex of die
22099 spec_die = die_specification (die, &spec_cu);
22100 if (spec_die == NULL)
22101 parent = die->parent;
22104 parent = spec_die->parent;
22108 if (parent == NULL)
22110 else if (parent->building_fullname)
22113 const char *parent_name;
22115 /* It has been seen on RealView 2.2 built binaries,
22116 DW_TAG_template_type_param types actually _defined_ as
22117 children of the parent class:
22120 template class <class Enum> Class{};
22121 Class<enum E> class_e;
22123 1: DW_TAG_class_type (Class)
22124 2: DW_TAG_enumeration_type (E)
22125 3: DW_TAG_enumerator (enum1:0)
22126 3: DW_TAG_enumerator (enum2:1)
22128 2: DW_TAG_template_type_param
22129 DW_AT_type DW_FORM_ref_udata (E)
22131 Besides being broken debug info, it can put GDB into an
22132 infinite loop. Consider:
22134 When we're building the full name for Class<E>, we'll start
22135 at Class, and go look over its template type parameters,
22136 finding E. We'll then try to build the full name of E, and
22137 reach here. We're now trying to build the full name of E,
22138 and look over the parent DIE for containing scope. In the
22139 broken case, if we followed the parent DIE of E, we'd again
22140 find Class, and once again go look at its template type
22141 arguments, etc., etc. Simply don't consider such parent die
22142 as source-level parent of this die (it can't be, the language
22143 doesn't allow it), and break the loop here. */
22144 name = dwarf2_name (die, cu);
22145 parent_name = dwarf2_name (parent, cu);
22146 complaint (_("template param type '%s' defined within parent '%s'"),
22147 name ? name : "<unknown>",
22148 parent_name ? parent_name : "<unknown>");
22152 switch (parent->tag)
22154 case DW_TAG_namespace:
22155 parent_type = read_type_die (parent, cu);
22156 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22157 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22158 Work around this problem here. */
22159 if (cu->language == language_cplus
22160 && strcmp (TYPE_NAME (parent_type), "::") == 0)
22162 /* We give a name to even anonymous namespaces. */
22163 return TYPE_NAME (parent_type);
22164 case DW_TAG_class_type:
22165 case DW_TAG_interface_type:
22166 case DW_TAG_structure_type:
22167 case DW_TAG_union_type:
22168 case DW_TAG_module:
22169 parent_type = read_type_die (parent, cu);
22170 if (TYPE_NAME (parent_type) != NULL)
22171 return TYPE_NAME (parent_type);
22173 /* An anonymous structure is only allowed non-static data
22174 members; no typedefs, no member functions, et cetera.
22175 So it does not need a prefix. */
22177 case DW_TAG_compile_unit:
22178 case DW_TAG_partial_unit:
22179 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22180 if (cu->language == language_cplus
22181 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22182 && die->child != NULL
22183 && (die->tag == DW_TAG_class_type
22184 || die->tag == DW_TAG_structure_type
22185 || die->tag == DW_TAG_union_type))
22187 char *name = guess_full_die_structure_name (die, cu);
22192 case DW_TAG_enumeration_type:
22193 parent_type = read_type_die (parent, cu);
22194 if (TYPE_DECLARED_CLASS (parent_type))
22196 if (TYPE_NAME (parent_type) != NULL)
22197 return TYPE_NAME (parent_type);
22200 /* Fall through. */
22202 return determine_prefix (parent, cu);
22206 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22207 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22208 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22209 an obconcat, otherwise allocate storage for the result. The CU argument is
22210 used to determine the language and hence, the appropriate separator. */
22212 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22215 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22216 int physname, struct dwarf2_cu *cu)
22218 const char *lead = "";
22221 if (suffix == NULL || suffix[0] == '\0'
22222 || prefix == NULL || prefix[0] == '\0')
22224 else if (cu->language == language_d)
22226 /* For D, the 'main' function could be defined in any module, but it
22227 should never be prefixed. */
22228 if (strcmp (suffix, "D main") == 0)
22236 else if (cu->language == language_fortran && physname)
22238 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22239 DW_AT_MIPS_linkage_name is preferred and used instead. */
22247 if (prefix == NULL)
22249 if (suffix == NULL)
22256 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22258 strcpy (retval, lead);
22259 strcat (retval, prefix);
22260 strcat (retval, sep);
22261 strcat (retval, suffix);
22266 /* We have an obstack. */
22267 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22271 /* Return sibling of die, NULL if no sibling. */
22273 static struct die_info *
22274 sibling_die (struct die_info *die)
22276 return die->sibling;
22279 /* Get name of a die, return NULL if not found. */
22281 static const char *
22282 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22283 struct obstack *obstack)
22285 if (name && cu->language == language_cplus)
22287 std::string canon_name = cp_canonicalize_string (name);
22289 if (!canon_name.empty ())
22291 if (canon_name != name)
22292 name = (const char *) obstack_copy0 (obstack,
22293 canon_name.c_str (),
22294 canon_name.length ());
22301 /* Get name of a die, return NULL if not found.
22302 Anonymous namespaces are converted to their magic string. */
22304 static const char *
22305 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22307 struct attribute *attr;
22308 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22310 attr = dwarf2_attr (die, DW_AT_name, cu);
22311 if ((!attr || !DW_STRING (attr))
22312 && die->tag != DW_TAG_namespace
22313 && die->tag != DW_TAG_class_type
22314 && die->tag != DW_TAG_interface_type
22315 && die->tag != DW_TAG_structure_type
22316 && die->tag != DW_TAG_union_type)
22321 case DW_TAG_compile_unit:
22322 case DW_TAG_partial_unit:
22323 /* Compilation units have a DW_AT_name that is a filename, not
22324 a source language identifier. */
22325 case DW_TAG_enumeration_type:
22326 case DW_TAG_enumerator:
22327 /* These tags always have simple identifiers already; no need
22328 to canonicalize them. */
22329 return DW_STRING (attr);
22331 case DW_TAG_namespace:
22332 if (attr != NULL && DW_STRING (attr) != NULL)
22333 return DW_STRING (attr);
22334 return CP_ANONYMOUS_NAMESPACE_STR;
22336 case DW_TAG_class_type:
22337 case DW_TAG_interface_type:
22338 case DW_TAG_structure_type:
22339 case DW_TAG_union_type:
22340 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22341 structures or unions. These were of the form "._%d" in GCC 4.1,
22342 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22343 and GCC 4.4. We work around this problem by ignoring these. */
22344 if (attr && DW_STRING (attr)
22345 && (startswith (DW_STRING (attr), "._")
22346 || startswith (DW_STRING (attr), "<anonymous")))
22349 /* GCC might emit a nameless typedef that has a linkage name. See
22350 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22351 if (!attr || DW_STRING (attr) == NULL)
22353 char *demangled = NULL;
22355 attr = dw2_linkage_name_attr (die, cu);
22356 if (attr == NULL || DW_STRING (attr) == NULL)
22359 /* Avoid demangling DW_STRING (attr) the second time on a second
22360 call for the same DIE. */
22361 if (!DW_STRING_IS_CANONICAL (attr))
22362 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22368 /* FIXME: we already did this for the partial symbol... */
22371 obstack_copy0 (&objfile->per_bfd->storage_obstack,
22372 demangled, strlen (demangled)));
22373 DW_STRING_IS_CANONICAL (attr) = 1;
22376 /* Strip any leading namespaces/classes, keep only the base name.
22377 DW_AT_name for named DIEs does not contain the prefixes. */
22378 base = strrchr (DW_STRING (attr), ':');
22379 if (base && base > DW_STRING (attr) && base[-1] == ':')
22382 return DW_STRING (attr);
22391 if (!DW_STRING_IS_CANONICAL (attr))
22394 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22395 &objfile->per_bfd->storage_obstack);
22396 DW_STRING_IS_CANONICAL (attr) = 1;
22398 return DW_STRING (attr);
22401 /* Return the die that this die in an extension of, or NULL if there
22402 is none. *EXT_CU is the CU containing DIE on input, and the CU
22403 containing the return value on output. */
22405 static struct die_info *
22406 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22408 struct attribute *attr;
22410 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22414 return follow_die_ref (die, attr, ext_cu);
22417 /* Convert a DIE tag into its string name. */
22419 static const char *
22420 dwarf_tag_name (unsigned tag)
22422 const char *name = get_DW_TAG_name (tag);
22425 return "DW_TAG_<unknown>";
22430 /* Convert a DWARF attribute code into its string name. */
22432 static const char *
22433 dwarf_attr_name (unsigned attr)
22437 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22438 if (attr == DW_AT_MIPS_fde)
22439 return "DW_AT_MIPS_fde";
22441 if (attr == DW_AT_HP_block_index)
22442 return "DW_AT_HP_block_index";
22445 name = get_DW_AT_name (attr);
22448 return "DW_AT_<unknown>";
22453 /* Convert a DWARF value form code into its string name. */
22455 static const char *
22456 dwarf_form_name (unsigned form)
22458 const char *name = get_DW_FORM_name (form);
22461 return "DW_FORM_<unknown>";
22466 static const char *
22467 dwarf_bool_name (unsigned mybool)
22475 /* Convert a DWARF type code into its string name. */
22477 static const char *
22478 dwarf_type_encoding_name (unsigned enc)
22480 const char *name = get_DW_ATE_name (enc);
22483 return "DW_ATE_<unknown>";
22489 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22493 print_spaces (indent, f);
22494 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22495 dwarf_tag_name (die->tag), die->abbrev,
22496 sect_offset_str (die->sect_off));
22498 if (die->parent != NULL)
22500 print_spaces (indent, f);
22501 fprintf_unfiltered (f, " parent at offset: %s\n",
22502 sect_offset_str (die->parent->sect_off));
22505 print_spaces (indent, f);
22506 fprintf_unfiltered (f, " has children: %s\n",
22507 dwarf_bool_name (die->child != NULL));
22509 print_spaces (indent, f);
22510 fprintf_unfiltered (f, " attributes:\n");
22512 for (i = 0; i < die->num_attrs; ++i)
22514 print_spaces (indent, f);
22515 fprintf_unfiltered (f, " %s (%s) ",
22516 dwarf_attr_name (die->attrs[i].name),
22517 dwarf_form_name (die->attrs[i].form));
22519 switch (die->attrs[i].form)
22522 case DW_FORM_GNU_addr_index:
22523 fprintf_unfiltered (f, "address: ");
22524 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22526 case DW_FORM_block2:
22527 case DW_FORM_block4:
22528 case DW_FORM_block:
22529 case DW_FORM_block1:
22530 fprintf_unfiltered (f, "block: size %s",
22531 pulongest (DW_BLOCK (&die->attrs[i])->size));
22533 case DW_FORM_exprloc:
22534 fprintf_unfiltered (f, "expression: size %s",
22535 pulongest (DW_BLOCK (&die->attrs[i])->size));
22537 case DW_FORM_data16:
22538 fprintf_unfiltered (f, "constant of 16 bytes");
22540 case DW_FORM_ref_addr:
22541 fprintf_unfiltered (f, "ref address: ");
22542 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22544 case DW_FORM_GNU_ref_alt:
22545 fprintf_unfiltered (f, "alt ref address: ");
22546 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22552 case DW_FORM_ref_udata:
22553 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22554 (long) (DW_UNSND (&die->attrs[i])));
22556 case DW_FORM_data1:
22557 case DW_FORM_data2:
22558 case DW_FORM_data4:
22559 case DW_FORM_data8:
22560 case DW_FORM_udata:
22561 case DW_FORM_sdata:
22562 fprintf_unfiltered (f, "constant: %s",
22563 pulongest (DW_UNSND (&die->attrs[i])));
22565 case DW_FORM_sec_offset:
22566 fprintf_unfiltered (f, "section offset: %s",
22567 pulongest (DW_UNSND (&die->attrs[i])));
22569 case DW_FORM_ref_sig8:
22570 fprintf_unfiltered (f, "signature: %s",
22571 hex_string (DW_SIGNATURE (&die->attrs[i])));
22573 case DW_FORM_string:
22575 case DW_FORM_line_strp:
22576 case DW_FORM_GNU_str_index:
22577 case DW_FORM_GNU_strp_alt:
22578 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22579 DW_STRING (&die->attrs[i])
22580 ? DW_STRING (&die->attrs[i]) : "",
22581 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22584 if (DW_UNSND (&die->attrs[i]))
22585 fprintf_unfiltered (f, "flag: TRUE");
22587 fprintf_unfiltered (f, "flag: FALSE");
22589 case DW_FORM_flag_present:
22590 fprintf_unfiltered (f, "flag: TRUE");
22592 case DW_FORM_indirect:
22593 /* The reader will have reduced the indirect form to
22594 the "base form" so this form should not occur. */
22595 fprintf_unfiltered (f,
22596 "unexpected attribute form: DW_FORM_indirect");
22598 case DW_FORM_implicit_const:
22599 fprintf_unfiltered (f, "constant: %s",
22600 plongest (DW_SND (&die->attrs[i])));
22603 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22604 die->attrs[i].form);
22607 fprintf_unfiltered (f, "\n");
22612 dump_die_for_error (struct die_info *die)
22614 dump_die_shallow (gdb_stderr, 0, die);
22618 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22620 int indent = level * 4;
22622 gdb_assert (die != NULL);
22624 if (level >= max_level)
22627 dump_die_shallow (f, indent, die);
22629 if (die->child != NULL)
22631 print_spaces (indent, f);
22632 fprintf_unfiltered (f, " Children:");
22633 if (level + 1 < max_level)
22635 fprintf_unfiltered (f, "\n");
22636 dump_die_1 (f, level + 1, max_level, die->child);
22640 fprintf_unfiltered (f,
22641 " [not printed, max nesting level reached]\n");
22645 if (die->sibling != NULL && level > 0)
22647 dump_die_1 (f, level, max_level, die->sibling);
22651 /* This is called from the pdie macro in gdbinit.in.
22652 It's not static so gcc will keep a copy callable from gdb. */
22655 dump_die (struct die_info *die, int max_level)
22657 dump_die_1 (gdb_stdlog, 0, max_level, die);
22661 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22665 slot = htab_find_slot_with_hash (cu->die_hash, die,
22666 to_underlying (die->sect_off),
22672 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22676 dwarf2_get_ref_die_offset (const struct attribute *attr)
22678 if (attr_form_is_ref (attr))
22679 return (sect_offset) DW_UNSND (attr);
22681 complaint (_("unsupported die ref attribute form: '%s'"),
22682 dwarf_form_name (attr->form));
22686 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22687 * the value held by the attribute is not constant. */
22690 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22692 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22693 return DW_SND (attr);
22694 else if (attr->form == DW_FORM_udata
22695 || attr->form == DW_FORM_data1
22696 || attr->form == DW_FORM_data2
22697 || attr->form == DW_FORM_data4
22698 || attr->form == DW_FORM_data8)
22699 return DW_UNSND (attr);
22702 /* For DW_FORM_data16 see attr_form_is_constant. */
22703 complaint (_("Attribute value is not a constant (%s)"),
22704 dwarf_form_name (attr->form));
22705 return default_value;
22709 /* Follow reference or signature attribute ATTR of SRC_DIE.
22710 On entry *REF_CU is the CU of SRC_DIE.
22711 On exit *REF_CU is the CU of the result. */
22713 static struct die_info *
22714 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22715 struct dwarf2_cu **ref_cu)
22717 struct die_info *die;
22719 if (attr_form_is_ref (attr))
22720 die = follow_die_ref (src_die, attr, ref_cu);
22721 else if (attr->form == DW_FORM_ref_sig8)
22722 die = follow_die_sig (src_die, attr, ref_cu);
22725 dump_die_for_error (src_die);
22726 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22727 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22733 /* Follow reference OFFSET.
22734 On entry *REF_CU is the CU of the source die referencing OFFSET.
22735 On exit *REF_CU is the CU of the result.
22736 Returns NULL if OFFSET is invalid. */
22738 static struct die_info *
22739 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22740 struct dwarf2_cu **ref_cu)
22742 struct die_info temp_die;
22743 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22744 struct dwarf2_per_objfile *dwarf2_per_objfile
22745 = cu->per_cu->dwarf2_per_objfile;
22747 gdb_assert (cu->per_cu != NULL);
22751 if (cu->per_cu->is_debug_types)
22753 /* .debug_types CUs cannot reference anything outside their CU.
22754 If they need to, they have to reference a signatured type via
22755 DW_FORM_ref_sig8. */
22756 if (!offset_in_cu_p (&cu->header, sect_off))
22759 else if (offset_in_dwz != cu->per_cu->is_dwz
22760 || !offset_in_cu_p (&cu->header, sect_off))
22762 struct dwarf2_per_cu_data *per_cu;
22764 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22765 dwarf2_per_objfile);
22767 /* If necessary, add it to the queue and load its DIEs. */
22768 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22769 load_full_comp_unit (per_cu, false, cu->language);
22771 target_cu = per_cu->cu;
22773 else if (cu->dies == NULL)
22775 /* We're loading full DIEs during partial symbol reading. */
22776 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22777 load_full_comp_unit (cu->per_cu, false, language_minimal);
22780 *ref_cu = target_cu;
22781 temp_die.sect_off = sect_off;
22782 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22784 to_underlying (sect_off));
22787 /* Follow reference attribute ATTR of SRC_DIE.
22788 On entry *REF_CU is the CU of SRC_DIE.
22789 On exit *REF_CU is the CU of the result. */
22791 static struct die_info *
22792 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22793 struct dwarf2_cu **ref_cu)
22795 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22796 struct dwarf2_cu *cu = *ref_cu;
22797 struct die_info *die;
22799 die = follow_die_offset (sect_off,
22800 (attr->form == DW_FORM_GNU_ref_alt
22801 || cu->per_cu->is_dwz),
22804 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22805 "at %s [in module %s]"),
22806 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22807 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22812 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22813 Returned value is intended for DW_OP_call*. Returned
22814 dwarf2_locexpr_baton->data has lifetime of
22815 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
22817 struct dwarf2_locexpr_baton
22818 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22819 struct dwarf2_per_cu_data *per_cu,
22820 CORE_ADDR (*get_frame_pc) (void *baton),
22823 struct dwarf2_cu *cu;
22824 struct die_info *die;
22825 struct attribute *attr;
22826 struct dwarf2_locexpr_baton retval;
22827 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22828 struct objfile *objfile = dwarf2_per_objfile->objfile;
22830 if (per_cu->cu == NULL)
22831 load_cu (per_cu, false);
22835 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22836 Instead just throw an error, not much else we can do. */
22837 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22838 sect_offset_str (sect_off), objfile_name (objfile));
22841 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22843 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22844 sect_offset_str (sect_off), objfile_name (objfile));
22846 attr = dwarf2_attr (die, DW_AT_location, cu);
22849 /* DWARF: "If there is no such attribute, then there is no effect.".
22850 DATA is ignored if SIZE is 0. */
22852 retval.data = NULL;
22855 else if (attr_form_is_section_offset (attr))
22857 struct dwarf2_loclist_baton loclist_baton;
22858 CORE_ADDR pc = (*get_frame_pc) (baton);
22861 fill_in_loclist_baton (cu, &loclist_baton, attr);
22863 retval.data = dwarf2_find_location_expression (&loclist_baton,
22865 retval.size = size;
22869 if (!attr_form_is_block (attr))
22870 error (_("Dwarf Error: DIE at %s referenced in module %s "
22871 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22872 sect_offset_str (sect_off), objfile_name (objfile));
22874 retval.data = DW_BLOCK (attr)->data;
22875 retval.size = DW_BLOCK (attr)->size;
22877 retval.per_cu = cu->per_cu;
22879 age_cached_comp_units (dwarf2_per_objfile);
22884 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
22887 struct dwarf2_locexpr_baton
22888 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22889 struct dwarf2_per_cu_data *per_cu,
22890 CORE_ADDR (*get_frame_pc) (void *baton),
22893 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22895 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22898 /* Write a constant of a given type as target-ordered bytes into
22901 static const gdb_byte *
22902 write_constant_as_bytes (struct obstack *obstack,
22903 enum bfd_endian byte_order,
22910 *len = TYPE_LENGTH (type);
22911 result = (gdb_byte *) obstack_alloc (obstack, *len);
22912 store_unsigned_integer (result, *len, byte_order, value);
22917 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
22918 pointer to the constant bytes and set LEN to the length of the
22919 data. If memory is needed, allocate it on OBSTACK. If the DIE
22920 does not have a DW_AT_const_value, return NULL. */
22923 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22924 struct dwarf2_per_cu_data *per_cu,
22925 struct obstack *obstack,
22928 struct dwarf2_cu *cu;
22929 struct die_info *die;
22930 struct attribute *attr;
22931 const gdb_byte *result = NULL;
22934 enum bfd_endian byte_order;
22935 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22937 if (per_cu->cu == NULL)
22938 load_cu (per_cu, false);
22942 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22943 Instead just throw an error, not much else we can do. */
22944 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22945 sect_offset_str (sect_off), objfile_name (objfile));
22948 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22950 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22951 sect_offset_str (sect_off), objfile_name (objfile));
22953 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22957 byte_order = (bfd_big_endian (objfile->obfd)
22958 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22960 switch (attr->form)
22963 case DW_FORM_GNU_addr_index:
22967 *len = cu->header.addr_size;
22968 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22969 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22973 case DW_FORM_string:
22975 case DW_FORM_GNU_str_index:
22976 case DW_FORM_GNU_strp_alt:
22977 /* DW_STRING is already allocated on the objfile obstack, point
22979 result = (const gdb_byte *) DW_STRING (attr);
22980 *len = strlen (DW_STRING (attr));
22982 case DW_FORM_block1:
22983 case DW_FORM_block2:
22984 case DW_FORM_block4:
22985 case DW_FORM_block:
22986 case DW_FORM_exprloc:
22987 case DW_FORM_data16:
22988 result = DW_BLOCK (attr)->data;
22989 *len = DW_BLOCK (attr)->size;
22992 /* The DW_AT_const_value attributes are supposed to carry the
22993 symbol's value "represented as it would be on the target
22994 architecture." By the time we get here, it's already been
22995 converted to host endianness, so we just need to sign- or
22996 zero-extend it as appropriate. */
22997 case DW_FORM_data1:
22998 type = die_type (die, cu);
22999 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23000 if (result == NULL)
23001 result = write_constant_as_bytes (obstack, byte_order,
23004 case DW_FORM_data2:
23005 type = die_type (die, cu);
23006 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23007 if (result == NULL)
23008 result = write_constant_as_bytes (obstack, byte_order,
23011 case DW_FORM_data4:
23012 type = die_type (die, cu);
23013 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23014 if (result == NULL)
23015 result = write_constant_as_bytes (obstack, byte_order,
23018 case DW_FORM_data8:
23019 type = die_type (die, cu);
23020 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23021 if (result == NULL)
23022 result = write_constant_as_bytes (obstack, byte_order,
23026 case DW_FORM_sdata:
23027 case DW_FORM_implicit_const:
23028 type = die_type (die, cu);
23029 result = write_constant_as_bytes (obstack, byte_order,
23030 type, DW_SND (attr), len);
23033 case DW_FORM_udata:
23034 type = die_type (die, cu);
23035 result = write_constant_as_bytes (obstack, byte_order,
23036 type, DW_UNSND (attr), len);
23040 complaint (_("unsupported const value attribute form: '%s'"),
23041 dwarf_form_name (attr->form));
23048 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23049 valid type for this die is found. */
23052 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23053 struct dwarf2_per_cu_data *per_cu)
23055 struct dwarf2_cu *cu;
23056 struct die_info *die;
23058 if (per_cu->cu == NULL)
23059 load_cu (per_cu, false);
23064 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23068 return die_type (die, cu);
23071 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23075 dwarf2_get_die_type (cu_offset die_offset,
23076 struct dwarf2_per_cu_data *per_cu)
23078 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23079 return get_die_type_at_offset (die_offset_sect, per_cu);
23082 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23083 On entry *REF_CU is the CU of SRC_DIE.
23084 On exit *REF_CU is the CU of the result.
23085 Returns NULL if the referenced DIE isn't found. */
23087 static struct die_info *
23088 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23089 struct dwarf2_cu **ref_cu)
23091 struct die_info temp_die;
23092 struct dwarf2_cu *sig_cu;
23093 struct die_info *die;
23095 /* While it might be nice to assert sig_type->type == NULL here,
23096 we can get here for DW_AT_imported_declaration where we need
23097 the DIE not the type. */
23099 /* If necessary, add it to the queue and load its DIEs. */
23101 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23102 read_signatured_type (sig_type);
23104 sig_cu = sig_type->per_cu.cu;
23105 gdb_assert (sig_cu != NULL);
23106 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23107 temp_die.sect_off = sig_type->type_offset_in_section;
23108 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23109 to_underlying (temp_die.sect_off));
23112 struct dwarf2_per_objfile *dwarf2_per_objfile
23113 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23115 /* For .gdb_index version 7 keep track of included TUs.
23116 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23117 if (dwarf2_per_objfile->index_table != NULL
23118 && dwarf2_per_objfile->index_table->version <= 7)
23120 VEC_safe_push (dwarf2_per_cu_ptr,
23121 (*ref_cu)->per_cu->imported_symtabs,
23132 /* Follow signatured type referenced by ATTR in SRC_DIE.
23133 On entry *REF_CU is the CU of SRC_DIE.
23134 On exit *REF_CU is the CU of the result.
23135 The result is the DIE of the type.
23136 If the referenced type cannot be found an error is thrown. */
23138 static struct die_info *
23139 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23140 struct dwarf2_cu **ref_cu)
23142 ULONGEST signature = DW_SIGNATURE (attr);
23143 struct signatured_type *sig_type;
23144 struct die_info *die;
23146 gdb_assert (attr->form == DW_FORM_ref_sig8);
23148 sig_type = lookup_signatured_type (*ref_cu, signature);
23149 /* sig_type will be NULL if the signatured type is missing from
23151 if (sig_type == NULL)
23153 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23154 " from DIE at %s [in module %s]"),
23155 hex_string (signature), sect_offset_str (src_die->sect_off),
23156 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23159 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23162 dump_die_for_error (src_die);
23163 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23164 " from DIE at %s [in module %s]"),
23165 hex_string (signature), sect_offset_str (src_die->sect_off),
23166 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23172 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23173 reading in and processing the type unit if necessary. */
23175 static struct type *
23176 get_signatured_type (struct die_info *die, ULONGEST signature,
23177 struct dwarf2_cu *cu)
23179 struct dwarf2_per_objfile *dwarf2_per_objfile
23180 = cu->per_cu->dwarf2_per_objfile;
23181 struct signatured_type *sig_type;
23182 struct dwarf2_cu *type_cu;
23183 struct die_info *type_die;
23186 sig_type = lookup_signatured_type (cu, signature);
23187 /* sig_type will be NULL if the signatured type is missing from
23189 if (sig_type == NULL)
23191 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23192 " from DIE at %s [in module %s]"),
23193 hex_string (signature), sect_offset_str (die->sect_off),
23194 objfile_name (dwarf2_per_objfile->objfile));
23195 return build_error_marker_type (cu, die);
23198 /* If we already know the type we're done. */
23199 if (sig_type->type != NULL)
23200 return sig_type->type;
23203 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23204 if (type_die != NULL)
23206 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23207 is created. This is important, for example, because for c++ classes
23208 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23209 type = read_type_die (type_die, type_cu);
23212 complaint (_("Dwarf Error: Cannot build signatured type %s"
23213 " referenced from DIE at %s [in module %s]"),
23214 hex_string (signature), sect_offset_str (die->sect_off),
23215 objfile_name (dwarf2_per_objfile->objfile));
23216 type = build_error_marker_type (cu, die);
23221 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23222 " from DIE at %s [in module %s]"),
23223 hex_string (signature), sect_offset_str (die->sect_off),
23224 objfile_name (dwarf2_per_objfile->objfile));
23225 type = build_error_marker_type (cu, die);
23227 sig_type->type = type;
23232 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23233 reading in and processing the type unit if necessary. */
23235 static struct type *
23236 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23237 struct dwarf2_cu *cu) /* ARI: editCase function */
23239 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23240 if (attr_form_is_ref (attr))
23242 struct dwarf2_cu *type_cu = cu;
23243 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23245 return read_type_die (type_die, type_cu);
23247 else if (attr->form == DW_FORM_ref_sig8)
23249 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23253 struct dwarf2_per_objfile *dwarf2_per_objfile
23254 = cu->per_cu->dwarf2_per_objfile;
23256 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23257 " at %s [in module %s]"),
23258 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23259 objfile_name (dwarf2_per_objfile->objfile));
23260 return build_error_marker_type (cu, die);
23264 /* Load the DIEs associated with type unit PER_CU into memory. */
23267 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23269 struct signatured_type *sig_type;
23271 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23272 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23274 /* We have the per_cu, but we need the signatured_type.
23275 Fortunately this is an easy translation. */
23276 gdb_assert (per_cu->is_debug_types);
23277 sig_type = (struct signatured_type *) per_cu;
23279 gdb_assert (per_cu->cu == NULL);
23281 read_signatured_type (sig_type);
23283 gdb_assert (per_cu->cu != NULL);
23286 /* die_reader_func for read_signatured_type.
23287 This is identical to load_full_comp_unit_reader,
23288 but is kept separate for now. */
23291 read_signatured_type_reader (const struct die_reader_specs *reader,
23292 const gdb_byte *info_ptr,
23293 struct die_info *comp_unit_die,
23297 struct dwarf2_cu *cu = reader->cu;
23299 gdb_assert (cu->die_hash == NULL);
23301 htab_create_alloc_ex (cu->header.length / 12,
23305 &cu->comp_unit_obstack,
23306 hashtab_obstack_allocate,
23307 dummy_obstack_deallocate);
23310 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23311 &info_ptr, comp_unit_die);
23312 cu->dies = comp_unit_die;
23313 /* comp_unit_die is not stored in die_hash, no need. */
23315 /* We try not to read any attributes in this function, because not
23316 all CUs needed for references have been loaded yet, and symbol
23317 table processing isn't initialized. But we have to set the CU language,
23318 or we won't be able to build types correctly.
23319 Similarly, if we do not read the producer, we can not apply
23320 producer-specific interpretation. */
23321 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23324 /* Read in a signatured type and build its CU and DIEs.
23325 If the type is a stub for the real type in a DWO file,
23326 read in the real type from the DWO file as well. */
23329 read_signatured_type (struct signatured_type *sig_type)
23331 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23333 gdb_assert (per_cu->is_debug_types);
23334 gdb_assert (per_cu->cu == NULL);
23336 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
23337 read_signatured_type_reader, NULL);
23338 sig_type->per_cu.tu_read = 1;
23341 /* Decode simple location descriptions.
23342 Given a pointer to a dwarf block that defines a location, compute
23343 the location and return the value.
23345 NOTE drow/2003-11-18: This function is called in two situations
23346 now: for the address of static or global variables (partial symbols
23347 only) and for offsets into structures which are expected to be
23348 (more or less) constant. The partial symbol case should go away,
23349 and only the constant case should remain. That will let this
23350 function complain more accurately. A few special modes are allowed
23351 without complaint for global variables (for instance, global
23352 register values and thread-local values).
23354 A location description containing no operations indicates that the
23355 object is optimized out. The return value is 0 for that case.
23356 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23357 callers will only want a very basic result and this can become a
23360 Note that stack[0] is unused except as a default error return. */
23363 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23365 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23367 size_t size = blk->size;
23368 const gdb_byte *data = blk->data;
23369 CORE_ADDR stack[64];
23371 unsigned int bytes_read, unsnd;
23377 stack[++stacki] = 0;
23416 stack[++stacki] = op - DW_OP_lit0;
23451 stack[++stacki] = op - DW_OP_reg0;
23453 dwarf2_complex_location_expr_complaint ();
23457 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23459 stack[++stacki] = unsnd;
23461 dwarf2_complex_location_expr_complaint ();
23465 stack[++stacki] = read_address (objfile->obfd, &data[i],
23470 case DW_OP_const1u:
23471 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23475 case DW_OP_const1s:
23476 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23480 case DW_OP_const2u:
23481 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23485 case DW_OP_const2s:
23486 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23490 case DW_OP_const4u:
23491 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23495 case DW_OP_const4s:
23496 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23500 case DW_OP_const8u:
23501 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23506 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23512 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23517 stack[stacki + 1] = stack[stacki];
23522 stack[stacki - 1] += stack[stacki];
23526 case DW_OP_plus_uconst:
23527 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23533 stack[stacki - 1] -= stack[stacki];
23538 /* If we're not the last op, then we definitely can't encode
23539 this using GDB's address_class enum. This is valid for partial
23540 global symbols, although the variable's address will be bogus
23543 dwarf2_complex_location_expr_complaint ();
23546 case DW_OP_GNU_push_tls_address:
23547 case DW_OP_form_tls_address:
23548 /* The top of the stack has the offset from the beginning
23549 of the thread control block at which the variable is located. */
23550 /* Nothing should follow this operator, so the top of stack would
23552 /* This is valid for partial global symbols, but the variable's
23553 address will be bogus in the psymtab. Make it always at least
23554 non-zero to not look as a variable garbage collected by linker
23555 which have DW_OP_addr 0. */
23557 dwarf2_complex_location_expr_complaint ();
23561 case DW_OP_GNU_uninit:
23564 case DW_OP_GNU_addr_index:
23565 case DW_OP_GNU_const_index:
23566 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23573 const char *name = get_DW_OP_name (op);
23576 complaint (_("unsupported stack op: '%s'"),
23579 complaint (_("unsupported stack op: '%02x'"),
23583 return (stack[stacki]);
23586 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23587 outside of the allocated space. Also enforce minimum>0. */
23588 if (stacki >= ARRAY_SIZE (stack) - 1)
23590 complaint (_("location description stack overflow"));
23596 complaint (_("location description stack underflow"));
23600 return (stack[stacki]);
23603 /* memory allocation interface */
23605 static struct dwarf_block *
23606 dwarf_alloc_block (struct dwarf2_cu *cu)
23608 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23611 static struct die_info *
23612 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23614 struct die_info *die;
23615 size_t size = sizeof (struct die_info);
23618 size += (num_attrs - 1) * sizeof (struct attribute);
23620 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23621 memset (die, 0, sizeof (struct die_info));
23626 /* Macro support. */
23628 /* Return file name relative to the compilation directory of file number I in
23629 *LH's file name table. The result is allocated using xmalloc; the caller is
23630 responsible for freeing it. */
23633 file_file_name (int file, struct line_header *lh)
23635 /* Is the file number a valid index into the line header's file name
23636 table? Remember that file numbers start with one, not zero. */
23637 if (1 <= file && file <= lh->file_names.size ())
23639 const file_entry &fe = lh->file_names[file - 1];
23641 if (!IS_ABSOLUTE_PATH (fe.name))
23643 const char *dir = fe.include_dir (lh);
23645 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23647 return xstrdup (fe.name);
23651 /* The compiler produced a bogus file number. We can at least
23652 record the macro definitions made in the file, even if we
23653 won't be able to find the file by name. */
23654 char fake_name[80];
23656 xsnprintf (fake_name, sizeof (fake_name),
23657 "<bad macro file number %d>", file);
23659 complaint (_("bad file number in macro information (%d)"),
23662 return xstrdup (fake_name);
23666 /* Return the full name of file number I in *LH's file name table.
23667 Use COMP_DIR as the name of the current directory of the
23668 compilation. The result is allocated using xmalloc; the caller is
23669 responsible for freeing it. */
23671 file_full_name (int file, struct line_header *lh, const char *comp_dir)
23673 /* Is the file number a valid index into the line header's file name
23674 table? Remember that file numbers start with one, not zero. */
23675 if (1 <= file && file <= lh->file_names.size ())
23677 char *relative = file_file_name (file, lh);
23679 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23681 return reconcat (relative, comp_dir, SLASH_STRING,
23682 relative, (char *) NULL);
23685 return file_file_name (file, lh);
23689 static struct macro_source_file *
23690 macro_start_file (int file, int line,
23691 struct macro_source_file *current_file,
23692 struct line_header *lh)
23694 /* File name relative to the compilation directory of this source file. */
23695 char *file_name = file_file_name (file, lh);
23697 if (! current_file)
23699 /* Note: We don't create a macro table for this compilation unit
23700 at all until we actually get a filename. */
23701 struct macro_table *macro_table = get_macro_table ();
23703 /* If we have no current file, then this must be the start_file
23704 directive for the compilation unit's main source file. */
23705 current_file = macro_set_main (macro_table, file_name);
23706 macro_define_special (macro_table);
23709 current_file = macro_include (current_file, line, file_name);
23713 return current_file;
23716 static const char *
23717 consume_improper_spaces (const char *p, const char *body)
23721 complaint (_("macro definition contains spaces "
23722 "in formal argument list:\n`%s'"),
23734 parse_macro_definition (struct macro_source_file *file, int line,
23739 /* The body string takes one of two forms. For object-like macro
23740 definitions, it should be:
23742 <macro name> " " <definition>
23744 For function-like macro definitions, it should be:
23746 <macro name> "() " <definition>
23748 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23750 Spaces may appear only where explicitly indicated, and in the
23753 The Dwarf 2 spec says that an object-like macro's name is always
23754 followed by a space, but versions of GCC around March 2002 omit
23755 the space when the macro's definition is the empty string.
23757 The Dwarf 2 spec says that there should be no spaces between the
23758 formal arguments in a function-like macro's formal argument list,
23759 but versions of GCC around March 2002 include spaces after the
23763 /* Find the extent of the macro name. The macro name is terminated
23764 by either a space or null character (for an object-like macro) or
23765 an opening paren (for a function-like macro). */
23766 for (p = body; *p; p++)
23767 if (*p == ' ' || *p == '(')
23770 if (*p == ' ' || *p == '\0')
23772 /* It's an object-like macro. */
23773 int name_len = p - body;
23774 char *name = savestring (body, name_len);
23775 const char *replacement;
23778 replacement = body + name_len + 1;
23781 dwarf2_macro_malformed_definition_complaint (body);
23782 replacement = body + name_len;
23785 macro_define_object (file, line, name, replacement);
23789 else if (*p == '(')
23791 /* It's a function-like macro. */
23792 char *name = savestring (body, p - body);
23795 char **argv = XNEWVEC (char *, argv_size);
23799 p = consume_improper_spaces (p, body);
23801 /* Parse the formal argument list. */
23802 while (*p && *p != ')')
23804 /* Find the extent of the current argument name. */
23805 const char *arg_start = p;
23807 while (*p && *p != ',' && *p != ')' && *p != ' ')
23810 if (! *p || p == arg_start)
23811 dwarf2_macro_malformed_definition_complaint (body);
23814 /* Make sure argv has room for the new argument. */
23815 if (argc >= argv_size)
23818 argv = XRESIZEVEC (char *, argv, argv_size);
23821 argv[argc++] = savestring (arg_start, p - arg_start);
23824 p = consume_improper_spaces (p, body);
23826 /* Consume the comma, if present. */
23831 p = consume_improper_spaces (p, body);
23840 /* Perfectly formed definition, no complaints. */
23841 macro_define_function (file, line, name,
23842 argc, (const char **) argv,
23844 else if (*p == '\0')
23846 /* Complain, but do define it. */
23847 dwarf2_macro_malformed_definition_complaint (body);
23848 macro_define_function (file, line, name,
23849 argc, (const char **) argv,
23853 /* Just complain. */
23854 dwarf2_macro_malformed_definition_complaint (body);
23857 /* Just complain. */
23858 dwarf2_macro_malformed_definition_complaint (body);
23864 for (i = 0; i < argc; i++)
23870 dwarf2_macro_malformed_definition_complaint (body);
23873 /* Skip some bytes from BYTES according to the form given in FORM.
23874 Returns the new pointer. */
23876 static const gdb_byte *
23877 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
23878 enum dwarf_form form,
23879 unsigned int offset_size,
23880 struct dwarf2_section_info *section)
23882 unsigned int bytes_read;
23886 case DW_FORM_data1:
23891 case DW_FORM_data2:
23895 case DW_FORM_data4:
23899 case DW_FORM_data8:
23903 case DW_FORM_data16:
23907 case DW_FORM_string:
23908 read_direct_string (abfd, bytes, &bytes_read);
23909 bytes += bytes_read;
23912 case DW_FORM_sec_offset:
23914 case DW_FORM_GNU_strp_alt:
23915 bytes += offset_size;
23918 case DW_FORM_block:
23919 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23920 bytes += bytes_read;
23923 case DW_FORM_block1:
23924 bytes += 1 + read_1_byte (abfd, bytes);
23926 case DW_FORM_block2:
23927 bytes += 2 + read_2_bytes (abfd, bytes);
23929 case DW_FORM_block4:
23930 bytes += 4 + read_4_bytes (abfd, bytes);
23933 case DW_FORM_sdata:
23934 case DW_FORM_udata:
23935 case DW_FORM_GNU_addr_index:
23936 case DW_FORM_GNU_str_index:
23937 bytes = gdb_skip_leb128 (bytes, buffer_end);
23940 dwarf2_section_buffer_overflow_complaint (section);
23945 case DW_FORM_implicit_const:
23950 complaint (_("invalid form 0x%x in `%s'"),
23951 form, get_section_name (section));
23959 /* A helper for dwarf_decode_macros that handles skipping an unknown
23960 opcode. Returns an updated pointer to the macro data buffer; or,
23961 on error, issues a complaint and returns NULL. */
23963 static const gdb_byte *
23964 skip_unknown_opcode (unsigned int opcode,
23965 const gdb_byte **opcode_definitions,
23966 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
23968 unsigned int offset_size,
23969 struct dwarf2_section_info *section)
23971 unsigned int bytes_read, i;
23973 const gdb_byte *defn;
23975 if (opcode_definitions[opcode] == NULL)
23977 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
23982 defn = opcode_definitions[opcode];
23983 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
23984 defn += bytes_read;
23986 for (i = 0; i < arg; ++i)
23988 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
23989 (enum dwarf_form) defn[i], offset_size,
23991 if (mac_ptr == NULL)
23993 /* skip_form_bytes already issued the complaint. */
24001 /* A helper function which parses the header of a macro section.
24002 If the macro section is the extended (for now called "GNU") type,
24003 then this updates *OFFSET_SIZE. Returns a pointer to just after
24004 the header, or issues a complaint and returns NULL on error. */
24006 static const gdb_byte *
24007 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24009 const gdb_byte *mac_ptr,
24010 unsigned int *offset_size,
24011 int section_is_gnu)
24013 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24015 if (section_is_gnu)
24017 unsigned int version, flags;
24019 version = read_2_bytes (abfd, mac_ptr);
24020 if (version != 4 && version != 5)
24022 complaint (_("unrecognized version `%d' in .debug_macro section"),
24028 flags = read_1_byte (abfd, mac_ptr);
24030 *offset_size = (flags & 1) ? 8 : 4;
24032 if ((flags & 2) != 0)
24033 /* We don't need the line table offset. */
24034 mac_ptr += *offset_size;
24036 /* Vendor opcode descriptions. */
24037 if ((flags & 4) != 0)
24039 unsigned int i, count;
24041 count = read_1_byte (abfd, mac_ptr);
24043 for (i = 0; i < count; ++i)
24045 unsigned int opcode, bytes_read;
24048 opcode = read_1_byte (abfd, mac_ptr);
24050 opcode_definitions[opcode] = mac_ptr;
24051 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24052 mac_ptr += bytes_read;
24061 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24062 including DW_MACRO_import. */
24065 dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24067 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24068 struct macro_source_file *current_file,
24069 struct line_header *lh,
24070 struct dwarf2_section_info *section,
24071 int section_is_gnu, int section_is_dwz,
24072 unsigned int offset_size,
24073 htab_t include_hash)
24075 struct objfile *objfile = dwarf2_per_objfile->objfile;
24076 enum dwarf_macro_record_type macinfo_type;
24077 int at_commandline;
24078 const gdb_byte *opcode_definitions[256];
24080 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24081 &offset_size, section_is_gnu);
24082 if (mac_ptr == NULL)
24084 /* We already issued a complaint. */
24088 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24089 GDB is still reading the definitions from command line. First
24090 DW_MACINFO_start_file will need to be ignored as it was already executed
24091 to create CURRENT_FILE for the main source holding also the command line
24092 definitions. On first met DW_MACINFO_start_file this flag is reset to
24093 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24095 at_commandline = 1;
24099 /* Do we at least have room for a macinfo type byte? */
24100 if (mac_ptr >= mac_end)
24102 dwarf2_section_buffer_overflow_complaint (section);
24106 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24109 /* Note that we rely on the fact that the corresponding GNU and
24110 DWARF constants are the same. */
24112 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24113 switch (macinfo_type)
24115 /* A zero macinfo type indicates the end of the macro
24120 case DW_MACRO_define:
24121 case DW_MACRO_undef:
24122 case DW_MACRO_define_strp:
24123 case DW_MACRO_undef_strp:
24124 case DW_MACRO_define_sup:
24125 case DW_MACRO_undef_sup:
24127 unsigned int bytes_read;
24132 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24133 mac_ptr += bytes_read;
24135 if (macinfo_type == DW_MACRO_define
24136 || macinfo_type == DW_MACRO_undef)
24138 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24139 mac_ptr += bytes_read;
24143 LONGEST str_offset;
24145 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24146 mac_ptr += offset_size;
24148 if (macinfo_type == DW_MACRO_define_sup
24149 || macinfo_type == DW_MACRO_undef_sup
24152 struct dwz_file *dwz
24153 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24155 body = read_indirect_string_from_dwz (objfile,
24159 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24163 is_define = (macinfo_type == DW_MACRO_define
24164 || macinfo_type == DW_MACRO_define_strp
24165 || macinfo_type == DW_MACRO_define_sup);
24166 if (! current_file)
24168 /* DWARF violation as no main source is present. */
24169 complaint (_("debug info with no main source gives macro %s "
24171 is_define ? _("definition") : _("undefinition"),
24175 if ((line == 0 && !at_commandline)
24176 || (line != 0 && at_commandline))
24177 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
24178 at_commandline ? _("command-line") : _("in-file"),
24179 is_define ? _("definition") : _("undefinition"),
24180 line == 0 ? _("zero") : _("non-zero"), line, body);
24183 parse_macro_definition (current_file, line, body);
24186 gdb_assert (macinfo_type == DW_MACRO_undef
24187 || macinfo_type == DW_MACRO_undef_strp
24188 || macinfo_type == DW_MACRO_undef_sup);
24189 macro_undef (current_file, line, body);
24194 case DW_MACRO_start_file:
24196 unsigned int bytes_read;
24199 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24200 mac_ptr += bytes_read;
24201 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24202 mac_ptr += bytes_read;
24204 if ((line == 0 && !at_commandline)
24205 || (line != 0 && at_commandline))
24206 complaint (_("debug info gives source %d included "
24207 "from %s at %s line %d"),
24208 file, at_commandline ? _("command-line") : _("file"),
24209 line == 0 ? _("zero") : _("non-zero"), line);
24211 if (at_commandline)
24213 /* This DW_MACRO_start_file was executed in the
24215 at_commandline = 0;
24218 current_file = macro_start_file (file, line, current_file, lh);
24222 case DW_MACRO_end_file:
24223 if (! current_file)
24224 complaint (_("macro debug info has an unmatched "
24225 "`close_file' directive"));
24228 current_file = current_file->included_by;
24229 if (! current_file)
24231 enum dwarf_macro_record_type next_type;
24233 /* GCC circa March 2002 doesn't produce the zero
24234 type byte marking the end of the compilation
24235 unit. Complain if it's not there, but exit no
24238 /* Do we at least have room for a macinfo type byte? */
24239 if (mac_ptr >= mac_end)
24241 dwarf2_section_buffer_overflow_complaint (section);
24245 /* We don't increment mac_ptr here, so this is just
24248 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24250 if (next_type != 0)
24251 complaint (_("no terminating 0-type entry for "
24252 "macros in `.debug_macinfo' section"));
24259 case DW_MACRO_import:
24260 case DW_MACRO_import_sup:
24264 bfd *include_bfd = abfd;
24265 struct dwarf2_section_info *include_section = section;
24266 const gdb_byte *include_mac_end = mac_end;
24267 int is_dwz = section_is_dwz;
24268 const gdb_byte *new_mac_ptr;
24270 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24271 mac_ptr += offset_size;
24273 if (macinfo_type == DW_MACRO_import_sup)
24275 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24277 dwarf2_read_section (objfile, &dwz->macro);
24279 include_section = &dwz->macro;
24280 include_bfd = get_section_bfd_owner (include_section);
24281 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24285 new_mac_ptr = include_section->buffer + offset;
24286 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24290 /* This has actually happened; see
24291 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24292 complaint (_("recursive DW_MACRO_import in "
24293 ".debug_macro section"));
24297 *slot = (void *) new_mac_ptr;
24299 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24300 include_bfd, new_mac_ptr,
24301 include_mac_end, current_file, lh,
24302 section, section_is_gnu, is_dwz,
24303 offset_size, include_hash);
24305 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24310 case DW_MACINFO_vendor_ext:
24311 if (!section_is_gnu)
24313 unsigned int bytes_read;
24315 /* This reads the constant, but since we don't recognize
24316 any vendor extensions, we ignore it. */
24317 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24318 mac_ptr += bytes_read;
24319 read_direct_string (abfd, mac_ptr, &bytes_read);
24320 mac_ptr += bytes_read;
24322 /* We don't recognize any vendor extensions. */
24328 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24329 mac_ptr, mac_end, abfd, offset_size,
24331 if (mac_ptr == NULL)
24336 } while (macinfo_type != 0);
24340 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24341 int section_is_gnu)
24343 struct dwarf2_per_objfile *dwarf2_per_objfile
24344 = cu->per_cu->dwarf2_per_objfile;
24345 struct objfile *objfile = dwarf2_per_objfile->objfile;
24346 struct line_header *lh = cu->line_header;
24348 const gdb_byte *mac_ptr, *mac_end;
24349 struct macro_source_file *current_file = 0;
24350 enum dwarf_macro_record_type macinfo_type;
24351 unsigned int offset_size = cu->header.offset_size;
24352 const gdb_byte *opcode_definitions[256];
24354 struct dwarf2_section_info *section;
24355 const char *section_name;
24357 if (cu->dwo_unit != NULL)
24359 if (section_is_gnu)
24361 section = &cu->dwo_unit->dwo_file->sections.macro;
24362 section_name = ".debug_macro.dwo";
24366 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24367 section_name = ".debug_macinfo.dwo";
24372 if (section_is_gnu)
24374 section = &dwarf2_per_objfile->macro;
24375 section_name = ".debug_macro";
24379 section = &dwarf2_per_objfile->macinfo;
24380 section_name = ".debug_macinfo";
24384 dwarf2_read_section (objfile, section);
24385 if (section->buffer == NULL)
24387 complaint (_("missing %s section"), section_name);
24390 abfd = get_section_bfd_owner (section);
24392 /* First pass: Find the name of the base filename.
24393 This filename is needed in order to process all macros whose definition
24394 (or undefinition) comes from the command line. These macros are defined
24395 before the first DW_MACINFO_start_file entry, and yet still need to be
24396 associated to the base file.
24398 To determine the base file name, we scan the macro definitions until we
24399 reach the first DW_MACINFO_start_file entry. We then initialize
24400 CURRENT_FILE accordingly so that any macro definition found before the
24401 first DW_MACINFO_start_file can still be associated to the base file. */
24403 mac_ptr = section->buffer + offset;
24404 mac_end = section->buffer + section->size;
24406 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24407 &offset_size, section_is_gnu);
24408 if (mac_ptr == NULL)
24410 /* We already issued a complaint. */
24416 /* Do we at least have room for a macinfo type byte? */
24417 if (mac_ptr >= mac_end)
24419 /* Complaint is printed during the second pass as GDB will probably
24420 stop the first pass earlier upon finding
24421 DW_MACINFO_start_file. */
24425 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24428 /* Note that we rely on the fact that the corresponding GNU and
24429 DWARF constants are the same. */
24431 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24432 switch (macinfo_type)
24434 /* A zero macinfo type indicates the end of the macro
24439 case DW_MACRO_define:
24440 case DW_MACRO_undef:
24441 /* Only skip the data by MAC_PTR. */
24443 unsigned int bytes_read;
24445 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24446 mac_ptr += bytes_read;
24447 read_direct_string (abfd, mac_ptr, &bytes_read);
24448 mac_ptr += bytes_read;
24452 case DW_MACRO_start_file:
24454 unsigned int bytes_read;
24457 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24458 mac_ptr += bytes_read;
24459 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24460 mac_ptr += bytes_read;
24462 current_file = macro_start_file (file, line, current_file, lh);
24466 case DW_MACRO_end_file:
24467 /* No data to skip by MAC_PTR. */
24470 case DW_MACRO_define_strp:
24471 case DW_MACRO_undef_strp:
24472 case DW_MACRO_define_sup:
24473 case DW_MACRO_undef_sup:
24475 unsigned int bytes_read;
24477 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24478 mac_ptr += bytes_read;
24479 mac_ptr += offset_size;
24483 case DW_MACRO_import:
24484 case DW_MACRO_import_sup:
24485 /* Note that, according to the spec, a transparent include
24486 chain cannot call DW_MACRO_start_file. So, we can just
24487 skip this opcode. */
24488 mac_ptr += offset_size;
24491 case DW_MACINFO_vendor_ext:
24492 /* Only skip the data by MAC_PTR. */
24493 if (!section_is_gnu)
24495 unsigned int bytes_read;
24497 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24498 mac_ptr += bytes_read;
24499 read_direct_string (abfd, mac_ptr, &bytes_read);
24500 mac_ptr += bytes_read;
24505 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24506 mac_ptr, mac_end, abfd, offset_size,
24508 if (mac_ptr == NULL)
24513 } while (macinfo_type != 0 && current_file == NULL);
24515 /* Second pass: Process all entries.
24517 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24518 command-line macro definitions/undefinitions. This flag is unset when we
24519 reach the first DW_MACINFO_start_file entry. */
24521 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24523 NULL, xcalloc, xfree));
24524 mac_ptr = section->buffer + offset;
24525 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24526 *slot = (void *) mac_ptr;
24527 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24528 abfd, mac_ptr, mac_end,
24529 current_file, lh, section,
24530 section_is_gnu, 0, offset_size,
24531 include_hash.get ());
24534 /* Check if the attribute's form is a DW_FORM_block*
24535 if so return true else false. */
24538 attr_form_is_block (const struct attribute *attr)
24540 return (attr == NULL ? 0 :
24541 attr->form == DW_FORM_block1
24542 || attr->form == DW_FORM_block2
24543 || attr->form == DW_FORM_block4
24544 || attr->form == DW_FORM_block
24545 || attr->form == DW_FORM_exprloc);
24548 /* Return non-zero if ATTR's value is a section offset --- classes
24549 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24550 You may use DW_UNSND (attr) to retrieve such offsets.
24552 Section 7.5.4, "Attribute Encodings", explains that no attribute
24553 may have a value that belongs to more than one of these classes; it
24554 would be ambiguous if we did, because we use the same forms for all
24558 attr_form_is_section_offset (const struct attribute *attr)
24560 return (attr->form == DW_FORM_data4
24561 || attr->form == DW_FORM_data8
24562 || attr->form == DW_FORM_sec_offset);
24565 /* Return non-zero if ATTR's value falls in the 'constant' class, or
24566 zero otherwise. When this function returns true, you can apply
24567 dwarf2_get_attr_constant_value to it.
24569 However, note that for some attributes you must check
24570 attr_form_is_section_offset before using this test. DW_FORM_data4
24571 and DW_FORM_data8 are members of both the constant class, and of
24572 the classes that contain offsets into other debug sections
24573 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24574 that, if an attribute's can be either a constant or one of the
24575 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
24576 taken as section offsets, not constants.
24578 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24579 cannot handle that. */
24582 attr_form_is_constant (const struct attribute *attr)
24584 switch (attr->form)
24586 case DW_FORM_sdata:
24587 case DW_FORM_udata:
24588 case DW_FORM_data1:
24589 case DW_FORM_data2:
24590 case DW_FORM_data4:
24591 case DW_FORM_data8:
24592 case DW_FORM_implicit_const:
24600 /* DW_ADDR is always stored already as sect_offset; despite for the forms
24601 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24604 attr_form_is_ref (const struct attribute *attr)
24606 switch (attr->form)
24608 case DW_FORM_ref_addr:
24613 case DW_FORM_ref_udata:
24614 case DW_FORM_GNU_ref_alt:
24621 /* Return the .debug_loc section to use for CU.
24622 For DWO files use .debug_loc.dwo. */
24624 static struct dwarf2_section_info *
24625 cu_debug_loc_section (struct dwarf2_cu *cu)
24627 struct dwarf2_per_objfile *dwarf2_per_objfile
24628 = cu->per_cu->dwarf2_per_objfile;
24632 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24634 return cu->header.version >= 5 ? §ions->loclists : §ions->loc;
24636 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24637 : &dwarf2_per_objfile->loc);
24640 /* A helper function that fills in a dwarf2_loclist_baton. */
24643 fill_in_loclist_baton (struct dwarf2_cu *cu,
24644 struct dwarf2_loclist_baton *baton,
24645 const struct attribute *attr)
24647 struct dwarf2_per_objfile *dwarf2_per_objfile
24648 = cu->per_cu->dwarf2_per_objfile;
24649 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24651 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
24653 baton->per_cu = cu->per_cu;
24654 gdb_assert (baton->per_cu);
24655 /* We don't know how long the location list is, but make sure we
24656 don't run off the edge of the section. */
24657 baton->size = section->size - DW_UNSND (attr);
24658 baton->data = section->buffer + DW_UNSND (attr);
24659 baton->base_address = cu->base_address;
24660 baton->from_dwo = cu->dwo_unit != NULL;
24664 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24665 struct dwarf2_cu *cu, int is_block)
24667 struct dwarf2_per_objfile *dwarf2_per_objfile
24668 = cu->per_cu->dwarf2_per_objfile;
24669 struct objfile *objfile = dwarf2_per_objfile->objfile;
24670 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24672 if (attr_form_is_section_offset (attr)
24673 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24674 the section. If so, fall through to the complaint in the
24676 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
24678 struct dwarf2_loclist_baton *baton;
24680 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24682 fill_in_loclist_baton (cu, baton, attr);
24684 if (cu->base_known == 0)
24685 complaint (_("Location list used without "
24686 "specifying the CU base address."));
24688 SYMBOL_ACLASS_INDEX (sym) = (is_block
24689 ? dwarf2_loclist_block_index
24690 : dwarf2_loclist_index);
24691 SYMBOL_LOCATION_BATON (sym) = baton;
24695 struct dwarf2_locexpr_baton *baton;
24697 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24698 baton->per_cu = cu->per_cu;
24699 gdb_assert (baton->per_cu);
24701 if (attr_form_is_block (attr))
24703 /* Note that we're just copying the block's data pointer
24704 here, not the actual data. We're still pointing into the
24705 info_buffer for SYM's objfile; right now we never release
24706 that buffer, but when we do clean up properly this may
24708 baton->size = DW_BLOCK (attr)->size;
24709 baton->data = DW_BLOCK (attr)->data;
24713 dwarf2_invalid_attrib_class_complaint ("location description",
24714 SYMBOL_NATURAL_NAME (sym));
24718 SYMBOL_ACLASS_INDEX (sym) = (is_block
24719 ? dwarf2_locexpr_block_index
24720 : dwarf2_locexpr_index);
24721 SYMBOL_LOCATION_BATON (sym) = baton;
24725 /* Return the OBJFILE associated with the compilation unit CU. If CU
24726 came from a separate debuginfo file, then the master objfile is
24730 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24732 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24734 /* Return the master objfile, so that we can report and look up the
24735 correct file containing this variable. */
24736 if (objfile->separate_debug_objfile_backlink)
24737 objfile = objfile->separate_debug_objfile_backlink;
24742 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24743 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24744 CU_HEADERP first. */
24746 static const struct comp_unit_head *
24747 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24748 struct dwarf2_per_cu_data *per_cu)
24750 const gdb_byte *info_ptr;
24753 return &per_cu->cu->header;
24755 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24757 memset (cu_headerp, 0, sizeof (*cu_headerp));
24758 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24759 rcuh_kind::COMPILE);
24764 /* Return the address size given in the compilation unit header for CU. */
24767 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24769 struct comp_unit_head cu_header_local;
24770 const struct comp_unit_head *cu_headerp;
24772 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24774 return cu_headerp->addr_size;
24777 /* Return the offset size given in the compilation unit header for CU. */
24780 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24782 struct comp_unit_head cu_header_local;
24783 const struct comp_unit_head *cu_headerp;
24785 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24787 return cu_headerp->offset_size;
24790 /* See its dwarf2loc.h declaration. */
24793 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24795 struct comp_unit_head cu_header_local;
24796 const struct comp_unit_head *cu_headerp;
24798 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24800 if (cu_headerp->version == 2)
24801 return cu_headerp->addr_size;
24803 return cu_headerp->offset_size;
24806 /* Return the text offset of the CU. The returned offset comes from
24807 this CU's objfile. If this objfile came from a separate debuginfo
24808 file, then the offset may be different from the corresponding
24809 offset in the parent objfile. */
24812 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24814 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24816 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24819 /* Return DWARF version number of PER_CU. */
24822 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24824 return per_cu->dwarf_version;
24827 /* Locate the .debug_info compilation unit from CU's objfile which contains
24828 the DIE at OFFSET. Raises an error on failure. */
24830 static struct dwarf2_per_cu_data *
24831 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24832 unsigned int offset_in_dwz,
24833 struct dwarf2_per_objfile *dwarf2_per_objfile)
24835 struct dwarf2_per_cu_data *this_cu;
24837 const sect_offset *cu_off;
24840 high = dwarf2_per_objfile->all_comp_units.size () - 1;
24843 struct dwarf2_per_cu_data *mid_cu;
24844 int mid = low + (high - low) / 2;
24846 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
24847 cu_off = &mid_cu->sect_off;
24848 if (mid_cu->is_dwz > offset_in_dwz
24849 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
24854 gdb_assert (low == high);
24855 this_cu = dwarf2_per_objfile->all_comp_units[low];
24856 cu_off = &this_cu->sect_off;
24857 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
24859 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24860 error (_("Dwarf Error: could not find partial DIE containing "
24861 "offset %s [in module %s]"),
24862 sect_offset_str (sect_off),
24863 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
24865 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24867 return dwarf2_per_objfile->all_comp_units[low-1];
24871 this_cu = dwarf2_per_objfile->all_comp_units[low];
24872 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
24873 && sect_off >= this_cu->sect_off + this_cu->length)
24874 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24875 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24880 /* Initialize dwarf2_cu CU, owned by PER_CU. */
24882 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24883 : per_cu (per_cu_),
24886 checked_producer (0),
24887 producer_is_gxx_lt_4_6 (0),
24888 producer_is_gcc_lt_4_3 (0),
24889 producer_is_icc_lt_14 (0),
24890 processing_has_namespace_info (0)
24895 /* Destroy a dwarf2_cu. */
24897 dwarf2_cu::~dwarf2_cu ()
24902 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24905 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24906 enum language pretend_language)
24908 struct attribute *attr;
24910 /* Set the language we're debugging. */
24911 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24913 set_cu_language (DW_UNSND (attr), cu);
24916 cu->language = pretend_language;
24917 cu->language_defn = language_def (cu->language);
24920 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24923 /* Increase the age counter on each cached compilation unit, and free
24924 any that are too old. */
24927 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
24929 struct dwarf2_per_cu_data *per_cu, **last_chain;
24931 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
24932 per_cu = dwarf2_per_objfile->read_in_chain;
24933 while (per_cu != NULL)
24935 per_cu->cu->last_used ++;
24936 if (per_cu->cu->last_used <= dwarf_max_cache_age)
24937 dwarf2_mark (per_cu->cu);
24938 per_cu = per_cu->cu->read_in_chain;
24941 per_cu = dwarf2_per_objfile->read_in_chain;
24942 last_chain = &dwarf2_per_objfile->read_in_chain;
24943 while (per_cu != NULL)
24945 struct dwarf2_per_cu_data *next_cu;
24947 next_cu = per_cu->cu->read_in_chain;
24949 if (!per_cu->cu->mark)
24952 *last_chain = next_cu;
24955 last_chain = &per_cu->cu->read_in_chain;
24961 /* Remove a single compilation unit from the cache. */
24964 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
24966 struct dwarf2_per_cu_data *per_cu, **last_chain;
24967 struct dwarf2_per_objfile *dwarf2_per_objfile
24968 = target_per_cu->dwarf2_per_objfile;
24970 per_cu = dwarf2_per_objfile->read_in_chain;
24971 last_chain = &dwarf2_per_objfile->read_in_chain;
24972 while (per_cu != NULL)
24974 struct dwarf2_per_cu_data *next_cu;
24976 next_cu = per_cu->cu->read_in_chain;
24978 if (per_cu == target_per_cu)
24982 *last_chain = next_cu;
24986 last_chain = &per_cu->cu->read_in_chain;
24992 /* Cleanup function for the dwarf2_per_objfile data. */
24995 dwarf2_free_objfile (struct objfile *objfile, void *datum)
24997 struct dwarf2_per_objfile *dwarf2_per_objfile
24998 = static_cast<struct dwarf2_per_objfile *> (datum);
25000 delete dwarf2_per_objfile;
25003 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25004 We store these in a hash table separate from the DIEs, and preserve them
25005 when the DIEs are flushed out of cache.
25007 The CU "per_cu" pointer is needed because offset alone is not enough to
25008 uniquely identify the type. A file may have multiple .debug_types sections,
25009 or the type may come from a DWO file. Furthermore, while it's more logical
25010 to use per_cu->section+offset, with Fission the section with the data is in
25011 the DWO file but we don't know that section at the point we need it.
25012 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25013 because we can enter the lookup routine, get_die_type_at_offset, from
25014 outside this file, and thus won't necessarily have PER_CU->cu.
25015 Fortunately, PER_CU is stable for the life of the objfile. */
25017 struct dwarf2_per_cu_offset_and_type
25019 const struct dwarf2_per_cu_data *per_cu;
25020 sect_offset sect_off;
25024 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25027 per_cu_offset_and_type_hash (const void *item)
25029 const struct dwarf2_per_cu_offset_and_type *ofs
25030 = (const struct dwarf2_per_cu_offset_and_type *) item;
25032 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25035 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25038 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25040 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25041 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25042 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25043 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25045 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25046 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25049 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25050 table if necessary. For convenience, return TYPE.
25052 The DIEs reading must have careful ordering to:
25053 * Not cause infite loops trying to read in DIEs as a prerequisite for
25054 reading current DIE.
25055 * Not trying to dereference contents of still incompletely read in types
25056 while reading in other DIEs.
25057 * Enable referencing still incompletely read in types just by a pointer to
25058 the type without accessing its fields.
25060 Therefore caller should follow these rules:
25061 * Try to fetch any prerequisite types we may need to build this DIE type
25062 before building the type and calling set_die_type.
25063 * After building type call set_die_type for current DIE as soon as
25064 possible before fetching more types to complete the current type.
25065 * Make the type as complete as possible before fetching more types. */
25067 static struct type *
25068 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25070 struct dwarf2_per_objfile *dwarf2_per_objfile
25071 = cu->per_cu->dwarf2_per_objfile;
25072 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25073 struct objfile *objfile = dwarf2_per_objfile->objfile;
25074 struct attribute *attr;
25075 struct dynamic_prop prop;
25077 /* For Ada types, make sure that the gnat-specific data is always
25078 initialized (if not already set). There are a few types where
25079 we should not be doing so, because the type-specific area is
25080 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25081 where the type-specific area is used to store the floatformat).
25082 But this is not a problem, because the gnat-specific information
25083 is actually not needed for these types. */
25084 if (need_gnat_info (cu)
25085 && TYPE_CODE (type) != TYPE_CODE_FUNC
25086 && TYPE_CODE (type) != TYPE_CODE_FLT
25087 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25088 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25089 && TYPE_CODE (type) != TYPE_CODE_METHOD
25090 && !HAVE_GNAT_AUX_INFO (type))
25091 INIT_GNAT_SPECIFIC (type);
25093 /* Read DW_AT_allocated and set in type. */
25094 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25095 if (attr_form_is_block (attr))
25097 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25098 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25100 else if (attr != NULL)
25102 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25103 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25104 sect_offset_str (die->sect_off));
25107 /* Read DW_AT_associated and set in type. */
25108 attr = dwarf2_attr (die, DW_AT_associated, cu);
25109 if (attr_form_is_block (attr))
25111 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25112 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25114 else if (attr != NULL)
25116 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
25117 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25118 sect_offset_str (die->sect_off));
25121 /* Read DW_AT_data_location and set in type. */
25122 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25123 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25124 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25126 if (dwarf2_per_objfile->die_type_hash == NULL)
25128 dwarf2_per_objfile->die_type_hash =
25129 htab_create_alloc_ex (127,
25130 per_cu_offset_and_type_hash,
25131 per_cu_offset_and_type_eq,
25133 &objfile->objfile_obstack,
25134 hashtab_obstack_allocate,
25135 dummy_obstack_deallocate);
25138 ofs.per_cu = cu->per_cu;
25139 ofs.sect_off = die->sect_off;
25141 slot = (struct dwarf2_per_cu_offset_and_type **)
25142 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25144 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25145 sect_offset_str (die->sect_off));
25146 *slot = XOBNEW (&objfile->objfile_obstack,
25147 struct dwarf2_per_cu_offset_and_type);
25152 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25153 or return NULL if the die does not have a saved type. */
25155 static struct type *
25156 get_die_type_at_offset (sect_offset sect_off,
25157 struct dwarf2_per_cu_data *per_cu)
25159 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25160 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25162 if (dwarf2_per_objfile->die_type_hash == NULL)
25165 ofs.per_cu = per_cu;
25166 ofs.sect_off = sect_off;
25167 slot = ((struct dwarf2_per_cu_offset_and_type *)
25168 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25175 /* Look up the type for DIE in CU in die_type_hash,
25176 or return NULL if DIE does not have a saved type. */
25178 static struct type *
25179 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25181 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25184 /* Add a dependence relationship from CU to REF_PER_CU. */
25187 dwarf2_add_dependence (struct dwarf2_cu *cu,
25188 struct dwarf2_per_cu_data *ref_per_cu)
25192 if (cu->dependencies == NULL)
25194 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25195 NULL, &cu->comp_unit_obstack,
25196 hashtab_obstack_allocate,
25197 dummy_obstack_deallocate);
25199 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25201 *slot = ref_per_cu;
25204 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25205 Set the mark field in every compilation unit in the
25206 cache that we must keep because we are keeping CU. */
25209 dwarf2_mark_helper (void **slot, void *data)
25211 struct dwarf2_per_cu_data *per_cu;
25213 per_cu = (struct dwarf2_per_cu_data *) *slot;
25215 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25216 reading of the chain. As such dependencies remain valid it is not much
25217 useful to track and undo them during QUIT cleanups. */
25218 if (per_cu->cu == NULL)
25221 if (per_cu->cu->mark)
25223 per_cu->cu->mark = 1;
25225 if (per_cu->cu->dependencies != NULL)
25226 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25231 /* Set the mark field in CU and in every other compilation unit in the
25232 cache that we must keep because we are keeping CU. */
25235 dwarf2_mark (struct dwarf2_cu *cu)
25240 if (cu->dependencies != NULL)
25241 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25245 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25249 per_cu->cu->mark = 0;
25250 per_cu = per_cu->cu->read_in_chain;
25254 /* Trivial hash function for partial_die_info: the hash value of a DIE
25255 is its offset in .debug_info for this objfile. */
25258 partial_die_hash (const void *item)
25260 const struct partial_die_info *part_die
25261 = (const struct partial_die_info *) item;
25263 return to_underlying (part_die->sect_off);
25266 /* Trivial comparison function for partial_die_info structures: two DIEs
25267 are equal if they have the same offset. */
25270 partial_die_eq (const void *item_lhs, const void *item_rhs)
25272 const struct partial_die_info *part_die_lhs
25273 = (const struct partial_die_info *) item_lhs;
25274 const struct partial_die_info *part_die_rhs
25275 = (const struct partial_die_info *) item_rhs;
25277 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25280 static struct cmd_list_element *set_dwarf_cmdlist;
25281 static struct cmd_list_element *show_dwarf_cmdlist;
25284 set_dwarf_cmd (const char *args, int from_tty)
25286 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25291 show_dwarf_cmd (const char *args, int from_tty)
25293 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25296 int dwarf_always_disassemble;
25299 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25300 struct cmd_list_element *c, const char *value)
25302 fprintf_filtered (file,
25303 _("Whether to always disassemble "
25304 "DWARF expressions is %s.\n"),
25309 show_check_physname (struct ui_file *file, int from_tty,
25310 struct cmd_list_element *c, const char *value)
25312 fprintf_filtered (file,
25313 _("Whether to check \"physname\" is %s.\n"),
25318 _initialize_dwarf2_read (void)
25320 dwarf2_objfile_data_key
25321 = register_objfile_data_with_cleanup (nullptr, dwarf2_free_objfile);
25323 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25324 Set DWARF specific variables.\n\
25325 Configure DWARF variables such as the cache size"),
25326 &set_dwarf_cmdlist, "maintenance set dwarf ",
25327 0/*allow-unknown*/, &maintenance_set_cmdlist);
25329 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25330 Show DWARF specific variables\n\
25331 Show DWARF variables such as the cache size"),
25332 &show_dwarf_cmdlist, "maintenance show dwarf ",
25333 0/*allow-unknown*/, &maintenance_show_cmdlist);
25335 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25336 &dwarf_max_cache_age, _("\
25337 Set the upper bound on the age of cached DWARF compilation units."), _("\
25338 Show the upper bound on the age of cached DWARF compilation units."), _("\
25339 A higher limit means that cached compilation units will be stored\n\
25340 in memory longer, and more total memory will be used. Zero disables\n\
25341 caching, which can slow down startup."),
25343 show_dwarf_max_cache_age,
25344 &set_dwarf_cmdlist,
25345 &show_dwarf_cmdlist);
25347 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25348 &dwarf_always_disassemble, _("\
25349 Set whether `info address' always disassembles DWARF expressions."), _("\
25350 Show whether `info address' always disassembles DWARF expressions."), _("\
25351 When enabled, DWARF expressions are always printed in an assembly-like\n\
25352 syntax. When disabled, expressions will be printed in a more\n\
25353 conversational style, when possible."),
25355 show_dwarf_always_disassemble,
25356 &set_dwarf_cmdlist,
25357 &show_dwarf_cmdlist);
25359 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25360 Set debugging of the DWARF reader."), _("\
25361 Show debugging of the DWARF reader."), _("\
25362 When enabled (non-zero), debugging messages are printed during DWARF\n\
25363 reading and symtab expansion. A value of 1 (one) provides basic\n\
25364 information. A value greater than 1 provides more verbose information."),
25367 &setdebuglist, &showdebuglist);
25369 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25370 Set debugging of the DWARF DIE reader."), _("\
25371 Show debugging of the DWARF DIE reader."), _("\
25372 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25373 The value is the maximum depth to print."),
25376 &setdebuglist, &showdebuglist);
25378 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25379 Set debugging of the dwarf line reader."), _("\
25380 Show debugging of the dwarf line reader."), _("\
25381 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25382 A value of 1 (one) provides basic information.\n\
25383 A value greater than 1 provides more verbose information."),
25386 &setdebuglist, &showdebuglist);
25388 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25389 Set cross-checking of \"physname\" code against demangler."), _("\
25390 Show cross-checking of \"physname\" code against demangler."), _("\
25391 When enabled, GDB's internal \"physname\" code is checked against\n\
25393 NULL, show_check_physname,
25394 &setdebuglist, &showdebuglist);
25396 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25397 no_class, &use_deprecated_index_sections, _("\
25398 Set whether to use deprecated gdb_index sections."), _("\
25399 Show whether to use deprecated gdb_index sections."), _("\
25400 When enabled, deprecated .gdb_index sections are used anyway.\n\
25401 Normally they are ignored either because of a missing feature or\n\
25402 performance issue.\n\
25403 Warning: This option must be enabled before gdb reads the file."),
25406 &setlist, &showlist);
25408 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25409 &dwarf2_locexpr_funcs);
25410 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25411 &dwarf2_loclist_funcs);
25413 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25414 &dwarf2_block_frame_base_locexpr_funcs);
25415 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25416 &dwarf2_block_frame_base_loclist_funcs);
25419 selftests::register_test ("dw2_expand_symtabs_matching",
25420 selftests::dw2_expand_symtabs_matching::run_test);