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. */
40 #include "gdb-demangle.h"
41 #include "expression.h"
42 #include "filenames.h" /* for DOSish file names */
45 #include "complaints.h"
47 #include "dwarf2expr.h"
48 #include "dwarf2loc.h"
49 #include "cp-support.h"
55 #include "typeprint.h"
58 #include "completer.h"
63 #include "gdbcore.h" /* for gnutarget */
64 #include "gdb/gdb-index.h"
69 #include "filestuff.h"
71 #include "namespace.h"
72 #include "common/gdb_unlinker.h"
73 #include "common/function-view.h"
74 #include "common/gdb_optional.h"
75 #include "common/underlying.h"
76 #include "common/byte-vector.h"
77 #include "common/hash_enum.h"
78 #include "filename-seen-cache.h"
81 #include <sys/types.h>
83 #include <unordered_set>
84 #include <unordered_map>
88 #include <forward_list>
89 #include "rust-lang.h"
90 #include "common/pathstuff.h"
92 /* When == 1, print basic high level tracing messages.
93 When > 1, be more verbose.
94 This is in contrast to the low level DIE reading of dwarf_die_debug. */
95 static unsigned int dwarf_read_debug = 0;
97 /* When non-zero, dump DIEs after they are read in. */
98 static unsigned int dwarf_die_debug = 0;
100 /* When non-zero, dump line number entries as they are read in. */
101 static unsigned int dwarf_line_debug = 0;
103 /* When non-zero, cross-check physname against demangler. */
104 static int check_physname = 0;
106 /* When non-zero, do not reject deprecated .gdb_index sections. */
107 static int use_deprecated_index_sections = 0;
109 static const struct objfile_data *dwarf2_objfile_data_key;
111 /* The "aclass" indices for various kinds of computed DWARF symbols. */
113 static int dwarf2_locexpr_index;
114 static int dwarf2_loclist_index;
115 static int dwarf2_locexpr_block_index;
116 static int dwarf2_loclist_block_index;
118 /* A descriptor for dwarf sections.
120 S.ASECTION, SIZE are typically initialized when the objfile is first
121 scanned. BUFFER, READIN are filled in later when the section is read.
122 If the section contained compressed data then SIZE is updated to record
123 the uncompressed size of the section.
125 DWP file format V2 introduces a wrinkle that is easiest to handle by
126 creating the concept of virtual sections contained within a real section.
127 In DWP V2 the sections of the input DWO files are concatenated together
128 into one section, but section offsets are kept relative to the original
130 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
131 the real section this "virtual" section is contained in, and BUFFER,SIZE
132 describe the virtual section. */
134 struct dwarf2_section_info
138 /* If this is a real section, the bfd section. */
140 /* If this is a virtual section, pointer to the containing ("real")
142 struct dwarf2_section_info *containing_section;
144 /* Pointer to section data, only valid if readin. */
145 const gdb_byte *buffer;
146 /* The size of the section, real or virtual. */
148 /* If this is a virtual section, the offset in the real section.
149 Only valid if is_virtual. */
150 bfd_size_type virtual_offset;
151 /* True if we have tried to read this section. */
153 /* True if this is a virtual section, False otherwise.
154 This specifies which of s.section and s.containing_section to use. */
158 typedef struct dwarf2_section_info dwarf2_section_info_def;
159 DEF_VEC_O (dwarf2_section_info_def);
161 /* All offsets in the index are of this type. It must be
162 architecture-independent. */
163 typedef uint32_t offset_type;
165 DEF_VEC_I (offset_type);
167 /* Ensure only legit values are used. */
168 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
170 gdb_assert ((unsigned int) (value) <= 1); \
171 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
174 /* Ensure only legit values are used. */
175 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
177 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
178 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
179 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
182 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
183 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
185 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
186 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
191 /* Convert VALUE between big- and little-endian. */
194 byte_swap (offset_type value)
198 result = (value & 0xff) << 24;
199 result |= (value & 0xff00) << 8;
200 result |= (value & 0xff0000) >> 8;
201 result |= (value & 0xff000000) >> 24;
205 #define MAYBE_SWAP(V) byte_swap (V)
208 #define MAYBE_SWAP(V) static_cast<offset_type> (V)
209 #endif /* WORDS_BIGENDIAN */
211 /* An index into a (C++) symbol name component in a symbol name as
212 recorded in the mapped_index's symbol table. For each C++ symbol
213 in the symbol table, we record one entry for the start of each
214 component in the symbol in a table of name components, and then
215 sort the table, in order to be able to binary search symbol names,
216 ignoring leading namespaces, both completion and regular look up.
217 For example, for symbol "A::B::C", we'll have an entry that points
218 to "A::B::C", another that points to "B::C", and another for "C".
219 Note that function symbols in GDB index have no parameter
220 information, just the function/method names. You can convert a
221 name_component to a "const char *" using the
222 'mapped_index::symbol_name_at(offset_type)' method. */
224 struct name_component
226 /* Offset in the symbol name where the component starts. Stored as
227 a (32-bit) offset instead of a pointer to save memory and improve
228 locality on 64-bit architectures. */
229 offset_type name_offset;
231 /* The symbol's index in the symbol and constant pool tables of a
236 /* Base class containing bits shared by both .gdb_index and
237 .debug_name indexes. */
239 struct mapped_index_base
241 /* The name_component table (a sorted vector). See name_component's
242 description above. */
243 std::vector<name_component> name_components;
245 /* How NAME_COMPONENTS is sorted. */
246 enum case_sensitivity name_components_casing;
248 /* Return the number of names in the symbol table. */
249 virtual size_t symbol_name_count () const = 0;
251 /* Get the name of the symbol at IDX in the symbol table. */
252 virtual const char *symbol_name_at (offset_type idx) const = 0;
254 /* Return whether the name at IDX in the symbol table should be
256 virtual bool symbol_name_slot_invalid (offset_type idx) const
261 /* Build the symbol name component sorted vector, if we haven't
263 void build_name_components ();
265 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
266 possible matches for LN_NO_PARAMS in the name component
268 std::pair<std::vector<name_component>::const_iterator,
269 std::vector<name_component>::const_iterator>
270 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
272 /* Prevent deleting/destroying via a base class pointer. */
274 ~mapped_index_base() = default;
277 /* A description of the mapped index. The file format is described in
278 a comment by the code that writes the index. */
279 struct mapped_index final : public mapped_index_base
281 /* A slot/bucket in the symbol table hash. */
282 struct symbol_table_slot
284 const offset_type name;
285 const offset_type vec;
288 /* Index data format version. */
291 /* The total length of the buffer. */
294 /* The address table data. */
295 gdb::array_view<const gdb_byte> address_table;
297 /* The symbol table, implemented as a hash table. */
298 gdb::array_view<symbol_table_slot> symbol_table;
300 /* A pointer to the constant pool. */
301 const char *constant_pool;
303 bool symbol_name_slot_invalid (offset_type idx) const override
305 const auto &bucket = this->symbol_table[idx];
306 return bucket.name == 0 && bucket.vec;
309 /* Convenience method to get at the name of the symbol at IDX in the
311 const char *symbol_name_at (offset_type idx) const override
312 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
314 size_t symbol_name_count () const override
315 { return this->symbol_table.size (); }
318 /* A description of the mapped .debug_names.
319 Uninitialized map has CU_COUNT 0. */
320 struct mapped_debug_names final : public mapped_index_base
322 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
323 : dwarf2_per_objfile (dwarf2_per_objfile_)
326 struct dwarf2_per_objfile *dwarf2_per_objfile;
327 bfd_endian dwarf5_byte_order;
328 bool dwarf5_is_dwarf64;
329 bool augmentation_is_gdb;
331 uint32_t cu_count = 0;
332 uint32_t tu_count, bucket_count, name_count;
333 const gdb_byte *cu_table_reordered, *tu_table_reordered;
334 const uint32_t *bucket_table_reordered, *hash_table_reordered;
335 const gdb_byte *name_table_string_offs_reordered;
336 const gdb_byte *name_table_entry_offs_reordered;
337 const gdb_byte *entry_pool;
344 /* Attribute name DW_IDX_*. */
347 /* Attribute form DW_FORM_*. */
350 /* Value if FORM is DW_FORM_implicit_const. */
351 LONGEST implicit_const;
353 std::vector<attr> attr_vec;
356 std::unordered_map<ULONGEST, index_val> abbrev_map;
358 const char *namei_to_name (uint32_t namei) const;
360 /* Implementation of the mapped_index_base virtual interface, for
361 the name_components cache. */
363 const char *symbol_name_at (offset_type idx) const override
364 { return namei_to_name (idx); }
366 size_t symbol_name_count () const override
367 { return this->name_count; }
370 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
371 DEF_VEC_P (dwarf2_per_cu_ptr);
375 int nr_uniq_abbrev_tables;
377 int nr_symtab_sharers;
378 int nr_stmt_less_type_units;
379 int nr_all_type_units_reallocs;
382 /* Collection of data recorded per objfile.
383 This hangs off of dwarf2_objfile_data_key. */
385 struct dwarf2_per_objfile : public allocate_on_obstack
387 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
388 dwarf2 section names, or is NULL if the standard ELF names are
390 dwarf2_per_objfile (struct objfile *objfile,
391 const dwarf2_debug_sections *names);
393 ~dwarf2_per_objfile ();
395 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
397 /* Free all cached compilation units. */
398 void free_cached_comp_units ();
400 /* This function is mapped across the sections and remembers the
401 offset and size of each of the debugging sections we are
403 void locate_sections (bfd *abfd, asection *sectp,
404 const dwarf2_debug_sections &names);
407 dwarf2_section_info info {};
408 dwarf2_section_info abbrev {};
409 dwarf2_section_info line {};
410 dwarf2_section_info loc {};
411 dwarf2_section_info loclists {};
412 dwarf2_section_info macinfo {};
413 dwarf2_section_info macro {};
414 dwarf2_section_info str {};
415 dwarf2_section_info line_str {};
416 dwarf2_section_info ranges {};
417 dwarf2_section_info rnglists {};
418 dwarf2_section_info addr {};
419 dwarf2_section_info frame {};
420 dwarf2_section_info eh_frame {};
421 dwarf2_section_info gdb_index {};
422 dwarf2_section_info debug_names {};
423 dwarf2_section_info debug_aranges {};
425 VEC (dwarf2_section_info_def) *types = NULL;
428 struct objfile *objfile = NULL;
430 /* Table of all the compilation units. This is used to locate
431 the target compilation unit of a particular reference. */
432 struct dwarf2_per_cu_data **all_comp_units = NULL;
434 /* The number of compilation units in ALL_COMP_UNITS. */
435 int n_comp_units = 0;
437 /* The number of .debug_types-related CUs. */
438 int n_type_units = 0;
440 /* The number of elements allocated in all_type_units.
441 If there are skeleton-less TUs, we add them to all_type_units lazily. */
442 int n_allocated_type_units = 0;
444 /* The .debug_types-related CUs (TUs).
445 This is stored in malloc space because we may realloc it. */
446 struct signatured_type **all_type_units = NULL;
448 /* Table of struct type_unit_group objects.
449 The hash key is the DW_AT_stmt_list value. */
450 htab_t type_unit_groups {};
452 /* A table mapping .debug_types signatures to its signatured_type entry.
453 This is NULL if the .debug_types section hasn't been read in yet. */
454 htab_t signatured_types {};
456 /* Type unit statistics, to see how well the scaling improvements
458 struct tu_stats tu_stats {};
460 /* A chain of compilation units that are currently read in, so that
461 they can be freed later. */
462 dwarf2_per_cu_data *read_in_chain = NULL;
464 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
465 This is NULL if the table hasn't been allocated yet. */
468 /* True if we've checked for whether there is a DWP file. */
469 bool dwp_checked = false;
471 /* The DWP file if there is one, or NULL. */
472 struct dwp_file *dwp_file = NULL;
474 /* The shared '.dwz' file, if one exists. This is used when the
475 original data was compressed using 'dwz -m'. */
476 struct dwz_file *dwz_file = NULL;
478 /* A flag indicating whether this objfile has a section loaded at a
480 bool has_section_at_zero = false;
482 /* True if we are using the mapped index,
483 or we are faking it for OBJF_READNOW's sake. */
484 bool using_index = false;
486 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
487 mapped_index *index_table = NULL;
489 /* The mapped index, or NULL if .debug_names is missing or not being used. */
490 std::unique_ptr<mapped_debug_names> debug_names_table;
492 /* When using index_table, this keeps track of all quick_file_names entries.
493 TUs typically share line table entries with a CU, so we maintain a
494 separate table of all line table entries to support the sharing.
495 Note that while there can be way more TUs than CUs, we've already
496 sorted all the TUs into "type unit groups", grouped by their
497 DW_AT_stmt_list value. Therefore the only sharing done here is with a
498 CU and its associated TU group if there is one. */
499 htab_t quick_file_names_table {};
501 /* Set during partial symbol reading, to prevent queueing of full
503 bool reading_partial_symbols = false;
505 /* Table mapping type DIEs to their struct type *.
506 This is NULL if not allocated yet.
507 The mapping is done via (CU/TU + DIE offset) -> type. */
508 htab_t die_type_hash {};
510 /* The CUs we recently read. */
511 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
513 /* Table containing line_header indexed by offset and offset_in_dwz. */
514 htab_t line_header_hash {};
516 /* Table containing all filenames. This is an optional because the
517 table is lazily constructed on first access. */
518 gdb::optional<filename_seen_cache> filenames_cache;
521 /* Get the dwarf2_per_objfile associated to OBJFILE. */
523 struct dwarf2_per_objfile *
524 get_dwarf2_per_objfile (struct objfile *objfile)
526 return ((struct dwarf2_per_objfile *)
527 objfile_data (objfile, dwarf2_objfile_data_key));
530 /* Set the dwarf2_per_objfile associated to OBJFILE. */
533 set_dwarf2_per_objfile (struct objfile *objfile,
534 struct dwarf2_per_objfile *dwarf2_per_objfile)
536 gdb_assert (get_dwarf2_per_objfile (objfile) == NULL);
537 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
540 /* Default names of the debugging sections. */
542 /* Note that if the debugging section has been compressed, it might
543 have a name like .zdebug_info. */
545 static const struct dwarf2_debug_sections dwarf2_elf_names =
547 { ".debug_info", ".zdebug_info" },
548 { ".debug_abbrev", ".zdebug_abbrev" },
549 { ".debug_line", ".zdebug_line" },
550 { ".debug_loc", ".zdebug_loc" },
551 { ".debug_loclists", ".zdebug_loclists" },
552 { ".debug_macinfo", ".zdebug_macinfo" },
553 { ".debug_macro", ".zdebug_macro" },
554 { ".debug_str", ".zdebug_str" },
555 { ".debug_line_str", ".zdebug_line_str" },
556 { ".debug_ranges", ".zdebug_ranges" },
557 { ".debug_rnglists", ".zdebug_rnglists" },
558 { ".debug_types", ".zdebug_types" },
559 { ".debug_addr", ".zdebug_addr" },
560 { ".debug_frame", ".zdebug_frame" },
561 { ".eh_frame", NULL },
562 { ".gdb_index", ".zgdb_index" },
563 { ".debug_names", ".zdebug_names" },
564 { ".debug_aranges", ".zdebug_aranges" },
568 /* List of DWO/DWP sections. */
570 static const struct dwop_section_names
572 struct dwarf2_section_names abbrev_dwo;
573 struct dwarf2_section_names info_dwo;
574 struct dwarf2_section_names line_dwo;
575 struct dwarf2_section_names loc_dwo;
576 struct dwarf2_section_names loclists_dwo;
577 struct dwarf2_section_names macinfo_dwo;
578 struct dwarf2_section_names macro_dwo;
579 struct dwarf2_section_names str_dwo;
580 struct dwarf2_section_names str_offsets_dwo;
581 struct dwarf2_section_names types_dwo;
582 struct dwarf2_section_names cu_index;
583 struct dwarf2_section_names tu_index;
587 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
588 { ".debug_info.dwo", ".zdebug_info.dwo" },
589 { ".debug_line.dwo", ".zdebug_line.dwo" },
590 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
591 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
592 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
593 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
594 { ".debug_str.dwo", ".zdebug_str.dwo" },
595 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
596 { ".debug_types.dwo", ".zdebug_types.dwo" },
597 { ".debug_cu_index", ".zdebug_cu_index" },
598 { ".debug_tu_index", ".zdebug_tu_index" },
601 /* local data types */
603 /* The data in a compilation unit header, after target2host
604 translation, looks like this. */
605 struct comp_unit_head
609 unsigned char addr_size;
610 unsigned char signed_addr_p;
611 sect_offset abbrev_sect_off;
613 /* Size of file offsets; either 4 or 8. */
614 unsigned int offset_size;
616 /* Size of the length field; either 4 or 12. */
617 unsigned int initial_length_size;
619 enum dwarf_unit_type unit_type;
621 /* Offset to the first byte of this compilation unit header in the
622 .debug_info section, for resolving relative reference dies. */
623 sect_offset sect_off;
625 /* Offset to first die in this cu from the start of the cu.
626 This will be the first byte following the compilation unit header. */
627 cu_offset first_die_cu_offset;
629 /* 64-bit signature of this type unit - it is valid only for
630 UNIT_TYPE DW_UT_type. */
633 /* For types, offset in the type's DIE of the type defined by this TU. */
634 cu_offset type_cu_offset_in_tu;
637 /* Type used for delaying computation of method physnames.
638 See comments for compute_delayed_physnames. */
639 struct delayed_method_info
641 /* The type to which the method is attached, i.e., its parent class. */
644 /* The index of the method in the type's function fieldlists. */
647 /* The index of the method in the fieldlist. */
650 /* The name of the DIE. */
653 /* The DIE associated with this method. */
654 struct die_info *die;
657 /* Internal state when decoding a particular compilation unit. */
660 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
663 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
665 /* The header of the compilation unit. */
666 struct comp_unit_head header {};
668 /* Base address of this compilation unit. */
669 CORE_ADDR base_address = 0;
671 /* Non-zero if base_address has been set. */
674 /* The language we are debugging. */
675 enum language language = language_unknown;
676 const struct language_defn *language_defn = nullptr;
678 const char *producer = nullptr;
680 /* The generic symbol table building routines have separate lists for
681 file scope symbols and all all other scopes (local scopes). So
682 we need to select the right one to pass to add_symbol_to_list().
683 We do it by keeping a pointer to the correct list in list_in_scope.
685 FIXME: The original dwarf code just treated the file scope as the
686 first local scope, and all other local scopes as nested local
687 scopes, and worked fine. Check to see if we really need to
688 distinguish these in buildsym.c. */
689 struct pending **list_in_scope = nullptr;
691 /* Hash table holding all the loaded partial DIEs
692 with partial_die->offset.SECT_OFF as hash. */
693 htab_t partial_dies = nullptr;
695 /* Storage for things with the same lifetime as this read-in compilation
696 unit, including partial DIEs. */
697 auto_obstack comp_unit_obstack;
699 /* When multiple dwarf2_cu structures are living in memory, this field
700 chains them all together, so that they can be released efficiently.
701 We will probably also want a generation counter so that most-recently-used
702 compilation units are cached... */
703 struct dwarf2_per_cu_data *read_in_chain = nullptr;
705 /* Backlink to our per_cu entry. */
706 struct dwarf2_per_cu_data *per_cu;
708 /* How many compilation units ago was this CU last referenced? */
711 /* A hash table of DIE cu_offset for following references with
712 die_info->offset.sect_off as hash. */
713 htab_t die_hash = nullptr;
715 /* Full DIEs if read in. */
716 struct die_info *dies = nullptr;
718 /* A set of pointers to dwarf2_per_cu_data objects for compilation
719 units referenced by this one. Only set during full symbol processing;
720 partial symbol tables do not have dependencies. */
721 htab_t dependencies = nullptr;
723 /* Header data from the line table, during full symbol processing. */
724 struct line_header *line_header = nullptr;
725 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
726 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
727 this is the DW_TAG_compile_unit die for this CU. We'll hold on
728 to the line header as long as this DIE is being processed. See
729 process_die_scope. */
730 die_info *line_header_die_owner = nullptr;
732 /* A list of methods which need to have physnames computed
733 after all type information has been read. */
734 std::vector<delayed_method_info> method_list;
736 /* To be copied to symtab->call_site_htab. */
737 htab_t call_site_htab = nullptr;
739 /* Non-NULL if this CU came from a DWO file.
740 There is an invariant here that is important to remember:
741 Except for attributes copied from the top level DIE in the "main"
742 (or "stub") file in preparation for reading the DWO file
743 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
744 Either there isn't a DWO file (in which case this is NULL and the point
745 is moot), or there is and either we're not going to read it (in which
746 case this is NULL) or there is and we are reading it (in which case this
748 struct dwo_unit *dwo_unit = nullptr;
750 /* The DW_AT_addr_base attribute if present, zero otherwise
751 (zero is a valid value though).
752 Note this value comes from the Fission stub CU/TU's DIE. */
753 ULONGEST addr_base = 0;
755 /* The DW_AT_ranges_base attribute if present, zero otherwise
756 (zero is a valid value though).
757 Note this value comes from the Fission stub CU/TU's DIE.
758 Also note that the value is zero in the non-DWO case so this value can
759 be used without needing to know whether DWO files are in use or not.
760 N.B. This does not apply to DW_AT_ranges appearing in
761 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
762 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
763 DW_AT_ranges_base *would* have to be applied, and we'd have to care
764 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
765 ULONGEST ranges_base = 0;
767 /* When reading debug info generated by older versions of rustc, we
768 have to rewrite some union types to be struct types with a
769 variant part. This rewriting must be done after the CU is fully
770 read in, because otherwise at the point of rewriting some struct
771 type might not have been fully processed. So, we keep a list of
772 all such types here and process them after expansion. */
773 std::vector<struct type *> rust_unions;
775 /* Mark used when releasing cached dies. */
776 unsigned int mark : 1;
778 /* This CU references .debug_loc. See the symtab->locations_valid field.
779 This test is imperfect as there may exist optimized debug code not using
780 any location list and still facing inlining issues if handled as
781 unoptimized code. For a future better test see GCC PR other/32998. */
782 unsigned int has_loclist : 1;
784 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
785 if all the producer_is_* fields are valid. This information is cached
786 because profiling CU expansion showed excessive time spent in
787 producer_is_gxx_lt_4_6. */
788 unsigned int checked_producer : 1;
789 unsigned int producer_is_gxx_lt_4_6 : 1;
790 unsigned int producer_is_gcc_lt_4_3 : 1;
791 unsigned int producer_is_icc_lt_14 : 1;
793 /* When set, the file that we're processing is known to have
794 debugging info for C++ namespaces. GCC 3.3.x did not produce
795 this information, but later versions do. */
797 unsigned int processing_has_namespace_info : 1;
799 struct partial_die_info *find_partial_die (sect_offset sect_off);
802 /* Persistent data held for a compilation unit, even when not
803 processing it. We put a pointer to this structure in the
804 read_symtab_private field of the psymtab. */
806 struct dwarf2_per_cu_data
808 /* The start offset and length of this compilation unit.
809 NOTE: Unlike comp_unit_head.length, this length includes
811 If the DIE refers to a DWO file, this is always of the original die,
813 sect_offset sect_off;
816 /* DWARF standard version this data has been read from (such as 4 or 5). */
819 /* Flag indicating this compilation unit will be read in before
820 any of the current compilation units are processed. */
821 unsigned int queued : 1;
823 /* This flag will be set when reading partial DIEs if we need to load
824 absolutely all DIEs for this compilation unit, instead of just the ones
825 we think are interesting. It gets set if we look for a DIE in the
826 hash table and don't find it. */
827 unsigned int load_all_dies : 1;
829 /* Non-zero if this CU is from .debug_types.
830 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
832 unsigned int is_debug_types : 1;
834 /* Non-zero if this CU is from the .dwz file. */
835 unsigned int is_dwz : 1;
837 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
838 This flag is only valid if is_debug_types is true.
839 We can't read a CU directly from a DWO file: There are required
840 attributes in the stub. */
841 unsigned int reading_dwo_directly : 1;
843 /* Non-zero if the TU has been read.
844 This is used to assist the "Stay in DWO Optimization" for Fission:
845 When reading a DWO, it's faster to read TUs from the DWO instead of
846 fetching them from random other DWOs (due to comdat folding).
847 If the TU has already been read, the optimization is unnecessary
848 (and unwise - we don't want to change where gdb thinks the TU lives
850 This flag is only valid if is_debug_types is true. */
851 unsigned int tu_read : 1;
853 /* The section this CU/TU lives in.
854 If the DIE refers to a DWO file, this is always the original die,
856 struct dwarf2_section_info *section;
858 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
859 of the CU cache it gets reset to NULL again. This is left as NULL for
860 dummy CUs (a CU header, but nothing else). */
861 struct dwarf2_cu *cu;
863 /* The corresponding dwarf2_per_objfile. */
864 struct dwarf2_per_objfile *dwarf2_per_objfile;
866 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
867 is active. Otherwise, the 'psymtab' field is active. */
870 /* The partial symbol table associated with this compilation unit,
871 or NULL for unread partial units. */
872 struct partial_symtab *psymtab;
874 /* Data needed by the "quick" functions. */
875 struct dwarf2_per_cu_quick_data *quick;
878 /* The CUs we import using DW_TAG_imported_unit. This is filled in
879 while reading psymtabs, used to compute the psymtab dependencies,
880 and then cleared. Then it is filled in again while reading full
881 symbols, and only deleted when the objfile is destroyed.
883 This is also used to work around a difference between the way gold
884 generates .gdb_index version <=7 and the way gdb does. Arguably this
885 is a gold bug. For symbols coming from TUs, gold records in the index
886 the CU that includes the TU instead of the TU itself. This breaks
887 dw2_lookup_symbol: It assumes that if the index says symbol X lives
888 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
889 will find X. Alas TUs live in their own symtab, so after expanding CU Y
890 we need to look in TU Z to find X. Fortunately, this is akin to
891 DW_TAG_imported_unit, so we just use the same mechanism: For
892 .gdb_index version <=7 this also records the TUs that the CU referred
893 to. Concurrently with this change gdb was modified to emit version 8
894 indices so we only pay a price for gold generated indices.
895 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
896 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
899 /* Entry in the signatured_types hash table. */
901 struct signatured_type
903 /* The "per_cu" object of this type.
904 This struct is used iff per_cu.is_debug_types.
905 N.B.: This is the first member so that it's easy to convert pointers
907 struct dwarf2_per_cu_data per_cu;
909 /* The type's signature. */
912 /* Offset in the TU of the type's DIE, as read from the TU header.
913 If this TU is a DWO stub and the definition lives in a DWO file
914 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
915 cu_offset type_offset_in_tu;
917 /* Offset in the section of the type's DIE.
918 If the definition lives in a DWO file, this is the offset in the
919 .debug_types.dwo section.
920 The value is zero until the actual value is known.
921 Zero is otherwise not a valid section offset. */
922 sect_offset type_offset_in_section;
924 /* Type units are grouped by their DW_AT_stmt_list entry so that they
925 can share them. This points to the containing symtab. */
926 struct type_unit_group *type_unit_group;
929 The first time we encounter this type we fully read it in and install it
930 in the symbol tables. Subsequent times we only need the type. */
933 /* Containing DWO unit.
934 This field is valid iff per_cu.reading_dwo_directly. */
935 struct dwo_unit *dwo_unit;
938 typedef struct signatured_type *sig_type_ptr;
939 DEF_VEC_P (sig_type_ptr);
941 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
942 This includes type_unit_group and quick_file_names. */
944 struct stmt_list_hash
946 /* The DWO unit this table is from or NULL if there is none. */
947 struct dwo_unit *dwo_unit;
949 /* Offset in .debug_line or .debug_line.dwo. */
950 sect_offset line_sect_off;
953 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
954 an object of this type. */
956 struct type_unit_group
958 /* dwarf2read.c's main "handle" on a TU symtab.
959 To simplify things we create an artificial CU that "includes" all the
960 type units using this stmt_list so that the rest of the code still has
961 a "per_cu" handle on the symtab.
962 This PER_CU is recognized by having no section. */
963 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
964 struct dwarf2_per_cu_data per_cu;
966 /* The TUs that share this DW_AT_stmt_list entry.
967 This is added to while parsing type units to build partial symtabs,
968 and is deleted afterwards and not used again. */
969 VEC (sig_type_ptr) *tus;
971 /* The compunit symtab.
972 Type units in a group needn't all be defined in the same source file,
973 so we create an essentially anonymous symtab as the compunit symtab. */
974 struct compunit_symtab *compunit_symtab;
976 /* The data used to construct the hash key. */
977 struct stmt_list_hash hash;
979 /* The number of symtabs from the line header.
980 The value here must match line_header.num_file_names. */
981 unsigned int num_symtabs;
983 /* The symbol tables for this TU (obtained from the files listed in
985 WARNING: The order of entries here must match the order of entries
986 in the line header. After the first TU using this type_unit_group, the
987 line header for the subsequent TUs is recreated from this. This is done
988 because we need to use the same symtabs for each TU using the same
989 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
990 there's no guarantee the line header doesn't have duplicate entries. */
991 struct symtab **symtabs;
994 /* These sections are what may appear in a (real or virtual) DWO file. */
998 struct dwarf2_section_info abbrev;
999 struct dwarf2_section_info line;
1000 struct dwarf2_section_info loc;
1001 struct dwarf2_section_info loclists;
1002 struct dwarf2_section_info macinfo;
1003 struct dwarf2_section_info macro;
1004 struct dwarf2_section_info str;
1005 struct dwarf2_section_info str_offsets;
1006 /* In the case of a virtual DWO file, these two are unused. */
1007 struct dwarf2_section_info info;
1008 VEC (dwarf2_section_info_def) *types;
1011 /* CUs/TUs in DWP/DWO files. */
1015 /* Backlink to the containing struct dwo_file. */
1016 struct dwo_file *dwo_file;
1018 /* The "id" that distinguishes this CU/TU.
1019 .debug_info calls this "dwo_id", .debug_types calls this "signature".
1020 Since signatures came first, we stick with it for consistency. */
1023 /* The section this CU/TU lives in, in the DWO file. */
1024 struct dwarf2_section_info *section;
1026 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
1027 sect_offset sect_off;
1028 unsigned int length;
1030 /* For types, offset in the type's DIE of the type defined by this TU. */
1031 cu_offset type_offset_in_tu;
1034 /* include/dwarf2.h defines the DWP section codes.
1035 It defines a max value but it doesn't define a min value, which we
1036 use for error checking, so provide one. */
1038 enum dwp_v2_section_ids
1043 /* Data for one DWO file.
1045 This includes virtual DWO files (a virtual DWO file is a DWO file as it
1046 appears in a DWP file). DWP files don't really have DWO files per se -
1047 comdat folding of types "loses" the DWO file they came from, and from
1048 a high level view DWP files appear to contain a mass of random types.
1049 However, to maintain consistency with the non-DWP case we pretend DWP
1050 files contain virtual DWO files, and we assign each TU with one virtual
1051 DWO file (generally based on the line and abbrev section offsets -
1052 a heuristic that seems to work in practice). */
1056 /* The DW_AT_GNU_dwo_name attribute.
1057 For virtual DWO files the name is constructed from the section offsets
1058 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
1059 from related CU+TUs. */
1060 const char *dwo_name;
1062 /* The DW_AT_comp_dir attribute. */
1063 const char *comp_dir;
1065 /* The bfd, when the file is open. Otherwise this is NULL.
1066 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
1069 /* The sections that make up this DWO file.
1070 Remember that for virtual DWO files in DWP V2, these are virtual
1071 sections (for lack of a better name). */
1072 struct dwo_sections sections;
1074 /* The CUs in the file.
1075 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
1076 an extension to handle LLVM's Link Time Optimization output (where
1077 multiple source files may be compiled into a single object/dwo pair). */
1080 /* Table of TUs in the file.
1081 Each element is a struct dwo_unit. */
1085 /* These sections are what may appear in a DWP file. */
1089 /* These are used by both DWP version 1 and 2. */
1090 struct dwarf2_section_info str;
1091 struct dwarf2_section_info cu_index;
1092 struct dwarf2_section_info tu_index;
1094 /* These are only used by DWP version 2 files.
1095 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
1096 sections are referenced by section number, and are not recorded here.
1097 In DWP version 2 there is at most one copy of all these sections, each
1098 section being (effectively) comprised of the concatenation of all of the
1099 individual sections that exist in the version 1 format.
1100 To keep the code simple we treat each of these concatenated pieces as a
1101 section itself (a virtual section?). */
1102 struct dwarf2_section_info abbrev;
1103 struct dwarf2_section_info info;
1104 struct dwarf2_section_info line;
1105 struct dwarf2_section_info loc;
1106 struct dwarf2_section_info macinfo;
1107 struct dwarf2_section_info macro;
1108 struct dwarf2_section_info str_offsets;
1109 struct dwarf2_section_info types;
1112 /* These sections are what may appear in a virtual DWO file in DWP version 1.
1113 A virtual DWO file is a DWO file as it appears in a DWP file. */
1115 struct virtual_v1_dwo_sections
1117 struct dwarf2_section_info abbrev;
1118 struct dwarf2_section_info line;
1119 struct dwarf2_section_info loc;
1120 struct dwarf2_section_info macinfo;
1121 struct dwarf2_section_info macro;
1122 struct dwarf2_section_info str_offsets;
1123 /* Each DWP hash table entry records one CU or one TU.
1124 That is recorded here, and copied to dwo_unit.section. */
1125 struct dwarf2_section_info info_or_types;
1128 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
1129 In version 2, the sections of the DWO files are concatenated together
1130 and stored in one section of that name. Thus each ELF section contains
1131 several "virtual" sections. */
1133 struct virtual_v2_dwo_sections
1135 bfd_size_type abbrev_offset;
1136 bfd_size_type abbrev_size;
1138 bfd_size_type line_offset;
1139 bfd_size_type line_size;
1141 bfd_size_type loc_offset;
1142 bfd_size_type loc_size;
1144 bfd_size_type macinfo_offset;
1145 bfd_size_type macinfo_size;
1147 bfd_size_type macro_offset;
1148 bfd_size_type macro_size;
1150 bfd_size_type str_offsets_offset;
1151 bfd_size_type str_offsets_size;
1153 /* Each DWP hash table entry records one CU or one TU.
1154 That is recorded here, and copied to dwo_unit.section. */
1155 bfd_size_type info_or_types_offset;
1156 bfd_size_type info_or_types_size;
1159 /* Contents of DWP hash tables. */
1161 struct dwp_hash_table
1163 uint32_t version, nr_columns;
1164 uint32_t nr_units, nr_slots;
1165 const gdb_byte *hash_table, *unit_table;
1170 const gdb_byte *indices;
1174 /* This is indexed by column number and gives the id of the section
1176 #define MAX_NR_V2_DWO_SECTIONS \
1177 (1 /* .debug_info or .debug_types */ \
1178 + 1 /* .debug_abbrev */ \
1179 + 1 /* .debug_line */ \
1180 + 1 /* .debug_loc */ \
1181 + 1 /* .debug_str_offsets */ \
1182 + 1 /* .debug_macro or .debug_macinfo */)
1183 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1184 const gdb_byte *offsets;
1185 const gdb_byte *sizes;
1190 /* Data for one DWP file. */
1194 /* Name of the file. */
1197 /* File format version. */
1203 /* Section info for this file. */
1204 struct dwp_sections sections;
1206 /* Table of CUs in the file. */
1207 const struct dwp_hash_table *cus;
1209 /* Table of TUs in the file. */
1210 const struct dwp_hash_table *tus;
1212 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1216 /* Table to map ELF section numbers to their sections.
1217 This is only needed for the DWP V1 file format. */
1218 unsigned int num_sections;
1219 asection **elf_sections;
1222 /* This represents a '.dwz' file. */
1226 /* A dwz file can only contain a few sections. */
1227 struct dwarf2_section_info abbrev;
1228 struct dwarf2_section_info info;
1229 struct dwarf2_section_info str;
1230 struct dwarf2_section_info line;
1231 struct dwarf2_section_info macro;
1232 struct dwarf2_section_info gdb_index;
1233 struct dwarf2_section_info debug_names;
1235 /* The dwz's BFD. */
1239 /* Struct used to pass misc. parameters to read_die_and_children, et
1240 al. which are used for both .debug_info and .debug_types dies.
1241 All parameters here are unchanging for the life of the call. This
1242 struct exists to abstract away the constant parameters of die reading. */
1244 struct die_reader_specs
1246 /* The bfd of die_section. */
1249 /* The CU of the DIE we are parsing. */
1250 struct dwarf2_cu *cu;
1252 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1253 struct dwo_file *dwo_file;
1255 /* The section the die comes from.
1256 This is either .debug_info or .debug_types, or the .dwo variants. */
1257 struct dwarf2_section_info *die_section;
1259 /* die_section->buffer. */
1260 const gdb_byte *buffer;
1262 /* The end of the buffer. */
1263 const gdb_byte *buffer_end;
1265 /* The value of the DW_AT_comp_dir attribute. */
1266 const char *comp_dir;
1268 /* The abbreviation table to use when reading the DIEs. */
1269 struct abbrev_table *abbrev_table;
1272 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1273 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1274 const gdb_byte *info_ptr,
1275 struct die_info *comp_unit_die,
1279 /* A 1-based directory index. This is a strong typedef to prevent
1280 accidentally using a directory index as a 0-based index into an
1282 enum class dir_index : unsigned int {};
1284 /* Likewise, a 1-based file name index. */
1285 enum class file_name_index : unsigned int {};
1289 file_entry () = default;
1291 file_entry (const char *name_, dir_index d_index_,
1292 unsigned int mod_time_, unsigned int length_)
1295 mod_time (mod_time_),
1299 /* Return the include directory at D_INDEX stored in LH. Returns
1300 NULL if D_INDEX is out of bounds. */
1301 const char *include_dir (const line_header *lh) const;
1303 /* The file name. Note this is an observing pointer. The memory is
1304 owned by debug_line_buffer. */
1305 const char *name {};
1307 /* The directory index (1-based). */
1308 dir_index d_index {};
1310 unsigned int mod_time {};
1312 unsigned int length {};
1314 /* True if referenced by the Line Number Program. */
1317 /* The associated symbol table, if any. */
1318 struct symtab *symtab {};
1321 /* The line number information for a compilation unit (found in the
1322 .debug_line section) begins with a "statement program header",
1323 which contains the following information. */
1330 /* Add an entry to the include directory table. */
1331 void add_include_dir (const char *include_dir);
1333 /* Add an entry to the file name table. */
1334 void add_file_name (const char *name, dir_index d_index,
1335 unsigned int mod_time, unsigned int length);
1337 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
1338 is out of bounds. */
1339 const char *include_dir_at (dir_index index) const
1341 /* Convert directory index number (1-based) to vector index
1343 size_t vec_index = to_underlying (index) - 1;
1345 if (vec_index >= include_dirs.size ())
1347 return include_dirs[vec_index];
1350 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
1351 is out of bounds. */
1352 file_entry *file_name_at (file_name_index index)
1354 /* Convert file name index number (1-based) to vector index
1356 size_t vec_index = to_underlying (index) - 1;
1358 if (vec_index >= file_names.size ())
1360 return &file_names[vec_index];
1363 /* Const version of the above. */
1364 const file_entry *file_name_at (unsigned int index) const
1366 if (index >= file_names.size ())
1368 return &file_names[index];
1371 /* Offset of line number information in .debug_line section. */
1372 sect_offset sect_off {};
1374 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1375 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1377 unsigned int total_length {};
1378 unsigned short version {};
1379 unsigned int header_length {};
1380 unsigned char minimum_instruction_length {};
1381 unsigned char maximum_ops_per_instruction {};
1382 unsigned char default_is_stmt {};
1384 unsigned char line_range {};
1385 unsigned char opcode_base {};
1387 /* standard_opcode_lengths[i] is the number of operands for the
1388 standard opcode whose value is i. This means that
1389 standard_opcode_lengths[0] is unused, and the last meaningful
1390 element is standard_opcode_lengths[opcode_base - 1]. */
1391 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1393 /* The include_directories table. Note these are observing
1394 pointers. The memory is owned by debug_line_buffer. */
1395 std::vector<const char *> include_dirs;
1397 /* The file_names table. */
1398 std::vector<file_entry> file_names;
1400 /* The start and end of the statement program following this
1401 header. These point into dwarf2_per_objfile->line_buffer. */
1402 const gdb_byte *statement_program_start {}, *statement_program_end {};
1405 typedef std::unique_ptr<line_header> line_header_up;
1408 file_entry::include_dir (const line_header *lh) const
1410 return lh->include_dir_at (d_index);
1413 /* When we construct a partial symbol table entry we only
1414 need this much information. */
1415 struct partial_die_info : public allocate_on_obstack
1417 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1419 /* Disable assign but still keep copy ctor, which is needed
1420 load_partial_dies. */
1421 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1423 /* Adjust the partial die before generating a symbol for it. This
1424 function may set the is_external flag or change the DIE's
1426 void fixup (struct dwarf2_cu *cu);
1428 /* Read a minimal amount of information into the minimal die
1430 const gdb_byte *read (const struct die_reader_specs *reader,
1431 const struct abbrev_info &abbrev,
1432 const gdb_byte *info_ptr);
1434 /* Offset of this DIE. */
1435 const sect_offset sect_off;
1437 /* DWARF-2 tag for this DIE. */
1438 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1440 /* Assorted flags describing the data found in this DIE. */
1441 const unsigned int has_children : 1;
1443 unsigned int is_external : 1;
1444 unsigned int is_declaration : 1;
1445 unsigned int has_type : 1;
1446 unsigned int has_specification : 1;
1447 unsigned int has_pc_info : 1;
1448 unsigned int may_be_inlined : 1;
1450 /* This DIE has been marked DW_AT_main_subprogram. */
1451 unsigned int main_subprogram : 1;
1453 /* Flag set if the SCOPE field of this structure has been
1455 unsigned int scope_set : 1;
1457 /* Flag set if the DIE has a byte_size attribute. */
1458 unsigned int has_byte_size : 1;
1460 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1461 unsigned int has_const_value : 1;
1463 /* Flag set if any of the DIE's children are template arguments. */
1464 unsigned int has_template_arguments : 1;
1466 /* Flag set if fixup has been called on this die. */
1467 unsigned int fixup_called : 1;
1469 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1470 unsigned int is_dwz : 1;
1472 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1473 unsigned int spec_is_dwz : 1;
1475 /* The name of this DIE. Normally the value of DW_AT_name, but
1476 sometimes a default name for unnamed DIEs. */
1477 const char *name = nullptr;
1479 /* The linkage name, if present. */
1480 const char *linkage_name = nullptr;
1482 /* The scope to prepend to our children. This is generally
1483 allocated on the comp_unit_obstack, so will disappear
1484 when this compilation unit leaves the cache. */
1485 const char *scope = nullptr;
1487 /* Some data associated with the partial DIE. The tag determines
1488 which field is live. */
1491 /* The location description associated with this DIE, if any. */
1492 struct dwarf_block *locdesc;
1493 /* The offset of an import, for DW_TAG_imported_unit. */
1494 sect_offset sect_off;
1497 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1498 CORE_ADDR lowpc = 0;
1499 CORE_ADDR highpc = 0;
1501 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1502 DW_AT_sibling, if any. */
1503 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1504 could return DW_AT_sibling values to its caller load_partial_dies. */
1505 const gdb_byte *sibling = nullptr;
1507 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1508 DW_AT_specification (or DW_AT_abstract_origin or
1509 DW_AT_extension). */
1510 sect_offset spec_offset {};
1512 /* Pointers to this DIE's parent, first child, and next sibling,
1514 struct partial_die_info *die_parent = nullptr;
1515 struct partial_die_info *die_child = nullptr;
1516 struct partial_die_info *die_sibling = nullptr;
1518 friend struct partial_die_info *
1519 dwarf2_cu::find_partial_die (sect_offset sect_off);
1522 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1523 partial_die_info (sect_offset sect_off)
1524 : partial_die_info (sect_off, DW_TAG_padding, 0)
1528 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1530 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1535 has_specification = 0;
1538 main_subprogram = 0;
1541 has_const_value = 0;
1542 has_template_arguments = 0;
1549 /* This data structure holds the information of an abbrev. */
1552 unsigned int number; /* number identifying abbrev */
1553 enum dwarf_tag tag; /* dwarf tag */
1554 unsigned short has_children; /* boolean */
1555 unsigned short num_attrs; /* number of attributes */
1556 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1557 struct abbrev_info *next; /* next in chain */
1562 ENUM_BITFIELD(dwarf_attribute) name : 16;
1563 ENUM_BITFIELD(dwarf_form) form : 16;
1565 /* It is valid only if FORM is DW_FORM_implicit_const. */
1566 LONGEST implicit_const;
1569 /* Size of abbrev_table.abbrev_hash_table. */
1570 #define ABBREV_HASH_SIZE 121
1572 /* Top level data structure to contain an abbreviation table. */
1576 explicit abbrev_table (sect_offset off)
1580 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1581 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1584 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1586 /* Allocate space for a struct abbrev_info object in
1588 struct abbrev_info *alloc_abbrev ();
1590 /* Add an abbreviation to the table. */
1591 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1593 /* Look up an abbrev in the table.
1594 Returns NULL if the abbrev is not found. */
1596 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1599 /* Where the abbrev table came from.
1600 This is used as a sanity check when the table is used. */
1601 const sect_offset sect_off;
1603 /* Storage for the abbrev table. */
1604 auto_obstack abbrev_obstack;
1608 /* Hash table of abbrevs.
1609 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1610 It could be statically allocated, but the previous code didn't so we
1612 struct abbrev_info **m_abbrevs;
1615 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1617 /* Attributes have a name and a value. */
1620 ENUM_BITFIELD(dwarf_attribute) name : 16;
1621 ENUM_BITFIELD(dwarf_form) form : 15;
1623 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1624 field should be in u.str (existing only for DW_STRING) but it is kept
1625 here for better struct attribute alignment. */
1626 unsigned int string_is_canonical : 1;
1631 struct dwarf_block *blk;
1640 /* This data structure holds a complete die structure. */
1643 /* DWARF-2 tag for this DIE. */
1644 ENUM_BITFIELD(dwarf_tag) tag : 16;
1646 /* Number of attributes */
1647 unsigned char num_attrs;
1649 /* True if we're presently building the full type name for the
1650 type derived from this DIE. */
1651 unsigned char building_fullname : 1;
1653 /* True if this die is in process. PR 16581. */
1654 unsigned char in_process : 1;
1657 unsigned int abbrev;
1659 /* Offset in .debug_info or .debug_types section. */
1660 sect_offset sect_off;
1662 /* The dies in a compilation unit form an n-ary tree. PARENT
1663 points to this die's parent; CHILD points to the first child of
1664 this node; and all the children of a given node are chained
1665 together via their SIBLING fields. */
1666 struct die_info *child; /* Its first child, if any. */
1667 struct die_info *sibling; /* Its next sibling, if any. */
1668 struct die_info *parent; /* Its parent, if any. */
1670 /* An array of attributes, with NUM_ATTRS elements. There may be
1671 zero, but it's not common and zero-sized arrays are not
1672 sufficiently portable C. */
1673 struct attribute attrs[1];
1676 /* Get at parts of an attribute structure. */
1678 #define DW_STRING(attr) ((attr)->u.str)
1679 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1680 #define DW_UNSND(attr) ((attr)->u.unsnd)
1681 #define DW_BLOCK(attr) ((attr)->u.blk)
1682 #define DW_SND(attr) ((attr)->u.snd)
1683 #define DW_ADDR(attr) ((attr)->u.addr)
1684 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1686 /* Blocks are a bunch of untyped bytes. */
1691 /* Valid only if SIZE is not zero. */
1692 const gdb_byte *data;
1695 #ifndef ATTR_ALLOC_CHUNK
1696 #define ATTR_ALLOC_CHUNK 4
1699 /* Allocate fields for structs, unions and enums in this size. */
1700 #ifndef DW_FIELD_ALLOC_CHUNK
1701 #define DW_FIELD_ALLOC_CHUNK 4
1704 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1705 but this would require a corresponding change in unpack_field_as_long
1707 static int bits_per_byte = 8;
1709 /* When reading a variant or variant part, we track a bit more
1710 information about the field, and store it in an object of this
1713 struct variant_field
1715 /* If we see a DW_TAG_variant, then this will be the discriminant
1717 ULONGEST discriminant_value;
1718 /* If we see a DW_TAG_variant, then this will be set if this is the
1720 bool default_branch;
1721 /* While reading a DW_TAG_variant_part, this will be set if this
1722 field is the discriminant. */
1723 bool is_discriminant;
1728 struct nextfield *next;
1731 /* Extra information to describe a variant or variant part. */
1732 struct variant_field variant;
1738 struct nextfnfield *next;
1739 struct fn_field fnfield;
1746 struct nextfnfield *head;
1749 struct decl_field_list
1751 struct decl_field field;
1752 struct decl_field_list *next;
1755 /* The routines that read and process dies for a C struct or C++ class
1756 pass lists of data member fields and lists of member function fields
1757 in an instance of a field_info structure, as defined below. */
1760 /* List of data member and baseclasses fields. */
1761 struct nextfield *fields, *baseclasses;
1763 /* Number of fields (including baseclasses). */
1766 /* Number of baseclasses. */
1769 /* Set if the accesibility of one of the fields is not public. */
1770 int non_public_fields;
1772 /* Member function fieldlist array, contains name of possibly overloaded
1773 member function, number of overloaded member functions and a pointer
1774 to the head of the member function field chain. */
1775 struct fnfieldlist *fnfieldlists;
1777 /* Number of entries in the fnfieldlists array. */
1780 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1781 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1782 struct decl_field_list *typedef_field_list;
1783 unsigned typedef_field_list_count;
1785 /* Nested types defined by this class and the number of elements in this
1787 struct decl_field_list *nested_types_list;
1788 unsigned nested_types_list_count;
1791 /* One item on the queue of compilation units to read in full symbols
1793 struct dwarf2_queue_item
1795 struct dwarf2_per_cu_data *per_cu;
1796 enum language pretend_language;
1797 struct dwarf2_queue_item *next;
1800 /* The current queue. */
1801 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1803 /* Loaded secondary compilation units are kept in memory until they
1804 have not been referenced for the processing of this many
1805 compilation units. Set this to zero to disable caching. Cache
1806 sizes of up to at least twenty will improve startup time for
1807 typical inter-CU-reference binaries, at an obvious memory cost. */
1808 static int dwarf_max_cache_age = 5;
1810 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1811 struct cmd_list_element *c, const char *value)
1813 fprintf_filtered (file, _("The upper bound on the age of cached "
1814 "DWARF compilation units is %s.\n"),
1818 /* local function prototypes */
1820 static const char *get_section_name (const struct dwarf2_section_info *);
1822 static const char *get_section_file_name (const struct dwarf2_section_info *);
1824 static void dwarf2_find_base_address (struct die_info *die,
1825 struct dwarf2_cu *cu);
1827 static struct partial_symtab *create_partial_symtab
1828 (struct dwarf2_per_cu_data *per_cu, const char *name);
1830 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1831 const gdb_byte *info_ptr,
1832 struct die_info *type_unit_die,
1833 int has_children, void *data);
1835 static void dwarf2_build_psymtabs_hard
1836 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1838 static void scan_partial_symbols (struct partial_die_info *,
1839 CORE_ADDR *, CORE_ADDR *,
1840 int, struct dwarf2_cu *);
1842 static void add_partial_symbol (struct partial_die_info *,
1843 struct dwarf2_cu *);
1845 static void add_partial_namespace (struct partial_die_info *pdi,
1846 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1847 int set_addrmap, struct dwarf2_cu *cu);
1849 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1850 CORE_ADDR *highpc, int set_addrmap,
1851 struct dwarf2_cu *cu);
1853 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1854 struct dwarf2_cu *cu);
1856 static void add_partial_subprogram (struct partial_die_info *pdi,
1857 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1858 int need_pc, struct dwarf2_cu *cu);
1860 static void dwarf2_read_symtab (struct partial_symtab *,
1863 static void psymtab_to_symtab_1 (struct partial_symtab *);
1865 static abbrev_table_up abbrev_table_read_table
1866 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1869 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1871 static struct partial_die_info *load_partial_dies
1872 (const struct die_reader_specs *, const gdb_byte *, int);
1874 static struct partial_die_info *find_partial_die (sect_offset, int,
1875 struct dwarf2_cu *);
1877 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1878 struct attribute *, struct attr_abbrev *,
1881 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1883 static int read_1_signed_byte (bfd *, const gdb_byte *);
1885 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1887 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1889 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1891 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1894 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1896 static LONGEST read_checked_initial_length_and_offset
1897 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1898 unsigned int *, unsigned int *);
1900 static LONGEST read_offset (bfd *, const gdb_byte *,
1901 const struct comp_unit_head *,
1904 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1906 static sect_offset read_abbrev_offset
1907 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1908 struct dwarf2_section_info *, sect_offset);
1910 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1912 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1914 static const char *read_indirect_string
1915 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1916 const struct comp_unit_head *, unsigned int *);
1918 static const char *read_indirect_line_string
1919 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1920 const struct comp_unit_head *, unsigned int *);
1922 static const char *read_indirect_string_at_offset
1923 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1924 LONGEST str_offset);
1926 static const char *read_indirect_string_from_dwz
1927 (struct objfile *objfile, struct dwz_file *, LONGEST);
1929 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1931 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1935 static const char *read_str_index (const struct die_reader_specs *reader,
1936 ULONGEST str_index);
1938 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1940 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1941 struct dwarf2_cu *);
1943 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1946 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1947 struct dwarf2_cu *cu);
1949 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1950 struct dwarf2_cu *cu);
1952 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1954 static struct die_info *die_specification (struct die_info *die,
1955 struct dwarf2_cu **);
1957 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1958 struct dwarf2_cu *cu);
1960 static void dwarf_decode_lines (struct line_header *, const char *,
1961 struct dwarf2_cu *, struct partial_symtab *,
1962 CORE_ADDR, int decode_mapping);
1964 static void dwarf2_start_subfile (const char *, const char *);
1966 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1967 const char *, const char *,
1970 static struct symbol *new_symbol (struct die_info *, struct type *,
1971 struct dwarf2_cu *, struct symbol * = NULL);
1973 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1974 struct dwarf2_cu *);
1976 static void dwarf2_const_value_attr (const struct attribute *attr,
1979 struct obstack *obstack,
1980 struct dwarf2_cu *cu, LONGEST *value,
1981 const gdb_byte **bytes,
1982 struct dwarf2_locexpr_baton **baton);
1984 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1986 static int need_gnat_info (struct dwarf2_cu *);
1988 static struct type *die_descriptive_type (struct die_info *,
1989 struct dwarf2_cu *);
1991 static void set_descriptive_type (struct type *, struct die_info *,
1992 struct dwarf2_cu *);
1994 static struct type *die_containing_type (struct die_info *,
1995 struct dwarf2_cu *);
1997 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1998 struct dwarf2_cu *);
2000 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
2002 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
2004 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
2006 static char *typename_concat (struct obstack *obs, const char *prefix,
2007 const char *suffix, int physname,
2008 struct dwarf2_cu *cu);
2010 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
2012 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
2014 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
2016 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
2018 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
2020 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
2022 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
2023 struct dwarf2_cu *, struct partial_symtab *);
2025 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
2026 values. Keep the items ordered with increasing constraints compliance. */
2029 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
2030 PC_BOUNDS_NOT_PRESENT,
2032 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
2033 were present but they do not form a valid range of PC addresses. */
2036 /* Discontiguous range was found - that is DW_AT_ranges was found. */
2039 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
2043 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
2044 CORE_ADDR *, CORE_ADDR *,
2046 struct partial_symtab *);
2048 static void get_scope_pc_bounds (struct die_info *,
2049 CORE_ADDR *, CORE_ADDR *,
2050 struct dwarf2_cu *);
2052 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
2053 CORE_ADDR, struct dwarf2_cu *);
2055 static void dwarf2_add_field (struct field_info *, struct die_info *,
2056 struct dwarf2_cu *);
2058 static void dwarf2_attach_fields_to_type (struct field_info *,
2059 struct type *, struct dwarf2_cu *);
2061 static void dwarf2_add_member_fn (struct field_info *,
2062 struct die_info *, struct type *,
2063 struct dwarf2_cu *);
2065 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
2067 struct dwarf2_cu *);
2069 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
2071 static void read_common_block (struct die_info *, struct dwarf2_cu *);
2073 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
2075 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
2077 static struct using_direct **using_directives (enum language);
2079 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
2081 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
2083 static struct type *read_module_type (struct die_info *die,
2084 struct dwarf2_cu *cu);
2086 static const char *namespace_name (struct die_info *die,
2087 int *is_anonymous, struct dwarf2_cu *);
2089 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
2091 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
2093 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
2094 struct dwarf2_cu *);
2096 static struct die_info *read_die_and_siblings_1
2097 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
2100 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
2101 const gdb_byte *info_ptr,
2102 const gdb_byte **new_info_ptr,
2103 struct die_info *parent);
2105 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
2106 struct die_info **, const gdb_byte *,
2109 static const gdb_byte *read_full_die (const struct die_reader_specs *,
2110 struct die_info **, const gdb_byte *,
2113 static void process_die (struct die_info *, struct dwarf2_cu *);
2115 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
2118 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
2120 static const char *dwarf2_full_name (const char *name,
2121 struct die_info *die,
2122 struct dwarf2_cu *cu);
2124 static const char *dwarf2_physname (const char *name, struct die_info *die,
2125 struct dwarf2_cu *cu);
2127 static struct die_info *dwarf2_extension (struct die_info *die,
2128 struct dwarf2_cu **);
2130 static const char *dwarf_tag_name (unsigned int);
2132 static const char *dwarf_attr_name (unsigned int);
2134 static const char *dwarf_form_name (unsigned int);
2136 static const char *dwarf_bool_name (unsigned int);
2138 static const char *dwarf_type_encoding_name (unsigned int);
2140 static struct die_info *sibling_die (struct die_info *);
2142 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
2144 static void dump_die_for_error (struct die_info *);
2146 static void dump_die_1 (struct ui_file *, int level, int max_level,
2149 /*static*/ void dump_die (struct die_info *, int max_level);
2151 static void store_in_ref_table (struct die_info *,
2152 struct dwarf2_cu *);
2154 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
2156 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
2158 static struct die_info *follow_die_ref_or_sig (struct die_info *,
2159 const struct attribute *,
2160 struct dwarf2_cu **);
2162 static struct die_info *follow_die_ref (struct die_info *,
2163 const struct attribute *,
2164 struct dwarf2_cu **);
2166 static struct die_info *follow_die_sig (struct die_info *,
2167 const struct attribute *,
2168 struct dwarf2_cu **);
2170 static struct type *get_signatured_type (struct die_info *, ULONGEST,
2171 struct dwarf2_cu *);
2173 static struct type *get_DW_AT_signature_type (struct die_info *,
2174 const struct attribute *,
2175 struct dwarf2_cu *);
2177 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
2179 static void read_signatured_type (struct signatured_type *);
2181 static int attr_to_dynamic_prop (const struct attribute *attr,
2182 struct die_info *die, struct dwarf2_cu *cu,
2183 struct dynamic_prop *prop);
2185 /* memory allocation interface */
2187 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
2189 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
2191 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2193 static int attr_form_is_block (const struct attribute *);
2195 static int attr_form_is_section_offset (const struct attribute *);
2197 static int attr_form_is_constant (const struct attribute *);
2199 static int attr_form_is_ref (const struct attribute *);
2201 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
2202 struct dwarf2_loclist_baton *baton,
2203 const struct attribute *attr);
2205 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
2207 struct dwarf2_cu *cu,
2210 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
2211 const gdb_byte *info_ptr,
2212 struct abbrev_info *abbrev);
2214 static hashval_t partial_die_hash (const void *item);
2216 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2218 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
2219 (sect_offset sect_off, unsigned int offset_in_dwz,
2220 struct dwarf2_per_objfile *dwarf2_per_objfile);
2222 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
2223 struct die_info *comp_unit_die,
2224 enum language pretend_language);
2226 static void free_cached_comp_units (void *);
2228 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2230 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
2232 static struct type *set_die_type (struct die_info *, struct type *,
2233 struct dwarf2_cu *);
2235 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2237 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2239 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2242 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2245 static void process_full_type_unit (struct dwarf2_per_cu_data *,
2248 static void dwarf2_add_dependence (struct dwarf2_cu *,
2249 struct dwarf2_per_cu_data *);
2251 static void dwarf2_mark (struct dwarf2_cu *);
2253 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2255 static struct type *get_die_type_at_offset (sect_offset,
2256 struct dwarf2_per_cu_data *);
2258 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
2260 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2261 enum language pretend_language);
2263 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
2265 /* Class, the destructor of which frees all allocated queue entries. This
2266 will only have work to do if an error was thrown while processing the
2267 dwarf. If no error was thrown then the queue entries should have all
2268 been processed, and freed, as we went along. */
2270 class dwarf2_queue_guard
2273 dwarf2_queue_guard () = default;
2275 /* Free any entries remaining on the queue. There should only be
2276 entries left if we hit an error while processing the dwarf. */
2277 ~dwarf2_queue_guard ()
2279 struct dwarf2_queue_item *item, *last;
2281 item = dwarf2_queue;
2284 /* Anything still marked queued is likely to be in an
2285 inconsistent state, so discard it. */
2286 if (item->per_cu->queued)
2288 if (item->per_cu->cu != NULL)
2289 free_one_cached_comp_unit (item->per_cu);
2290 item->per_cu->queued = 0;
2298 dwarf2_queue = dwarf2_queue_tail = NULL;
2302 /* The return type of find_file_and_directory. Note, the enclosed
2303 string pointers are only valid while this object is valid. */
2305 struct file_and_directory
2307 /* The filename. This is never NULL. */
2310 /* The compilation directory. NULL if not known. If we needed to
2311 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2312 points directly to the DW_AT_comp_dir string attribute owned by
2313 the obstack that owns the DIE. */
2314 const char *comp_dir;
2316 /* If we needed to build a new string for comp_dir, this is what
2317 owns the storage. */
2318 std::string comp_dir_storage;
2321 static file_and_directory find_file_and_directory (struct die_info *die,
2322 struct dwarf2_cu *cu);
2324 static char *file_full_name (int file, struct line_header *lh,
2325 const char *comp_dir);
2327 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
2328 enum class rcuh_kind { COMPILE, TYPE };
2330 static const gdb_byte *read_and_check_comp_unit_head
2331 (struct dwarf2_per_objfile* dwarf2_per_objfile,
2332 struct comp_unit_head *header,
2333 struct dwarf2_section_info *section,
2334 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
2335 rcuh_kind section_kind);
2337 static void init_cutu_and_read_dies
2338 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2339 int use_existing_cu, int keep,
2340 die_reader_func_ftype *die_reader_func, void *data);
2342 static void init_cutu_and_read_dies_simple
2343 (struct dwarf2_per_cu_data *this_cu,
2344 die_reader_func_ftype *die_reader_func, void *data);
2346 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2348 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2350 static struct dwo_unit *lookup_dwo_unit_in_dwp
2351 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2352 struct dwp_file *dwp_file, const char *comp_dir,
2353 ULONGEST signature, int is_debug_types);
2355 static struct dwp_file *get_dwp_file
2356 (struct dwarf2_per_objfile *dwarf2_per_objfile);
2358 static struct dwo_unit *lookup_dwo_comp_unit
2359 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2361 static struct dwo_unit *lookup_dwo_type_unit
2362 (struct signatured_type *, const char *, const char *);
2364 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2366 static void free_dwo_file_cleanup (void *);
2368 struct free_dwo_file_cleanup_data
2370 struct dwo_file *dwo_file;
2371 struct dwarf2_per_objfile *dwarf2_per_objfile;
2374 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2376 static void check_producer (struct dwarf2_cu *cu);
2378 static void free_line_header_voidp (void *arg);
2380 /* Various complaints about symbol reading that don't abort the process. */
2383 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2385 complaint (&symfile_complaints,
2386 _("statement list doesn't fit in .debug_line section"));
2390 dwarf2_debug_line_missing_file_complaint (void)
2392 complaint (&symfile_complaints,
2393 _(".debug_line section has line data without a file"));
2397 dwarf2_debug_line_missing_end_sequence_complaint (void)
2399 complaint (&symfile_complaints,
2400 _(".debug_line section has line "
2401 "program sequence without an end"));
2405 dwarf2_complex_location_expr_complaint (void)
2407 complaint (&symfile_complaints, _("location expression too complex"));
2411 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2414 complaint (&symfile_complaints,
2415 _("const value length mismatch for '%s', got %d, expected %d"),
2420 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2422 complaint (&symfile_complaints,
2423 _("debug info runs off end of %s section"
2425 get_section_name (section),
2426 get_section_file_name (section));
2430 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2432 complaint (&symfile_complaints,
2433 _("macro debug info contains a "
2434 "malformed macro definition:\n`%s'"),
2439 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2441 complaint (&symfile_complaints,
2442 _("invalid attribute class or form for '%s' in '%s'"),
2446 /* Hash function for line_header_hash. */
2449 line_header_hash (const struct line_header *ofs)
2451 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2454 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2457 line_header_hash_voidp (const void *item)
2459 const struct line_header *ofs = (const struct line_header *) item;
2461 return line_header_hash (ofs);
2464 /* Equality function for line_header_hash. */
2467 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2469 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2470 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2472 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2473 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2478 /* Read the given attribute value as an address, taking the attribute's
2479 form into account. */
2482 attr_value_as_address (struct attribute *attr)
2486 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2488 /* Aside from a few clearly defined exceptions, attributes that
2489 contain an address must always be in DW_FORM_addr form.
2490 Unfortunately, some compilers happen to be violating this
2491 requirement by encoding addresses using other forms, such
2492 as DW_FORM_data4 for example. For those broken compilers,
2493 we try to do our best, without any guarantee of success,
2494 to interpret the address correctly. It would also be nice
2495 to generate a complaint, but that would require us to maintain
2496 a list of legitimate cases where a non-address form is allowed,
2497 as well as update callers to pass in at least the CU's DWARF
2498 version. This is more overhead than what we're willing to
2499 expand for a pretty rare case. */
2500 addr = DW_UNSND (attr);
2503 addr = DW_ADDR (attr);
2508 /* The suffix for an index file. */
2509 #define INDEX4_SUFFIX ".gdb-index"
2510 #define INDEX5_SUFFIX ".debug_names"
2511 #define DEBUG_STR_SUFFIX ".debug_str"
2513 /* See declaration. */
2515 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2516 const dwarf2_debug_sections *names)
2517 : objfile (objfile_)
2520 names = &dwarf2_elf_names;
2522 bfd *obfd = objfile->obfd;
2524 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2525 locate_sections (obfd, sec, *names);
2528 static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2530 dwarf2_per_objfile::~dwarf2_per_objfile ()
2532 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2533 free_cached_comp_units ();
2535 if (quick_file_names_table)
2536 htab_delete (quick_file_names_table);
2538 if (line_header_hash)
2539 htab_delete (line_header_hash);
2541 for (int ix = 0; ix < n_comp_units; ++ix)
2542 VEC_free (dwarf2_per_cu_ptr, all_comp_units[ix]->imported_symtabs);
2544 for (int ix = 0; ix < n_type_units; ++ix)
2545 VEC_free (dwarf2_per_cu_ptr,
2546 all_type_units[ix]->per_cu.imported_symtabs);
2547 xfree (all_type_units);
2549 VEC_free (dwarf2_section_info_def, types);
2551 if (dwo_files != NULL)
2552 free_dwo_files (dwo_files, objfile);
2553 if (dwp_file != NULL)
2554 gdb_bfd_unref (dwp_file->dbfd);
2556 if (dwz_file != NULL && dwz_file->dwz_bfd)
2557 gdb_bfd_unref (dwz_file->dwz_bfd);
2559 if (index_table != NULL)
2560 index_table->~mapped_index ();
2562 /* Everything else should be on the objfile obstack. */
2565 /* See declaration. */
2568 dwarf2_per_objfile::free_cached_comp_units ()
2570 dwarf2_per_cu_data *per_cu = read_in_chain;
2571 dwarf2_per_cu_data **last_chain = &read_in_chain;
2572 while (per_cu != NULL)
2574 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2577 *last_chain = next_cu;
2582 /* Try to locate the sections we need for DWARF 2 debugging
2583 information and return true if we have enough to do something.
2584 NAMES points to the dwarf2 section names, or is NULL if the standard
2585 ELF names are used. */
2588 dwarf2_has_info (struct objfile *objfile,
2589 const struct dwarf2_debug_sections *names)
2591 if (objfile->flags & OBJF_READNEVER)
2594 struct dwarf2_per_objfile *dwarf2_per_objfile
2595 = get_dwarf2_per_objfile (objfile);
2597 if (dwarf2_per_objfile == NULL)
2599 /* Initialize per-objfile state. */
2601 = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
2603 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
2605 return (!dwarf2_per_objfile->info.is_virtual
2606 && dwarf2_per_objfile->info.s.section != NULL
2607 && !dwarf2_per_objfile->abbrev.is_virtual
2608 && dwarf2_per_objfile->abbrev.s.section != NULL);
2611 /* Return the containing section of virtual section SECTION. */
2613 static struct dwarf2_section_info *
2614 get_containing_section (const struct dwarf2_section_info *section)
2616 gdb_assert (section->is_virtual);
2617 return section->s.containing_section;
2620 /* Return the bfd owner of SECTION. */
2623 get_section_bfd_owner (const struct dwarf2_section_info *section)
2625 if (section->is_virtual)
2627 section = get_containing_section (section);
2628 gdb_assert (!section->is_virtual);
2630 return section->s.section->owner;
2633 /* Return the bfd section of SECTION.
2634 Returns NULL if the section is not present. */
2637 get_section_bfd_section (const struct dwarf2_section_info *section)
2639 if (section->is_virtual)
2641 section = get_containing_section (section);
2642 gdb_assert (!section->is_virtual);
2644 return section->s.section;
2647 /* Return the name of SECTION. */
2650 get_section_name (const struct dwarf2_section_info *section)
2652 asection *sectp = get_section_bfd_section (section);
2654 gdb_assert (sectp != NULL);
2655 return bfd_section_name (get_section_bfd_owner (section), sectp);
2658 /* Return the name of the file SECTION is in. */
2661 get_section_file_name (const struct dwarf2_section_info *section)
2663 bfd *abfd = get_section_bfd_owner (section);
2665 return bfd_get_filename (abfd);
2668 /* Return the id of SECTION.
2669 Returns 0 if SECTION doesn't exist. */
2672 get_section_id (const struct dwarf2_section_info *section)
2674 asection *sectp = get_section_bfd_section (section);
2681 /* Return the flags of SECTION.
2682 SECTION (or containing section if this is a virtual section) must exist. */
2685 get_section_flags (const struct dwarf2_section_info *section)
2687 asection *sectp = get_section_bfd_section (section);
2689 gdb_assert (sectp != NULL);
2690 return bfd_get_section_flags (sectp->owner, sectp);
2693 /* When loading sections, we look either for uncompressed section or for
2694 compressed section names. */
2697 section_is_p (const char *section_name,
2698 const struct dwarf2_section_names *names)
2700 if (names->normal != NULL
2701 && strcmp (section_name, names->normal) == 0)
2703 if (names->compressed != NULL
2704 && strcmp (section_name, names->compressed) == 0)
2709 /* See declaration. */
2712 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2713 const dwarf2_debug_sections &names)
2715 flagword aflag = bfd_get_section_flags (abfd, sectp);
2717 if ((aflag & SEC_HAS_CONTENTS) == 0)
2720 else if (section_is_p (sectp->name, &names.info))
2722 this->info.s.section = sectp;
2723 this->info.size = bfd_get_section_size (sectp);
2725 else if (section_is_p (sectp->name, &names.abbrev))
2727 this->abbrev.s.section = sectp;
2728 this->abbrev.size = bfd_get_section_size (sectp);
2730 else if (section_is_p (sectp->name, &names.line))
2732 this->line.s.section = sectp;
2733 this->line.size = bfd_get_section_size (sectp);
2735 else if (section_is_p (sectp->name, &names.loc))
2737 this->loc.s.section = sectp;
2738 this->loc.size = bfd_get_section_size (sectp);
2740 else if (section_is_p (sectp->name, &names.loclists))
2742 this->loclists.s.section = sectp;
2743 this->loclists.size = bfd_get_section_size (sectp);
2745 else if (section_is_p (sectp->name, &names.macinfo))
2747 this->macinfo.s.section = sectp;
2748 this->macinfo.size = bfd_get_section_size (sectp);
2750 else if (section_is_p (sectp->name, &names.macro))
2752 this->macro.s.section = sectp;
2753 this->macro.size = bfd_get_section_size (sectp);
2755 else if (section_is_p (sectp->name, &names.str))
2757 this->str.s.section = sectp;
2758 this->str.size = bfd_get_section_size (sectp);
2760 else if (section_is_p (sectp->name, &names.line_str))
2762 this->line_str.s.section = sectp;
2763 this->line_str.size = bfd_get_section_size (sectp);
2765 else if (section_is_p (sectp->name, &names.addr))
2767 this->addr.s.section = sectp;
2768 this->addr.size = bfd_get_section_size (sectp);
2770 else if (section_is_p (sectp->name, &names.frame))
2772 this->frame.s.section = sectp;
2773 this->frame.size = bfd_get_section_size (sectp);
2775 else if (section_is_p (sectp->name, &names.eh_frame))
2777 this->eh_frame.s.section = sectp;
2778 this->eh_frame.size = bfd_get_section_size (sectp);
2780 else if (section_is_p (sectp->name, &names.ranges))
2782 this->ranges.s.section = sectp;
2783 this->ranges.size = bfd_get_section_size (sectp);
2785 else if (section_is_p (sectp->name, &names.rnglists))
2787 this->rnglists.s.section = sectp;
2788 this->rnglists.size = bfd_get_section_size (sectp);
2790 else if (section_is_p (sectp->name, &names.types))
2792 struct dwarf2_section_info type_section;
2794 memset (&type_section, 0, sizeof (type_section));
2795 type_section.s.section = sectp;
2796 type_section.size = bfd_get_section_size (sectp);
2798 VEC_safe_push (dwarf2_section_info_def, this->types,
2801 else if (section_is_p (sectp->name, &names.gdb_index))
2803 this->gdb_index.s.section = sectp;
2804 this->gdb_index.size = bfd_get_section_size (sectp);
2806 else if (section_is_p (sectp->name, &names.debug_names))
2808 this->debug_names.s.section = sectp;
2809 this->debug_names.size = bfd_get_section_size (sectp);
2811 else if (section_is_p (sectp->name, &names.debug_aranges))
2813 this->debug_aranges.s.section = sectp;
2814 this->debug_aranges.size = bfd_get_section_size (sectp);
2817 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2818 && bfd_section_vma (abfd, sectp) == 0)
2819 this->has_section_at_zero = true;
2822 /* A helper function that decides whether a section is empty,
2826 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2828 if (section->is_virtual)
2829 return section->size == 0;
2830 return section->s.section == NULL || section->size == 0;
2833 /* Read the contents of the section INFO.
2834 OBJFILE is the main object file, but not necessarily the file where
2835 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2837 If the section is compressed, uncompress it before returning. */
2840 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2844 gdb_byte *buf, *retbuf;
2848 info->buffer = NULL;
2851 if (dwarf2_section_empty_p (info))
2854 sectp = get_section_bfd_section (info);
2856 /* If this is a virtual section we need to read in the real one first. */
2857 if (info->is_virtual)
2859 struct dwarf2_section_info *containing_section =
2860 get_containing_section (info);
2862 gdb_assert (sectp != NULL);
2863 if ((sectp->flags & SEC_RELOC) != 0)
2865 error (_("Dwarf Error: DWP format V2 with relocations is not"
2866 " supported in section %s [in module %s]"),
2867 get_section_name (info), get_section_file_name (info));
2869 dwarf2_read_section (objfile, containing_section);
2870 /* Other code should have already caught virtual sections that don't
2872 gdb_assert (info->virtual_offset + info->size
2873 <= containing_section->size);
2874 /* If the real section is empty or there was a problem reading the
2875 section we shouldn't get here. */
2876 gdb_assert (containing_section->buffer != NULL);
2877 info->buffer = containing_section->buffer + info->virtual_offset;
2881 /* If the section has relocations, we must read it ourselves.
2882 Otherwise we attach it to the BFD. */
2883 if ((sectp->flags & SEC_RELOC) == 0)
2885 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2889 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2892 /* When debugging .o files, we may need to apply relocations; see
2893 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2894 We never compress sections in .o files, so we only need to
2895 try this when the section is not compressed. */
2896 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2899 info->buffer = retbuf;
2903 abfd = get_section_bfd_owner (info);
2904 gdb_assert (abfd != NULL);
2906 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2907 || bfd_bread (buf, info->size, abfd) != info->size)
2909 error (_("Dwarf Error: Can't read DWARF data"
2910 " in section %s [in module %s]"),
2911 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2915 /* A helper function that returns the size of a section in a safe way.
2916 If you are positive that the section has been read before using the
2917 size, then it is safe to refer to the dwarf2_section_info object's
2918 "size" field directly. In other cases, you must call this
2919 function, because for compressed sections the size field is not set
2920 correctly until the section has been read. */
2922 static bfd_size_type
2923 dwarf2_section_size (struct objfile *objfile,
2924 struct dwarf2_section_info *info)
2927 dwarf2_read_section (objfile, info);
2931 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2935 dwarf2_get_section_info (struct objfile *objfile,
2936 enum dwarf2_section_enum sect,
2937 asection **sectp, const gdb_byte **bufp,
2938 bfd_size_type *sizep)
2940 struct dwarf2_per_objfile *data
2941 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2942 dwarf2_objfile_data_key);
2943 struct dwarf2_section_info *info;
2945 /* We may see an objfile without any DWARF, in which case we just
2956 case DWARF2_DEBUG_FRAME:
2957 info = &data->frame;
2959 case DWARF2_EH_FRAME:
2960 info = &data->eh_frame;
2963 gdb_assert_not_reached ("unexpected section");
2966 dwarf2_read_section (objfile, info);
2968 *sectp = get_section_bfd_section (info);
2969 *bufp = info->buffer;
2970 *sizep = info->size;
2973 /* A helper function to find the sections for a .dwz file. */
2976 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2978 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2980 /* Note that we only support the standard ELF names, because .dwz
2981 is ELF-only (at the time of writing). */
2982 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2984 dwz_file->abbrev.s.section = sectp;
2985 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2987 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2989 dwz_file->info.s.section = sectp;
2990 dwz_file->info.size = bfd_get_section_size (sectp);
2992 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2994 dwz_file->str.s.section = sectp;
2995 dwz_file->str.size = bfd_get_section_size (sectp);
2997 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2999 dwz_file->line.s.section = sectp;
3000 dwz_file->line.size = bfd_get_section_size (sectp);
3002 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
3004 dwz_file->macro.s.section = sectp;
3005 dwz_file->macro.size = bfd_get_section_size (sectp);
3007 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
3009 dwz_file->gdb_index.s.section = sectp;
3010 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
3012 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
3014 dwz_file->debug_names.s.section = sectp;
3015 dwz_file->debug_names.size = bfd_get_section_size (sectp);
3019 /* Open the separate '.dwz' debug file, if needed. Return NULL if
3020 there is no .gnu_debugaltlink section in the file. Error if there
3021 is such a section but the file cannot be found. */
3023 static struct dwz_file *
3024 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
3026 const char *filename;
3027 struct dwz_file *result;
3028 bfd_size_type buildid_len_arg;
3032 if (dwarf2_per_objfile->dwz_file != NULL)
3033 return dwarf2_per_objfile->dwz_file;
3035 bfd_set_error (bfd_error_no_error);
3036 gdb::unique_xmalloc_ptr<char> data
3037 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
3038 &buildid_len_arg, &buildid));
3041 if (bfd_get_error () == bfd_error_no_error)
3043 error (_("could not read '.gnu_debugaltlink' section: %s"),
3044 bfd_errmsg (bfd_get_error ()));
3047 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
3049 buildid_len = (size_t) buildid_len_arg;
3051 filename = data.get ();
3053 std::string abs_storage;
3054 if (!IS_ABSOLUTE_PATH (filename))
3056 gdb::unique_xmalloc_ptr<char> abs
3057 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
3059 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
3060 filename = abs_storage.c_str ();
3063 /* First try the file name given in the section. If that doesn't
3064 work, try to use the build-id instead. */
3065 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
3066 if (dwz_bfd != NULL)
3068 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
3072 if (dwz_bfd == NULL)
3073 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
3075 if (dwz_bfd == NULL)
3076 error (_("could not find '.gnu_debugaltlink' file for %s"),
3077 objfile_name (dwarf2_per_objfile->objfile));
3079 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
3081 result->dwz_bfd = dwz_bfd.release ();
3083 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
3085 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
3086 dwarf2_per_objfile->dwz_file = result;
3090 /* DWARF quick_symbols_functions support. */
3092 /* TUs can share .debug_line entries, and there can be a lot more TUs than
3093 unique line tables, so we maintain a separate table of all .debug_line
3094 derived entries to support the sharing.
3095 All the quick functions need is the list of file names. We discard the
3096 line_header when we're done and don't need to record it here. */
3097 struct quick_file_names
3099 /* The data used to construct the hash key. */
3100 struct stmt_list_hash hash;
3102 /* The number of entries in file_names, real_names. */
3103 unsigned int num_file_names;
3105 /* The file names from the line table, after being run through
3107 const char **file_names;
3109 /* The file names from the line table after being run through
3110 gdb_realpath. These are computed lazily. */
3111 const char **real_names;
3114 /* When using the index (and thus not using psymtabs), each CU has an
3115 object of this type. This is used to hold information needed by
3116 the various "quick" methods. */
3117 struct dwarf2_per_cu_quick_data
3119 /* The file table. This can be NULL if there was no file table
3120 or it's currently not read in.
3121 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
3122 struct quick_file_names *file_names;
3124 /* The corresponding symbol table. This is NULL if symbols for this
3125 CU have not yet been read. */
3126 struct compunit_symtab *compunit_symtab;
3128 /* A temporary mark bit used when iterating over all CUs in
3129 expand_symtabs_matching. */
3130 unsigned int mark : 1;
3132 /* True if we've tried to read the file table and found there isn't one.
3133 There will be no point in trying to read it again next time. */
3134 unsigned int no_file_data : 1;
3137 /* Utility hash function for a stmt_list_hash. */
3140 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
3144 if (stmt_list_hash->dwo_unit != NULL)
3145 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
3146 v += to_underlying (stmt_list_hash->line_sect_off);
3150 /* Utility equality function for a stmt_list_hash. */
3153 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
3154 const struct stmt_list_hash *rhs)
3156 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
3158 if (lhs->dwo_unit != NULL
3159 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
3162 return lhs->line_sect_off == rhs->line_sect_off;
3165 /* Hash function for a quick_file_names. */
3168 hash_file_name_entry (const void *e)
3170 const struct quick_file_names *file_data
3171 = (const struct quick_file_names *) e;
3173 return hash_stmt_list_entry (&file_data->hash);
3176 /* Equality function for a quick_file_names. */
3179 eq_file_name_entry (const void *a, const void *b)
3181 const struct quick_file_names *ea = (const struct quick_file_names *) a;
3182 const struct quick_file_names *eb = (const struct quick_file_names *) b;
3184 return eq_stmt_list_entry (&ea->hash, &eb->hash);
3187 /* Delete function for a quick_file_names. */
3190 delete_file_name_entry (void *e)
3192 struct quick_file_names *file_data = (struct quick_file_names *) e;
3195 for (i = 0; i < file_data->num_file_names; ++i)
3197 xfree ((void*) file_data->file_names[i]);
3198 if (file_data->real_names)
3199 xfree ((void*) file_data->real_names[i]);
3202 /* The space for the struct itself lives on objfile_obstack,
3203 so we don't free it here. */
3206 /* Create a quick_file_names hash table. */
3209 create_quick_file_names_table (unsigned int nr_initial_entries)
3211 return htab_create_alloc (nr_initial_entries,
3212 hash_file_name_entry, eq_file_name_entry,
3213 delete_file_name_entry, xcalloc, xfree);
3216 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
3217 have to be created afterwards. You should call age_cached_comp_units after
3218 processing PER_CU->CU. dw2_setup must have been already called. */
3221 load_cu (struct dwarf2_per_cu_data *per_cu)
3223 if (per_cu->is_debug_types)
3224 load_full_type_unit (per_cu);
3226 load_full_comp_unit (per_cu, language_minimal);
3228 if (per_cu->cu == NULL)
3229 return; /* Dummy CU. */
3231 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
3234 /* Read in the symbols for PER_CU. */
3237 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3239 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3241 /* Skip type_unit_groups, reading the type units they contain
3242 is handled elsewhere. */
3243 if (IS_TYPE_UNIT_GROUP (per_cu))
3246 /* The destructor of dwarf2_queue_guard frees any entries left on
3247 the queue. After this point we're guaranteed to leave this function
3248 with the dwarf queue empty. */
3249 dwarf2_queue_guard q_guard;
3251 if (dwarf2_per_objfile->using_index
3252 ? per_cu->v.quick->compunit_symtab == NULL
3253 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
3255 queue_comp_unit (per_cu, language_minimal);
3258 /* If we just loaded a CU from a DWO, and we're working with an index
3259 that may badly handle TUs, load all the TUs in that DWO as well.
3260 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
3261 if (!per_cu->is_debug_types
3262 && per_cu->cu != NULL
3263 && per_cu->cu->dwo_unit != NULL
3264 && dwarf2_per_objfile->index_table != NULL
3265 && dwarf2_per_objfile->index_table->version <= 7
3266 /* DWP files aren't supported yet. */
3267 && get_dwp_file (dwarf2_per_objfile) == NULL)
3268 queue_and_load_all_dwo_tus (per_cu);
3271 process_queue (dwarf2_per_objfile);
3273 /* Age the cache, releasing compilation units that have not
3274 been used recently. */
3275 age_cached_comp_units (dwarf2_per_objfile);
3278 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
3279 the objfile from which this CU came. Returns the resulting symbol
3282 static struct compunit_symtab *
3283 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3285 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3287 gdb_assert (dwarf2_per_objfile->using_index);
3288 if (!per_cu->v.quick->compunit_symtab)
3290 struct cleanup *back_to = make_cleanup (free_cached_comp_units,
3291 dwarf2_per_objfile);
3292 scoped_restore decrementer = increment_reading_symtab ();
3293 dw2_do_instantiate_symtab (per_cu);
3294 process_cu_includes (dwarf2_per_objfile);
3295 do_cleanups (back_to);
3298 return per_cu->v.quick->compunit_symtab;
3301 /* Return the CU/TU given its index.
3303 This is intended for loops like:
3305 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3306 + dwarf2_per_objfile->n_type_units); ++i)
3308 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3314 static struct dwarf2_per_cu_data *
3315 dw2_get_cutu (struct dwarf2_per_objfile *dwarf2_per_objfile,
3318 if (index >= dwarf2_per_objfile->n_comp_units)
3320 index -= dwarf2_per_objfile->n_comp_units;
3321 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3322 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
3325 return dwarf2_per_objfile->all_comp_units[index];
3328 /* Return the CU given its index.
3329 This differs from dw2_get_cutu in that it's for when you know INDEX
3332 static struct dwarf2_per_cu_data *
3333 dw2_get_cu (struct dwarf2_per_objfile *dwarf2_per_objfile, int index)
3335 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
3337 return dwarf2_per_objfile->all_comp_units[index];
3340 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3341 objfile_obstack, and constructed with the specified field
3344 static dwarf2_per_cu_data *
3345 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3346 struct dwarf2_section_info *section,
3348 sect_offset sect_off, ULONGEST length)
3350 struct objfile *objfile = dwarf2_per_objfile->objfile;
3351 dwarf2_per_cu_data *the_cu
3352 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3353 struct dwarf2_per_cu_data);
3354 the_cu->sect_off = sect_off;
3355 the_cu->length = length;
3356 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
3357 the_cu->section = section;
3358 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3359 struct dwarf2_per_cu_quick_data);
3360 the_cu->is_dwz = is_dwz;
3364 /* A helper for create_cus_from_index that handles a given list of
3368 create_cus_from_index_list (struct objfile *objfile,
3369 const gdb_byte *cu_list, offset_type n_elements,
3370 struct dwarf2_section_info *section,
3375 struct dwarf2_per_objfile *dwarf2_per_objfile
3376 = get_dwarf2_per_objfile (objfile);
3378 for (i = 0; i < n_elements; i += 2)
3380 gdb_static_assert (sizeof (ULONGEST) >= 8);
3382 sect_offset sect_off
3383 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3384 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3387 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
3388 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3393 /* Read the CU list from the mapped index, and use it to create all
3394 the CU objects for this objfile. */
3397 create_cus_from_index (struct objfile *objfile,
3398 const gdb_byte *cu_list, offset_type cu_list_elements,
3399 const gdb_byte *dwz_list, offset_type dwz_elements)
3401 struct dwz_file *dwz;
3402 struct dwarf2_per_objfile *dwarf2_per_objfile
3403 = get_dwarf2_per_objfile (objfile);
3405 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
3406 dwarf2_per_objfile->all_comp_units =
3407 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3408 dwarf2_per_objfile->n_comp_units);
3410 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3411 &dwarf2_per_objfile->info, 0, 0);
3413 if (dwz_elements == 0)
3416 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3417 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3418 cu_list_elements / 2);
3421 /* Create the signatured type hash table from the index. */
3424 create_signatured_type_table_from_index (struct objfile *objfile,
3425 struct dwarf2_section_info *section,
3426 const gdb_byte *bytes,
3427 offset_type elements)
3430 htab_t sig_types_hash;
3431 struct dwarf2_per_objfile *dwarf2_per_objfile
3432 = get_dwarf2_per_objfile (objfile);
3434 dwarf2_per_objfile->n_type_units
3435 = dwarf2_per_objfile->n_allocated_type_units
3437 dwarf2_per_objfile->all_type_units =
3438 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3440 sig_types_hash = allocate_signatured_type_table (objfile);
3442 for (i = 0; i < elements; i += 3)
3444 struct signatured_type *sig_type;
3447 cu_offset type_offset_in_tu;
3449 gdb_static_assert (sizeof (ULONGEST) >= 8);
3450 sect_offset sect_off
3451 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3453 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3455 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3458 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3459 struct signatured_type);
3460 sig_type->signature = signature;
3461 sig_type->type_offset_in_tu = type_offset_in_tu;
3462 sig_type->per_cu.is_debug_types = 1;
3463 sig_type->per_cu.section = section;
3464 sig_type->per_cu.sect_off = sect_off;
3465 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3466 sig_type->per_cu.v.quick
3467 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3468 struct dwarf2_per_cu_quick_data);
3470 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3473 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3476 dwarf2_per_objfile->signatured_types = sig_types_hash;
3479 /* Create the signatured type hash table from .debug_names. */
3482 create_signatured_type_table_from_debug_names
3483 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3484 const mapped_debug_names &map,
3485 struct dwarf2_section_info *section,
3486 struct dwarf2_section_info *abbrev_section)
3488 struct objfile *objfile = dwarf2_per_objfile->objfile;
3490 dwarf2_read_section (objfile, section);
3491 dwarf2_read_section (objfile, abbrev_section);
3493 dwarf2_per_objfile->n_type_units
3494 = dwarf2_per_objfile->n_allocated_type_units
3496 dwarf2_per_objfile->all_type_units
3497 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3499 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3501 for (uint32_t i = 0; i < map.tu_count; ++i)
3503 struct signatured_type *sig_type;
3506 cu_offset type_offset_in_tu;
3508 sect_offset sect_off
3509 = (sect_offset) (extract_unsigned_integer
3510 (map.tu_table_reordered + i * map.offset_size,
3512 map.dwarf5_byte_order));
3514 comp_unit_head cu_header;
3515 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3517 section->buffer + to_underlying (sect_off),
3520 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3521 struct signatured_type);
3522 sig_type->signature = cu_header.signature;
3523 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3524 sig_type->per_cu.is_debug_types = 1;
3525 sig_type->per_cu.section = section;
3526 sig_type->per_cu.sect_off = sect_off;
3527 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3528 sig_type->per_cu.v.quick
3529 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3530 struct dwarf2_per_cu_quick_data);
3532 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3535 dwarf2_per_objfile->all_type_units[i] = sig_type;
3538 dwarf2_per_objfile->signatured_types = sig_types_hash;
3541 /* Read the address map data from the mapped index, and use it to
3542 populate the objfile's psymtabs_addrmap. */
3545 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3546 struct mapped_index *index)
3548 struct objfile *objfile = dwarf2_per_objfile->objfile;
3549 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3550 const gdb_byte *iter, *end;
3551 struct addrmap *mutable_map;
3554 auto_obstack temp_obstack;
3556 mutable_map = addrmap_create_mutable (&temp_obstack);
3558 iter = index->address_table.data ();
3559 end = iter + index->address_table.size ();
3561 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3565 ULONGEST hi, lo, cu_index;
3566 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3568 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3570 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3575 complaint (&symfile_complaints,
3576 _(".gdb_index address table has invalid range (%s - %s)"),
3577 hex_string (lo), hex_string (hi));
3581 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3583 complaint (&symfile_complaints,
3584 _(".gdb_index address table has invalid CU number %u"),
3585 (unsigned) cu_index);
3589 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3590 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3591 addrmap_set_empty (mutable_map, lo, hi - 1,
3592 dw2_get_cutu (dwarf2_per_objfile, cu_index));
3595 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3596 &objfile->objfile_obstack);
3599 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3600 populate the objfile's psymtabs_addrmap. */
3603 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3604 struct dwarf2_section_info *section)
3606 struct objfile *objfile = dwarf2_per_objfile->objfile;
3607 bfd *abfd = objfile->obfd;
3608 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3609 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3610 SECT_OFF_TEXT (objfile));
3612 auto_obstack temp_obstack;
3613 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3615 std::unordered_map<sect_offset,
3616 dwarf2_per_cu_data *,
3617 gdb::hash_enum<sect_offset>>
3618 debug_info_offset_to_per_cu;
3619 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3621 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, cui);
3622 const auto insertpair
3623 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3624 if (!insertpair.second)
3626 warning (_("Section .debug_aranges in %s has duplicate "
3627 "debug_info_offset %s, ignoring .debug_aranges."),
3628 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3633 dwarf2_read_section (objfile, section);
3635 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3637 const gdb_byte *addr = section->buffer;
3639 while (addr < section->buffer + section->size)
3641 const gdb_byte *const entry_addr = addr;
3642 unsigned int bytes_read;
3644 const LONGEST entry_length = read_initial_length (abfd, addr,
3648 const gdb_byte *const entry_end = addr + entry_length;
3649 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3650 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3651 if (addr + entry_length > section->buffer + section->size)
3653 warning (_("Section .debug_aranges in %s entry at offset %zu "
3654 "length %s exceeds section length %s, "
3655 "ignoring .debug_aranges."),
3656 objfile_name (objfile), entry_addr - section->buffer,
3657 plongest (bytes_read + entry_length),
3658 pulongest (section->size));
3662 /* The version number. */
3663 const uint16_t version = read_2_bytes (abfd, addr);
3667 warning (_("Section .debug_aranges in %s entry at offset %zu "
3668 "has unsupported version %d, ignoring .debug_aranges."),
3669 objfile_name (objfile), entry_addr - section->buffer,
3674 const uint64_t debug_info_offset
3675 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3676 addr += offset_size;
3677 const auto per_cu_it
3678 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3679 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3681 warning (_("Section .debug_aranges in %s entry at offset %zu "
3682 "debug_info_offset %s does not exists, "
3683 "ignoring .debug_aranges."),
3684 objfile_name (objfile), entry_addr - section->buffer,
3685 pulongest (debug_info_offset));
3688 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3690 const uint8_t address_size = *addr++;
3691 if (address_size < 1 || address_size > 8)
3693 warning (_("Section .debug_aranges in %s entry at offset %zu "
3694 "address_size %u is invalid, ignoring .debug_aranges."),
3695 objfile_name (objfile), entry_addr - section->buffer,
3700 const uint8_t segment_selector_size = *addr++;
3701 if (segment_selector_size != 0)
3703 warning (_("Section .debug_aranges in %s entry at offset %zu "
3704 "segment_selector_size %u is not supported, "
3705 "ignoring .debug_aranges."),
3706 objfile_name (objfile), entry_addr - section->buffer,
3707 segment_selector_size);
3711 /* Must pad to an alignment boundary that is twice the address
3712 size. It is undocumented by the DWARF standard but GCC does
3714 for (size_t padding = ((-(addr - section->buffer))
3715 & (2 * address_size - 1));
3716 padding > 0; padding--)
3719 warning (_("Section .debug_aranges in %s entry at offset %zu "
3720 "padding is not zero, ignoring .debug_aranges."),
3721 objfile_name (objfile), entry_addr - section->buffer);
3727 if (addr + 2 * address_size > entry_end)
3729 warning (_("Section .debug_aranges in %s entry at offset %zu "
3730 "address list is not properly terminated, "
3731 "ignoring .debug_aranges."),
3732 objfile_name (objfile), entry_addr - section->buffer);
3735 ULONGEST start = extract_unsigned_integer (addr, address_size,
3737 addr += address_size;
3738 ULONGEST length = extract_unsigned_integer (addr, address_size,
3740 addr += address_size;
3741 if (start == 0 && length == 0)
3743 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3745 /* Symbol was eliminated due to a COMDAT group. */
3748 ULONGEST end = start + length;
3749 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3750 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3751 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3755 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3756 &objfile->objfile_obstack);
3759 /* The hash function for strings in the mapped index. This is the same as
3760 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3761 implementation. This is necessary because the hash function is tied to the
3762 format of the mapped index file. The hash values do not have to match with
3765 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3768 mapped_index_string_hash (int index_version, const void *p)
3770 const unsigned char *str = (const unsigned char *) p;
3774 while ((c = *str++) != 0)
3776 if (index_version >= 5)
3778 r = r * 67 + c - 113;
3784 /* Find a slot in the mapped index INDEX for the object named NAME.
3785 If NAME is found, set *VEC_OUT to point to the CU vector in the
3786 constant pool and return true. If NAME cannot be found, return
3790 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3791 offset_type **vec_out)
3794 offset_type slot, step;
3795 int (*cmp) (const char *, const char *);
3797 gdb::unique_xmalloc_ptr<char> without_params;
3798 if (current_language->la_language == language_cplus
3799 || current_language->la_language == language_fortran
3800 || current_language->la_language == language_d)
3802 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3805 if (strchr (name, '(') != NULL)
3807 without_params = cp_remove_params (name);
3809 if (without_params != NULL)
3810 name = without_params.get ();
3814 /* Index version 4 did not support case insensitive searches. But the
3815 indices for case insensitive languages are built in lowercase, therefore
3816 simulate our NAME being searched is also lowercased. */
3817 hash = mapped_index_string_hash ((index->version == 4
3818 && case_sensitivity == case_sensitive_off
3819 ? 5 : index->version),
3822 slot = hash & (index->symbol_table.size () - 1);
3823 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3824 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3830 const auto &bucket = index->symbol_table[slot];
3831 if (bucket.name == 0 && bucket.vec == 0)
3834 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3835 if (!cmp (name, str))
3837 *vec_out = (offset_type *) (index->constant_pool
3838 + MAYBE_SWAP (bucket.vec));
3842 slot = (slot + step) & (index->symbol_table.size () - 1);
3846 /* A helper function that reads the .gdb_index from SECTION and fills
3847 in MAP. FILENAME is the name of the file containing the section;
3848 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3849 ok to use deprecated sections.
3851 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3852 out parameters that are filled in with information about the CU and
3853 TU lists in the section.
3855 Returns 1 if all went well, 0 otherwise. */
3858 read_index_from_section (struct objfile *objfile,
3859 const char *filename,
3861 struct dwarf2_section_info *section,
3862 struct mapped_index *map,
3863 const gdb_byte **cu_list,
3864 offset_type *cu_list_elements,
3865 const gdb_byte **types_list,
3866 offset_type *types_list_elements)
3868 const gdb_byte *addr;
3869 offset_type version;
3870 offset_type *metadata;
3873 if (dwarf2_section_empty_p (section))
3876 /* Older elfutils strip versions could keep the section in the main
3877 executable while splitting it for the separate debug info file. */
3878 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3881 dwarf2_read_section (objfile, section);
3883 addr = section->buffer;
3884 /* Version check. */
3885 version = MAYBE_SWAP (*(offset_type *) addr);
3886 /* Versions earlier than 3 emitted every copy of a psymbol. This
3887 causes the index to behave very poorly for certain requests. Version 3
3888 contained incomplete addrmap. So, it seems better to just ignore such
3892 static int warning_printed = 0;
3893 if (!warning_printed)
3895 warning (_("Skipping obsolete .gdb_index section in %s."),
3897 warning_printed = 1;
3901 /* Index version 4 uses a different hash function than index version
3904 Versions earlier than 6 did not emit psymbols for inlined
3905 functions. Using these files will cause GDB not to be able to
3906 set breakpoints on inlined functions by name, so we ignore these
3907 indices unless the user has done
3908 "set use-deprecated-index-sections on". */
3909 if (version < 6 && !deprecated_ok)
3911 static int warning_printed = 0;
3912 if (!warning_printed)
3915 Skipping deprecated .gdb_index section in %s.\n\
3916 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3917 to use the section anyway."),
3919 warning_printed = 1;
3923 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3924 of the TU (for symbols coming from TUs),
3925 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3926 Plus gold-generated indices can have duplicate entries for global symbols,
3927 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3928 These are just performance bugs, and we can't distinguish gdb-generated
3929 indices from gold-generated ones, so issue no warning here. */
3931 /* Indexes with higher version than the one supported by GDB may be no
3932 longer backward compatible. */
3936 map->version = version;
3937 map->total_size = section->size;
3939 metadata = (offset_type *) (addr + sizeof (offset_type));
3942 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3943 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3947 *types_list = addr + MAYBE_SWAP (metadata[i]);
3948 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3949 - MAYBE_SWAP (metadata[i]))
3953 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3954 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3956 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3959 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3960 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3962 = gdb::array_view<mapped_index::symbol_table_slot>
3963 ((mapped_index::symbol_table_slot *) symbol_table,
3964 (mapped_index::symbol_table_slot *) symbol_table_end);
3967 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3972 /* Read .gdb_index. If everything went ok, initialize the "quick"
3973 elements of all the CUs and return 1. Otherwise, return 0. */
3976 dwarf2_read_index (struct objfile *objfile)
3978 struct mapped_index local_map, *map;
3979 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3980 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3981 struct dwz_file *dwz;
3982 struct dwarf2_per_objfile *dwarf2_per_objfile
3983 = get_dwarf2_per_objfile (objfile);
3985 if (!read_index_from_section (objfile, objfile_name (objfile),
3986 use_deprecated_index_sections,
3987 &dwarf2_per_objfile->gdb_index, &local_map,
3988 &cu_list, &cu_list_elements,
3989 &types_list, &types_list_elements))
3992 /* Don't use the index if it's empty. */
3993 if (local_map.symbol_table.empty ())
3996 /* If there is a .dwz file, read it so we can get its CU list as
3998 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4001 struct mapped_index dwz_map;
4002 const gdb_byte *dwz_types_ignore;
4003 offset_type dwz_types_elements_ignore;
4005 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
4007 &dwz->gdb_index, &dwz_map,
4008 &dwz_list, &dwz_list_elements,
4010 &dwz_types_elements_ignore))
4012 warning (_("could not read '.gdb_index' section from %s; skipping"),
4013 bfd_get_filename (dwz->dwz_bfd));
4018 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
4021 if (types_list_elements)
4023 struct dwarf2_section_info *section;
4025 /* We can only handle a single .debug_types when we have an
4027 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
4030 section = VEC_index (dwarf2_section_info_def,
4031 dwarf2_per_objfile->types, 0);
4033 create_signatured_type_table_from_index (objfile, section, types_list,
4034 types_list_elements);
4037 create_addrmap_from_index (dwarf2_per_objfile, &local_map);
4039 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
4040 map = new (map) mapped_index ();
4043 dwarf2_per_objfile->index_table = map;
4044 dwarf2_per_objfile->using_index = 1;
4045 dwarf2_per_objfile->quick_file_names_table =
4046 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
4051 /* die_reader_func for dw2_get_file_names. */
4054 dw2_get_file_names_reader (const struct die_reader_specs *reader,
4055 const gdb_byte *info_ptr,
4056 struct die_info *comp_unit_die,
4060 struct dwarf2_cu *cu = reader->cu;
4061 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
4062 struct dwarf2_per_objfile *dwarf2_per_objfile
4063 = cu->per_cu->dwarf2_per_objfile;
4064 struct objfile *objfile = dwarf2_per_objfile->objfile;
4065 struct dwarf2_per_cu_data *lh_cu;
4066 struct attribute *attr;
4069 struct quick_file_names *qfn;
4071 gdb_assert (! this_cu->is_debug_types);
4073 /* Our callers never want to match partial units -- instead they
4074 will match the enclosing full CU. */
4075 if (comp_unit_die->tag == DW_TAG_partial_unit)
4077 this_cu->v.quick->no_file_data = 1;
4085 sect_offset line_offset {};
4087 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4090 struct quick_file_names find_entry;
4092 line_offset = (sect_offset) DW_UNSND (attr);
4094 /* We may have already read in this line header (TU line header sharing).
4095 If we have we're done. */
4096 find_entry.hash.dwo_unit = cu->dwo_unit;
4097 find_entry.hash.line_sect_off = line_offset;
4098 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
4099 &find_entry, INSERT);
4102 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
4106 lh = dwarf_decode_line_header (line_offset, cu);
4110 lh_cu->v.quick->no_file_data = 1;
4114 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
4115 qfn->hash.dwo_unit = cu->dwo_unit;
4116 qfn->hash.line_sect_off = line_offset;
4117 gdb_assert (slot != NULL);
4120 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
4122 qfn->num_file_names = lh->file_names.size ();
4124 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
4125 for (i = 0; i < lh->file_names.size (); ++i)
4126 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
4127 qfn->real_names = NULL;
4129 lh_cu->v.quick->file_names = qfn;
4132 /* A helper for the "quick" functions which attempts to read the line
4133 table for THIS_CU. */
4135 static struct quick_file_names *
4136 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
4138 /* This should never be called for TUs. */
4139 gdb_assert (! this_cu->is_debug_types);
4140 /* Nor type unit groups. */
4141 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
4143 if (this_cu->v.quick->file_names != NULL)
4144 return this_cu->v.quick->file_names;
4145 /* If we know there is no line data, no point in looking again. */
4146 if (this_cu->v.quick->no_file_data)
4149 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
4151 if (this_cu->v.quick->no_file_data)
4153 return this_cu->v.quick->file_names;
4156 /* A helper for the "quick" functions which computes and caches the
4157 real path for a given file name from the line table. */
4160 dw2_get_real_path (struct objfile *objfile,
4161 struct quick_file_names *qfn, int index)
4163 if (qfn->real_names == NULL)
4164 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
4165 qfn->num_file_names, const char *);
4167 if (qfn->real_names[index] == NULL)
4168 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
4170 return qfn->real_names[index];
4173 static struct symtab *
4174 dw2_find_last_source_symtab (struct objfile *objfile)
4176 struct dwarf2_per_objfile *dwarf2_per_objfile
4177 = get_dwarf2_per_objfile (objfile);
4178 int index = dwarf2_per_objfile->n_comp_units - 1;
4179 dwarf2_per_cu_data *dwarf_cu = dw2_get_cutu (dwarf2_per_objfile, index);
4180 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu);
4185 return compunit_primary_filetab (cust);
4188 /* Traversal function for dw2_forget_cached_source_info. */
4191 dw2_free_cached_file_names (void **slot, void *info)
4193 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
4195 if (file_data->real_names)
4199 for (i = 0; i < file_data->num_file_names; ++i)
4201 xfree ((void*) file_data->real_names[i]);
4202 file_data->real_names[i] = NULL;
4210 dw2_forget_cached_source_info (struct objfile *objfile)
4212 struct dwarf2_per_objfile *dwarf2_per_objfile
4213 = get_dwarf2_per_objfile (objfile);
4215 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
4216 dw2_free_cached_file_names, NULL);
4219 /* Helper function for dw2_map_symtabs_matching_filename that expands
4220 the symtabs and calls the iterator. */
4223 dw2_map_expand_apply (struct objfile *objfile,
4224 struct dwarf2_per_cu_data *per_cu,
4225 const char *name, const char *real_path,
4226 gdb::function_view<bool (symtab *)> callback)
4228 struct compunit_symtab *last_made = objfile->compunit_symtabs;
4230 /* Don't visit already-expanded CUs. */
4231 if (per_cu->v.quick->compunit_symtab)
4234 /* This may expand more than one symtab, and we want to iterate over
4236 dw2_instantiate_symtab (per_cu);
4238 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
4239 last_made, callback);
4242 /* Implementation of the map_symtabs_matching_filename method. */
4245 dw2_map_symtabs_matching_filename
4246 (struct objfile *objfile, const char *name, const char *real_path,
4247 gdb::function_view<bool (symtab *)> callback)
4250 const char *name_basename = lbasename (name);
4251 struct dwarf2_per_objfile *dwarf2_per_objfile
4252 = get_dwarf2_per_objfile (objfile);
4254 /* The rule is CUs specify all the files, including those used by
4255 any TU, so there's no need to scan TUs here. */
4257 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4260 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
4261 struct quick_file_names *file_data;
4263 /* We only need to look at symtabs not already expanded. */
4264 if (per_cu->v.quick->compunit_symtab)
4267 file_data = dw2_get_file_names (per_cu);
4268 if (file_data == NULL)
4271 for (j = 0; j < file_data->num_file_names; ++j)
4273 const char *this_name = file_data->file_names[j];
4274 const char *this_real_name;
4276 if (compare_filenames_for_search (this_name, name))
4278 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4284 /* Before we invoke realpath, which can get expensive when many
4285 files are involved, do a quick comparison of the basenames. */
4286 if (! basenames_may_differ
4287 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
4290 this_real_name = dw2_get_real_path (objfile, file_data, j);
4291 if (compare_filenames_for_search (this_real_name, name))
4293 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4299 if (real_path != NULL)
4301 gdb_assert (IS_ABSOLUTE_PATH (real_path));
4302 gdb_assert (IS_ABSOLUTE_PATH (name));
4303 if (this_real_name != NULL
4304 && FILENAME_CMP (real_path, this_real_name) == 0)
4306 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4318 /* Struct used to manage iterating over all CUs looking for a symbol. */
4320 struct dw2_symtab_iterator
4322 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
4323 struct dwarf2_per_objfile *dwarf2_per_objfile;
4324 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
4325 int want_specific_block;
4326 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
4327 Unused if !WANT_SPECIFIC_BLOCK. */
4329 /* The kind of symbol we're looking for. */
4331 /* The list of CUs from the index entry of the symbol,
4332 or NULL if not found. */
4334 /* The next element in VEC to look at. */
4336 /* The number of elements in VEC, or zero if there is no match. */
4338 /* Have we seen a global version of the symbol?
4339 If so we can ignore all further global instances.
4340 This is to work around gold/15646, inefficient gold-generated
4345 /* Initialize the index symtab iterator ITER.
4346 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
4347 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
4350 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
4351 struct dwarf2_per_objfile *dwarf2_per_objfile,
4352 int want_specific_block,
4357 iter->dwarf2_per_objfile = dwarf2_per_objfile;
4358 iter->want_specific_block = want_specific_block;
4359 iter->block_index = block_index;
4360 iter->domain = domain;
4362 iter->global_seen = 0;
4364 mapped_index *index = dwarf2_per_objfile->index_table;
4366 /* index is NULL if OBJF_READNOW. */
4367 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
4368 iter->length = MAYBE_SWAP (*iter->vec);
4376 /* Return the next matching CU or NULL if there are no more. */
4378 static struct dwarf2_per_cu_data *
4379 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4381 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
4383 for ( ; iter->next < iter->length; ++iter->next)
4385 offset_type cu_index_and_attrs =
4386 MAYBE_SWAP (iter->vec[iter->next + 1]);
4387 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4388 struct dwarf2_per_cu_data *per_cu;
4389 int want_static = iter->block_index != GLOBAL_BLOCK;
4390 /* This value is only valid for index versions >= 7. */
4391 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4392 gdb_index_symbol_kind symbol_kind =
4393 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4394 /* Only check the symbol attributes if they're present.
4395 Indices prior to version 7 don't record them,
4396 and indices >= 7 may elide them for certain symbols
4397 (gold does this). */
4399 (dwarf2_per_objfile->index_table->version >= 7
4400 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4402 /* Don't crash on bad data. */
4403 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4404 + dwarf2_per_objfile->n_type_units))
4406 complaint (&symfile_complaints,
4407 _(".gdb_index entry has bad CU index"
4409 objfile_name (dwarf2_per_objfile->objfile));
4413 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
4415 /* Skip if already read in. */
4416 if (per_cu->v.quick->compunit_symtab)
4419 /* Check static vs global. */
4422 if (iter->want_specific_block
4423 && want_static != is_static)
4425 /* Work around gold/15646. */
4426 if (!is_static && iter->global_seen)
4429 iter->global_seen = 1;
4432 /* Only check the symbol's kind if it has one. */
4435 switch (iter->domain)
4438 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4439 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4440 /* Some types are also in VAR_DOMAIN. */
4441 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4445 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4449 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4464 static struct compunit_symtab *
4465 dw2_lookup_symbol (struct objfile *objfile, int block_index,
4466 const char *name, domain_enum domain)
4468 struct compunit_symtab *stab_best = NULL;
4469 struct dwarf2_per_objfile *dwarf2_per_objfile
4470 = get_dwarf2_per_objfile (objfile);
4472 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4474 struct dw2_symtab_iterator iter;
4475 struct dwarf2_per_cu_data *per_cu;
4477 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
4479 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4481 struct symbol *sym, *with_opaque = NULL;
4482 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4483 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4484 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4486 sym = block_find_symbol (block, name, domain,
4487 block_find_non_opaque_type_preferred,
4490 /* Some caution must be observed with overloaded functions
4491 and methods, since the index will not contain any overload
4492 information (but NAME might contain it). */
4495 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4497 if (with_opaque != NULL
4498 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4501 /* Keep looking through other CUs. */
4508 dw2_print_stats (struct objfile *objfile)
4510 struct dwarf2_per_objfile *dwarf2_per_objfile
4511 = get_dwarf2_per_objfile (objfile);
4512 int total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4515 for (int i = 0; i < total; ++i)
4517 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4519 if (!per_cu->v.quick->compunit_symtab)
4522 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4523 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4526 /* This dumps minimal information about the index.
4527 It is called via "mt print objfiles".
4528 One use is to verify .gdb_index has been loaded by the
4529 gdb.dwarf2/gdb-index.exp testcase. */
4532 dw2_dump (struct objfile *objfile)
4534 struct dwarf2_per_objfile *dwarf2_per_objfile
4535 = get_dwarf2_per_objfile (objfile);
4537 gdb_assert (dwarf2_per_objfile->using_index);
4538 printf_filtered (".gdb_index:");
4539 if (dwarf2_per_objfile->index_table != NULL)
4541 printf_filtered (" version %d\n",
4542 dwarf2_per_objfile->index_table->version);
4545 printf_filtered (" faked for \"readnow\"\n");
4546 printf_filtered ("\n");
4550 dw2_relocate (struct objfile *objfile,
4551 const struct section_offsets *new_offsets,
4552 const struct section_offsets *delta)
4554 /* There's nothing to relocate here. */
4558 dw2_expand_symtabs_for_function (struct objfile *objfile,
4559 const char *func_name)
4561 struct dwarf2_per_objfile *dwarf2_per_objfile
4562 = get_dwarf2_per_objfile (objfile);
4564 struct dw2_symtab_iterator iter;
4565 struct dwarf2_per_cu_data *per_cu;
4567 /* Note: It doesn't matter what we pass for block_index here. */
4568 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4571 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4572 dw2_instantiate_symtab (per_cu);
4577 dw2_expand_all_symtabs (struct objfile *objfile)
4579 struct dwarf2_per_objfile *dwarf2_per_objfile
4580 = get_dwarf2_per_objfile (objfile);
4581 int total_units = (dwarf2_per_objfile->n_comp_units
4582 + dwarf2_per_objfile->n_type_units);
4584 for (int i = 0; i < total_units; ++i)
4586 struct dwarf2_per_cu_data *per_cu
4587 = dw2_get_cutu (dwarf2_per_objfile, i);
4589 dw2_instantiate_symtab (per_cu);
4594 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4595 const char *fullname)
4597 struct dwarf2_per_objfile *dwarf2_per_objfile
4598 = get_dwarf2_per_objfile (objfile);
4600 /* We don't need to consider type units here.
4601 This is only called for examining code, e.g. expand_line_sal.
4602 There can be an order of magnitude (or more) more type units
4603 than comp units, and we avoid them if we can. */
4605 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4608 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4609 struct quick_file_names *file_data;
4611 /* We only need to look at symtabs not already expanded. */
4612 if (per_cu->v.quick->compunit_symtab)
4615 file_data = dw2_get_file_names (per_cu);
4616 if (file_data == NULL)
4619 for (j = 0; j < file_data->num_file_names; ++j)
4621 const char *this_fullname = file_data->file_names[j];
4623 if (filename_cmp (this_fullname, fullname) == 0)
4625 dw2_instantiate_symtab (per_cu);
4633 dw2_map_matching_symbols (struct objfile *objfile,
4634 const char * name, domain_enum domain,
4636 int (*callback) (struct block *,
4637 struct symbol *, void *),
4638 void *data, symbol_name_match_type match,
4639 symbol_compare_ftype *ordered_compare)
4641 /* Currently unimplemented; used for Ada. The function can be called if the
4642 current language is Ada for a non-Ada objfile using GNU index. As Ada
4643 does not look for non-Ada symbols this function should just return. */
4646 /* Symbol name matcher for .gdb_index names.
4648 Symbol names in .gdb_index have a few particularities:
4650 - There's no indication of which is the language of each symbol.
4652 Since each language has its own symbol name matching algorithm,
4653 and we don't know which language is the right one, we must match
4654 each symbol against all languages. This would be a potential
4655 performance problem if it were not mitigated by the
4656 mapped_index::name_components lookup table, which significantly
4657 reduces the number of times we need to call into this matcher,
4658 making it a non-issue.
4660 - Symbol names in the index have no overload (parameter)
4661 information. I.e., in C++, "foo(int)" and "foo(long)" both
4662 appear as "foo" in the index, for example.
4664 This means that the lookup names passed to the symbol name
4665 matcher functions must have no parameter information either
4666 because (e.g.) symbol search name "foo" does not match
4667 lookup-name "foo(int)" [while swapping search name for lookup
4670 class gdb_index_symbol_name_matcher
4673 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4674 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4676 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4677 Returns true if any matcher matches. */
4678 bool matches (const char *symbol_name);
4681 /* A reference to the lookup name we're matching against. */
4682 const lookup_name_info &m_lookup_name;
4684 /* A vector holding all the different symbol name matchers, for all
4686 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4689 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4690 (const lookup_name_info &lookup_name)
4691 : m_lookup_name (lookup_name)
4693 /* Prepare the vector of comparison functions upfront, to avoid
4694 doing the same work for each symbol. Care is taken to avoid
4695 matching with the same matcher more than once if/when multiple
4696 languages use the same matcher function. */
4697 auto &matchers = m_symbol_name_matcher_funcs;
4698 matchers.reserve (nr_languages);
4700 matchers.push_back (default_symbol_name_matcher);
4702 for (int i = 0; i < nr_languages; i++)
4704 const language_defn *lang = language_def ((enum language) i);
4705 symbol_name_matcher_ftype *name_matcher
4706 = get_symbol_name_matcher (lang, m_lookup_name);
4708 /* Don't insert the same comparison routine more than once.
4709 Note that we do this linear walk instead of a seemingly
4710 cheaper sorted insert, or use a std::set or something like
4711 that, because relative order of function addresses is not
4712 stable. This is not a problem in practice because the number
4713 of supported languages is low, and the cost here is tiny
4714 compared to the number of searches we'll do afterwards using
4716 if (name_matcher != default_symbol_name_matcher
4717 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4718 == matchers.end ()))
4719 matchers.push_back (name_matcher);
4724 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4726 for (auto matches_name : m_symbol_name_matcher_funcs)
4727 if (matches_name (symbol_name, m_lookup_name, NULL))
4733 /* Starting from a search name, return the string that finds the upper
4734 bound of all strings that start with SEARCH_NAME in a sorted name
4735 list. Returns the empty string to indicate that the upper bound is
4736 the end of the list. */
4739 make_sort_after_prefix_name (const char *search_name)
4741 /* When looking to complete "func", we find the upper bound of all
4742 symbols that start with "func" by looking for where we'd insert
4743 the closest string that would follow "func" in lexicographical
4744 order. Usually, that's "func"-with-last-character-incremented,
4745 i.e. "fund". Mind non-ASCII characters, though. Usually those
4746 will be UTF-8 multi-byte sequences, but we can't be certain.
4747 Especially mind the 0xff character, which is a valid character in
4748 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4749 rule out compilers allowing it in identifiers. Note that
4750 conveniently, strcmp/strcasecmp are specified to compare
4751 characters interpreted as unsigned char. So what we do is treat
4752 the whole string as a base 256 number composed of a sequence of
4753 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4754 to 0, and carries 1 to the following more-significant position.
4755 If the very first character in SEARCH_NAME ends up incremented
4756 and carries/overflows, then the upper bound is the end of the
4757 list. The string after the empty string is also the empty
4760 Some examples of this operation:
4762 SEARCH_NAME => "+1" RESULT
4766 "\xff" "a" "\xff" => "\xff" "b"
4771 Then, with these symbols for example:
4777 completing "func" looks for symbols between "func" and
4778 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4779 which finds "func" and "func1", but not "fund".
4783 funcÿ (Latin1 'ÿ' [0xff])
4787 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4788 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4792 ÿÿ (Latin1 'ÿ' [0xff])
4795 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4796 the end of the list.
4798 std::string after = search_name;
4799 while (!after.empty () && (unsigned char) after.back () == 0xff)
4801 if (!after.empty ())
4802 after.back () = (unsigned char) after.back () + 1;
4806 /* See declaration. */
4808 std::pair<std::vector<name_component>::const_iterator,
4809 std::vector<name_component>::const_iterator>
4810 mapped_index_base::find_name_components_bounds
4811 (const lookup_name_info &lookup_name_without_params) const
4814 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4817 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4819 /* Comparison function object for lower_bound that matches against a
4820 given symbol name. */
4821 auto lookup_compare_lower = [&] (const name_component &elem,
4824 const char *elem_qualified = this->symbol_name_at (elem.idx);
4825 const char *elem_name = elem_qualified + elem.name_offset;
4826 return name_cmp (elem_name, name) < 0;
4829 /* Comparison function object for upper_bound that matches against a
4830 given symbol name. */
4831 auto lookup_compare_upper = [&] (const char *name,
4832 const name_component &elem)
4834 const char *elem_qualified = this->symbol_name_at (elem.idx);
4835 const char *elem_name = elem_qualified + elem.name_offset;
4836 return name_cmp (name, elem_name) < 0;
4839 auto begin = this->name_components.begin ();
4840 auto end = this->name_components.end ();
4842 /* Find the lower bound. */
4845 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4848 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4851 /* Find the upper bound. */
4854 if (lookup_name_without_params.completion_mode ())
4856 /* In completion mode, we want UPPER to point past all
4857 symbols names that have the same prefix. I.e., with
4858 these symbols, and completing "func":
4860 function << lower bound
4862 other_function << upper bound
4864 We find the upper bound by looking for the insertion
4865 point of "func"-with-last-character-incremented,
4867 std::string after = make_sort_after_prefix_name (cplus);
4870 return std::lower_bound (lower, end, after.c_str (),
4871 lookup_compare_lower);
4874 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4877 return {lower, upper};
4880 /* See declaration. */
4883 mapped_index_base::build_name_components ()
4885 if (!this->name_components.empty ())
4888 this->name_components_casing = case_sensitivity;
4890 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4892 /* The code below only knows how to break apart components of C++
4893 symbol names (and other languages that use '::' as
4894 namespace/module separator). If we add support for wild matching
4895 to some language that uses some other operator (E.g., Ada, Go and
4896 D use '.'), then we'll need to try splitting the symbol name
4897 according to that language too. Note that Ada does support wild
4898 matching, but doesn't currently support .gdb_index. */
4899 auto count = this->symbol_name_count ();
4900 for (offset_type idx = 0; idx < count; idx++)
4902 if (this->symbol_name_slot_invalid (idx))
4905 const char *name = this->symbol_name_at (idx);
4907 /* Add each name component to the name component table. */
4908 unsigned int previous_len = 0;
4909 for (unsigned int current_len = cp_find_first_component (name);
4910 name[current_len] != '\0';
4911 current_len += cp_find_first_component (name + current_len))
4913 gdb_assert (name[current_len] == ':');
4914 this->name_components.push_back ({previous_len, idx});
4915 /* Skip the '::'. */
4917 previous_len = current_len;
4919 this->name_components.push_back ({previous_len, idx});
4922 /* Sort name_components elements by name. */
4923 auto name_comp_compare = [&] (const name_component &left,
4924 const name_component &right)
4926 const char *left_qualified = this->symbol_name_at (left.idx);
4927 const char *right_qualified = this->symbol_name_at (right.idx);
4929 const char *left_name = left_qualified + left.name_offset;
4930 const char *right_name = right_qualified + right.name_offset;
4932 return name_cmp (left_name, right_name) < 0;
4935 std::sort (this->name_components.begin (),
4936 this->name_components.end (),
4940 /* Helper for dw2_expand_symtabs_matching that works with a
4941 mapped_index_base instead of the containing objfile. This is split
4942 to a separate function in order to be able to unit test the
4943 name_components matching using a mock mapped_index_base. For each
4944 symbol name that matches, calls MATCH_CALLBACK, passing it the
4945 symbol's index in the mapped_index_base symbol table. */
4948 dw2_expand_symtabs_matching_symbol
4949 (mapped_index_base &index,
4950 const lookup_name_info &lookup_name_in,
4951 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4952 enum search_domain kind,
4953 gdb::function_view<void (offset_type)> match_callback)
4955 lookup_name_info lookup_name_without_params
4956 = lookup_name_in.make_ignore_params ();
4957 gdb_index_symbol_name_matcher lookup_name_matcher
4958 (lookup_name_without_params);
4960 /* Build the symbol name component sorted vector, if we haven't
4962 index.build_name_components ();
4964 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4966 /* Now for each symbol name in range, check to see if we have a name
4967 match, and if so, call the MATCH_CALLBACK callback. */
4969 /* The same symbol may appear more than once in the range though.
4970 E.g., if we're looking for symbols that complete "w", and we have
4971 a symbol named "w1::w2", we'll find the two name components for
4972 that same symbol in the range. To be sure we only call the
4973 callback once per symbol, we first collect the symbol name
4974 indexes that matched in a temporary vector and ignore
4976 std::vector<offset_type> matches;
4977 matches.reserve (std::distance (bounds.first, bounds.second));
4979 for (; bounds.first != bounds.second; ++bounds.first)
4981 const char *qualified = index.symbol_name_at (bounds.first->idx);
4983 if (!lookup_name_matcher.matches (qualified)
4984 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4987 matches.push_back (bounds.first->idx);
4990 std::sort (matches.begin (), matches.end ());
4992 /* Finally call the callback, once per match. */
4994 for (offset_type idx : matches)
4998 match_callback (idx);
5003 /* Above we use a type wider than idx's for 'prev', since 0 and
5004 (offset_type)-1 are both possible values. */
5005 static_assert (sizeof (prev) > sizeof (offset_type), "");
5010 namespace selftests { namespace dw2_expand_symtabs_matching {
5012 /* A mock .gdb_index/.debug_names-like name index table, enough to
5013 exercise dw2_expand_symtabs_matching_symbol, which works with the
5014 mapped_index_base interface. Builds an index from the symbol list
5015 passed as parameter to the constructor. */
5016 class mock_mapped_index : public mapped_index_base
5019 mock_mapped_index (gdb::array_view<const char *> symbols)
5020 : m_symbol_table (symbols)
5023 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
5025 /* Return the number of names in the symbol table. */
5026 virtual size_t symbol_name_count () const
5028 return m_symbol_table.size ();
5031 /* Get the name of the symbol at IDX in the symbol table. */
5032 virtual const char *symbol_name_at (offset_type idx) const
5034 return m_symbol_table[idx];
5038 gdb::array_view<const char *> m_symbol_table;
5041 /* Convenience function that converts a NULL pointer to a "<null>"
5042 string, to pass to print routines. */
5045 string_or_null (const char *str)
5047 return str != NULL ? str : "<null>";
5050 /* Check if a lookup_name_info built from
5051 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
5052 index. EXPECTED_LIST is the list of expected matches, in expected
5053 matching order. If no match expected, then an empty list is
5054 specified. Returns true on success. On failure prints a warning
5055 indicating the file:line that failed, and returns false. */
5058 check_match (const char *file, int line,
5059 mock_mapped_index &mock_index,
5060 const char *name, symbol_name_match_type match_type,
5061 bool completion_mode,
5062 std::initializer_list<const char *> expected_list)
5064 lookup_name_info lookup_name (name, match_type, completion_mode);
5066 bool matched = true;
5068 auto mismatch = [&] (const char *expected_str,
5071 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
5072 "expected=\"%s\", got=\"%s\"\n"),
5074 (match_type == symbol_name_match_type::FULL
5076 name, string_or_null (expected_str), string_or_null (got));
5080 auto expected_it = expected_list.begin ();
5081 auto expected_end = expected_list.end ();
5083 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
5085 [&] (offset_type idx)
5087 const char *matched_name = mock_index.symbol_name_at (idx);
5088 const char *expected_str
5089 = expected_it == expected_end ? NULL : *expected_it++;
5091 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
5092 mismatch (expected_str, matched_name);
5095 const char *expected_str
5096 = expected_it == expected_end ? NULL : *expected_it++;
5097 if (expected_str != NULL)
5098 mismatch (expected_str, NULL);
5103 /* The symbols added to the mock mapped_index for testing (in
5105 static const char *test_symbols[] = {
5114 "ns2::tmpl<int>::foo2",
5115 "(anonymous namespace)::A::B::C",
5117 /* These are used to check that the increment-last-char in the
5118 matching algorithm for completion doesn't match "t1_fund" when
5119 completing "t1_func". */
5125 /* A UTF-8 name with multi-byte sequences to make sure that
5126 cp-name-parser understands this as a single identifier ("função"
5127 is "function" in PT). */
5130 /* \377 (0xff) is Latin1 'ÿ'. */
5133 /* \377 (0xff) is Latin1 'ÿ'. */
5137 /* A name with all sorts of complications. Starts with "z" to make
5138 it easier for the completion tests below. */
5139 #define Z_SYM_NAME \
5140 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
5141 "::tuple<(anonymous namespace)::ui*, " \
5142 "std::default_delete<(anonymous namespace)::ui>, void>"
5147 /* Returns true if the mapped_index_base::find_name_component_bounds
5148 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
5149 in completion mode. */
5152 check_find_bounds_finds (mapped_index_base &index,
5153 const char *search_name,
5154 gdb::array_view<const char *> expected_syms)
5156 lookup_name_info lookup_name (search_name,
5157 symbol_name_match_type::FULL, true);
5159 auto bounds = index.find_name_components_bounds (lookup_name);
5161 size_t distance = std::distance (bounds.first, bounds.second);
5162 if (distance != expected_syms.size ())
5165 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
5167 auto nc_elem = bounds.first + exp_elem;
5168 const char *qualified = index.symbol_name_at (nc_elem->idx);
5169 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
5176 /* Test the lower-level mapped_index::find_name_component_bounds
5180 test_mapped_index_find_name_component_bounds ()
5182 mock_mapped_index mock_index (test_symbols);
5184 mock_index.build_name_components ();
5186 /* Test the lower-level mapped_index::find_name_component_bounds
5187 method in completion mode. */
5189 static const char *expected_syms[] = {
5194 SELF_CHECK (check_find_bounds_finds (mock_index,
5195 "t1_func", expected_syms));
5198 /* Check that the increment-last-char in the name matching algorithm
5199 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
5201 static const char *expected_syms1[] = {
5205 SELF_CHECK (check_find_bounds_finds (mock_index,
5206 "\377", expected_syms1));
5208 static const char *expected_syms2[] = {
5211 SELF_CHECK (check_find_bounds_finds (mock_index,
5212 "\377\377", expected_syms2));
5216 /* Test dw2_expand_symtabs_matching_symbol. */
5219 test_dw2_expand_symtabs_matching_symbol ()
5221 mock_mapped_index mock_index (test_symbols);
5223 /* We let all tests run until the end even if some fails, for debug
5225 bool any_mismatch = false;
5227 /* Create the expected symbols list (an initializer_list). Needed
5228 because lists have commas, and we need to pass them to CHECK,
5229 which is a macro. */
5230 #define EXPECT(...) { __VA_ARGS__ }
5232 /* Wrapper for check_match that passes down the current
5233 __FILE__/__LINE__. */
5234 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
5235 any_mismatch |= !check_match (__FILE__, __LINE__, \
5237 NAME, MATCH_TYPE, COMPLETION_MODE, \
5240 /* Identity checks. */
5241 for (const char *sym : test_symbols)
5243 /* Should be able to match all existing symbols. */
5244 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
5247 /* Should be able to match all existing symbols with
5249 std::string with_params = std::string (sym) + "(int)";
5250 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5253 /* Should be able to match all existing symbols with
5254 parameters and qualifiers. */
5255 with_params = std::string (sym) + " ( int ) const";
5256 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5259 /* This should really find sym, but cp-name-parser.y doesn't
5260 know about lvalue/rvalue qualifiers yet. */
5261 with_params = std::string (sym) + " ( int ) &&";
5262 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5266 /* Check that the name matching algorithm for completion doesn't get
5267 confused with Latin1 'ÿ' / 0xff. */
5269 static const char str[] = "\377";
5270 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5271 EXPECT ("\377", "\377\377123"));
5274 /* Check that the increment-last-char in the matching algorithm for
5275 completion doesn't match "t1_fund" when completing "t1_func". */
5277 static const char str[] = "t1_func";
5278 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5279 EXPECT ("t1_func", "t1_func1"));
5282 /* Check that completion mode works at each prefix of the expected
5285 static const char str[] = "function(int)";
5286 size_t len = strlen (str);
5289 for (size_t i = 1; i < len; i++)
5291 lookup.assign (str, i);
5292 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5293 EXPECT ("function"));
5297 /* While "w" is a prefix of both components, the match function
5298 should still only be called once. */
5300 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
5302 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
5306 /* Same, with a "complicated" symbol. */
5308 static const char str[] = Z_SYM_NAME;
5309 size_t len = strlen (str);
5312 for (size_t i = 1; i < len; i++)
5314 lookup.assign (str, i);
5315 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5316 EXPECT (Z_SYM_NAME));
5320 /* In FULL mode, an incomplete symbol doesn't match. */
5322 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
5326 /* A complete symbol with parameters matches any overload, since the
5327 index has no overload info. */
5329 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
5330 EXPECT ("std::zfunction", "std::zfunction2"));
5331 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
5332 EXPECT ("std::zfunction", "std::zfunction2"));
5333 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
5334 EXPECT ("std::zfunction", "std::zfunction2"));
5337 /* Check that whitespace is ignored appropriately. A symbol with a
5338 template argument list. */
5340 static const char expected[] = "ns::foo<int>";
5341 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
5343 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
5347 /* Check that whitespace is ignored appropriately. A symbol with a
5348 template argument list that includes a pointer. */
5350 static const char expected[] = "ns::foo<char*>";
5351 /* Try both completion and non-completion modes. */
5352 static const bool completion_mode[2] = {false, true};
5353 for (size_t i = 0; i < 2; i++)
5355 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
5356 completion_mode[i], EXPECT (expected));
5357 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
5358 completion_mode[i], EXPECT (expected));
5360 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
5361 completion_mode[i], EXPECT (expected));
5362 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
5363 completion_mode[i], EXPECT (expected));
5368 /* Check method qualifiers are ignored. */
5369 static const char expected[] = "ns::foo<char*>";
5370 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
5371 symbol_name_match_type::FULL, true, EXPECT (expected));
5372 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
5373 symbol_name_match_type::FULL, true, EXPECT (expected));
5374 CHECK_MATCH ("foo < char * > ( int ) const",
5375 symbol_name_match_type::WILD, true, EXPECT (expected));
5376 CHECK_MATCH ("foo < char * > ( int ) &&",
5377 symbol_name_match_type::WILD, true, EXPECT (expected));
5380 /* Test lookup names that don't match anything. */
5382 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
5385 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
5389 /* Some wild matching tests, exercising "(anonymous namespace)",
5390 which should not be confused with a parameter list. */
5392 static const char *syms[] = {
5396 "A :: B :: C ( int )",
5401 for (const char *s : syms)
5403 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
5404 EXPECT ("(anonymous namespace)::A::B::C"));
5409 static const char expected[] = "ns2::tmpl<int>::foo2";
5410 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5412 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5416 SELF_CHECK (!any_mismatch);
5425 test_mapped_index_find_name_component_bounds ();
5426 test_dw2_expand_symtabs_matching_symbol ();
5429 }} // namespace selftests::dw2_expand_symtabs_matching
5431 #endif /* GDB_SELF_TEST */
5433 /* If FILE_MATCHER is NULL or if PER_CU has
5434 dwarf2_per_cu_quick_data::MARK set (see
5435 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5436 EXPANSION_NOTIFY on it. */
5439 dw2_expand_symtabs_matching_one
5440 (struct dwarf2_per_cu_data *per_cu,
5441 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5442 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5444 if (file_matcher == NULL || per_cu->v.quick->mark)
5446 bool symtab_was_null
5447 = (per_cu->v.quick->compunit_symtab == NULL);
5449 dw2_instantiate_symtab (per_cu);
5451 if (expansion_notify != NULL
5453 && per_cu->v.quick->compunit_symtab != NULL)
5454 expansion_notify (per_cu->v.quick->compunit_symtab);
5458 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5459 matched, to expand corresponding CUs that were marked. IDX is the
5460 index of the symbol name that matched. */
5463 dw2_expand_marked_cus
5464 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5465 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5466 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5469 offset_type *vec, vec_len, vec_idx;
5470 bool global_seen = false;
5471 mapped_index &index = *dwarf2_per_objfile->index_table;
5473 vec = (offset_type *) (index.constant_pool
5474 + MAYBE_SWAP (index.symbol_table[idx].vec));
5475 vec_len = MAYBE_SWAP (vec[0]);
5476 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5478 struct dwarf2_per_cu_data *per_cu;
5479 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5480 /* This value is only valid for index versions >= 7. */
5481 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5482 gdb_index_symbol_kind symbol_kind =
5483 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5484 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5485 /* Only check the symbol attributes if they're present.
5486 Indices prior to version 7 don't record them,
5487 and indices >= 7 may elide them for certain symbols
5488 (gold does this). */
5491 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5493 /* Work around gold/15646. */
5496 if (!is_static && global_seen)
5502 /* Only check the symbol's kind if it has one. */
5507 case VARIABLES_DOMAIN:
5508 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5511 case FUNCTIONS_DOMAIN:
5512 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5516 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5524 /* Don't crash on bad data. */
5525 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5526 + dwarf2_per_objfile->n_type_units))
5528 complaint (&symfile_complaints,
5529 _(".gdb_index entry has bad CU index"
5531 objfile_name (dwarf2_per_objfile->objfile));
5535 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
5536 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5541 /* If FILE_MATCHER is non-NULL, set all the
5542 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5543 that match FILE_MATCHER. */
5546 dw_expand_symtabs_matching_file_matcher
5547 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5548 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5550 if (file_matcher == NULL)
5553 objfile *const objfile = dwarf2_per_objfile->objfile;
5555 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5557 NULL, xcalloc, xfree));
5558 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5560 NULL, xcalloc, xfree));
5562 /* The rule is CUs specify all the files, including those used by
5563 any TU, so there's no need to scan TUs here. */
5565 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5568 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5569 struct quick_file_names *file_data;
5574 per_cu->v.quick->mark = 0;
5576 /* We only need to look at symtabs not already expanded. */
5577 if (per_cu->v.quick->compunit_symtab)
5580 file_data = dw2_get_file_names (per_cu);
5581 if (file_data == NULL)
5584 if (htab_find (visited_not_found.get (), file_data) != NULL)
5586 else if (htab_find (visited_found.get (), file_data) != NULL)
5588 per_cu->v.quick->mark = 1;
5592 for (j = 0; j < file_data->num_file_names; ++j)
5594 const char *this_real_name;
5596 if (file_matcher (file_data->file_names[j], false))
5598 per_cu->v.quick->mark = 1;
5602 /* Before we invoke realpath, which can get expensive when many
5603 files are involved, do a quick comparison of the basenames. */
5604 if (!basenames_may_differ
5605 && !file_matcher (lbasename (file_data->file_names[j]),
5609 this_real_name = dw2_get_real_path (objfile, file_data, j);
5610 if (file_matcher (this_real_name, false))
5612 per_cu->v.quick->mark = 1;
5617 slot = htab_find_slot (per_cu->v.quick->mark
5618 ? visited_found.get ()
5619 : visited_not_found.get (),
5626 dw2_expand_symtabs_matching
5627 (struct objfile *objfile,
5628 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5629 const lookup_name_info &lookup_name,
5630 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5631 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5632 enum search_domain kind)
5634 struct dwarf2_per_objfile *dwarf2_per_objfile
5635 = get_dwarf2_per_objfile (objfile);
5637 /* index_table is NULL if OBJF_READNOW. */
5638 if (!dwarf2_per_objfile->index_table)
5641 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5643 mapped_index &index = *dwarf2_per_objfile->index_table;
5645 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5647 kind, [&] (offset_type idx)
5649 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5650 expansion_notify, kind);
5654 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5657 static struct compunit_symtab *
5658 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5663 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5664 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5667 if (cust->includes == NULL)
5670 for (i = 0; cust->includes[i]; ++i)
5672 struct compunit_symtab *s = cust->includes[i];
5674 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5682 static struct compunit_symtab *
5683 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5684 struct bound_minimal_symbol msymbol,
5686 struct obj_section *section,
5689 struct dwarf2_per_cu_data *data;
5690 struct compunit_symtab *result;
5692 if (!objfile->psymtabs_addrmap)
5695 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5700 if (warn_if_readin && data->v.quick->compunit_symtab)
5701 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5702 paddress (get_objfile_arch (objfile), pc));
5705 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5707 gdb_assert (result != NULL);
5712 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5713 void *data, int need_fullname)
5715 struct dwarf2_per_objfile *dwarf2_per_objfile
5716 = get_dwarf2_per_objfile (objfile);
5718 if (!dwarf2_per_objfile->filenames_cache)
5720 dwarf2_per_objfile->filenames_cache.emplace ();
5722 htab_up visited (htab_create_alloc (10,
5723 htab_hash_pointer, htab_eq_pointer,
5724 NULL, xcalloc, xfree));
5726 /* The rule is CUs specify all the files, including those used
5727 by any TU, so there's no need to scan TUs here. We can
5728 ignore file names coming from already-expanded CUs. */
5730 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5732 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
5734 if (per_cu->v.quick->compunit_symtab)
5736 void **slot = htab_find_slot (visited.get (),
5737 per_cu->v.quick->file_names,
5740 *slot = per_cu->v.quick->file_names;
5744 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5746 dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5747 struct quick_file_names *file_data;
5750 /* We only need to look at symtabs not already expanded. */
5751 if (per_cu->v.quick->compunit_symtab)
5754 file_data = dw2_get_file_names (per_cu);
5755 if (file_data == NULL)
5758 slot = htab_find_slot (visited.get (), file_data, INSERT);
5761 /* Already visited. */
5766 for (int j = 0; j < file_data->num_file_names; ++j)
5768 const char *filename = file_data->file_names[j];
5769 dwarf2_per_objfile->filenames_cache->seen (filename);
5774 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5776 gdb::unique_xmalloc_ptr<char> this_real_name;
5779 this_real_name = gdb_realpath (filename);
5780 (*fun) (filename, this_real_name.get (), data);
5785 dw2_has_symbols (struct objfile *objfile)
5790 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5793 dw2_find_last_source_symtab,
5794 dw2_forget_cached_source_info,
5795 dw2_map_symtabs_matching_filename,
5800 dw2_expand_symtabs_for_function,
5801 dw2_expand_all_symtabs,
5802 dw2_expand_symtabs_with_fullname,
5803 dw2_map_matching_symbols,
5804 dw2_expand_symtabs_matching,
5805 dw2_find_pc_sect_compunit_symtab,
5807 dw2_map_symbol_filenames
5810 /* DWARF-5 debug_names reader. */
5812 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5813 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5815 /* A helper function that reads the .debug_names section in SECTION
5816 and fills in MAP. FILENAME is the name of the file containing the
5817 section; it is used for error reporting.
5819 Returns true if all went well, false otherwise. */
5822 read_debug_names_from_section (struct objfile *objfile,
5823 const char *filename,
5824 struct dwarf2_section_info *section,
5825 mapped_debug_names &map)
5827 if (dwarf2_section_empty_p (section))
5830 /* Older elfutils strip versions could keep the section in the main
5831 executable while splitting it for the separate debug info file. */
5832 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5835 dwarf2_read_section (objfile, section);
5837 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5839 const gdb_byte *addr = section->buffer;
5841 bfd *const abfd = get_section_bfd_owner (section);
5843 unsigned int bytes_read;
5844 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5847 map.dwarf5_is_dwarf64 = bytes_read != 4;
5848 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5849 if (bytes_read + length != section->size)
5851 /* There may be multiple per-CU indices. */
5852 warning (_("Section .debug_names in %s length %s does not match "
5853 "section length %s, ignoring .debug_names."),
5854 filename, plongest (bytes_read + length),
5855 pulongest (section->size));
5859 /* The version number. */
5860 uint16_t version = read_2_bytes (abfd, addr);
5864 warning (_("Section .debug_names in %s has unsupported version %d, "
5865 "ignoring .debug_names."),
5871 uint16_t padding = read_2_bytes (abfd, addr);
5875 warning (_("Section .debug_names in %s has unsupported padding %d, "
5876 "ignoring .debug_names."),
5881 /* comp_unit_count - The number of CUs in the CU list. */
5882 map.cu_count = read_4_bytes (abfd, addr);
5885 /* local_type_unit_count - The number of TUs in the local TU
5887 map.tu_count = read_4_bytes (abfd, addr);
5890 /* foreign_type_unit_count - The number of TUs in the foreign TU
5892 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5894 if (foreign_tu_count != 0)
5896 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5897 "ignoring .debug_names."),
5898 filename, static_cast<unsigned long> (foreign_tu_count));
5902 /* bucket_count - The number of hash buckets in the hash lookup
5904 map.bucket_count = read_4_bytes (abfd, addr);
5907 /* name_count - The number of unique names in the index. */
5908 map.name_count = read_4_bytes (abfd, addr);
5911 /* abbrev_table_size - The size in bytes of the abbreviations
5913 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5916 /* augmentation_string_size - The size in bytes of the augmentation
5917 string. This value is rounded up to a multiple of 4. */
5918 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5920 map.augmentation_is_gdb = ((augmentation_string_size
5921 == sizeof (dwarf5_augmentation))
5922 && memcmp (addr, dwarf5_augmentation,
5923 sizeof (dwarf5_augmentation)) == 0);
5924 augmentation_string_size += (-augmentation_string_size) & 3;
5925 addr += augmentation_string_size;
5928 map.cu_table_reordered = addr;
5929 addr += map.cu_count * map.offset_size;
5931 /* List of Local TUs */
5932 map.tu_table_reordered = addr;
5933 addr += map.tu_count * map.offset_size;
5935 /* Hash Lookup Table */
5936 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5937 addr += map.bucket_count * 4;
5938 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5939 addr += map.name_count * 4;
5942 map.name_table_string_offs_reordered = addr;
5943 addr += map.name_count * map.offset_size;
5944 map.name_table_entry_offs_reordered = addr;
5945 addr += map.name_count * map.offset_size;
5947 const gdb_byte *abbrev_table_start = addr;
5950 unsigned int bytes_read;
5951 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5956 const auto insertpair
5957 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5958 if (!insertpair.second)
5960 warning (_("Section .debug_names in %s has duplicate index %s, "
5961 "ignoring .debug_names."),
5962 filename, pulongest (index_num));
5965 mapped_debug_names::index_val &indexval = insertpair.first->second;
5966 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5971 mapped_debug_names::index_val::attr attr;
5972 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5974 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5976 if (attr.form == DW_FORM_implicit_const)
5978 attr.implicit_const = read_signed_leb128 (abfd, addr,
5982 if (attr.dw_idx == 0 && attr.form == 0)
5984 indexval.attr_vec.push_back (std::move (attr));
5987 if (addr != abbrev_table_start + abbrev_table_size)
5989 warning (_("Section .debug_names in %s has abbreviation_table "
5990 "of size %zu vs. written as %u, ignoring .debug_names."),
5991 filename, addr - abbrev_table_start, abbrev_table_size);
5994 map.entry_pool = addr;
5999 /* A helper for create_cus_from_debug_names that handles the MAP's CU
6003 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
6004 const mapped_debug_names &map,
6005 dwarf2_section_info §ion,
6006 bool is_dwz, int base_offset)
6008 sect_offset sect_off_prev;
6009 for (uint32_t i = 0; i <= map.cu_count; ++i)
6011 sect_offset sect_off_next;
6012 if (i < map.cu_count)
6015 = (sect_offset) (extract_unsigned_integer
6016 (map.cu_table_reordered + i * map.offset_size,
6018 map.dwarf5_byte_order));
6021 sect_off_next = (sect_offset) section.size;
6024 const ULONGEST length = sect_off_next - sect_off_prev;
6025 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
6026 = create_cu_from_index_list (dwarf2_per_objfile, §ion, is_dwz,
6027 sect_off_prev, length);
6029 sect_off_prev = sect_off_next;
6033 /* Read the CU list from the mapped index, and use it to create all
6034 the CU objects for this dwarf2_per_objfile. */
6037 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
6038 const mapped_debug_names &map,
6039 const mapped_debug_names &dwz_map)
6041 struct objfile *objfile = dwarf2_per_objfile->objfile;
6043 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
6044 dwarf2_per_objfile->all_comp_units
6045 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
6046 dwarf2_per_objfile->n_comp_units);
6048 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
6049 dwarf2_per_objfile->info,
6051 0 /* base_offset */);
6053 if (dwz_map.cu_count == 0)
6056 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
6057 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
6059 map.cu_count /* base_offset */);
6062 /* Read .debug_names. If everything went ok, initialize the "quick"
6063 elements of all the CUs and return true. Otherwise, return false. */
6066 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
6068 mapped_debug_names local_map (dwarf2_per_objfile);
6069 mapped_debug_names dwz_map (dwarf2_per_objfile);
6070 struct objfile *objfile = dwarf2_per_objfile->objfile;
6072 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
6073 &dwarf2_per_objfile->debug_names,
6077 /* Don't use the index if it's empty. */
6078 if (local_map.name_count == 0)
6081 /* If there is a .dwz file, read it so we can get its CU list as
6083 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
6086 if (!read_debug_names_from_section (objfile,
6087 bfd_get_filename (dwz->dwz_bfd),
6088 &dwz->debug_names, dwz_map))
6090 warning (_("could not read '.debug_names' section from %s; skipping"),
6091 bfd_get_filename (dwz->dwz_bfd));
6096 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
6098 if (local_map.tu_count != 0)
6100 /* We can only handle a single .debug_types when we have an
6102 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
6105 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
6106 dwarf2_per_objfile->types, 0);
6108 create_signatured_type_table_from_debug_names
6109 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
6112 create_addrmap_from_aranges (dwarf2_per_objfile,
6113 &dwarf2_per_objfile->debug_aranges);
6115 dwarf2_per_objfile->debug_names_table.reset
6116 (new mapped_debug_names (dwarf2_per_objfile));
6117 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
6118 dwarf2_per_objfile->using_index = 1;
6119 dwarf2_per_objfile->quick_file_names_table =
6120 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6125 /* Symbol name hashing function as specified by DWARF-5. */
6128 dwarf5_djb_hash (const char *str_)
6130 const unsigned char *str = (const unsigned char *) str_;
6132 /* Note: tolower here ignores UTF-8, which isn't fully compliant.
6133 See http://dwarfstd.org/ShowIssue.php?issue=161027.1. */
6135 uint32_t hash = 5381;
6136 while (int c = *str++)
6137 hash = hash * 33 + tolower (c);
6141 /* Type used to manage iterating over all CUs looking for a symbol for
6144 class dw2_debug_names_iterator
6147 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
6148 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
6149 dw2_debug_names_iterator (const mapped_debug_names &map,
6150 bool want_specific_block,
6151 block_enum block_index, domain_enum domain,
6153 : m_map (map), m_want_specific_block (want_specific_block),
6154 m_block_index (block_index), m_domain (domain),
6155 m_addr (find_vec_in_debug_names (map, name))
6158 dw2_debug_names_iterator (const mapped_debug_names &map,
6159 search_domain search, uint32_t namei)
6162 m_addr (find_vec_in_debug_names (map, namei))
6165 /* Return the next matching CU or NULL if there are no more. */
6166 dwarf2_per_cu_data *next ();
6169 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6171 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6174 /* The internalized form of .debug_names. */
6175 const mapped_debug_names &m_map;
6177 /* If true, only look for symbols that match BLOCK_INDEX. */
6178 const bool m_want_specific_block = false;
6180 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
6181 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
6183 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
6185 /* The kind of symbol we're looking for. */
6186 const domain_enum m_domain = UNDEF_DOMAIN;
6187 const search_domain m_search = ALL_DOMAIN;
6189 /* The list of CUs from the index entry of the symbol, or NULL if
6191 const gdb_byte *m_addr;
6195 mapped_debug_names::namei_to_name (uint32_t namei) const
6197 const ULONGEST namei_string_offs
6198 = extract_unsigned_integer ((name_table_string_offs_reordered
6199 + namei * offset_size),
6202 return read_indirect_string_at_offset
6203 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
6206 /* Find a slot in .debug_names for the object named NAME. If NAME is
6207 found, return pointer to its pool data. If NAME cannot be found,
6211 dw2_debug_names_iterator::find_vec_in_debug_names
6212 (const mapped_debug_names &map, const char *name)
6214 int (*cmp) (const char *, const char *);
6216 if (current_language->la_language == language_cplus
6217 || current_language->la_language == language_fortran
6218 || current_language->la_language == language_d)
6220 /* NAME is already canonical. Drop any qualifiers as
6221 .debug_names does not contain any. */
6223 if (strchr (name, '(') != NULL)
6225 gdb::unique_xmalloc_ptr<char> without_params
6226 = cp_remove_params (name);
6228 if (without_params != NULL)
6230 name = without_params.get();
6235 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
6237 const uint32_t full_hash = dwarf5_djb_hash (name);
6239 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6240 (map.bucket_table_reordered
6241 + (full_hash % map.bucket_count)), 4,
6242 map.dwarf5_byte_order);
6246 if (namei >= map.name_count)
6248 complaint (&symfile_complaints,
6249 _("Wrong .debug_names with name index %u but name_count=%u "
6251 namei, map.name_count,
6252 objfile_name (map.dwarf2_per_objfile->objfile));
6258 const uint32_t namei_full_hash
6259 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6260 (map.hash_table_reordered + namei), 4,
6261 map.dwarf5_byte_order);
6262 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
6265 if (full_hash == namei_full_hash)
6267 const char *const namei_string = map.namei_to_name (namei);
6269 #if 0 /* An expensive sanity check. */
6270 if (namei_full_hash != dwarf5_djb_hash (namei_string))
6272 complaint (&symfile_complaints,
6273 _("Wrong .debug_names hash for string at index %u "
6275 namei, objfile_name (dwarf2_per_objfile->objfile));
6280 if (cmp (namei_string, name) == 0)
6282 const ULONGEST namei_entry_offs
6283 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6284 + namei * map.offset_size),
6285 map.offset_size, map.dwarf5_byte_order);
6286 return map.entry_pool + namei_entry_offs;
6291 if (namei >= map.name_count)
6297 dw2_debug_names_iterator::find_vec_in_debug_names
6298 (const mapped_debug_names &map, uint32_t namei)
6300 if (namei >= map.name_count)
6302 complaint (&symfile_complaints,
6303 _("Wrong .debug_names with name index %u but name_count=%u "
6305 namei, map.name_count,
6306 objfile_name (map.dwarf2_per_objfile->objfile));
6310 const ULONGEST namei_entry_offs
6311 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6312 + namei * map.offset_size),
6313 map.offset_size, map.dwarf5_byte_order);
6314 return map.entry_pool + namei_entry_offs;
6317 /* See dw2_debug_names_iterator. */
6319 dwarf2_per_cu_data *
6320 dw2_debug_names_iterator::next ()
6325 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
6326 struct objfile *objfile = dwarf2_per_objfile->objfile;
6327 bfd *const abfd = objfile->obfd;
6331 unsigned int bytes_read;
6332 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6333 m_addr += bytes_read;
6337 const auto indexval_it = m_map.abbrev_map.find (abbrev);
6338 if (indexval_it == m_map.abbrev_map.cend ())
6340 complaint (&symfile_complaints,
6341 _("Wrong .debug_names undefined abbrev code %s "
6343 pulongest (abbrev), objfile_name (objfile));
6346 const mapped_debug_names::index_val &indexval = indexval_it->second;
6347 bool have_is_static = false;
6349 dwarf2_per_cu_data *per_cu = NULL;
6350 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
6355 case DW_FORM_implicit_const:
6356 ull = attr.implicit_const;
6358 case DW_FORM_flag_present:
6362 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6363 m_addr += bytes_read;
6366 complaint (&symfile_complaints,
6367 _("Unsupported .debug_names form %s [in module %s]"),
6368 dwarf_form_name (attr.form),
6369 objfile_name (objfile));
6372 switch (attr.dw_idx)
6374 case DW_IDX_compile_unit:
6375 /* Don't crash on bad data. */
6376 if (ull >= dwarf2_per_objfile->n_comp_units)
6378 complaint (&symfile_complaints,
6379 _(".debug_names entry has bad CU index %s"
6382 objfile_name (dwarf2_per_objfile->objfile));
6385 per_cu = dw2_get_cutu (dwarf2_per_objfile, ull);
6387 case DW_IDX_type_unit:
6388 /* Don't crash on bad data. */
6389 if (ull >= dwarf2_per_objfile->n_type_units)
6391 complaint (&symfile_complaints,
6392 _(".debug_names entry has bad TU index %s"
6395 objfile_name (dwarf2_per_objfile->objfile));
6398 per_cu = dw2_get_cutu (dwarf2_per_objfile,
6399 dwarf2_per_objfile->n_comp_units + ull);
6401 case DW_IDX_GNU_internal:
6402 if (!m_map.augmentation_is_gdb)
6404 have_is_static = true;
6407 case DW_IDX_GNU_external:
6408 if (!m_map.augmentation_is_gdb)
6410 have_is_static = true;
6416 /* Skip if already read in. */
6417 if (per_cu->v.quick->compunit_symtab)
6420 /* Check static vs global. */
6423 const bool want_static = m_block_index != GLOBAL_BLOCK;
6424 if (m_want_specific_block && want_static != is_static)
6428 /* Match dw2_symtab_iter_next, symbol_kind
6429 and debug_names::psymbol_tag. */
6433 switch (indexval.dwarf_tag)
6435 case DW_TAG_variable:
6436 case DW_TAG_subprogram:
6437 /* Some types are also in VAR_DOMAIN. */
6438 case DW_TAG_typedef:
6439 case DW_TAG_structure_type:
6446 switch (indexval.dwarf_tag)
6448 case DW_TAG_typedef:
6449 case DW_TAG_structure_type:
6456 switch (indexval.dwarf_tag)
6459 case DW_TAG_variable:
6469 /* Match dw2_expand_symtabs_matching, symbol_kind and
6470 debug_names::psymbol_tag. */
6473 case VARIABLES_DOMAIN:
6474 switch (indexval.dwarf_tag)
6476 case DW_TAG_variable:
6482 case FUNCTIONS_DOMAIN:
6483 switch (indexval.dwarf_tag)
6485 case DW_TAG_subprogram:
6492 switch (indexval.dwarf_tag)
6494 case DW_TAG_typedef:
6495 case DW_TAG_structure_type:
6508 static struct compunit_symtab *
6509 dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6510 const char *name, domain_enum domain)
6512 const block_enum block_index = static_cast<block_enum> (block_index_int);
6513 struct dwarf2_per_objfile *dwarf2_per_objfile
6514 = get_dwarf2_per_objfile (objfile);
6516 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6519 /* index is NULL if OBJF_READNOW. */
6522 const auto &map = *mapp;
6524 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6525 block_index, domain, name);
6527 struct compunit_symtab *stab_best = NULL;
6528 struct dwarf2_per_cu_data *per_cu;
6529 while ((per_cu = iter.next ()) != NULL)
6531 struct symbol *sym, *with_opaque = NULL;
6532 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6533 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6534 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6536 sym = block_find_symbol (block, name, domain,
6537 block_find_non_opaque_type_preferred,
6540 /* Some caution must be observed with overloaded functions and
6541 methods, since the index will not contain any overload
6542 information (but NAME might contain it). */
6545 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6547 if (with_opaque != NULL
6548 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6551 /* Keep looking through other CUs. */
6557 /* This dumps minimal information about .debug_names. It is called
6558 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6559 uses this to verify that .debug_names has been loaded. */
6562 dw2_debug_names_dump (struct objfile *objfile)
6564 struct dwarf2_per_objfile *dwarf2_per_objfile
6565 = get_dwarf2_per_objfile (objfile);
6567 gdb_assert (dwarf2_per_objfile->using_index);
6568 printf_filtered (".debug_names:");
6569 if (dwarf2_per_objfile->debug_names_table)
6570 printf_filtered (" exists\n");
6572 printf_filtered (" faked for \"readnow\"\n");
6573 printf_filtered ("\n");
6577 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6578 const char *func_name)
6580 struct dwarf2_per_objfile *dwarf2_per_objfile
6581 = get_dwarf2_per_objfile (objfile);
6583 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6584 if (dwarf2_per_objfile->debug_names_table)
6586 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6588 /* Note: It doesn't matter what we pass for block_index here. */
6589 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6590 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
6592 struct dwarf2_per_cu_data *per_cu;
6593 while ((per_cu = iter.next ()) != NULL)
6594 dw2_instantiate_symtab (per_cu);
6599 dw2_debug_names_expand_symtabs_matching
6600 (struct objfile *objfile,
6601 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6602 const lookup_name_info &lookup_name,
6603 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6604 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6605 enum search_domain kind)
6607 struct dwarf2_per_objfile *dwarf2_per_objfile
6608 = get_dwarf2_per_objfile (objfile);
6610 /* debug_names_table is NULL if OBJF_READNOW. */
6611 if (!dwarf2_per_objfile->debug_names_table)
6614 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6616 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6618 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6620 kind, [&] (offset_type namei)
6622 /* The name was matched, now expand corresponding CUs that were
6624 dw2_debug_names_iterator iter (map, kind, namei);
6626 struct dwarf2_per_cu_data *per_cu;
6627 while ((per_cu = iter.next ()) != NULL)
6628 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6633 const struct quick_symbol_functions dwarf2_debug_names_functions =
6636 dw2_find_last_source_symtab,
6637 dw2_forget_cached_source_info,
6638 dw2_map_symtabs_matching_filename,
6639 dw2_debug_names_lookup_symbol,
6641 dw2_debug_names_dump,
6643 dw2_debug_names_expand_symtabs_for_function,
6644 dw2_expand_all_symtabs,
6645 dw2_expand_symtabs_with_fullname,
6646 dw2_map_matching_symbols,
6647 dw2_debug_names_expand_symtabs_matching,
6648 dw2_find_pc_sect_compunit_symtab,
6650 dw2_map_symbol_filenames
6653 /* See symfile.h. */
6656 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6658 struct dwarf2_per_objfile *dwarf2_per_objfile
6659 = get_dwarf2_per_objfile (objfile);
6661 /* If we're about to read full symbols, don't bother with the
6662 indices. In this case we also don't care if some other debug
6663 format is making psymtabs, because they are all about to be
6665 if ((objfile->flags & OBJF_READNOW))
6669 dwarf2_per_objfile->using_index = 1;
6670 create_all_comp_units (dwarf2_per_objfile);
6671 create_all_type_units (dwarf2_per_objfile);
6672 dwarf2_per_objfile->quick_file_names_table =
6673 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6675 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
6676 + dwarf2_per_objfile->n_type_units); ++i)
6678 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
6680 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6681 struct dwarf2_per_cu_quick_data);
6684 /* Return 1 so that gdb sees the "quick" functions. However,
6685 these functions will be no-ops because we will have expanded
6687 *index_kind = dw_index_kind::GDB_INDEX;
6691 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6693 *index_kind = dw_index_kind::DEBUG_NAMES;
6697 if (dwarf2_read_index (objfile))
6699 *index_kind = dw_index_kind::GDB_INDEX;
6708 /* Build a partial symbol table. */
6711 dwarf2_build_psymtabs (struct objfile *objfile)
6713 struct dwarf2_per_objfile *dwarf2_per_objfile
6714 = get_dwarf2_per_objfile (objfile);
6716 if (objfile->global_psymbols.capacity () == 0
6717 && objfile->static_psymbols.capacity () == 0)
6718 init_psymbol_list (objfile, 1024);
6722 /* This isn't really ideal: all the data we allocate on the
6723 objfile's obstack is still uselessly kept around. However,
6724 freeing it seems unsafe. */
6725 psymtab_discarder psymtabs (objfile);
6726 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6729 CATCH (except, RETURN_MASK_ERROR)
6731 exception_print (gdb_stderr, except);
6736 /* Return the total length of the CU described by HEADER. */
6739 get_cu_length (const struct comp_unit_head *header)
6741 return header->initial_length_size + header->length;
6744 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6747 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6749 sect_offset bottom = cu_header->sect_off;
6750 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6752 return sect_off >= bottom && sect_off < top;
6755 /* Find the base address of the compilation unit for range lists and
6756 location lists. It will normally be specified by DW_AT_low_pc.
6757 In DWARF-3 draft 4, the base address could be overridden by
6758 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6759 compilation units with discontinuous ranges. */
6762 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6764 struct attribute *attr;
6767 cu->base_address = 0;
6769 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6772 cu->base_address = attr_value_as_address (attr);
6777 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6780 cu->base_address = attr_value_as_address (attr);
6786 /* Read in the comp unit header information from the debug_info at info_ptr.
6787 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6788 NOTE: This leaves members offset, first_die_offset to be filled in
6791 static const gdb_byte *
6792 read_comp_unit_head (struct comp_unit_head *cu_header,
6793 const gdb_byte *info_ptr,
6794 struct dwarf2_section_info *section,
6795 rcuh_kind section_kind)
6798 unsigned int bytes_read;
6799 const char *filename = get_section_file_name (section);
6800 bfd *abfd = get_section_bfd_owner (section);
6802 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6803 cu_header->initial_length_size = bytes_read;
6804 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6805 info_ptr += bytes_read;
6806 cu_header->version = read_2_bytes (abfd, info_ptr);
6808 if (cu_header->version < 5)
6809 switch (section_kind)
6811 case rcuh_kind::COMPILE:
6812 cu_header->unit_type = DW_UT_compile;
6814 case rcuh_kind::TYPE:
6815 cu_header->unit_type = DW_UT_type;
6818 internal_error (__FILE__, __LINE__,
6819 _("read_comp_unit_head: invalid section_kind"));
6823 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6824 (read_1_byte (abfd, info_ptr));
6826 switch (cu_header->unit_type)
6829 if (section_kind != rcuh_kind::COMPILE)
6830 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6831 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6835 section_kind = rcuh_kind::TYPE;
6838 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6839 "(is %d, should be %d or %d) [in module %s]"),
6840 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6843 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6846 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6849 info_ptr += bytes_read;
6850 if (cu_header->version < 5)
6852 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6855 signed_addr = bfd_get_sign_extend_vma (abfd);
6856 if (signed_addr < 0)
6857 internal_error (__FILE__, __LINE__,
6858 _("read_comp_unit_head: dwarf from non elf file"));
6859 cu_header->signed_addr_p = signed_addr;
6861 if (section_kind == rcuh_kind::TYPE)
6863 LONGEST type_offset;
6865 cu_header->signature = read_8_bytes (abfd, info_ptr);
6868 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6869 info_ptr += bytes_read;
6870 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6871 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6872 error (_("Dwarf Error: Too big type_offset in compilation unit "
6873 "header (is %s) [in module %s]"), plongest (type_offset),
6880 /* Helper function that returns the proper abbrev section for
6883 static struct dwarf2_section_info *
6884 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6886 struct dwarf2_section_info *abbrev;
6887 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6889 if (this_cu->is_dwz)
6890 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6892 abbrev = &dwarf2_per_objfile->abbrev;
6897 /* Subroutine of read_and_check_comp_unit_head and
6898 read_and_check_type_unit_head to simplify them.
6899 Perform various error checking on the header. */
6902 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6903 struct comp_unit_head *header,
6904 struct dwarf2_section_info *section,
6905 struct dwarf2_section_info *abbrev_section)
6907 const char *filename = get_section_file_name (section);
6909 if (header->version < 2 || header->version > 5)
6910 error (_("Dwarf Error: wrong version in compilation unit header "
6911 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
6914 if (to_underlying (header->abbrev_sect_off)
6915 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6916 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6917 "(offset %s + 6) [in module %s]"),
6918 sect_offset_str (header->abbrev_sect_off),
6919 sect_offset_str (header->sect_off),
6922 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6923 avoid potential 32-bit overflow. */
6924 if (((ULONGEST) header->sect_off + get_cu_length (header))
6926 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6927 "(offset %s + 0) [in module %s]"),
6928 header->length, sect_offset_str (header->sect_off),
6932 /* Read in a CU/TU header and perform some basic error checking.
6933 The contents of the header are stored in HEADER.
6934 The result is a pointer to the start of the first DIE. */
6936 static const gdb_byte *
6937 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6938 struct comp_unit_head *header,
6939 struct dwarf2_section_info *section,
6940 struct dwarf2_section_info *abbrev_section,
6941 const gdb_byte *info_ptr,
6942 rcuh_kind section_kind)
6944 const gdb_byte *beg_of_comp_unit = info_ptr;
6946 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6948 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6950 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6952 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6958 /* Fetch the abbreviation table offset from a comp or type unit header. */
6961 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6962 struct dwarf2_section_info *section,
6963 sect_offset sect_off)
6965 bfd *abfd = get_section_bfd_owner (section);
6966 const gdb_byte *info_ptr;
6967 unsigned int initial_length_size, offset_size;
6970 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6971 info_ptr = section->buffer + to_underlying (sect_off);
6972 read_initial_length (abfd, info_ptr, &initial_length_size);
6973 offset_size = initial_length_size == 4 ? 4 : 8;
6974 info_ptr += initial_length_size;
6976 version = read_2_bytes (abfd, info_ptr);
6980 /* Skip unit type and address size. */
6984 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6987 /* Allocate a new partial symtab for file named NAME and mark this new
6988 partial symtab as being an include of PST. */
6991 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6992 struct objfile *objfile)
6994 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6996 if (!IS_ABSOLUTE_PATH (subpst->filename))
6998 /* It shares objfile->objfile_obstack. */
6999 subpst->dirname = pst->dirname;
7002 subpst->textlow = 0;
7003 subpst->texthigh = 0;
7005 subpst->dependencies
7006 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
7007 subpst->dependencies[0] = pst;
7008 subpst->number_of_dependencies = 1;
7010 subpst->globals_offset = 0;
7011 subpst->n_global_syms = 0;
7012 subpst->statics_offset = 0;
7013 subpst->n_static_syms = 0;
7014 subpst->compunit_symtab = NULL;
7015 subpst->read_symtab = pst->read_symtab;
7018 /* No private part is necessary for include psymtabs. This property
7019 can be used to differentiate between such include psymtabs and
7020 the regular ones. */
7021 subpst->read_symtab_private = NULL;
7024 /* Read the Line Number Program data and extract the list of files
7025 included by the source file represented by PST. Build an include
7026 partial symtab for each of these included files. */
7029 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
7030 struct die_info *die,
7031 struct partial_symtab *pst)
7034 struct attribute *attr;
7036 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7038 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
7040 return; /* No linetable, so no includes. */
7042 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
7043 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
7047 hash_signatured_type (const void *item)
7049 const struct signatured_type *sig_type
7050 = (const struct signatured_type *) item;
7052 /* This drops the top 32 bits of the signature, but is ok for a hash. */
7053 return sig_type->signature;
7057 eq_signatured_type (const void *item_lhs, const void *item_rhs)
7059 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
7060 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
7062 return lhs->signature == rhs->signature;
7065 /* Allocate a hash table for signatured types. */
7068 allocate_signatured_type_table (struct objfile *objfile)
7070 return htab_create_alloc_ex (41,
7071 hash_signatured_type,
7074 &objfile->objfile_obstack,
7075 hashtab_obstack_allocate,
7076 dummy_obstack_deallocate);
7079 /* A helper function to add a signatured type CU to a table. */
7082 add_signatured_type_cu_to_table (void **slot, void *datum)
7084 struct signatured_type *sigt = (struct signatured_type *) *slot;
7085 struct signatured_type ***datap = (struct signatured_type ***) datum;
7093 /* A helper for create_debug_types_hash_table. Read types from SECTION
7094 and fill them into TYPES_HTAB. It will process only type units,
7095 therefore DW_UT_type. */
7098 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7099 struct dwo_file *dwo_file,
7100 dwarf2_section_info *section, htab_t &types_htab,
7101 rcuh_kind section_kind)
7103 struct objfile *objfile = dwarf2_per_objfile->objfile;
7104 struct dwarf2_section_info *abbrev_section;
7106 const gdb_byte *info_ptr, *end_ptr;
7108 abbrev_section = (dwo_file != NULL
7109 ? &dwo_file->sections.abbrev
7110 : &dwarf2_per_objfile->abbrev);
7112 if (dwarf_read_debug)
7113 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
7114 get_section_name (section),
7115 get_section_file_name (abbrev_section));
7117 dwarf2_read_section (objfile, section);
7118 info_ptr = section->buffer;
7120 if (info_ptr == NULL)
7123 /* We can't set abfd until now because the section may be empty or
7124 not present, in which case the bfd is unknown. */
7125 abfd = get_section_bfd_owner (section);
7127 /* We don't use init_cutu_and_read_dies_simple, or some such, here
7128 because we don't need to read any dies: the signature is in the
7131 end_ptr = info_ptr + section->size;
7132 while (info_ptr < end_ptr)
7134 struct signatured_type *sig_type;
7135 struct dwo_unit *dwo_tu;
7137 const gdb_byte *ptr = info_ptr;
7138 struct comp_unit_head header;
7139 unsigned int length;
7141 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
7143 /* Initialize it due to a false compiler warning. */
7144 header.signature = -1;
7145 header.type_cu_offset_in_tu = (cu_offset) -1;
7147 /* We need to read the type's signature in order to build the hash
7148 table, but we don't need anything else just yet. */
7150 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
7151 abbrev_section, ptr, section_kind);
7153 length = get_cu_length (&header);
7155 /* Skip dummy type units. */
7156 if (ptr >= info_ptr + length
7157 || peek_abbrev_code (abfd, ptr) == 0
7158 || header.unit_type != DW_UT_type)
7164 if (types_htab == NULL)
7167 types_htab = allocate_dwo_unit_table (objfile);
7169 types_htab = allocate_signatured_type_table (objfile);
7175 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7177 dwo_tu->dwo_file = dwo_file;
7178 dwo_tu->signature = header.signature;
7179 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
7180 dwo_tu->section = section;
7181 dwo_tu->sect_off = sect_off;
7182 dwo_tu->length = length;
7186 /* N.B.: type_offset is not usable if this type uses a DWO file.
7187 The real type_offset is in the DWO file. */
7189 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7190 struct signatured_type);
7191 sig_type->signature = header.signature;
7192 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
7193 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7194 sig_type->per_cu.is_debug_types = 1;
7195 sig_type->per_cu.section = section;
7196 sig_type->per_cu.sect_off = sect_off;
7197 sig_type->per_cu.length = length;
7200 slot = htab_find_slot (types_htab,
7201 dwo_file ? (void*) dwo_tu : (void *) sig_type,
7203 gdb_assert (slot != NULL);
7206 sect_offset dup_sect_off;
7210 const struct dwo_unit *dup_tu
7211 = (const struct dwo_unit *) *slot;
7213 dup_sect_off = dup_tu->sect_off;
7217 const struct signatured_type *dup_tu
7218 = (const struct signatured_type *) *slot;
7220 dup_sect_off = dup_tu->per_cu.sect_off;
7223 complaint (&symfile_complaints,
7224 _("debug type entry at offset %s is duplicate to"
7225 " the entry at offset %s, signature %s"),
7226 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
7227 hex_string (header.signature));
7229 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
7231 if (dwarf_read_debug > 1)
7232 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
7233 sect_offset_str (sect_off),
7234 hex_string (header.signature));
7240 /* Create the hash table of all entries in the .debug_types
7241 (or .debug_types.dwo) section(s).
7242 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
7243 otherwise it is NULL.
7245 The result is a pointer to the hash table or NULL if there are no types.
7247 Note: This function processes DWO files only, not DWP files. */
7250 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7251 struct dwo_file *dwo_file,
7252 VEC (dwarf2_section_info_def) *types,
7256 struct dwarf2_section_info *section;
7258 if (VEC_empty (dwarf2_section_info_def, types))
7262 VEC_iterate (dwarf2_section_info_def, types, ix, section);
7264 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
7265 types_htab, rcuh_kind::TYPE);
7268 /* Create the hash table of all entries in the .debug_types section,
7269 and initialize all_type_units.
7270 The result is zero if there is an error (e.g. missing .debug_types section),
7271 otherwise non-zero. */
7274 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7276 htab_t types_htab = NULL;
7277 struct signatured_type **iter;
7279 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
7280 &dwarf2_per_objfile->info, types_htab,
7281 rcuh_kind::COMPILE);
7282 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
7283 dwarf2_per_objfile->types, types_htab);
7284 if (types_htab == NULL)
7286 dwarf2_per_objfile->signatured_types = NULL;
7290 dwarf2_per_objfile->signatured_types = types_htab;
7292 dwarf2_per_objfile->n_type_units
7293 = dwarf2_per_objfile->n_allocated_type_units
7294 = htab_elements (types_htab);
7295 dwarf2_per_objfile->all_type_units =
7296 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
7297 iter = &dwarf2_per_objfile->all_type_units[0];
7298 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
7299 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
7300 == dwarf2_per_objfile->n_type_units);
7305 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
7306 If SLOT is non-NULL, it is the entry to use in the hash table.
7307 Otherwise we find one. */
7309 static struct signatured_type *
7310 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
7313 struct objfile *objfile = dwarf2_per_objfile->objfile;
7314 int n_type_units = dwarf2_per_objfile->n_type_units;
7315 struct signatured_type *sig_type;
7317 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
7319 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
7321 if (dwarf2_per_objfile->n_allocated_type_units == 0)
7322 dwarf2_per_objfile->n_allocated_type_units = 1;
7323 dwarf2_per_objfile->n_allocated_type_units *= 2;
7324 dwarf2_per_objfile->all_type_units
7325 = XRESIZEVEC (struct signatured_type *,
7326 dwarf2_per_objfile->all_type_units,
7327 dwarf2_per_objfile->n_allocated_type_units);
7328 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
7330 dwarf2_per_objfile->n_type_units = n_type_units;
7332 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7333 struct signatured_type);
7334 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
7335 sig_type->signature = sig;
7336 sig_type->per_cu.is_debug_types = 1;
7337 if (dwarf2_per_objfile->using_index)
7339 sig_type->per_cu.v.quick =
7340 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7341 struct dwarf2_per_cu_quick_data);
7346 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7349 gdb_assert (*slot == NULL);
7351 /* The rest of sig_type must be filled in by the caller. */
7355 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7356 Fill in SIG_ENTRY with DWO_ENTRY. */
7359 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
7360 struct signatured_type *sig_entry,
7361 struct dwo_unit *dwo_entry)
7363 /* Make sure we're not clobbering something we don't expect to. */
7364 gdb_assert (! sig_entry->per_cu.queued);
7365 gdb_assert (sig_entry->per_cu.cu == NULL);
7366 if (dwarf2_per_objfile->using_index)
7368 gdb_assert (sig_entry->per_cu.v.quick != NULL);
7369 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
7372 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7373 gdb_assert (sig_entry->signature == dwo_entry->signature);
7374 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7375 gdb_assert (sig_entry->type_unit_group == NULL);
7376 gdb_assert (sig_entry->dwo_unit == NULL);
7378 sig_entry->per_cu.section = dwo_entry->section;
7379 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7380 sig_entry->per_cu.length = dwo_entry->length;
7381 sig_entry->per_cu.reading_dwo_directly = 1;
7382 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7383 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7384 sig_entry->dwo_unit = dwo_entry;
7387 /* Subroutine of lookup_signatured_type.
7388 If we haven't read the TU yet, create the signatured_type data structure
7389 for a TU to be read in directly from a DWO file, bypassing the stub.
7390 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7391 using .gdb_index, then when reading a CU we want to stay in the DWO file
7392 containing that CU. Otherwise we could end up reading several other DWO
7393 files (due to comdat folding) to process the transitive closure of all the
7394 mentioned TUs, and that can be slow. The current DWO file will have every
7395 type signature that it needs.
7396 We only do this for .gdb_index because in the psymtab case we already have
7397 to read all the DWOs to build the type unit groups. */
7399 static struct signatured_type *
7400 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7402 struct dwarf2_per_objfile *dwarf2_per_objfile
7403 = cu->per_cu->dwarf2_per_objfile;
7404 struct objfile *objfile = dwarf2_per_objfile->objfile;
7405 struct dwo_file *dwo_file;
7406 struct dwo_unit find_dwo_entry, *dwo_entry;
7407 struct signatured_type find_sig_entry, *sig_entry;
7410 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7412 /* If TU skeletons have been removed then we may not have read in any
7414 if (dwarf2_per_objfile->signatured_types == NULL)
7416 dwarf2_per_objfile->signatured_types
7417 = allocate_signatured_type_table (objfile);
7420 /* We only ever need to read in one copy of a signatured type.
7421 Use the global signatured_types array to do our own comdat-folding
7422 of types. If this is the first time we're reading this TU, and
7423 the TU has an entry in .gdb_index, replace the recorded data from
7424 .gdb_index with this TU. */
7426 find_sig_entry.signature = sig;
7427 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7428 &find_sig_entry, INSERT);
7429 sig_entry = (struct signatured_type *) *slot;
7431 /* We can get here with the TU already read, *or* in the process of being
7432 read. Don't reassign the global entry to point to this DWO if that's
7433 the case. Also note that if the TU is already being read, it may not
7434 have come from a DWO, the program may be a mix of Fission-compiled
7435 code and non-Fission-compiled code. */
7437 /* Have we already tried to read this TU?
7438 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7439 needn't exist in the global table yet). */
7440 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7443 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7444 dwo_unit of the TU itself. */
7445 dwo_file = cu->dwo_unit->dwo_file;
7447 /* Ok, this is the first time we're reading this TU. */
7448 if (dwo_file->tus == NULL)
7450 find_dwo_entry.signature = sig;
7451 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7452 if (dwo_entry == NULL)
7455 /* If the global table doesn't have an entry for this TU, add one. */
7456 if (sig_entry == NULL)
7457 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7459 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7460 sig_entry->per_cu.tu_read = 1;
7464 /* Subroutine of lookup_signatured_type.
7465 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7466 then try the DWP file. If the TU stub (skeleton) has been removed then
7467 it won't be in .gdb_index. */
7469 static struct signatured_type *
7470 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7472 struct dwarf2_per_objfile *dwarf2_per_objfile
7473 = cu->per_cu->dwarf2_per_objfile;
7474 struct objfile *objfile = dwarf2_per_objfile->objfile;
7475 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7476 struct dwo_unit *dwo_entry;
7477 struct signatured_type find_sig_entry, *sig_entry;
7480 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7481 gdb_assert (dwp_file != NULL);
7483 /* If TU skeletons have been removed then we may not have read in any
7485 if (dwarf2_per_objfile->signatured_types == NULL)
7487 dwarf2_per_objfile->signatured_types
7488 = allocate_signatured_type_table (objfile);
7491 find_sig_entry.signature = sig;
7492 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7493 &find_sig_entry, INSERT);
7494 sig_entry = (struct signatured_type *) *slot;
7496 /* Have we already tried to read this TU?
7497 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7498 needn't exist in the global table yet). */
7499 if (sig_entry != NULL)
7502 if (dwp_file->tus == NULL)
7504 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7505 sig, 1 /* is_debug_types */);
7506 if (dwo_entry == NULL)
7509 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7510 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7515 /* Lookup a signature based type for DW_FORM_ref_sig8.
7516 Returns NULL if signature SIG is not present in the table.
7517 It is up to the caller to complain about this. */
7519 static struct signatured_type *
7520 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7522 struct dwarf2_per_objfile *dwarf2_per_objfile
7523 = cu->per_cu->dwarf2_per_objfile;
7526 && dwarf2_per_objfile->using_index)
7528 /* We're in a DWO/DWP file, and we're using .gdb_index.
7529 These cases require special processing. */
7530 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7531 return lookup_dwo_signatured_type (cu, sig);
7533 return lookup_dwp_signatured_type (cu, sig);
7537 struct signatured_type find_entry, *entry;
7539 if (dwarf2_per_objfile->signatured_types == NULL)
7541 find_entry.signature = sig;
7542 entry = ((struct signatured_type *)
7543 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7548 /* Low level DIE reading support. */
7550 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7553 init_cu_die_reader (struct die_reader_specs *reader,
7554 struct dwarf2_cu *cu,
7555 struct dwarf2_section_info *section,
7556 struct dwo_file *dwo_file,
7557 struct abbrev_table *abbrev_table)
7559 gdb_assert (section->readin && section->buffer != NULL);
7560 reader->abfd = get_section_bfd_owner (section);
7562 reader->dwo_file = dwo_file;
7563 reader->die_section = section;
7564 reader->buffer = section->buffer;
7565 reader->buffer_end = section->buffer + section->size;
7566 reader->comp_dir = NULL;
7567 reader->abbrev_table = abbrev_table;
7570 /* Subroutine of init_cutu_and_read_dies to simplify it.
7571 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7572 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7575 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7576 from it to the DIE in the DWO. If NULL we are skipping the stub.
7577 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7578 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7579 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7580 STUB_COMP_DIR may be non-NULL.
7581 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7582 are filled in with the info of the DIE from the DWO file.
7583 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7584 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7585 kept around for at least as long as *RESULT_READER.
7587 The result is non-zero if a valid (non-dummy) DIE was found. */
7590 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7591 struct dwo_unit *dwo_unit,
7592 struct die_info *stub_comp_unit_die,
7593 const char *stub_comp_dir,
7594 struct die_reader_specs *result_reader,
7595 const gdb_byte **result_info_ptr,
7596 struct die_info **result_comp_unit_die,
7597 int *result_has_children,
7598 abbrev_table_up *result_dwo_abbrev_table)
7600 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7601 struct objfile *objfile = dwarf2_per_objfile->objfile;
7602 struct dwarf2_cu *cu = this_cu->cu;
7604 const gdb_byte *begin_info_ptr, *info_ptr;
7605 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7606 int i,num_extra_attrs;
7607 struct dwarf2_section_info *dwo_abbrev_section;
7608 struct attribute *attr;
7609 struct die_info *comp_unit_die;
7611 /* At most one of these may be provided. */
7612 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7614 /* These attributes aren't processed until later:
7615 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7616 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7617 referenced later. However, these attributes are found in the stub
7618 which we won't have later. In order to not impose this complication
7619 on the rest of the code, we read them here and copy them to the
7628 if (stub_comp_unit_die != NULL)
7630 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7632 if (! this_cu->is_debug_types)
7633 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7634 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7635 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7636 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7637 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7639 /* There should be a DW_AT_addr_base attribute here (if needed).
7640 We need the value before we can process DW_FORM_GNU_addr_index. */
7642 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7644 cu->addr_base = DW_UNSND (attr);
7646 /* There should be a DW_AT_ranges_base attribute here (if needed).
7647 We need the value before we can process DW_AT_ranges. */
7648 cu->ranges_base = 0;
7649 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7651 cu->ranges_base = DW_UNSND (attr);
7653 else if (stub_comp_dir != NULL)
7655 /* Reconstruct the comp_dir attribute to simplify the code below. */
7656 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7657 comp_dir->name = DW_AT_comp_dir;
7658 comp_dir->form = DW_FORM_string;
7659 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7660 DW_STRING (comp_dir) = stub_comp_dir;
7663 /* Set up for reading the DWO CU/TU. */
7664 cu->dwo_unit = dwo_unit;
7665 dwarf2_section_info *section = dwo_unit->section;
7666 dwarf2_read_section (objfile, section);
7667 abfd = get_section_bfd_owner (section);
7668 begin_info_ptr = info_ptr = (section->buffer
7669 + to_underlying (dwo_unit->sect_off));
7670 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7672 if (this_cu->is_debug_types)
7674 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7676 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7677 &cu->header, section,
7679 info_ptr, rcuh_kind::TYPE);
7680 /* This is not an assert because it can be caused by bad debug info. */
7681 if (sig_type->signature != cu->header.signature)
7683 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7684 " TU at offset %s [in module %s]"),
7685 hex_string (sig_type->signature),
7686 hex_string (cu->header.signature),
7687 sect_offset_str (dwo_unit->sect_off),
7688 bfd_get_filename (abfd));
7690 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7691 /* For DWOs coming from DWP files, we don't know the CU length
7692 nor the type's offset in the TU until now. */
7693 dwo_unit->length = get_cu_length (&cu->header);
7694 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7696 /* Establish the type offset that can be used to lookup the type.
7697 For DWO files, we don't know it until now. */
7698 sig_type->type_offset_in_section
7699 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7703 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7704 &cu->header, section,
7706 info_ptr, rcuh_kind::COMPILE);
7707 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7708 /* For DWOs coming from DWP files, we don't know the CU length
7710 dwo_unit->length = get_cu_length (&cu->header);
7713 *result_dwo_abbrev_table
7714 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7715 cu->header.abbrev_sect_off);
7716 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7717 result_dwo_abbrev_table->get ());
7719 /* Read in the die, but leave space to copy over the attributes
7720 from the stub. This has the benefit of simplifying the rest of
7721 the code - all the work to maintain the illusion of a single
7722 DW_TAG_{compile,type}_unit DIE is done here. */
7723 num_extra_attrs = ((stmt_list != NULL)
7727 + (comp_dir != NULL));
7728 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7729 result_has_children, num_extra_attrs);
7731 /* Copy over the attributes from the stub to the DIE we just read in. */
7732 comp_unit_die = *result_comp_unit_die;
7733 i = comp_unit_die->num_attrs;
7734 if (stmt_list != NULL)
7735 comp_unit_die->attrs[i++] = *stmt_list;
7737 comp_unit_die->attrs[i++] = *low_pc;
7738 if (high_pc != NULL)
7739 comp_unit_die->attrs[i++] = *high_pc;
7741 comp_unit_die->attrs[i++] = *ranges;
7742 if (comp_dir != NULL)
7743 comp_unit_die->attrs[i++] = *comp_dir;
7744 comp_unit_die->num_attrs += num_extra_attrs;
7746 if (dwarf_die_debug)
7748 fprintf_unfiltered (gdb_stdlog,
7749 "Read die from %s@0x%x of %s:\n",
7750 get_section_name (section),
7751 (unsigned) (begin_info_ptr - section->buffer),
7752 bfd_get_filename (abfd));
7753 dump_die (comp_unit_die, dwarf_die_debug);
7756 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7757 TUs by skipping the stub and going directly to the entry in the DWO file.
7758 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7759 to get it via circuitous means. Blech. */
7760 if (comp_dir != NULL)
7761 result_reader->comp_dir = DW_STRING (comp_dir);
7763 /* Skip dummy compilation units. */
7764 if (info_ptr >= begin_info_ptr + dwo_unit->length
7765 || peek_abbrev_code (abfd, info_ptr) == 0)
7768 *result_info_ptr = info_ptr;
7772 /* Subroutine of init_cutu_and_read_dies to simplify it.
7773 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7774 Returns NULL if the specified DWO unit cannot be found. */
7776 static struct dwo_unit *
7777 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7778 struct die_info *comp_unit_die)
7780 struct dwarf2_cu *cu = this_cu->cu;
7782 struct dwo_unit *dwo_unit;
7783 const char *comp_dir, *dwo_name;
7785 gdb_assert (cu != NULL);
7787 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7788 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7789 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7791 if (this_cu->is_debug_types)
7793 struct signatured_type *sig_type;
7795 /* Since this_cu is the first member of struct signatured_type,
7796 we can go from a pointer to one to a pointer to the other. */
7797 sig_type = (struct signatured_type *) this_cu;
7798 signature = sig_type->signature;
7799 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7803 struct attribute *attr;
7805 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7807 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7809 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7810 signature = DW_UNSND (attr);
7811 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7818 /* Subroutine of init_cutu_and_read_dies to simplify it.
7819 See it for a description of the parameters.
7820 Read a TU directly from a DWO file, bypassing the stub. */
7823 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7824 int use_existing_cu, int keep,
7825 die_reader_func_ftype *die_reader_func,
7828 std::unique_ptr<dwarf2_cu> new_cu;
7829 struct signatured_type *sig_type;
7830 struct die_reader_specs reader;
7831 const gdb_byte *info_ptr;
7832 struct die_info *comp_unit_die;
7834 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7836 /* Verify we can do the following downcast, and that we have the
7838 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7839 sig_type = (struct signatured_type *) this_cu;
7840 gdb_assert (sig_type->dwo_unit != NULL);
7842 if (use_existing_cu && this_cu->cu != NULL)
7844 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7845 /* There's no need to do the rereading_dwo_cu handling that
7846 init_cutu_and_read_dies does since we don't read the stub. */
7850 /* If !use_existing_cu, this_cu->cu must be NULL. */
7851 gdb_assert (this_cu->cu == NULL);
7852 new_cu.reset (new dwarf2_cu (this_cu));
7855 /* A future optimization, if needed, would be to use an existing
7856 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7857 could share abbrev tables. */
7859 /* The abbreviation table used by READER, this must live at least as long as
7861 abbrev_table_up dwo_abbrev_table;
7863 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7864 NULL /* stub_comp_unit_die */,
7865 sig_type->dwo_unit->dwo_file->comp_dir,
7867 &comp_unit_die, &has_children,
7868 &dwo_abbrev_table) == 0)
7874 /* All the "real" work is done here. */
7875 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7877 /* This duplicates the code in init_cutu_and_read_dies,
7878 but the alternative is making the latter more complex.
7879 This function is only for the special case of using DWO files directly:
7880 no point in overly complicating the general case just to handle this. */
7881 if (new_cu != NULL && keep)
7883 /* Link this CU into read_in_chain. */
7884 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7885 dwarf2_per_objfile->read_in_chain = this_cu;
7886 /* The chain owns it now. */
7891 /* Initialize a CU (or TU) and read its DIEs.
7892 If the CU defers to a DWO file, read the DWO file as well.
7894 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7895 Otherwise the table specified in the comp unit header is read in and used.
7896 This is an optimization for when we already have the abbrev table.
7898 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7899 Otherwise, a new CU is allocated with xmalloc.
7901 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7902 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7904 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7905 linker) then DIE_READER_FUNC will not get called. */
7908 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7909 struct abbrev_table *abbrev_table,
7910 int use_existing_cu, int keep,
7911 die_reader_func_ftype *die_reader_func,
7914 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7915 struct objfile *objfile = dwarf2_per_objfile->objfile;
7916 struct dwarf2_section_info *section = this_cu->section;
7917 bfd *abfd = get_section_bfd_owner (section);
7918 struct dwarf2_cu *cu;
7919 const gdb_byte *begin_info_ptr, *info_ptr;
7920 struct die_reader_specs reader;
7921 struct die_info *comp_unit_die;
7923 struct attribute *attr;
7924 struct signatured_type *sig_type = NULL;
7925 struct dwarf2_section_info *abbrev_section;
7926 /* Non-zero if CU currently points to a DWO file and we need to
7927 reread it. When this happens we need to reread the skeleton die
7928 before we can reread the DWO file (this only applies to CUs, not TUs). */
7929 int rereading_dwo_cu = 0;
7931 if (dwarf_die_debug)
7932 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7933 this_cu->is_debug_types ? "type" : "comp",
7934 sect_offset_str (this_cu->sect_off));
7936 if (use_existing_cu)
7939 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7940 file (instead of going through the stub), short-circuit all of this. */
7941 if (this_cu->reading_dwo_directly)
7943 /* Narrow down the scope of possibilities to have to understand. */
7944 gdb_assert (this_cu->is_debug_types);
7945 gdb_assert (abbrev_table == NULL);
7946 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7947 die_reader_func, data);
7951 /* This is cheap if the section is already read in. */
7952 dwarf2_read_section (objfile, section);
7954 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7956 abbrev_section = get_abbrev_section_for_cu (this_cu);
7958 std::unique_ptr<dwarf2_cu> new_cu;
7959 if (use_existing_cu && this_cu->cu != NULL)
7962 /* If this CU is from a DWO file we need to start over, we need to
7963 refetch the attributes from the skeleton CU.
7964 This could be optimized by retrieving those attributes from when we
7965 were here the first time: the previous comp_unit_die was stored in
7966 comp_unit_obstack. But there's no data yet that we need this
7968 if (cu->dwo_unit != NULL)
7969 rereading_dwo_cu = 1;
7973 /* If !use_existing_cu, this_cu->cu must be NULL. */
7974 gdb_assert (this_cu->cu == NULL);
7975 new_cu.reset (new dwarf2_cu (this_cu));
7979 /* Get the header. */
7980 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7982 /* We already have the header, there's no need to read it in again. */
7983 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7987 if (this_cu->is_debug_types)
7989 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7990 &cu->header, section,
7991 abbrev_section, info_ptr,
7994 /* Since per_cu is the first member of struct signatured_type,
7995 we can go from a pointer to one to a pointer to the other. */
7996 sig_type = (struct signatured_type *) this_cu;
7997 gdb_assert (sig_type->signature == cu->header.signature);
7998 gdb_assert (sig_type->type_offset_in_tu
7999 == cu->header.type_cu_offset_in_tu);
8000 gdb_assert (this_cu->sect_off == cu->header.sect_off);
8002 /* LENGTH has not been set yet for type units if we're
8003 using .gdb_index. */
8004 this_cu->length = get_cu_length (&cu->header);
8006 /* Establish the type offset that can be used to lookup the type. */
8007 sig_type->type_offset_in_section =
8008 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
8010 this_cu->dwarf_version = cu->header.version;
8014 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
8015 &cu->header, section,
8018 rcuh_kind::COMPILE);
8020 gdb_assert (this_cu->sect_off == cu->header.sect_off);
8021 gdb_assert (this_cu->length == get_cu_length (&cu->header));
8022 this_cu->dwarf_version = cu->header.version;
8026 /* Skip dummy compilation units. */
8027 if (info_ptr >= begin_info_ptr + this_cu->length
8028 || peek_abbrev_code (abfd, info_ptr) == 0)
8031 /* If we don't have them yet, read the abbrevs for this compilation unit.
8032 And if we need to read them now, make sure they're freed when we're
8033 done (own the table through ABBREV_TABLE_HOLDER). */
8034 abbrev_table_up abbrev_table_holder;
8035 if (abbrev_table != NULL)
8036 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
8040 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
8041 cu->header.abbrev_sect_off);
8042 abbrev_table = abbrev_table_holder.get ();
8045 /* Read the top level CU/TU die. */
8046 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
8047 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8049 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
8050 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
8051 table from the DWO file and pass the ownership over to us. It will be
8052 referenced from READER, so we must make sure to free it after we're done
8055 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
8056 DWO CU, that this test will fail (the attribute will not be present). */
8057 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
8058 abbrev_table_up dwo_abbrev_table;
8061 struct dwo_unit *dwo_unit;
8062 struct die_info *dwo_comp_unit_die;
8066 complaint (&symfile_complaints,
8067 _("compilation unit with DW_AT_GNU_dwo_name"
8068 " has children (offset %s) [in module %s]"),
8069 sect_offset_str (this_cu->sect_off),
8070 bfd_get_filename (abfd));
8072 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
8073 if (dwo_unit != NULL)
8075 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
8076 comp_unit_die, NULL,
8078 &dwo_comp_unit_die, &has_children,
8079 &dwo_abbrev_table) == 0)
8084 comp_unit_die = dwo_comp_unit_die;
8088 /* Yikes, we couldn't find the rest of the DIE, we only have
8089 the stub. A complaint has already been logged. There's
8090 not much more we can do except pass on the stub DIE to
8091 die_reader_func. We don't want to throw an error on bad
8096 /* All of the above is setup for this call. Yikes. */
8097 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8099 /* Done, clean up. */
8100 if (new_cu != NULL && keep)
8102 /* Link this CU into read_in_chain. */
8103 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
8104 dwarf2_per_objfile->read_in_chain = this_cu;
8105 /* The chain owns it now. */
8110 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
8111 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
8112 to have already done the lookup to find the DWO file).
8114 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
8115 THIS_CU->is_debug_types, but nothing else.
8117 We fill in THIS_CU->length.
8119 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
8120 linker) then DIE_READER_FUNC will not get called.
8122 THIS_CU->cu is always freed when done.
8123 This is done in order to not leave THIS_CU->cu in a state where we have
8124 to care whether it refers to the "main" CU or the DWO CU. */
8127 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
8128 struct dwo_file *dwo_file,
8129 die_reader_func_ftype *die_reader_func,
8132 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
8133 struct objfile *objfile = dwarf2_per_objfile->objfile;
8134 struct dwarf2_section_info *section = this_cu->section;
8135 bfd *abfd = get_section_bfd_owner (section);
8136 struct dwarf2_section_info *abbrev_section;
8137 const gdb_byte *begin_info_ptr, *info_ptr;
8138 struct die_reader_specs reader;
8139 struct die_info *comp_unit_die;
8142 if (dwarf_die_debug)
8143 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
8144 this_cu->is_debug_types ? "type" : "comp",
8145 sect_offset_str (this_cu->sect_off));
8147 gdb_assert (this_cu->cu == NULL);
8149 abbrev_section = (dwo_file != NULL
8150 ? &dwo_file->sections.abbrev
8151 : get_abbrev_section_for_cu (this_cu));
8153 /* This is cheap if the section is already read in. */
8154 dwarf2_read_section (objfile, section);
8156 struct dwarf2_cu cu (this_cu);
8158 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
8159 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
8160 &cu.header, section,
8161 abbrev_section, info_ptr,
8162 (this_cu->is_debug_types
8164 : rcuh_kind::COMPILE));
8166 this_cu->length = get_cu_length (&cu.header);
8168 /* Skip dummy compilation units. */
8169 if (info_ptr >= begin_info_ptr + this_cu->length
8170 || peek_abbrev_code (abfd, info_ptr) == 0)
8173 abbrev_table_up abbrev_table
8174 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
8175 cu.header.abbrev_sect_off);
8177 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
8178 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8180 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8183 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
8184 does not lookup the specified DWO file.
8185 This cannot be used to read DWO files.
8187 THIS_CU->cu is always freed when done.
8188 This is done in order to not leave THIS_CU->cu in a state where we have
8189 to care whether it refers to the "main" CU or the DWO CU.
8190 We can revisit this if the data shows there's a performance issue. */
8193 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
8194 die_reader_func_ftype *die_reader_func,
8197 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
8200 /* Type Unit Groups.
8202 Type Unit Groups are a way to collapse the set of all TUs (type units) into
8203 a more manageable set. The grouping is done by DW_AT_stmt_list entry
8204 so that all types coming from the same compilation (.o file) are grouped
8205 together. A future step could be to put the types in the same symtab as
8206 the CU the types ultimately came from. */
8209 hash_type_unit_group (const void *item)
8211 const struct type_unit_group *tu_group
8212 = (const struct type_unit_group *) item;
8214 return hash_stmt_list_entry (&tu_group->hash);
8218 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
8220 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
8221 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
8223 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
8226 /* Allocate a hash table for type unit groups. */
8229 allocate_type_unit_groups_table (struct objfile *objfile)
8231 return htab_create_alloc_ex (3,
8232 hash_type_unit_group,
8235 &objfile->objfile_obstack,
8236 hashtab_obstack_allocate,
8237 dummy_obstack_deallocate);
8240 /* Type units that don't have DW_AT_stmt_list are grouped into their own
8241 partial symtabs. We combine several TUs per psymtab to not let the size
8242 of any one psymtab grow too big. */
8243 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
8244 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
8246 /* Helper routine for get_type_unit_group.
8247 Create the type_unit_group object used to hold one or more TUs. */
8249 static struct type_unit_group *
8250 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
8252 struct dwarf2_per_objfile *dwarf2_per_objfile
8253 = cu->per_cu->dwarf2_per_objfile;
8254 struct objfile *objfile = dwarf2_per_objfile->objfile;
8255 struct dwarf2_per_cu_data *per_cu;
8256 struct type_unit_group *tu_group;
8258 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8259 struct type_unit_group);
8260 per_cu = &tu_group->per_cu;
8261 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8263 if (dwarf2_per_objfile->using_index)
8265 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8266 struct dwarf2_per_cu_quick_data);
8270 unsigned int line_offset = to_underlying (line_offset_struct);
8271 struct partial_symtab *pst;
8274 /* Give the symtab a useful name for debug purposes. */
8275 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
8276 name = xstrprintf ("<type_units_%d>",
8277 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
8279 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
8281 pst = create_partial_symtab (per_cu, name);
8287 tu_group->hash.dwo_unit = cu->dwo_unit;
8288 tu_group->hash.line_sect_off = line_offset_struct;
8293 /* Look up the type_unit_group for type unit CU, and create it if necessary.
8294 STMT_LIST is a DW_AT_stmt_list attribute. */
8296 static struct type_unit_group *
8297 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
8299 struct dwarf2_per_objfile *dwarf2_per_objfile
8300 = cu->per_cu->dwarf2_per_objfile;
8301 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8302 struct type_unit_group *tu_group;
8304 unsigned int line_offset;
8305 struct type_unit_group type_unit_group_for_lookup;
8307 if (dwarf2_per_objfile->type_unit_groups == NULL)
8309 dwarf2_per_objfile->type_unit_groups =
8310 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
8313 /* Do we need to create a new group, or can we use an existing one? */
8317 line_offset = DW_UNSND (stmt_list);
8318 ++tu_stats->nr_symtab_sharers;
8322 /* Ugh, no stmt_list. Rare, but we have to handle it.
8323 We can do various things here like create one group per TU or
8324 spread them over multiple groups to split up the expansion work.
8325 To avoid worst case scenarios (too many groups or too large groups)
8326 we, umm, group them in bunches. */
8327 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
8328 | (tu_stats->nr_stmt_less_type_units
8329 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
8330 ++tu_stats->nr_stmt_less_type_units;
8333 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
8334 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
8335 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8336 &type_unit_group_for_lookup, INSERT);
8339 tu_group = (struct type_unit_group *) *slot;
8340 gdb_assert (tu_group != NULL);
8344 sect_offset line_offset_struct = (sect_offset) line_offset;
8345 tu_group = create_type_unit_group (cu, line_offset_struct);
8347 ++tu_stats->nr_symtabs;
8353 /* Partial symbol tables. */
8355 /* Create a psymtab named NAME and assign it to PER_CU.
8357 The caller must fill in the following details:
8358 dirname, textlow, texthigh. */
8360 static struct partial_symtab *
8361 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8363 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
8364 struct partial_symtab *pst;
8366 pst = start_psymtab_common (objfile, name, 0,
8367 objfile->global_psymbols,
8368 objfile->static_psymbols);
8370 pst->psymtabs_addrmap_supported = 1;
8372 /* This is the glue that links PST into GDB's symbol API. */
8373 pst->read_symtab_private = per_cu;
8374 pst->read_symtab = dwarf2_read_symtab;
8375 per_cu->v.psymtab = pst;
8380 /* The DATA object passed to process_psymtab_comp_unit_reader has this
8383 struct process_psymtab_comp_unit_data
8385 /* True if we are reading a DW_TAG_partial_unit. */
8387 int want_partial_unit;
8389 /* The "pretend" language that is used if the CU doesn't declare a
8392 enum language pretend_language;
8395 /* die_reader_func for process_psymtab_comp_unit. */
8398 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8399 const gdb_byte *info_ptr,
8400 struct die_info *comp_unit_die,
8404 struct dwarf2_cu *cu = reader->cu;
8405 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8406 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8407 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8409 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8410 struct partial_symtab *pst;
8411 enum pc_bounds_kind cu_bounds_kind;
8412 const char *filename;
8413 struct process_psymtab_comp_unit_data *info
8414 = (struct process_psymtab_comp_unit_data *) data;
8416 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8419 gdb_assert (! per_cu->is_debug_types);
8421 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8423 cu->list_in_scope = &file_symbols;
8425 /* Allocate a new partial symbol table structure. */
8426 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8427 if (filename == NULL)
8430 pst = create_partial_symtab (per_cu, filename);
8432 /* This must be done before calling dwarf2_build_include_psymtabs. */
8433 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8435 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8437 dwarf2_find_base_address (comp_unit_die, cu);
8439 /* Possibly set the default values of LOWPC and HIGHPC from
8441 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8442 &best_highpc, cu, pst);
8443 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8444 /* Store the contiguous range if it is not empty; it can be empty for
8445 CUs with no code. */
8446 addrmap_set_empty (objfile->psymtabs_addrmap,
8447 gdbarch_adjust_dwarf2_addr (gdbarch,
8448 best_lowpc + baseaddr),
8449 gdbarch_adjust_dwarf2_addr (gdbarch,
8450 best_highpc + baseaddr) - 1,
8453 /* Check if comp unit has_children.
8454 If so, read the rest of the partial symbols from this comp unit.
8455 If not, there's no more debug_info for this comp unit. */
8458 struct partial_die_info *first_die;
8459 CORE_ADDR lowpc, highpc;
8461 lowpc = ((CORE_ADDR) -1);
8462 highpc = ((CORE_ADDR) 0);
8464 first_die = load_partial_dies (reader, info_ptr, 1);
8466 scan_partial_symbols (first_die, &lowpc, &highpc,
8467 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8469 /* If we didn't find a lowpc, set it to highpc to avoid
8470 complaints from `maint check'. */
8471 if (lowpc == ((CORE_ADDR) -1))
8474 /* If the compilation unit didn't have an explicit address range,
8475 then use the information extracted from its child dies. */
8476 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8479 best_highpc = highpc;
8482 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8483 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
8485 end_psymtab_common (objfile, pst);
8487 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8490 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8491 struct dwarf2_per_cu_data *iter;
8493 /* Fill in 'dependencies' here; we fill in 'users' in a
8495 pst->number_of_dependencies = len;
8497 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8499 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8502 pst->dependencies[i] = iter->v.psymtab;
8504 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8507 /* Get the list of files included in the current compilation unit,
8508 and build a psymtab for each of them. */
8509 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8511 if (dwarf_read_debug)
8513 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8515 fprintf_unfiltered (gdb_stdlog,
8516 "Psymtab for %s unit @%s: %s - %s"
8517 ", %d global, %d static syms\n",
8518 per_cu->is_debug_types ? "type" : "comp",
8519 sect_offset_str (per_cu->sect_off),
8520 paddress (gdbarch, pst->textlow),
8521 paddress (gdbarch, pst->texthigh),
8522 pst->n_global_syms, pst->n_static_syms);
8526 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8527 Process compilation unit THIS_CU for a psymtab. */
8530 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8531 int want_partial_unit,
8532 enum language pretend_language)
8534 /* If this compilation unit was already read in, free the
8535 cached copy in order to read it in again. This is
8536 necessary because we skipped some symbols when we first
8537 read in the compilation unit (see load_partial_dies).
8538 This problem could be avoided, but the benefit is unclear. */
8539 if (this_cu->cu != NULL)
8540 free_one_cached_comp_unit (this_cu);
8542 if (this_cu->is_debug_types)
8543 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8547 process_psymtab_comp_unit_data info;
8548 info.want_partial_unit = want_partial_unit;
8549 info.pretend_language = pretend_language;
8550 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8551 process_psymtab_comp_unit_reader, &info);
8554 /* Age out any secondary CUs. */
8555 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8558 /* Reader function for build_type_psymtabs. */
8561 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8562 const gdb_byte *info_ptr,
8563 struct die_info *type_unit_die,
8567 struct dwarf2_per_objfile *dwarf2_per_objfile
8568 = reader->cu->per_cu->dwarf2_per_objfile;
8569 struct objfile *objfile = dwarf2_per_objfile->objfile;
8570 struct dwarf2_cu *cu = reader->cu;
8571 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8572 struct signatured_type *sig_type;
8573 struct type_unit_group *tu_group;
8574 struct attribute *attr;
8575 struct partial_die_info *first_die;
8576 CORE_ADDR lowpc, highpc;
8577 struct partial_symtab *pst;
8579 gdb_assert (data == NULL);
8580 gdb_assert (per_cu->is_debug_types);
8581 sig_type = (struct signatured_type *) per_cu;
8586 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8587 tu_group = get_type_unit_group (cu, attr);
8589 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8591 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8592 cu->list_in_scope = &file_symbols;
8593 pst = create_partial_symtab (per_cu, "");
8596 first_die = load_partial_dies (reader, info_ptr, 1);
8598 lowpc = (CORE_ADDR) -1;
8599 highpc = (CORE_ADDR) 0;
8600 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8602 end_psymtab_common (objfile, pst);
8605 /* Struct used to sort TUs by their abbreviation table offset. */
8607 struct tu_abbrev_offset
8609 struct signatured_type *sig_type;
8610 sect_offset abbrev_offset;
8613 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
8616 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
8618 const struct tu_abbrev_offset * const *a
8619 = (const struct tu_abbrev_offset * const*) ap;
8620 const struct tu_abbrev_offset * const *b
8621 = (const struct tu_abbrev_offset * const*) bp;
8622 sect_offset aoff = (*a)->abbrev_offset;
8623 sect_offset boff = (*b)->abbrev_offset;
8625 return (aoff > boff) - (aoff < boff);
8628 /* Efficiently read all the type units.
8629 This does the bulk of the work for build_type_psymtabs.
8631 The efficiency is because we sort TUs by the abbrev table they use and
8632 only read each abbrev table once. In one program there are 200K TUs
8633 sharing 8K abbrev tables.
8635 The main purpose of this function is to support building the
8636 dwarf2_per_objfile->type_unit_groups table.
8637 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8638 can collapse the search space by grouping them by stmt_list.
8639 The savings can be significant, in the same program from above the 200K TUs
8640 share 8K stmt_list tables.
8642 FUNC is expected to call get_type_unit_group, which will create the
8643 struct type_unit_group if necessary and add it to
8644 dwarf2_per_objfile->type_unit_groups. */
8647 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8649 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8650 struct cleanup *cleanups;
8651 abbrev_table_up abbrev_table;
8652 sect_offset abbrev_offset;
8653 struct tu_abbrev_offset *sorted_by_abbrev;
8656 /* It's up to the caller to not call us multiple times. */
8657 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8659 if (dwarf2_per_objfile->n_type_units == 0)
8662 /* TUs typically share abbrev tables, and there can be way more TUs than
8663 abbrev tables. Sort by abbrev table to reduce the number of times we
8664 read each abbrev table in.
8665 Alternatives are to punt or to maintain a cache of abbrev tables.
8666 This is simpler and efficient enough for now.
8668 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8669 symtab to use). Typically TUs with the same abbrev offset have the same
8670 stmt_list value too so in practice this should work well.
8672 The basic algorithm here is:
8674 sort TUs by abbrev table
8675 for each TU with same abbrev table:
8676 read abbrev table if first user
8677 read TU top level DIE
8678 [IWBN if DWO skeletons had DW_AT_stmt_list]
8681 if (dwarf_read_debug)
8682 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8684 /* Sort in a separate table to maintain the order of all_type_units
8685 for .gdb_index: TU indices directly index all_type_units. */
8686 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
8687 dwarf2_per_objfile->n_type_units);
8688 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8690 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8692 sorted_by_abbrev[i].sig_type = sig_type;
8693 sorted_by_abbrev[i].abbrev_offset =
8694 read_abbrev_offset (dwarf2_per_objfile,
8695 sig_type->per_cu.section,
8696 sig_type->per_cu.sect_off);
8698 cleanups = make_cleanup (xfree, sorted_by_abbrev);
8699 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
8700 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
8702 abbrev_offset = (sect_offset) ~(unsigned) 0;
8704 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8706 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8708 /* Switch to the next abbrev table if necessary. */
8709 if (abbrev_table == NULL
8710 || tu->abbrev_offset != abbrev_offset)
8712 abbrev_offset = tu->abbrev_offset;
8714 abbrev_table_read_table (dwarf2_per_objfile,
8715 &dwarf2_per_objfile->abbrev,
8717 ++tu_stats->nr_uniq_abbrev_tables;
8720 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table.get (),
8721 0, 0, build_type_psymtabs_reader, NULL);
8724 do_cleanups (cleanups);
8727 /* Print collected type unit statistics. */
8730 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8732 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8734 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8735 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8736 dwarf2_per_objfile->n_type_units);
8737 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8738 tu_stats->nr_uniq_abbrev_tables);
8739 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8740 tu_stats->nr_symtabs);
8741 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8742 tu_stats->nr_symtab_sharers);
8743 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8744 tu_stats->nr_stmt_less_type_units);
8745 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8746 tu_stats->nr_all_type_units_reallocs);
8749 /* Traversal function for build_type_psymtabs. */
8752 build_type_psymtab_dependencies (void **slot, void *info)
8754 struct dwarf2_per_objfile *dwarf2_per_objfile
8755 = (struct dwarf2_per_objfile *) info;
8756 struct objfile *objfile = dwarf2_per_objfile->objfile;
8757 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8758 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8759 struct partial_symtab *pst = per_cu->v.psymtab;
8760 int len = VEC_length (sig_type_ptr, tu_group->tus);
8761 struct signatured_type *iter;
8764 gdb_assert (len > 0);
8765 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8767 pst->number_of_dependencies = len;
8769 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8771 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8774 gdb_assert (iter->per_cu.is_debug_types);
8775 pst->dependencies[i] = iter->per_cu.v.psymtab;
8776 iter->type_unit_group = tu_group;
8779 VEC_free (sig_type_ptr, tu_group->tus);
8784 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8785 Build partial symbol tables for the .debug_types comp-units. */
8788 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8790 if (! create_all_type_units (dwarf2_per_objfile))
8793 build_type_psymtabs_1 (dwarf2_per_objfile);
8796 /* Traversal function for process_skeletonless_type_unit.
8797 Read a TU in a DWO file and build partial symbols for it. */
8800 process_skeletonless_type_unit (void **slot, void *info)
8802 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8803 struct dwarf2_per_objfile *dwarf2_per_objfile
8804 = (struct dwarf2_per_objfile *) info;
8805 struct signatured_type find_entry, *entry;
8807 /* If this TU doesn't exist in the global table, add it and read it in. */
8809 if (dwarf2_per_objfile->signatured_types == NULL)
8811 dwarf2_per_objfile->signatured_types
8812 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8815 find_entry.signature = dwo_unit->signature;
8816 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8818 /* If we've already seen this type there's nothing to do. What's happening
8819 is we're doing our own version of comdat-folding here. */
8823 /* This does the job that create_all_type_units would have done for
8825 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8826 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8829 /* This does the job that build_type_psymtabs_1 would have done. */
8830 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8831 build_type_psymtabs_reader, NULL);
8836 /* Traversal function for process_skeletonless_type_units. */
8839 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8841 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8843 if (dwo_file->tus != NULL)
8845 htab_traverse_noresize (dwo_file->tus,
8846 process_skeletonless_type_unit, info);
8852 /* Scan all TUs of DWO files, verifying we've processed them.
8853 This is needed in case a TU was emitted without its skeleton.
8854 Note: This can't be done until we know what all the DWO files are. */
8857 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8859 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8860 if (get_dwp_file (dwarf2_per_objfile) == NULL
8861 && dwarf2_per_objfile->dwo_files != NULL)
8863 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8864 process_dwo_file_for_skeletonless_type_units,
8865 dwarf2_per_objfile);
8869 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8872 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8876 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8878 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8879 struct partial_symtab *pst = per_cu->v.psymtab;
8885 for (j = 0; j < pst->number_of_dependencies; ++j)
8887 /* Set the 'user' field only if it is not already set. */
8888 if (pst->dependencies[j]->user == NULL)
8889 pst->dependencies[j]->user = pst;
8894 /* Build the partial symbol table by doing a quick pass through the
8895 .debug_info and .debug_abbrev sections. */
8898 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8900 struct cleanup *back_to;
8902 struct objfile *objfile = dwarf2_per_objfile->objfile;
8904 if (dwarf_read_debug)
8906 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8907 objfile_name (objfile));
8910 dwarf2_per_objfile->reading_partial_symbols = 1;
8912 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8914 /* Any cached compilation units will be linked by the per-objfile
8915 read_in_chain. Make sure to free them when we're done. */
8916 back_to = make_cleanup (free_cached_comp_units, dwarf2_per_objfile);
8918 build_type_psymtabs (dwarf2_per_objfile);
8920 create_all_comp_units (dwarf2_per_objfile);
8922 /* Create a temporary address map on a temporary obstack. We later
8923 copy this to the final obstack. */
8924 auto_obstack temp_obstack;
8926 scoped_restore save_psymtabs_addrmap
8927 = make_scoped_restore (&objfile->psymtabs_addrmap,
8928 addrmap_create_mutable (&temp_obstack));
8930 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8932 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8934 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8937 /* This has to wait until we read the CUs, we need the list of DWOs. */
8938 process_skeletonless_type_units (dwarf2_per_objfile);
8940 /* Now that all TUs have been processed we can fill in the dependencies. */
8941 if (dwarf2_per_objfile->type_unit_groups != NULL)
8943 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8944 build_type_psymtab_dependencies, dwarf2_per_objfile);
8947 if (dwarf_read_debug)
8948 print_tu_stats (dwarf2_per_objfile);
8950 set_partial_user (dwarf2_per_objfile);
8952 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8953 &objfile->objfile_obstack);
8954 /* At this point we want to keep the address map. */
8955 save_psymtabs_addrmap.release ();
8957 do_cleanups (back_to);
8959 if (dwarf_read_debug)
8960 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8961 objfile_name (objfile));
8964 /* die_reader_func for load_partial_comp_unit. */
8967 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8968 const gdb_byte *info_ptr,
8969 struct die_info *comp_unit_die,
8973 struct dwarf2_cu *cu = reader->cu;
8975 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8977 /* Check if comp unit has_children.
8978 If so, read the rest of the partial symbols from this comp unit.
8979 If not, there's no more debug_info for this comp unit. */
8981 load_partial_dies (reader, info_ptr, 0);
8984 /* Load the partial DIEs for a secondary CU into memory.
8985 This is also used when rereading a primary CU with load_all_dies. */
8988 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8990 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8991 load_partial_comp_unit_reader, NULL);
8995 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8996 struct dwarf2_section_info *section,
8997 struct dwarf2_section_info *abbrev_section,
8998 unsigned int is_dwz,
9001 struct dwarf2_per_cu_data ***all_comp_units)
9003 const gdb_byte *info_ptr;
9004 struct objfile *objfile = dwarf2_per_objfile->objfile;
9006 if (dwarf_read_debug)
9007 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
9008 get_section_name (section),
9009 get_section_file_name (section));
9011 dwarf2_read_section (objfile, section);
9013 info_ptr = section->buffer;
9015 while (info_ptr < section->buffer + section->size)
9017 struct dwarf2_per_cu_data *this_cu;
9019 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
9021 comp_unit_head cu_header;
9022 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
9023 abbrev_section, info_ptr,
9024 rcuh_kind::COMPILE);
9026 /* Save the compilation unit for later lookup. */
9027 if (cu_header.unit_type != DW_UT_type)
9029 this_cu = XOBNEW (&objfile->objfile_obstack,
9030 struct dwarf2_per_cu_data);
9031 memset (this_cu, 0, sizeof (*this_cu));
9035 auto sig_type = XOBNEW (&objfile->objfile_obstack,
9036 struct signatured_type);
9037 memset (sig_type, 0, sizeof (*sig_type));
9038 sig_type->signature = cu_header.signature;
9039 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
9040 this_cu = &sig_type->per_cu;
9042 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9043 this_cu->sect_off = sect_off;
9044 this_cu->length = cu_header.length + cu_header.initial_length_size;
9045 this_cu->is_dwz = is_dwz;
9046 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
9047 this_cu->section = section;
9049 if (*n_comp_units == *n_allocated)
9052 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
9053 *all_comp_units, *n_allocated);
9055 (*all_comp_units)[*n_comp_units] = this_cu;
9058 info_ptr = info_ptr + this_cu->length;
9062 /* Create a list of all compilation units in OBJFILE.
9063 This is only done for -readnow and building partial symtabs. */
9066 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
9070 struct dwarf2_per_cu_data **all_comp_units;
9071 struct dwz_file *dwz;
9072 struct objfile *objfile = dwarf2_per_objfile->objfile;
9076 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
9078 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
9079 &dwarf2_per_objfile->abbrev, 0,
9080 &n_allocated, &n_comp_units, &all_comp_units);
9082 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
9084 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
9085 1, &n_allocated, &n_comp_units,
9088 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
9089 struct dwarf2_per_cu_data *,
9091 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
9092 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
9093 xfree (all_comp_units);
9094 dwarf2_per_objfile->n_comp_units = n_comp_units;
9097 /* Process all loaded DIEs for compilation unit CU, starting at
9098 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
9099 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
9100 DW_AT_ranges). See the comments of add_partial_subprogram on how
9101 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
9104 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
9105 CORE_ADDR *highpc, int set_addrmap,
9106 struct dwarf2_cu *cu)
9108 struct partial_die_info *pdi;
9110 /* Now, march along the PDI's, descending into ones which have
9111 interesting children but skipping the children of the other ones,
9112 until we reach the end of the compilation unit. */
9120 /* Anonymous namespaces or modules have no name but have interesting
9121 children, so we need to look at them. Ditto for anonymous
9124 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
9125 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
9126 || pdi->tag == DW_TAG_imported_unit
9127 || pdi->tag == DW_TAG_inlined_subroutine)
9131 case DW_TAG_subprogram:
9132 case DW_TAG_inlined_subroutine:
9133 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9135 case DW_TAG_constant:
9136 case DW_TAG_variable:
9137 case DW_TAG_typedef:
9138 case DW_TAG_union_type:
9139 if (!pdi->is_declaration)
9141 add_partial_symbol (pdi, cu);
9144 case DW_TAG_class_type:
9145 case DW_TAG_interface_type:
9146 case DW_TAG_structure_type:
9147 if (!pdi->is_declaration)
9149 add_partial_symbol (pdi, cu);
9151 if (cu->language == language_rust && pdi->has_children)
9152 scan_partial_symbols (pdi->die_child, lowpc, highpc,
9155 case DW_TAG_enumeration_type:
9156 if (!pdi->is_declaration)
9157 add_partial_enumeration (pdi, cu);
9159 case DW_TAG_base_type:
9160 case DW_TAG_subrange_type:
9161 /* File scope base type definitions are added to the partial
9163 add_partial_symbol (pdi, cu);
9165 case DW_TAG_namespace:
9166 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
9169 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
9171 case DW_TAG_imported_unit:
9173 struct dwarf2_per_cu_data *per_cu;
9175 /* For now we don't handle imported units in type units. */
9176 if (cu->per_cu->is_debug_types)
9178 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9179 " supported in type units [in module %s]"),
9180 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9183 per_cu = dwarf2_find_containing_comp_unit
9184 (pdi->d.sect_off, pdi->is_dwz,
9185 cu->per_cu->dwarf2_per_objfile);
9187 /* Go read the partial unit, if needed. */
9188 if (per_cu->v.psymtab == NULL)
9189 process_psymtab_comp_unit (per_cu, 1, cu->language);
9191 VEC_safe_push (dwarf2_per_cu_ptr,
9192 cu->per_cu->imported_symtabs, per_cu);
9195 case DW_TAG_imported_declaration:
9196 add_partial_symbol (pdi, cu);
9203 /* If the die has a sibling, skip to the sibling. */
9205 pdi = pdi->die_sibling;
9209 /* Functions used to compute the fully scoped name of a partial DIE.
9211 Normally, this is simple. For C++, the parent DIE's fully scoped
9212 name is concatenated with "::" and the partial DIE's name.
9213 Enumerators are an exception; they use the scope of their parent
9214 enumeration type, i.e. the name of the enumeration type is not
9215 prepended to the enumerator.
9217 There are two complexities. One is DW_AT_specification; in this
9218 case "parent" means the parent of the target of the specification,
9219 instead of the direct parent of the DIE. The other is compilers
9220 which do not emit DW_TAG_namespace; in this case we try to guess
9221 the fully qualified name of structure types from their members'
9222 linkage names. This must be done using the DIE's children rather
9223 than the children of any DW_AT_specification target. We only need
9224 to do this for structures at the top level, i.e. if the target of
9225 any DW_AT_specification (if any; otherwise the DIE itself) does not
9228 /* Compute the scope prefix associated with PDI's parent, in
9229 compilation unit CU. The result will be allocated on CU's
9230 comp_unit_obstack, or a copy of the already allocated PDI->NAME
9231 field. NULL is returned if no prefix is necessary. */
9233 partial_die_parent_scope (struct partial_die_info *pdi,
9234 struct dwarf2_cu *cu)
9236 const char *grandparent_scope;
9237 struct partial_die_info *parent, *real_pdi;
9239 /* We need to look at our parent DIE; if we have a DW_AT_specification,
9240 then this means the parent of the specification DIE. */
9243 while (real_pdi->has_specification)
9244 real_pdi = find_partial_die (real_pdi->spec_offset,
9245 real_pdi->spec_is_dwz, cu);
9247 parent = real_pdi->die_parent;
9251 if (parent->scope_set)
9252 return parent->scope;
9256 grandparent_scope = partial_die_parent_scope (parent, cu);
9258 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9259 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9260 Work around this problem here. */
9261 if (cu->language == language_cplus
9262 && parent->tag == DW_TAG_namespace
9263 && strcmp (parent->name, "::") == 0
9264 && grandparent_scope == NULL)
9266 parent->scope = NULL;
9267 parent->scope_set = 1;
9271 if (pdi->tag == DW_TAG_enumerator)
9272 /* Enumerators should not get the name of the enumeration as a prefix. */
9273 parent->scope = grandparent_scope;
9274 else if (parent->tag == DW_TAG_namespace
9275 || parent->tag == DW_TAG_module
9276 || parent->tag == DW_TAG_structure_type
9277 || parent->tag == DW_TAG_class_type
9278 || parent->tag == DW_TAG_interface_type
9279 || parent->tag == DW_TAG_union_type
9280 || parent->tag == DW_TAG_enumeration_type)
9282 if (grandparent_scope == NULL)
9283 parent->scope = parent->name;
9285 parent->scope = typename_concat (&cu->comp_unit_obstack,
9287 parent->name, 0, cu);
9291 /* FIXME drow/2004-04-01: What should we be doing with
9292 function-local names? For partial symbols, we should probably be
9294 complaint (&symfile_complaints,
9295 _("unhandled containing DIE tag %d for DIE at %s"),
9296 parent->tag, sect_offset_str (pdi->sect_off));
9297 parent->scope = grandparent_scope;
9300 parent->scope_set = 1;
9301 return parent->scope;
9304 /* Return the fully scoped name associated with PDI, from compilation unit
9305 CU. The result will be allocated with malloc. */
9308 partial_die_full_name (struct partial_die_info *pdi,
9309 struct dwarf2_cu *cu)
9311 const char *parent_scope;
9313 /* If this is a template instantiation, we can not work out the
9314 template arguments from partial DIEs. So, unfortunately, we have
9315 to go through the full DIEs. At least any work we do building
9316 types here will be reused if full symbols are loaded later. */
9317 if (pdi->has_template_arguments)
9321 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
9323 struct die_info *die;
9324 struct attribute attr;
9325 struct dwarf2_cu *ref_cu = cu;
9327 /* DW_FORM_ref_addr is using section offset. */
9328 attr.name = (enum dwarf_attribute) 0;
9329 attr.form = DW_FORM_ref_addr;
9330 attr.u.unsnd = to_underlying (pdi->sect_off);
9331 die = follow_die_ref (NULL, &attr, &ref_cu);
9333 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
9337 parent_scope = partial_die_parent_scope (pdi, cu);
9338 if (parent_scope == NULL)
9341 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
9345 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
9347 struct dwarf2_per_objfile *dwarf2_per_objfile
9348 = cu->per_cu->dwarf2_per_objfile;
9349 struct objfile *objfile = dwarf2_per_objfile->objfile;
9350 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9352 const char *actual_name = NULL;
9354 char *built_actual_name;
9356 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9358 built_actual_name = partial_die_full_name (pdi, cu);
9359 if (built_actual_name != NULL)
9360 actual_name = built_actual_name;
9362 if (actual_name == NULL)
9363 actual_name = pdi->name;
9367 case DW_TAG_inlined_subroutine:
9368 case DW_TAG_subprogram:
9369 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
9370 if (pdi->is_external || cu->language == language_ada)
9372 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
9373 of the global scope. But in Ada, we want to be able to access
9374 nested procedures globally. So all Ada subprograms are stored
9375 in the global scope. */
9376 add_psymbol_to_list (actual_name, strlen (actual_name),
9377 built_actual_name != NULL,
9378 VAR_DOMAIN, LOC_BLOCK,
9379 &objfile->global_psymbols,
9380 addr, cu->language, objfile);
9384 add_psymbol_to_list (actual_name, strlen (actual_name),
9385 built_actual_name != NULL,
9386 VAR_DOMAIN, LOC_BLOCK,
9387 &objfile->static_psymbols,
9388 addr, cu->language, objfile);
9391 if (pdi->main_subprogram && actual_name != NULL)
9392 set_objfile_main_name (objfile, actual_name, cu->language);
9394 case DW_TAG_constant:
9396 std::vector<partial_symbol *> *list;
9398 if (pdi->is_external)
9399 list = &objfile->global_psymbols;
9401 list = &objfile->static_psymbols;
9402 add_psymbol_to_list (actual_name, strlen (actual_name),
9403 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9404 list, 0, cu->language, objfile);
9407 case DW_TAG_variable:
9409 addr = decode_locdesc (pdi->d.locdesc, cu);
9413 && !dwarf2_per_objfile->has_section_at_zero)
9415 /* A global or static variable may also have been stripped
9416 out by the linker if unused, in which case its address
9417 will be nullified; do not add such variables into partial
9418 symbol table then. */
9420 else if (pdi->is_external)
9423 Don't enter into the minimal symbol tables as there is
9424 a minimal symbol table entry from the ELF symbols already.
9425 Enter into partial symbol table if it has a location
9426 descriptor or a type.
9427 If the location descriptor is missing, new_symbol will create
9428 a LOC_UNRESOLVED symbol, the address of the variable will then
9429 be determined from the minimal symbol table whenever the variable
9431 The address for the partial symbol table entry is not
9432 used by GDB, but it comes in handy for debugging partial symbol
9435 if (pdi->d.locdesc || pdi->has_type)
9436 add_psymbol_to_list (actual_name, strlen (actual_name),
9437 built_actual_name != NULL,
9438 VAR_DOMAIN, LOC_STATIC,
9439 &objfile->global_psymbols,
9441 cu->language, objfile);
9445 int has_loc = pdi->d.locdesc != NULL;
9447 /* Static Variable. Skip symbols whose value we cannot know (those
9448 without location descriptors or constant values). */
9449 if (!has_loc && !pdi->has_const_value)
9451 xfree (built_actual_name);
9455 add_psymbol_to_list (actual_name, strlen (actual_name),
9456 built_actual_name != NULL,
9457 VAR_DOMAIN, LOC_STATIC,
9458 &objfile->static_psymbols,
9459 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
9460 cu->language, objfile);
9463 case DW_TAG_typedef:
9464 case DW_TAG_base_type:
9465 case DW_TAG_subrange_type:
9466 add_psymbol_to_list (actual_name, strlen (actual_name),
9467 built_actual_name != NULL,
9468 VAR_DOMAIN, LOC_TYPEDEF,
9469 &objfile->static_psymbols,
9470 0, cu->language, objfile);
9472 case DW_TAG_imported_declaration:
9473 case DW_TAG_namespace:
9474 add_psymbol_to_list (actual_name, strlen (actual_name),
9475 built_actual_name != NULL,
9476 VAR_DOMAIN, LOC_TYPEDEF,
9477 &objfile->global_psymbols,
9478 0, cu->language, objfile);
9481 add_psymbol_to_list (actual_name, strlen (actual_name),
9482 built_actual_name != NULL,
9483 MODULE_DOMAIN, LOC_TYPEDEF,
9484 &objfile->global_psymbols,
9485 0, cu->language, objfile);
9487 case DW_TAG_class_type:
9488 case DW_TAG_interface_type:
9489 case DW_TAG_structure_type:
9490 case DW_TAG_union_type:
9491 case DW_TAG_enumeration_type:
9492 /* Skip external references. The DWARF standard says in the section
9493 about "Structure, Union, and Class Type Entries": "An incomplete
9494 structure, union or class type is represented by a structure,
9495 union or class entry that does not have a byte size attribute
9496 and that has a DW_AT_declaration attribute." */
9497 if (!pdi->has_byte_size && pdi->is_declaration)
9499 xfree (built_actual_name);
9503 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9504 static vs. global. */
9505 add_psymbol_to_list (actual_name, strlen (actual_name),
9506 built_actual_name != NULL,
9507 STRUCT_DOMAIN, LOC_TYPEDEF,
9508 cu->language == language_cplus
9509 ? &objfile->global_psymbols
9510 : &objfile->static_psymbols,
9511 0, cu->language, objfile);
9514 case DW_TAG_enumerator:
9515 add_psymbol_to_list (actual_name, strlen (actual_name),
9516 built_actual_name != NULL,
9517 VAR_DOMAIN, LOC_CONST,
9518 cu->language == language_cplus
9519 ? &objfile->global_psymbols
9520 : &objfile->static_psymbols,
9521 0, cu->language, objfile);
9527 xfree (built_actual_name);
9530 /* Read a partial die corresponding to a namespace; also, add a symbol
9531 corresponding to that namespace to the symbol table. NAMESPACE is
9532 the name of the enclosing namespace. */
9535 add_partial_namespace (struct partial_die_info *pdi,
9536 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9537 int set_addrmap, struct dwarf2_cu *cu)
9539 /* Add a symbol for the namespace. */
9541 add_partial_symbol (pdi, cu);
9543 /* Now scan partial symbols in that namespace. */
9545 if (pdi->has_children)
9546 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9549 /* Read a partial die corresponding to a Fortran module. */
9552 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9553 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9555 /* Add a symbol for the namespace. */
9557 add_partial_symbol (pdi, cu);
9559 /* Now scan partial symbols in that module. */
9561 if (pdi->has_children)
9562 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9565 /* Read a partial die corresponding to a subprogram or an inlined
9566 subprogram and create a partial symbol for that subprogram.
9567 When the CU language allows it, this routine also defines a partial
9568 symbol for each nested subprogram that this subprogram contains.
9569 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9570 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9572 PDI may also be a lexical block, in which case we simply search
9573 recursively for subprograms defined inside that lexical block.
9574 Again, this is only performed when the CU language allows this
9575 type of definitions. */
9578 add_partial_subprogram (struct partial_die_info *pdi,
9579 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9580 int set_addrmap, struct dwarf2_cu *cu)
9582 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9584 if (pdi->has_pc_info)
9586 if (pdi->lowpc < *lowpc)
9587 *lowpc = pdi->lowpc;
9588 if (pdi->highpc > *highpc)
9589 *highpc = pdi->highpc;
9592 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9593 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9598 baseaddr = ANOFFSET (objfile->section_offsets,
9599 SECT_OFF_TEXT (objfile));
9600 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9601 pdi->lowpc + baseaddr);
9602 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9603 pdi->highpc + baseaddr);
9604 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9605 cu->per_cu->v.psymtab);
9609 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9611 if (!pdi->is_declaration)
9612 /* Ignore subprogram DIEs that do not have a name, they are
9613 illegal. Do not emit a complaint at this point, we will
9614 do so when we convert this psymtab into a symtab. */
9616 add_partial_symbol (pdi, cu);
9620 if (! pdi->has_children)
9623 if (cu->language == language_ada)
9625 pdi = pdi->die_child;
9629 if (pdi->tag == DW_TAG_subprogram
9630 || pdi->tag == DW_TAG_inlined_subroutine
9631 || pdi->tag == DW_TAG_lexical_block)
9632 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9633 pdi = pdi->die_sibling;
9638 /* Read a partial die corresponding to an enumeration type. */
9641 add_partial_enumeration (struct partial_die_info *enum_pdi,
9642 struct dwarf2_cu *cu)
9644 struct partial_die_info *pdi;
9646 if (enum_pdi->name != NULL)
9647 add_partial_symbol (enum_pdi, cu);
9649 pdi = enum_pdi->die_child;
9652 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9653 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
9655 add_partial_symbol (pdi, cu);
9656 pdi = pdi->die_sibling;
9660 /* Return the initial uleb128 in the die at INFO_PTR. */
9663 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9665 unsigned int bytes_read;
9667 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9670 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9671 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9673 Return the corresponding abbrev, or NULL if the number is zero (indicating
9674 an empty DIE). In either case *BYTES_READ will be set to the length of
9675 the initial number. */
9677 static struct abbrev_info *
9678 peek_die_abbrev (const die_reader_specs &reader,
9679 const gdb_byte *info_ptr, unsigned int *bytes_read)
9681 dwarf2_cu *cu = reader.cu;
9682 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9683 unsigned int abbrev_number
9684 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9686 if (abbrev_number == 0)
9689 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9692 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9693 " at offset %s [in module %s]"),
9694 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9695 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9701 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9702 Returns a pointer to the end of a series of DIEs, terminated by an empty
9703 DIE. Any children of the skipped DIEs will also be skipped. */
9705 static const gdb_byte *
9706 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9710 unsigned int bytes_read;
9711 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9714 return info_ptr + bytes_read;
9716 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9720 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9721 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9722 abbrev corresponding to that skipped uleb128 should be passed in
9723 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9726 static const gdb_byte *
9727 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9728 struct abbrev_info *abbrev)
9730 unsigned int bytes_read;
9731 struct attribute attr;
9732 bfd *abfd = reader->abfd;
9733 struct dwarf2_cu *cu = reader->cu;
9734 const gdb_byte *buffer = reader->buffer;
9735 const gdb_byte *buffer_end = reader->buffer_end;
9736 unsigned int form, i;
9738 for (i = 0; i < abbrev->num_attrs; i++)
9740 /* The only abbrev we care about is DW_AT_sibling. */
9741 if (abbrev->attrs[i].name == DW_AT_sibling)
9743 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9744 if (attr.form == DW_FORM_ref_addr)
9745 complaint (&symfile_complaints,
9746 _("ignoring absolute DW_AT_sibling"));
9749 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9750 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9752 if (sibling_ptr < info_ptr)
9753 complaint (&symfile_complaints,
9754 _("DW_AT_sibling points backwards"));
9755 else if (sibling_ptr > reader->buffer_end)
9756 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9762 /* If it isn't DW_AT_sibling, skip this attribute. */
9763 form = abbrev->attrs[i].form;
9767 case DW_FORM_ref_addr:
9768 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9769 and later it is offset sized. */
9770 if (cu->header.version == 2)
9771 info_ptr += cu->header.addr_size;
9773 info_ptr += cu->header.offset_size;
9775 case DW_FORM_GNU_ref_alt:
9776 info_ptr += cu->header.offset_size;
9779 info_ptr += cu->header.addr_size;
9786 case DW_FORM_flag_present:
9787 case DW_FORM_implicit_const:
9799 case DW_FORM_ref_sig8:
9802 case DW_FORM_data16:
9805 case DW_FORM_string:
9806 read_direct_string (abfd, info_ptr, &bytes_read);
9807 info_ptr += bytes_read;
9809 case DW_FORM_sec_offset:
9811 case DW_FORM_GNU_strp_alt:
9812 info_ptr += cu->header.offset_size;
9814 case DW_FORM_exprloc:
9816 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9817 info_ptr += bytes_read;
9819 case DW_FORM_block1:
9820 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9822 case DW_FORM_block2:
9823 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9825 case DW_FORM_block4:
9826 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9830 case DW_FORM_ref_udata:
9831 case DW_FORM_GNU_addr_index:
9832 case DW_FORM_GNU_str_index:
9833 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9835 case DW_FORM_indirect:
9836 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9837 info_ptr += bytes_read;
9838 /* We need to continue parsing from here, so just go back to
9840 goto skip_attribute;
9843 error (_("Dwarf Error: Cannot handle %s "
9844 "in DWARF reader [in module %s]"),
9845 dwarf_form_name (form),
9846 bfd_get_filename (abfd));
9850 if (abbrev->has_children)
9851 return skip_children (reader, info_ptr);
9856 /* Locate ORIG_PDI's sibling.
9857 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9859 static const gdb_byte *
9860 locate_pdi_sibling (const struct die_reader_specs *reader,
9861 struct partial_die_info *orig_pdi,
9862 const gdb_byte *info_ptr)
9864 /* Do we know the sibling already? */
9866 if (orig_pdi->sibling)
9867 return orig_pdi->sibling;
9869 /* Are there any children to deal with? */
9871 if (!orig_pdi->has_children)
9874 /* Skip the children the long way. */
9876 return skip_children (reader, info_ptr);
9879 /* Expand this partial symbol table into a full symbol table. SELF is
9883 dwarf2_read_symtab (struct partial_symtab *self,
9884 struct objfile *objfile)
9886 struct dwarf2_per_objfile *dwarf2_per_objfile
9887 = get_dwarf2_per_objfile (objfile);
9891 warning (_("bug: psymtab for %s is already read in."),
9898 printf_filtered (_("Reading in symbols for %s..."),
9900 gdb_flush (gdb_stdout);
9903 /* If this psymtab is constructed from a debug-only objfile, the
9904 has_section_at_zero flag will not necessarily be correct. We
9905 can get the correct value for this flag by looking at the data
9906 associated with the (presumably stripped) associated objfile. */
9907 if (objfile->separate_debug_objfile_backlink)
9909 struct dwarf2_per_objfile *dpo_backlink
9910 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9912 dwarf2_per_objfile->has_section_at_zero
9913 = dpo_backlink->has_section_at_zero;
9916 dwarf2_per_objfile->reading_partial_symbols = 0;
9918 psymtab_to_symtab_1 (self);
9920 /* Finish up the debug error message. */
9922 printf_filtered (_("done.\n"));
9925 process_cu_includes (dwarf2_per_objfile);
9928 /* Reading in full CUs. */
9930 /* Add PER_CU to the queue. */
9933 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9934 enum language pretend_language)
9936 struct dwarf2_queue_item *item;
9939 item = XNEW (struct dwarf2_queue_item);
9940 item->per_cu = per_cu;
9941 item->pretend_language = pretend_language;
9944 if (dwarf2_queue == NULL)
9945 dwarf2_queue = item;
9947 dwarf2_queue_tail->next = item;
9949 dwarf2_queue_tail = item;
9952 /* If PER_CU is not yet queued, add it to the queue.
9953 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9955 The result is non-zero if PER_CU was queued, otherwise the result is zero
9956 meaning either PER_CU is already queued or it is already loaded.
9958 N.B. There is an invariant here that if a CU is queued then it is loaded.
9959 The caller is required to load PER_CU if we return non-zero. */
9962 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9963 struct dwarf2_per_cu_data *per_cu,
9964 enum language pretend_language)
9966 /* We may arrive here during partial symbol reading, if we need full
9967 DIEs to process an unusual case (e.g. template arguments). Do
9968 not queue PER_CU, just tell our caller to load its DIEs. */
9969 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9971 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9976 /* Mark the dependence relation so that we don't flush PER_CU
9978 if (dependent_cu != NULL)
9979 dwarf2_add_dependence (dependent_cu, per_cu);
9981 /* If it's already on the queue, we have nothing to do. */
9985 /* If the compilation unit is already loaded, just mark it as
9987 if (per_cu->cu != NULL)
9989 per_cu->cu->last_used = 0;
9993 /* Add it to the queue. */
9994 queue_comp_unit (per_cu, pretend_language);
9999 /* Process the queue. */
10002 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10004 struct dwarf2_queue_item *item, *next_item;
10006 if (dwarf_read_debug)
10008 fprintf_unfiltered (gdb_stdlog,
10009 "Expanding one or more symtabs of objfile %s ...\n",
10010 objfile_name (dwarf2_per_objfile->objfile));
10013 /* The queue starts out with one item, but following a DIE reference
10014 may load a new CU, adding it to the end of the queue. */
10015 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
10017 if ((dwarf2_per_objfile->using_index
10018 ? !item->per_cu->v.quick->compunit_symtab
10019 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10020 /* Skip dummy CUs. */
10021 && item->per_cu->cu != NULL)
10023 struct dwarf2_per_cu_data *per_cu = item->per_cu;
10024 unsigned int debug_print_threshold;
10027 if (per_cu->is_debug_types)
10029 struct signatured_type *sig_type =
10030 (struct signatured_type *) per_cu;
10032 sprintf (buf, "TU %s at offset %s",
10033 hex_string (sig_type->signature),
10034 sect_offset_str (per_cu->sect_off));
10035 /* There can be 100s of TUs.
10036 Only print them in verbose mode. */
10037 debug_print_threshold = 2;
10041 sprintf (buf, "CU at offset %s",
10042 sect_offset_str (per_cu->sect_off));
10043 debug_print_threshold = 1;
10046 if (dwarf_read_debug >= debug_print_threshold)
10047 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
10049 if (per_cu->is_debug_types)
10050 process_full_type_unit (per_cu, item->pretend_language);
10052 process_full_comp_unit (per_cu, item->pretend_language);
10054 if (dwarf_read_debug >= debug_print_threshold)
10055 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
10058 item->per_cu->queued = 0;
10059 next_item = item->next;
10063 dwarf2_queue_tail = NULL;
10065 if (dwarf_read_debug)
10067 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
10068 objfile_name (dwarf2_per_objfile->objfile));
10072 /* Read in full symbols for PST, and anything it depends on. */
10075 psymtab_to_symtab_1 (struct partial_symtab *pst)
10077 struct dwarf2_per_cu_data *per_cu;
10083 for (i = 0; i < pst->number_of_dependencies; i++)
10084 if (!pst->dependencies[i]->readin
10085 && pst->dependencies[i]->user == NULL)
10087 /* Inform about additional files that need to be read in. */
10090 /* FIXME: i18n: Need to make this a single string. */
10091 fputs_filtered (" ", gdb_stdout);
10093 fputs_filtered ("and ", gdb_stdout);
10095 printf_filtered ("%s...", pst->dependencies[i]->filename);
10096 wrap_here (""); /* Flush output. */
10097 gdb_flush (gdb_stdout);
10099 psymtab_to_symtab_1 (pst->dependencies[i]);
10102 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10104 if (per_cu == NULL)
10106 /* It's an include file, no symbols to read for it.
10107 Everything is in the parent symtab. */
10112 dw2_do_instantiate_symtab (per_cu);
10115 /* Trivial hash function for die_info: the hash value of a DIE
10116 is its offset in .debug_info for this objfile. */
10119 die_hash (const void *item)
10121 const struct die_info *die = (const struct die_info *) item;
10123 return to_underlying (die->sect_off);
10126 /* Trivial comparison function for die_info structures: two DIEs
10127 are equal if they have the same offset. */
10130 die_eq (const void *item_lhs, const void *item_rhs)
10132 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
10133 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
10135 return die_lhs->sect_off == die_rhs->sect_off;
10138 /* die_reader_func for load_full_comp_unit.
10139 This is identical to read_signatured_type_reader,
10140 but is kept separate for now. */
10143 load_full_comp_unit_reader (const struct die_reader_specs *reader,
10144 const gdb_byte *info_ptr,
10145 struct die_info *comp_unit_die,
10149 struct dwarf2_cu *cu = reader->cu;
10150 enum language *language_ptr = (enum language *) data;
10152 gdb_assert (cu->die_hash == NULL);
10154 htab_create_alloc_ex (cu->header.length / 12,
10158 &cu->comp_unit_obstack,
10159 hashtab_obstack_allocate,
10160 dummy_obstack_deallocate);
10163 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
10164 &info_ptr, comp_unit_die);
10165 cu->dies = comp_unit_die;
10166 /* comp_unit_die is not stored in die_hash, no need. */
10168 /* We try not to read any attributes in this function, because not
10169 all CUs needed for references have been loaded yet, and symbol
10170 table processing isn't initialized. But we have to set the CU language,
10171 or we won't be able to build types correctly.
10172 Similarly, if we do not read the producer, we can not apply
10173 producer-specific interpretation. */
10174 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
10177 /* Load the DIEs associated with PER_CU into memory. */
10180 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
10181 enum language pretend_language)
10183 gdb_assert (! this_cu->is_debug_types);
10185 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
10186 load_full_comp_unit_reader, &pretend_language);
10189 /* Add a DIE to the delayed physname list. */
10192 add_to_method_list (struct type *type, int fnfield_index, int index,
10193 const char *name, struct die_info *die,
10194 struct dwarf2_cu *cu)
10196 struct delayed_method_info mi;
10198 mi.fnfield_index = fnfield_index;
10202 cu->method_list.push_back (mi);
10205 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
10206 "const" / "volatile". If so, decrements LEN by the length of the
10207 modifier and return true. Otherwise return false. */
10211 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
10213 size_t mod_len = sizeof (mod) - 1;
10214 if (len > mod_len && startswith (physname + (len - mod_len), mod))
10222 /* Compute the physnames of any methods on the CU's method list.
10224 The computation of method physnames is delayed in order to avoid the
10225 (bad) condition that one of the method's formal parameters is of an as yet
10226 incomplete type. */
10229 compute_delayed_physnames (struct dwarf2_cu *cu)
10231 /* Only C++ delays computing physnames. */
10232 if (cu->method_list.empty ())
10234 gdb_assert (cu->language == language_cplus);
10236 for (struct delayed_method_info &mi : cu->method_list)
10238 const char *physname;
10239 struct fn_fieldlist *fn_flp
10240 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
10241 physname = dwarf2_physname (mi.name, mi.die, cu);
10242 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
10243 = physname ? physname : "";
10245 /* Since there's no tag to indicate whether a method is a
10246 const/volatile overload, extract that information out of the
10248 if (physname != NULL)
10250 size_t len = strlen (physname);
10254 if (physname[len] == ')') /* shortcut */
10256 else if (check_modifier (physname, len, " const"))
10257 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
10258 else if (check_modifier (physname, len, " volatile"))
10259 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
10266 /* The list is no longer needed. */
10267 cu->method_list.clear ();
10270 /* Go objects should be embedded in a DW_TAG_module DIE,
10271 and it's not clear if/how imported objects will appear.
10272 To keep Go support simple until that's worked out,
10273 go back through what we've read and create something usable.
10274 We could do this while processing each DIE, and feels kinda cleaner,
10275 but that way is more invasive.
10276 This is to, for example, allow the user to type "p var" or "b main"
10277 without having to specify the package name, and allow lookups
10278 of module.object to work in contexts that use the expression
10282 fixup_go_packaging (struct dwarf2_cu *cu)
10284 char *package_name = NULL;
10285 struct pending *list;
10288 for (list = global_symbols; list != NULL; list = list->next)
10290 for (i = 0; i < list->nsyms; ++i)
10292 struct symbol *sym = list->symbol[i];
10294 if (SYMBOL_LANGUAGE (sym) == language_go
10295 && SYMBOL_CLASS (sym) == LOC_BLOCK)
10297 char *this_package_name = go_symbol_package_name (sym);
10299 if (this_package_name == NULL)
10301 if (package_name == NULL)
10302 package_name = this_package_name;
10305 struct objfile *objfile
10306 = cu->per_cu->dwarf2_per_objfile->objfile;
10307 if (strcmp (package_name, this_package_name) != 0)
10308 complaint (&symfile_complaints,
10309 _("Symtab %s has objects from two different Go packages: %s and %s"),
10310 (symbol_symtab (sym) != NULL
10311 ? symtab_to_filename_for_display
10312 (symbol_symtab (sym))
10313 : objfile_name (objfile)),
10314 this_package_name, package_name);
10315 xfree (this_package_name);
10321 if (package_name != NULL)
10323 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10324 const char *saved_package_name
10325 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
10327 strlen (package_name));
10328 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
10329 saved_package_name);
10330 struct symbol *sym;
10332 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10334 sym = allocate_symbol (objfile);
10335 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
10336 SYMBOL_SET_NAMES (sym, saved_package_name,
10337 strlen (saved_package_name), 0, objfile);
10338 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
10339 e.g., "main" finds the "main" module and not C's main(). */
10340 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10341 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
10342 SYMBOL_TYPE (sym) = type;
10344 add_symbol_to_list (sym, &global_symbols);
10346 xfree (package_name);
10350 /* Allocate a fully-qualified name consisting of the two parts on the
10353 static const char *
10354 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
10356 return obconcat (obstack, p1, "::", p2, (char *) NULL);
10359 /* A helper that allocates a struct discriminant_info to attach to a
10362 static struct discriminant_info *
10363 alloc_discriminant_info (struct type *type, int discriminant_index,
10366 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10367 gdb_assert (discriminant_index == -1
10368 || (discriminant_index >= 0
10369 && discriminant_index < TYPE_NFIELDS (type)));
10370 gdb_assert (default_index == -1
10371 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
10373 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
10375 struct discriminant_info *disc
10376 = ((struct discriminant_info *)
10378 offsetof (struct discriminant_info, discriminants)
10379 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
10380 disc->default_index = default_index;
10381 disc->discriminant_index = discriminant_index;
10383 struct dynamic_prop prop;
10384 prop.kind = PROP_UNDEFINED;
10385 prop.data.baton = disc;
10387 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
10392 /* Some versions of rustc emitted enums in an unusual way.
10394 Ordinary enums were emitted as unions. The first element of each
10395 structure in the union was named "RUST$ENUM$DISR". This element
10396 held the discriminant.
10398 These versions of Rust also implemented the "non-zero"
10399 optimization. When the enum had two values, and one is empty and
10400 the other holds a pointer that cannot be zero, the pointer is used
10401 as the discriminant, with a zero value meaning the empty variant.
10402 Here, the union's first member is of the form
10403 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
10404 where the fieldnos are the indices of the fields that should be
10405 traversed in order to find the field (which may be several fields deep)
10406 and the variantname is the name of the variant of the case when the
10409 This function recognizes whether TYPE is of one of these forms,
10410 and, if so, smashes it to be a variant type. */
10413 quirk_rust_enum (struct type *type, struct objfile *objfile)
10415 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10417 /* We don't need to deal with empty enums. */
10418 if (TYPE_NFIELDS (type) == 0)
10421 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
10422 if (TYPE_NFIELDS (type) == 1
10423 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10425 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10427 /* Decode the field name to find the offset of the
10429 ULONGEST bit_offset = 0;
10430 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10431 while (name[0] >= '0' && name[0] <= '9')
10434 unsigned long index = strtoul (name, &tail, 10);
10437 || index >= TYPE_NFIELDS (field_type)
10438 || (TYPE_FIELD_LOC_KIND (field_type, index)
10439 != FIELD_LOC_KIND_BITPOS))
10441 complaint (&symfile_complaints,
10442 _("Could not parse Rust enum encoding string \"%s\""
10444 TYPE_FIELD_NAME (type, 0),
10445 objfile_name (objfile));
10450 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10451 field_type = TYPE_FIELD_TYPE (field_type, index);
10454 /* Make a union to hold the variants. */
10455 struct type *union_type = alloc_type (objfile);
10456 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10457 TYPE_NFIELDS (union_type) = 3;
10458 TYPE_FIELDS (union_type)
10459 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10460 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10462 /* Put the discriminant must at index 0. */
10463 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10464 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10465 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10466 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10468 /* The order of fields doesn't really matter, so put the real
10469 field at index 1 and the data-less field at index 2. */
10470 struct discriminant_info *disc
10471 = alloc_discriminant_info (union_type, 0, 1);
10472 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10473 TYPE_FIELD_NAME (union_type, 1)
10474 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10475 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10476 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10477 TYPE_FIELD_NAME (union_type, 1));
10479 const char *dataless_name
10480 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10482 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10484 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10485 /* NAME points into the original discriminant name, which
10486 already has the correct lifetime. */
10487 TYPE_FIELD_NAME (union_type, 2) = name;
10488 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10489 disc->discriminants[2] = 0;
10491 /* Smash this type to be a structure type. We have to do this
10492 because the type has already been recorded. */
10493 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10494 TYPE_NFIELDS (type) = 1;
10496 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10498 /* Install the variant part. */
10499 TYPE_FIELD_TYPE (type, 0) = union_type;
10500 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10501 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10503 else if (TYPE_NFIELDS (type) == 1)
10505 /* We assume that a union with a single field is a univariant
10507 /* Smash this type to be a structure type. We have to do this
10508 because the type has already been recorded. */
10509 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10511 /* Make a union to hold the variants. */
10512 struct type *union_type = alloc_type (objfile);
10513 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10514 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10515 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10516 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10518 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10519 const char *variant_name
10520 = rust_last_path_segment (TYPE_NAME (field_type));
10521 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10522 TYPE_NAME (field_type)
10523 = rust_fully_qualify (&objfile->objfile_obstack,
10524 TYPE_NAME (type), variant_name);
10526 /* Install the union in the outer struct type. */
10527 TYPE_NFIELDS (type) = 1;
10529 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10530 TYPE_FIELD_TYPE (type, 0) = union_type;
10531 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10532 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10534 alloc_discriminant_info (union_type, -1, 0);
10538 struct type *disr_type = nullptr;
10539 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10541 disr_type = TYPE_FIELD_TYPE (type, i);
10543 if (TYPE_NFIELDS (disr_type) == 0)
10545 /* Could be data-less variant, so keep going. */
10547 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10548 "RUST$ENUM$DISR") != 0)
10550 /* Not a Rust enum. */
10560 /* If we got here without a discriminant, then it's probably
10562 if (disr_type == nullptr)
10565 /* Smash this type to be a structure type. We have to do this
10566 because the type has already been recorded. */
10567 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10569 /* Make a union to hold the variants. */
10570 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10571 struct type *union_type = alloc_type (objfile);
10572 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10573 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10574 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10575 TYPE_FIELDS (union_type)
10576 = (struct field *) TYPE_ZALLOC (union_type,
10577 (TYPE_NFIELDS (union_type)
10578 * sizeof (struct field)));
10580 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10581 TYPE_NFIELDS (type) * sizeof (struct field));
10583 /* Install the discriminant at index 0 in the union. */
10584 TYPE_FIELD (union_type, 0) = *disr_field;
10585 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10586 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10588 /* Install the union in the outer struct type. */
10589 TYPE_FIELD_TYPE (type, 0) = union_type;
10590 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10591 TYPE_NFIELDS (type) = 1;
10593 /* Set the size and offset of the union type. */
10594 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10596 /* We need a way to find the correct discriminant given a
10597 variant name. For convenience we build a map here. */
10598 struct type *enum_type = FIELD_TYPE (*disr_field);
10599 std::unordered_map<std::string, ULONGEST> discriminant_map;
10600 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10602 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10605 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10606 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10610 int n_fields = TYPE_NFIELDS (union_type);
10611 struct discriminant_info *disc
10612 = alloc_discriminant_info (union_type, 0, -1);
10613 /* Skip the discriminant here. */
10614 for (int i = 1; i < n_fields; ++i)
10616 /* Find the final word in the name of this variant's type.
10617 That name can be used to look up the correct
10619 const char *variant_name
10620 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10623 auto iter = discriminant_map.find (variant_name);
10624 if (iter != discriminant_map.end ())
10625 disc->discriminants[i] = iter->second;
10627 /* Remove the discriminant field. */
10628 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10629 --TYPE_NFIELDS (sub_type);
10630 ++TYPE_FIELDS (sub_type);
10631 TYPE_FIELD_NAME (union_type, i) = variant_name;
10632 TYPE_NAME (sub_type)
10633 = rust_fully_qualify (&objfile->objfile_obstack,
10634 TYPE_NAME (type), variant_name);
10639 /* Rewrite some Rust unions to be structures with variants parts. */
10642 rust_union_quirks (struct dwarf2_cu *cu)
10644 gdb_assert (cu->language == language_rust);
10645 for (struct type *type : cu->rust_unions)
10646 quirk_rust_enum (type, cu->per_cu->dwarf2_per_objfile->objfile);
10649 /* Return the symtab for PER_CU. This works properly regardless of
10650 whether we're using the index or psymtabs. */
10652 static struct compunit_symtab *
10653 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10655 return (per_cu->dwarf2_per_objfile->using_index
10656 ? per_cu->v.quick->compunit_symtab
10657 : per_cu->v.psymtab->compunit_symtab);
10660 /* A helper function for computing the list of all symbol tables
10661 included by PER_CU. */
10664 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
10665 htab_t all_children, htab_t all_type_symtabs,
10666 struct dwarf2_per_cu_data *per_cu,
10667 struct compunit_symtab *immediate_parent)
10671 struct compunit_symtab *cust;
10672 struct dwarf2_per_cu_data *iter;
10674 slot = htab_find_slot (all_children, per_cu, INSERT);
10677 /* This inclusion and its children have been processed. */
10682 /* Only add a CU if it has a symbol table. */
10683 cust = get_compunit_symtab (per_cu);
10686 /* If this is a type unit only add its symbol table if we haven't
10687 seen it yet (type unit per_cu's can share symtabs). */
10688 if (per_cu->is_debug_types)
10690 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10694 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10695 if (cust->user == NULL)
10696 cust->user = immediate_parent;
10701 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10702 if (cust->user == NULL)
10703 cust->user = immediate_parent;
10708 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10711 recursively_compute_inclusions (result, all_children,
10712 all_type_symtabs, iter, cust);
10716 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10720 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10722 gdb_assert (! per_cu->is_debug_types);
10724 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10727 struct dwarf2_per_cu_data *per_cu_iter;
10728 struct compunit_symtab *compunit_symtab_iter;
10729 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
10730 htab_t all_children, all_type_symtabs;
10731 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10733 /* If we don't have a symtab, we can just skip this case. */
10737 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10738 NULL, xcalloc, xfree);
10739 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10740 NULL, xcalloc, xfree);
10743 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10747 recursively_compute_inclusions (&result_symtabs, all_children,
10748 all_type_symtabs, per_cu_iter,
10752 /* Now we have a transitive closure of all the included symtabs. */
10753 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10755 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10756 struct compunit_symtab *, len + 1);
10758 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10759 compunit_symtab_iter);
10761 cust->includes[ix] = compunit_symtab_iter;
10762 cust->includes[len] = NULL;
10764 VEC_free (compunit_symtab_ptr, result_symtabs);
10765 htab_delete (all_children);
10766 htab_delete (all_type_symtabs);
10770 /* Compute the 'includes' field for the symtabs of all the CUs we just
10774 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10777 struct dwarf2_per_cu_data *iter;
10780 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10784 if (! iter->is_debug_types)
10785 compute_compunit_symtab_includes (iter);
10788 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10791 /* Generate full symbol information for PER_CU, whose DIEs have
10792 already been loaded into memory. */
10795 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10796 enum language pretend_language)
10798 struct dwarf2_cu *cu = per_cu->cu;
10799 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10800 struct objfile *objfile = dwarf2_per_objfile->objfile;
10801 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10802 CORE_ADDR lowpc, highpc;
10803 struct compunit_symtab *cust;
10804 CORE_ADDR baseaddr;
10805 struct block *static_block;
10808 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10811 scoped_free_pendings free_pending;
10813 /* Clear the list here in case something was left over. */
10814 cu->method_list.clear ();
10816 cu->list_in_scope = &file_symbols;
10818 cu->language = pretend_language;
10819 cu->language_defn = language_def (cu->language);
10821 /* Do line number decoding in read_file_scope () */
10822 process_die (cu->dies, cu);
10824 /* For now fudge the Go package. */
10825 if (cu->language == language_go)
10826 fixup_go_packaging (cu);
10828 /* Now that we have processed all the DIEs in the CU, all the types
10829 should be complete, and it should now be safe to compute all of the
10831 compute_delayed_physnames (cu);
10833 if (cu->language == language_rust)
10834 rust_union_quirks (cu);
10836 /* Some compilers don't define a DW_AT_high_pc attribute for the
10837 compilation unit. If the DW_AT_high_pc is missing, synthesize
10838 it, by scanning the DIE's below the compilation unit. */
10839 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10841 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10842 static_block = end_symtab_get_static_block (addr, 0, 1);
10844 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10845 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10846 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10847 addrmap to help ensure it has an accurate map of pc values belonging to
10849 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10851 cust = end_symtab_from_static_block (static_block,
10852 SECT_OFF_TEXT (objfile), 0);
10856 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10858 /* Set symtab language to language from DW_AT_language. If the
10859 compilation is from a C file generated by language preprocessors, do
10860 not set the language if it was already deduced by start_subfile. */
10861 if (!(cu->language == language_c
10862 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10863 COMPUNIT_FILETABS (cust)->language = cu->language;
10865 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10866 produce DW_AT_location with location lists but it can be possibly
10867 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10868 there were bugs in prologue debug info, fixed later in GCC-4.5
10869 by "unwind info for epilogues" patch (which is not directly related).
10871 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10872 needed, it would be wrong due to missing DW_AT_producer there.
10874 Still one can confuse GDB by using non-standard GCC compilation
10875 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10877 if (cu->has_loclist && gcc_4_minor >= 5)
10878 cust->locations_valid = 1;
10880 if (gcc_4_minor >= 5)
10881 cust->epilogue_unwind_valid = 1;
10883 cust->call_site_htab = cu->call_site_htab;
10886 if (dwarf2_per_objfile->using_index)
10887 per_cu->v.quick->compunit_symtab = cust;
10890 struct partial_symtab *pst = per_cu->v.psymtab;
10891 pst->compunit_symtab = cust;
10895 /* Push it for inclusion processing later. */
10896 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
10899 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10900 already been loaded into memory. */
10903 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10904 enum language pretend_language)
10906 struct dwarf2_cu *cu = per_cu->cu;
10907 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10908 struct objfile *objfile = dwarf2_per_objfile->objfile;
10909 struct compunit_symtab *cust;
10910 struct signatured_type *sig_type;
10912 gdb_assert (per_cu->is_debug_types);
10913 sig_type = (struct signatured_type *) per_cu;
10916 scoped_free_pendings free_pending;
10918 /* Clear the list here in case something was left over. */
10919 cu->method_list.clear ();
10921 cu->list_in_scope = &file_symbols;
10923 cu->language = pretend_language;
10924 cu->language_defn = language_def (cu->language);
10926 /* The symbol tables are set up in read_type_unit_scope. */
10927 process_die (cu->dies, cu);
10929 /* For now fudge the Go package. */
10930 if (cu->language == language_go)
10931 fixup_go_packaging (cu);
10933 /* Now that we have processed all the DIEs in the CU, all the types
10934 should be complete, and it should now be safe to compute all of the
10936 compute_delayed_physnames (cu);
10938 if (cu->language == language_rust)
10939 rust_union_quirks (cu);
10941 /* TUs share symbol tables.
10942 If this is the first TU to use this symtab, complete the construction
10943 of it with end_expandable_symtab. Otherwise, complete the addition of
10944 this TU's symbols to the existing symtab. */
10945 if (sig_type->type_unit_group->compunit_symtab == NULL)
10947 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10948 sig_type->type_unit_group->compunit_symtab = cust;
10952 /* Set symtab language to language from DW_AT_language. If the
10953 compilation is from a C file generated by language preprocessors,
10954 do not set the language if it was already deduced by
10956 if (!(cu->language == language_c
10957 && COMPUNIT_FILETABS (cust)->language != language_c))
10958 COMPUNIT_FILETABS (cust)->language = cu->language;
10963 augment_type_symtab ();
10964 cust = sig_type->type_unit_group->compunit_symtab;
10967 if (dwarf2_per_objfile->using_index)
10968 per_cu->v.quick->compunit_symtab = cust;
10971 struct partial_symtab *pst = per_cu->v.psymtab;
10972 pst->compunit_symtab = cust;
10977 /* Process an imported unit DIE. */
10980 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10982 struct attribute *attr;
10984 /* For now we don't handle imported units in type units. */
10985 if (cu->per_cu->is_debug_types)
10987 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10988 " supported in type units [in module %s]"),
10989 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10992 attr = dwarf2_attr (die, DW_AT_import, cu);
10995 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10996 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10997 dwarf2_per_cu_data *per_cu
10998 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10999 cu->per_cu->dwarf2_per_objfile);
11001 /* If necessary, add it to the queue and load its DIEs. */
11002 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
11003 load_full_comp_unit (per_cu, cu->language);
11005 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
11010 /* RAII object that represents a process_die scope: i.e.,
11011 starts/finishes processing a DIE. */
11012 class process_die_scope
11015 process_die_scope (die_info *die, dwarf2_cu *cu)
11016 : m_die (die), m_cu (cu)
11018 /* We should only be processing DIEs not already in process. */
11019 gdb_assert (!m_die->in_process);
11020 m_die->in_process = true;
11023 ~process_die_scope ()
11025 m_die->in_process = false;
11027 /* If we're done processing the DIE for the CU that owns the line
11028 header, we don't need the line header anymore. */
11029 if (m_cu->line_header_die_owner == m_die)
11031 delete m_cu->line_header;
11032 m_cu->line_header = NULL;
11033 m_cu->line_header_die_owner = NULL;
11042 /* Process a die and its children. */
11045 process_die (struct die_info *die, struct dwarf2_cu *cu)
11047 process_die_scope scope (die, cu);
11051 case DW_TAG_padding:
11053 case DW_TAG_compile_unit:
11054 case DW_TAG_partial_unit:
11055 read_file_scope (die, cu);
11057 case DW_TAG_type_unit:
11058 read_type_unit_scope (die, cu);
11060 case DW_TAG_subprogram:
11061 case DW_TAG_inlined_subroutine:
11062 read_func_scope (die, cu);
11064 case DW_TAG_lexical_block:
11065 case DW_TAG_try_block:
11066 case DW_TAG_catch_block:
11067 read_lexical_block_scope (die, cu);
11069 case DW_TAG_call_site:
11070 case DW_TAG_GNU_call_site:
11071 read_call_site_scope (die, cu);
11073 case DW_TAG_class_type:
11074 case DW_TAG_interface_type:
11075 case DW_TAG_structure_type:
11076 case DW_TAG_union_type:
11077 process_structure_scope (die, cu);
11079 case DW_TAG_enumeration_type:
11080 process_enumeration_scope (die, cu);
11083 /* These dies have a type, but processing them does not create
11084 a symbol or recurse to process the children. Therefore we can
11085 read them on-demand through read_type_die. */
11086 case DW_TAG_subroutine_type:
11087 case DW_TAG_set_type:
11088 case DW_TAG_array_type:
11089 case DW_TAG_pointer_type:
11090 case DW_TAG_ptr_to_member_type:
11091 case DW_TAG_reference_type:
11092 case DW_TAG_rvalue_reference_type:
11093 case DW_TAG_string_type:
11096 case DW_TAG_base_type:
11097 case DW_TAG_subrange_type:
11098 case DW_TAG_typedef:
11099 /* Add a typedef symbol for the type definition, if it has a
11101 new_symbol (die, read_type_die (die, cu), cu);
11103 case DW_TAG_common_block:
11104 read_common_block (die, cu);
11106 case DW_TAG_common_inclusion:
11108 case DW_TAG_namespace:
11109 cu->processing_has_namespace_info = 1;
11110 read_namespace (die, cu);
11112 case DW_TAG_module:
11113 cu->processing_has_namespace_info = 1;
11114 read_module (die, cu);
11116 case DW_TAG_imported_declaration:
11117 cu->processing_has_namespace_info = 1;
11118 if (read_namespace_alias (die, cu))
11120 /* The declaration is not a global namespace alias: fall through. */
11121 case DW_TAG_imported_module:
11122 cu->processing_has_namespace_info = 1;
11123 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
11124 || cu->language != language_fortran))
11125 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
11126 dwarf_tag_name (die->tag));
11127 read_import_statement (die, cu);
11130 case DW_TAG_imported_unit:
11131 process_imported_unit_die (die, cu);
11134 case DW_TAG_variable:
11135 read_variable (die, cu);
11139 new_symbol (die, NULL, cu);
11144 /* DWARF name computation. */
11146 /* A helper function for dwarf2_compute_name which determines whether DIE
11147 needs to have the name of the scope prepended to the name listed in the
11151 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
11153 struct attribute *attr;
11157 case DW_TAG_namespace:
11158 case DW_TAG_typedef:
11159 case DW_TAG_class_type:
11160 case DW_TAG_interface_type:
11161 case DW_TAG_structure_type:
11162 case DW_TAG_union_type:
11163 case DW_TAG_enumeration_type:
11164 case DW_TAG_enumerator:
11165 case DW_TAG_subprogram:
11166 case DW_TAG_inlined_subroutine:
11167 case DW_TAG_member:
11168 case DW_TAG_imported_declaration:
11171 case DW_TAG_variable:
11172 case DW_TAG_constant:
11173 /* We only need to prefix "globally" visible variables. These include
11174 any variable marked with DW_AT_external or any variable that
11175 lives in a namespace. [Variables in anonymous namespaces
11176 require prefixing, but they are not DW_AT_external.] */
11178 if (dwarf2_attr (die, DW_AT_specification, cu))
11180 struct dwarf2_cu *spec_cu = cu;
11182 return die_needs_namespace (die_specification (die, &spec_cu),
11186 attr = dwarf2_attr (die, DW_AT_external, cu);
11187 if (attr == NULL && die->parent->tag != DW_TAG_namespace
11188 && die->parent->tag != DW_TAG_module)
11190 /* A variable in a lexical block of some kind does not need a
11191 namespace, even though in C++ such variables may be external
11192 and have a mangled name. */
11193 if (die->parent->tag == DW_TAG_lexical_block
11194 || die->parent->tag == DW_TAG_try_block
11195 || die->parent->tag == DW_TAG_catch_block
11196 || die->parent->tag == DW_TAG_subprogram)
11205 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
11206 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
11207 defined for the given DIE. */
11209 static struct attribute *
11210 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
11212 struct attribute *attr;
11214 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
11216 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
11221 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
11222 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
11223 defined for the given DIE. */
11225 static const char *
11226 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
11228 const char *linkage_name;
11230 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
11231 if (linkage_name == NULL)
11232 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
11234 return linkage_name;
11237 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
11238 compute the physname for the object, which include a method's:
11239 - formal parameters (C++),
11240 - receiver type (Go),
11242 The term "physname" is a bit confusing.
11243 For C++, for example, it is the demangled name.
11244 For Go, for example, it's the mangled name.
11246 For Ada, return the DIE's linkage name rather than the fully qualified
11247 name. PHYSNAME is ignored..
11249 The result is allocated on the objfile_obstack and canonicalized. */
11251 static const char *
11252 dwarf2_compute_name (const char *name,
11253 struct die_info *die, struct dwarf2_cu *cu,
11256 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11259 name = dwarf2_name (die, cu);
11261 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
11262 but otherwise compute it by typename_concat inside GDB.
11263 FIXME: Actually this is not really true, or at least not always true.
11264 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
11265 Fortran names because there is no mangling standard. So new_symbol
11266 will set the demangled name to the result of dwarf2_full_name, and it is
11267 the demangled name that GDB uses if it exists. */
11268 if (cu->language == language_ada
11269 || (cu->language == language_fortran && physname))
11271 /* For Ada unit, we prefer the linkage name over the name, as
11272 the former contains the exported name, which the user expects
11273 to be able to reference. Ideally, we want the user to be able
11274 to reference this entity using either natural or linkage name,
11275 but we haven't started looking at this enhancement yet. */
11276 const char *linkage_name = dw2_linkage_name (die, cu);
11278 if (linkage_name != NULL)
11279 return linkage_name;
11282 /* These are the only languages we know how to qualify names in. */
11284 && (cu->language == language_cplus
11285 || cu->language == language_fortran || cu->language == language_d
11286 || cu->language == language_rust))
11288 if (die_needs_namespace (die, cu))
11290 const char *prefix;
11291 const char *canonical_name = NULL;
11295 prefix = determine_prefix (die, cu);
11296 if (*prefix != '\0')
11298 char *prefixed_name = typename_concat (NULL, prefix, name,
11301 buf.puts (prefixed_name);
11302 xfree (prefixed_name);
11307 /* Template parameters may be specified in the DIE's DW_AT_name, or
11308 as children with DW_TAG_template_type_param or
11309 DW_TAG_value_type_param. If the latter, add them to the name
11310 here. If the name already has template parameters, then
11311 skip this step; some versions of GCC emit both, and
11312 it is more efficient to use the pre-computed name.
11314 Something to keep in mind about this process: it is very
11315 unlikely, or in some cases downright impossible, to produce
11316 something that will match the mangled name of a function.
11317 If the definition of the function has the same debug info,
11318 we should be able to match up with it anyway. But fallbacks
11319 using the minimal symbol, for instance to find a method
11320 implemented in a stripped copy of libstdc++, will not work.
11321 If we do not have debug info for the definition, we will have to
11322 match them up some other way.
11324 When we do name matching there is a related problem with function
11325 templates; two instantiated function templates are allowed to
11326 differ only by their return types, which we do not add here. */
11328 if (cu->language == language_cplus && strchr (name, '<') == NULL)
11330 struct attribute *attr;
11331 struct die_info *child;
11334 die->building_fullname = 1;
11336 for (child = die->child; child != NULL; child = child->sibling)
11340 const gdb_byte *bytes;
11341 struct dwarf2_locexpr_baton *baton;
11344 if (child->tag != DW_TAG_template_type_param
11345 && child->tag != DW_TAG_template_value_param)
11356 attr = dwarf2_attr (child, DW_AT_type, cu);
11359 complaint (&symfile_complaints,
11360 _("template parameter missing DW_AT_type"));
11361 buf.puts ("UNKNOWN_TYPE");
11364 type = die_type (child, cu);
11366 if (child->tag == DW_TAG_template_type_param)
11368 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
11372 attr = dwarf2_attr (child, DW_AT_const_value, cu);
11375 complaint (&symfile_complaints,
11376 _("template parameter missing "
11377 "DW_AT_const_value"));
11378 buf.puts ("UNKNOWN_VALUE");
11382 dwarf2_const_value_attr (attr, type, name,
11383 &cu->comp_unit_obstack, cu,
11384 &value, &bytes, &baton);
11386 if (TYPE_NOSIGN (type))
11387 /* GDB prints characters as NUMBER 'CHAR'. If that's
11388 changed, this can use value_print instead. */
11389 c_printchar (value, type, &buf);
11392 struct value_print_options opts;
11395 v = dwarf2_evaluate_loc_desc (type, NULL,
11399 else if (bytes != NULL)
11401 v = allocate_value (type);
11402 memcpy (value_contents_writeable (v), bytes,
11403 TYPE_LENGTH (type));
11406 v = value_from_longest (type, value);
11408 /* Specify decimal so that we do not depend on
11410 get_formatted_print_options (&opts, 'd');
11412 value_print (v, &buf, &opts);
11418 die->building_fullname = 0;
11422 /* Close the argument list, with a space if necessary
11423 (nested templates). */
11424 if (!buf.empty () && buf.string ().back () == '>')
11431 /* For C++ methods, append formal parameter type
11432 information, if PHYSNAME. */
11434 if (physname && die->tag == DW_TAG_subprogram
11435 && cu->language == language_cplus)
11437 struct type *type = read_type_die (die, cu);
11439 c_type_print_args (type, &buf, 1, cu->language,
11440 &type_print_raw_options);
11442 if (cu->language == language_cplus)
11444 /* Assume that an artificial first parameter is
11445 "this", but do not crash if it is not. RealView
11446 marks unnamed (and thus unused) parameters as
11447 artificial; there is no way to differentiate
11449 if (TYPE_NFIELDS (type) > 0
11450 && TYPE_FIELD_ARTIFICIAL (type, 0)
11451 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11452 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11454 buf.puts (" const");
11458 const std::string &intermediate_name = buf.string ();
11460 if (cu->language == language_cplus)
11462 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11463 &objfile->per_bfd->storage_obstack);
11465 /* If we only computed INTERMEDIATE_NAME, or if
11466 INTERMEDIATE_NAME is already canonical, then we need to
11467 copy it to the appropriate obstack. */
11468 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11469 name = ((const char *)
11470 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11471 intermediate_name.c_str (),
11472 intermediate_name.length ()));
11474 name = canonical_name;
11481 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11482 If scope qualifiers are appropriate they will be added. The result
11483 will be allocated on the storage_obstack, or NULL if the DIE does
11484 not have a name. NAME may either be from a previous call to
11485 dwarf2_name or NULL.
11487 The output string will be canonicalized (if C++). */
11489 static const char *
11490 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11492 return dwarf2_compute_name (name, die, cu, 0);
11495 /* Construct a physname for the given DIE in CU. NAME may either be
11496 from a previous call to dwarf2_name or NULL. The result will be
11497 allocated on the objfile_objstack or NULL if the DIE does not have a
11500 The output string will be canonicalized (if C++). */
11502 static const char *
11503 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11505 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11506 const char *retval, *mangled = NULL, *canon = NULL;
11509 /* In this case dwarf2_compute_name is just a shortcut not building anything
11511 if (!die_needs_namespace (die, cu))
11512 return dwarf2_compute_name (name, die, cu, 1);
11514 mangled = dw2_linkage_name (die, cu);
11516 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11517 See https://github.com/rust-lang/rust/issues/32925. */
11518 if (cu->language == language_rust && mangled != NULL
11519 && strchr (mangled, '{') != NULL)
11522 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11524 gdb::unique_xmalloc_ptr<char> demangled;
11525 if (mangled != NULL)
11528 if (cu->language == language_go)
11530 /* This is a lie, but we already lie to the caller new_symbol.
11531 new_symbol assumes we return the mangled name.
11532 This just undoes that lie until things are cleaned up. */
11536 /* Use DMGL_RET_DROP for C++ template functions to suppress
11537 their return type. It is easier for GDB users to search
11538 for such functions as `name(params)' than `long name(params)'.
11539 In such case the minimal symbol names do not match the full
11540 symbol names but for template functions there is never a need
11541 to look up their definition from their declaration so
11542 the only disadvantage remains the minimal symbol variant
11543 `long name(params)' does not have the proper inferior type. */
11544 demangled.reset (gdb_demangle (mangled,
11545 (DMGL_PARAMS | DMGL_ANSI
11546 | DMGL_RET_DROP)));
11549 canon = demangled.get ();
11557 if (canon == NULL || check_physname)
11559 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11561 if (canon != NULL && strcmp (physname, canon) != 0)
11563 /* It may not mean a bug in GDB. The compiler could also
11564 compute DW_AT_linkage_name incorrectly. But in such case
11565 GDB would need to be bug-to-bug compatible. */
11567 complaint (&symfile_complaints,
11568 _("Computed physname <%s> does not match demangled <%s> "
11569 "(from linkage <%s>) - DIE at %s [in module %s]"),
11570 physname, canon, mangled, sect_offset_str (die->sect_off),
11571 objfile_name (objfile));
11573 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11574 is available here - over computed PHYSNAME. It is safer
11575 against both buggy GDB and buggy compilers. */
11589 retval = ((const char *)
11590 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11591 retval, strlen (retval)));
11596 /* Inspect DIE in CU for a namespace alias. If one exists, record
11597 a new symbol for it.
11599 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11602 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11604 struct attribute *attr;
11606 /* If the die does not have a name, this is not a namespace
11608 attr = dwarf2_attr (die, DW_AT_name, cu);
11612 struct die_info *d = die;
11613 struct dwarf2_cu *imported_cu = cu;
11615 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11616 keep inspecting DIEs until we hit the underlying import. */
11617 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11618 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11620 attr = dwarf2_attr (d, DW_AT_import, cu);
11624 d = follow_die_ref (d, attr, &imported_cu);
11625 if (d->tag != DW_TAG_imported_declaration)
11629 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11631 complaint (&symfile_complaints,
11632 _("DIE at %s has too many recursively imported "
11633 "declarations"), sect_offset_str (d->sect_off));
11640 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11642 type = get_die_type_at_offset (sect_off, cu->per_cu);
11643 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11645 /* This declaration is a global namespace alias. Add
11646 a symbol for it whose type is the aliased namespace. */
11647 new_symbol (die, type, cu);
11656 /* Return the using directives repository (global or local?) to use in the
11657 current context for LANGUAGE.
11659 For Ada, imported declarations can materialize renamings, which *may* be
11660 global. However it is impossible (for now?) in DWARF to distinguish
11661 "external" imported declarations and "static" ones. As all imported
11662 declarations seem to be static in all other languages, make them all CU-wide
11663 global only in Ada. */
11665 static struct using_direct **
11666 using_directives (enum language language)
11668 if (language == language_ada && context_stack_depth == 0)
11669 return &global_using_directives;
11671 return &local_using_directives;
11674 /* Read the import statement specified by the given die and record it. */
11677 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11679 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11680 struct attribute *import_attr;
11681 struct die_info *imported_die, *child_die;
11682 struct dwarf2_cu *imported_cu;
11683 const char *imported_name;
11684 const char *imported_name_prefix;
11685 const char *canonical_name;
11686 const char *import_alias;
11687 const char *imported_declaration = NULL;
11688 const char *import_prefix;
11689 std::vector<const char *> excludes;
11691 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11692 if (import_attr == NULL)
11694 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11695 dwarf_tag_name (die->tag));
11700 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11701 imported_name = dwarf2_name (imported_die, imported_cu);
11702 if (imported_name == NULL)
11704 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11706 The import in the following code:
11720 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11721 <52> DW_AT_decl_file : 1
11722 <53> DW_AT_decl_line : 6
11723 <54> DW_AT_import : <0x75>
11724 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11725 <59> DW_AT_name : B
11726 <5b> DW_AT_decl_file : 1
11727 <5c> DW_AT_decl_line : 2
11728 <5d> DW_AT_type : <0x6e>
11730 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11731 <76> DW_AT_byte_size : 4
11732 <77> DW_AT_encoding : 5 (signed)
11734 imports the wrong die ( 0x75 instead of 0x58 ).
11735 This case will be ignored until the gcc bug is fixed. */
11739 /* Figure out the local name after import. */
11740 import_alias = dwarf2_name (die, cu);
11742 /* Figure out where the statement is being imported to. */
11743 import_prefix = determine_prefix (die, cu);
11745 /* Figure out what the scope of the imported die is and prepend it
11746 to the name of the imported die. */
11747 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11749 if (imported_die->tag != DW_TAG_namespace
11750 && imported_die->tag != DW_TAG_module)
11752 imported_declaration = imported_name;
11753 canonical_name = imported_name_prefix;
11755 else if (strlen (imported_name_prefix) > 0)
11756 canonical_name = obconcat (&objfile->objfile_obstack,
11757 imported_name_prefix,
11758 (cu->language == language_d ? "." : "::"),
11759 imported_name, (char *) NULL);
11761 canonical_name = imported_name;
11763 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11764 for (child_die = die->child; child_die && child_die->tag;
11765 child_die = sibling_die (child_die))
11767 /* DWARF-4: A Fortran use statement with a “rename list” may be
11768 represented by an imported module entry with an import attribute
11769 referring to the module and owned entries corresponding to those
11770 entities that are renamed as part of being imported. */
11772 if (child_die->tag != DW_TAG_imported_declaration)
11774 complaint (&symfile_complaints,
11775 _("child DW_TAG_imported_declaration expected "
11776 "- DIE at %s [in module %s]"),
11777 sect_offset_str (child_die->sect_off),
11778 objfile_name (objfile));
11782 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11783 if (import_attr == NULL)
11785 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11786 dwarf_tag_name (child_die->tag));
11791 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11793 imported_name = dwarf2_name (imported_die, imported_cu);
11794 if (imported_name == NULL)
11796 complaint (&symfile_complaints,
11797 _("child DW_TAG_imported_declaration has unknown "
11798 "imported name - DIE at %s [in module %s]"),
11799 sect_offset_str (child_die->sect_off),
11800 objfile_name (objfile));
11804 excludes.push_back (imported_name);
11806 process_die (child_die, cu);
11809 add_using_directive (using_directives (cu->language),
11813 imported_declaration,
11816 &objfile->objfile_obstack);
11819 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11820 types, but gives them a size of zero. Starting with version 14,
11821 ICC is compatible with GCC. */
11824 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11826 if (!cu->checked_producer)
11827 check_producer (cu);
11829 return cu->producer_is_icc_lt_14;
11832 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11833 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11834 this, it was first present in GCC release 4.3.0. */
11837 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11839 if (!cu->checked_producer)
11840 check_producer (cu);
11842 return cu->producer_is_gcc_lt_4_3;
11845 static file_and_directory
11846 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11848 file_and_directory res;
11850 /* Find the filename. Do not use dwarf2_name here, since the filename
11851 is not a source language identifier. */
11852 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11853 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11855 if (res.comp_dir == NULL
11856 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11857 && IS_ABSOLUTE_PATH (res.name))
11859 res.comp_dir_storage = ldirname (res.name);
11860 if (!res.comp_dir_storage.empty ())
11861 res.comp_dir = res.comp_dir_storage.c_str ();
11863 if (res.comp_dir != NULL)
11865 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11866 directory, get rid of it. */
11867 const char *cp = strchr (res.comp_dir, ':');
11869 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11870 res.comp_dir = cp + 1;
11873 if (res.name == NULL)
11874 res.name = "<unknown>";
11879 /* Handle DW_AT_stmt_list for a compilation unit.
11880 DIE is the DW_TAG_compile_unit die for CU.
11881 COMP_DIR is the compilation directory. LOWPC is passed to
11882 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11885 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11886 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11888 struct dwarf2_per_objfile *dwarf2_per_objfile
11889 = cu->per_cu->dwarf2_per_objfile;
11890 struct objfile *objfile = dwarf2_per_objfile->objfile;
11891 struct attribute *attr;
11892 struct line_header line_header_local;
11893 hashval_t line_header_local_hash;
11895 int decode_mapping;
11897 gdb_assert (! cu->per_cu->is_debug_types);
11899 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11903 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11905 /* The line header hash table is only created if needed (it exists to
11906 prevent redundant reading of the line table for partial_units).
11907 If we're given a partial_unit, we'll need it. If we're given a
11908 compile_unit, then use the line header hash table if it's already
11909 created, but don't create one just yet. */
11911 if (dwarf2_per_objfile->line_header_hash == NULL
11912 && die->tag == DW_TAG_partial_unit)
11914 dwarf2_per_objfile->line_header_hash
11915 = htab_create_alloc_ex (127, line_header_hash_voidp,
11916 line_header_eq_voidp,
11917 free_line_header_voidp,
11918 &objfile->objfile_obstack,
11919 hashtab_obstack_allocate,
11920 dummy_obstack_deallocate);
11923 line_header_local.sect_off = line_offset;
11924 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11925 line_header_local_hash = line_header_hash (&line_header_local);
11926 if (dwarf2_per_objfile->line_header_hash != NULL)
11928 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11929 &line_header_local,
11930 line_header_local_hash, NO_INSERT);
11932 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11933 is not present in *SLOT (since if there is something in *SLOT then
11934 it will be for a partial_unit). */
11935 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11937 gdb_assert (*slot != NULL);
11938 cu->line_header = (struct line_header *) *slot;
11943 /* dwarf_decode_line_header does not yet provide sufficient information.
11944 We always have to call also dwarf_decode_lines for it. */
11945 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11949 cu->line_header = lh.release ();
11950 cu->line_header_die_owner = die;
11952 if (dwarf2_per_objfile->line_header_hash == NULL)
11956 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11957 &line_header_local,
11958 line_header_local_hash, INSERT);
11959 gdb_assert (slot != NULL);
11961 if (slot != NULL && *slot == NULL)
11963 /* This newly decoded line number information unit will be owned
11964 by line_header_hash hash table. */
11965 *slot = cu->line_header;
11966 cu->line_header_die_owner = NULL;
11970 /* We cannot free any current entry in (*slot) as that struct line_header
11971 may be already used by multiple CUs. Create only temporary decoded
11972 line_header for this CU - it may happen at most once for each line
11973 number information unit. And if we're not using line_header_hash
11974 then this is what we want as well. */
11975 gdb_assert (die->tag != DW_TAG_partial_unit);
11977 decode_mapping = (die->tag != DW_TAG_partial_unit);
11978 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11983 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11986 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11988 struct dwarf2_per_objfile *dwarf2_per_objfile
11989 = cu->per_cu->dwarf2_per_objfile;
11990 struct objfile *objfile = dwarf2_per_objfile->objfile;
11991 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11992 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11993 CORE_ADDR highpc = ((CORE_ADDR) 0);
11994 struct attribute *attr;
11995 struct die_info *child_die;
11996 CORE_ADDR baseaddr;
11998 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12000 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
12002 /* If we didn't find a lowpc, set it to highpc to avoid complaints
12003 from finish_block. */
12004 if (lowpc == ((CORE_ADDR) -1))
12006 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12008 file_and_directory fnd = find_file_and_directory (die, cu);
12010 prepare_one_comp_unit (cu, die, cu->language);
12012 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
12013 standardised yet. As a workaround for the language detection we fall
12014 back to the DW_AT_producer string. */
12015 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
12016 cu->language = language_opencl;
12018 /* Similar hack for Go. */
12019 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
12020 set_cu_language (DW_LANG_Go, cu);
12022 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
12024 /* Decode line number information if present. We do this before
12025 processing child DIEs, so that the line header table is available
12026 for DW_AT_decl_file. */
12027 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
12029 /* Process all dies in compilation unit. */
12030 if (die->child != NULL)
12032 child_die = die->child;
12033 while (child_die && child_die->tag)
12035 process_die (child_die, cu);
12036 child_die = sibling_die (child_die);
12040 /* Decode macro information, if present. Dwarf 2 macro information
12041 refers to information in the line number info statement program
12042 header, so we can only read it if we've read the header
12044 attr = dwarf2_attr (die, DW_AT_macros, cu);
12046 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
12047 if (attr && cu->line_header)
12049 if (dwarf2_attr (die, DW_AT_macro_info, cu))
12050 complaint (&symfile_complaints,
12051 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
12053 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
12057 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
12058 if (attr && cu->line_header)
12060 unsigned int macro_offset = DW_UNSND (attr);
12062 dwarf_decode_macros (cu, macro_offset, 0);
12067 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
12068 Create the set of symtabs used by this TU, or if this TU is sharing
12069 symtabs with another TU and the symtabs have already been created
12070 then restore those symtabs in the line header.
12071 We don't need the pc/line-number mapping for type units. */
12074 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
12076 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
12077 struct type_unit_group *tu_group;
12079 struct attribute *attr;
12081 struct signatured_type *sig_type;
12083 gdb_assert (per_cu->is_debug_types);
12084 sig_type = (struct signatured_type *) per_cu;
12086 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
12088 /* If we're using .gdb_index (includes -readnow) then
12089 per_cu->type_unit_group may not have been set up yet. */
12090 if (sig_type->type_unit_group == NULL)
12091 sig_type->type_unit_group = get_type_unit_group (cu, attr);
12092 tu_group = sig_type->type_unit_group;
12094 /* If we've already processed this stmt_list there's no real need to
12095 do it again, we could fake it and just recreate the part we need
12096 (file name,index -> symtab mapping). If data shows this optimization
12097 is useful we can do it then. */
12098 first_time = tu_group->compunit_symtab == NULL;
12100 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
12105 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
12106 lh = dwarf_decode_line_header (line_offset, cu);
12111 dwarf2_start_symtab (cu, "", NULL, 0);
12114 gdb_assert (tu_group->symtabs == NULL);
12115 restart_symtab (tu_group->compunit_symtab, "", 0);
12120 cu->line_header = lh.release ();
12121 cu->line_header_die_owner = die;
12125 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
12127 /* Note: We don't assign tu_group->compunit_symtab yet because we're
12128 still initializing it, and our caller (a few levels up)
12129 process_full_type_unit still needs to know if this is the first
12132 tu_group->num_symtabs = cu->line_header->file_names.size ();
12133 tu_group->symtabs = XNEWVEC (struct symtab *,
12134 cu->line_header->file_names.size ());
12136 for (i = 0; i < cu->line_header->file_names.size (); ++i)
12138 file_entry &fe = cu->line_header->file_names[i];
12140 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
12142 if (current_subfile->symtab == NULL)
12144 /* NOTE: start_subfile will recognize when it's been
12145 passed a file it has already seen. So we can't
12146 assume there's a simple mapping from
12147 cu->line_header->file_names to subfiles, plus
12148 cu->line_header->file_names may contain dups. */
12149 current_subfile->symtab
12150 = allocate_symtab (cust, current_subfile->name);
12153 fe.symtab = current_subfile->symtab;
12154 tu_group->symtabs[i] = fe.symtab;
12159 restart_symtab (tu_group->compunit_symtab, "", 0);
12161 for (i = 0; i < cu->line_header->file_names.size (); ++i)
12163 file_entry &fe = cu->line_header->file_names[i];
12165 fe.symtab = tu_group->symtabs[i];
12169 /* The main symtab is allocated last. Type units don't have DW_AT_name
12170 so they don't have a "real" (so to speak) symtab anyway.
12171 There is later code that will assign the main symtab to all symbols
12172 that don't have one. We need to handle the case of a symbol with a
12173 missing symtab (DW_AT_decl_file) anyway. */
12176 /* Process DW_TAG_type_unit.
12177 For TUs we want to skip the first top level sibling if it's not the
12178 actual type being defined by this TU. In this case the first top
12179 level sibling is there to provide context only. */
12182 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
12184 struct die_info *child_die;
12186 prepare_one_comp_unit (cu, die, language_minimal);
12188 /* Initialize (or reinitialize) the machinery for building symtabs.
12189 We do this before processing child DIEs, so that the line header table
12190 is available for DW_AT_decl_file. */
12191 setup_type_unit_groups (die, cu);
12193 if (die->child != NULL)
12195 child_die = die->child;
12196 while (child_die && child_die->tag)
12198 process_die (child_die, cu);
12199 child_die = sibling_die (child_die);
12206 http://gcc.gnu.org/wiki/DebugFission
12207 http://gcc.gnu.org/wiki/DebugFissionDWP
12209 To simplify handling of both DWO files ("object" files with the DWARF info)
12210 and DWP files (a file with the DWOs packaged up into one file), we treat
12211 DWP files as having a collection of virtual DWO files. */
12214 hash_dwo_file (const void *item)
12216 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
12219 hash = htab_hash_string (dwo_file->dwo_name);
12220 if (dwo_file->comp_dir != NULL)
12221 hash += htab_hash_string (dwo_file->comp_dir);
12226 eq_dwo_file (const void *item_lhs, const void *item_rhs)
12228 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
12229 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
12231 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
12233 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
12234 return lhs->comp_dir == rhs->comp_dir;
12235 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
12238 /* Allocate a hash table for DWO files. */
12241 allocate_dwo_file_hash_table (struct objfile *objfile)
12243 return htab_create_alloc_ex (41,
12247 &objfile->objfile_obstack,
12248 hashtab_obstack_allocate,
12249 dummy_obstack_deallocate);
12252 /* Lookup DWO file DWO_NAME. */
12255 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
12256 const char *dwo_name,
12257 const char *comp_dir)
12259 struct dwo_file find_entry;
12262 if (dwarf2_per_objfile->dwo_files == NULL)
12263 dwarf2_per_objfile->dwo_files
12264 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
12266 memset (&find_entry, 0, sizeof (find_entry));
12267 find_entry.dwo_name = dwo_name;
12268 find_entry.comp_dir = comp_dir;
12269 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
12275 hash_dwo_unit (const void *item)
12277 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12279 /* This drops the top 32 bits of the id, but is ok for a hash. */
12280 return dwo_unit->signature;
12284 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
12286 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
12287 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
12289 /* The signature is assumed to be unique within the DWO file.
12290 So while object file CU dwo_id's always have the value zero,
12291 that's OK, assuming each object file DWO file has only one CU,
12292 and that's the rule for now. */
12293 return lhs->signature == rhs->signature;
12296 /* Allocate a hash table for DWO CUs,TUs.
12297 There is one of these tables for each of CUs,TUs for each DWO file. */
12300 allocate_dwo_unit_table (struct objfile *objfile)
12302 /* Start out with a pretty small number.
12303 Generally DWO files contain only one CU and maybe some TUs. */
12304 return htab_create_alloc_ex (3,
12308 &objfile->objfile_obstack,
12309 hashtab_obstack_allocate,
12310 dummy_obstack_deallocate);
12313 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
12315 struct create_dwo_cu_data
12317 struct dwo_file *dwo_file;
12318 struct dwo_unit dwo_unit;
12321 /* die_reader_func for create_dwo_cu. */
12324 create_dwo_cu_reader (const struct die_reader_specs *reader,
12325 const gdb_byte *info_ptr,
12326 struct die_info *comp_unit_die,
12330 struct dwarf2_cu *cu = reader->cu;
12331 sect_offset sect_off = cu->per_cu->sect_off;
12332 struct dwarf2_section_info *section = cu->per_cu->section;
12333 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
12334 struct dwo_file *dwo_file = data->dwo_file;
12335 struct dwo_unit *dwo_unit = &data->dwo_unit;
12336 struct attribute *attr;
12338 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
12341 complaint (&symfile_complaints,
12342 _("Dwarf Error: debug entry at offset %s is missing"
12343 " its dwo_id [in module %s]"),
12344 sect_offset_str (sect_off), dwo_file->dwo_name);
12348 dwo_unit->dwo_file = dwo_file;
12349 dwo_unit->signature = DW_UNSND (attr);
12350 dwo_unit->section = section;
12351 dwo_unit->sect_off = sect_off;
12352 dwo_unit->length = cu->per_cu->length;
12354 if (dwarf_read_debug)
12355 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
12356 sect_offset_str (sect_off),
12357 hex_string (dwo_unit->signature));
12360 /* Create the dwo_units for the CUs in a DWO_FILE.
12361 Note: This function processes DWO files only, not DWP files. */
12364 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12365 struct dwo_file &dwo_file, dwarf2_section_info §ion,
12368 struct objfile *objfile = dwarf2_per_objfile->objfile;
12369 const gdb_byte *info_ptr, *end_ptr;
12371 dwarf2_read_section (objfile, §ion);
12372 info_ptr = section.buffer;
12374 if (info_ptr == NULL)
12377 if (dwarf_read_debug)
12379 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
12380 get_section_name (§ion),
12381 get_section_file_name (§ion));
12384 end_ptr = info_ptr + section.size;
12385 while (info_ptr < end_ptr)
12387 struct dwarf2_per_cu_data per_cu;
12388 struct create_dwo_cu_data create_dwo_cu_data;
12389 struct dwo_unit *dwo_unit;
12391 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
12393 memset (&create_dwo_cu_data.dwo_unit, 0,
12394 sizeof (create_dwo_cu_data.dwo_unit));
12395 memset (&per_cu, 0, sizeof (per_cu));
12396 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
12397 per_cu.is_debug_types = 0;
12398 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12399 per_cu.section = §ion;
12400 create_dwo_cu_data.dwo_file = &dwo_file;
12402 init_cutu_and_read_dies_no_follow (
12403 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12404 info_ptr += per_cu.length;
12406 // If the unit could not be parsed, skip it.
12407 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12410 if (cus_htab == NULL)
12411 cus_htab = allocate_dwo_unit_table (objfile);
12413 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12414 *dwo_unit = create_dwo_cu_data.dwo_unit;
12415 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12416 gdb_assert (slot != NULL);
12419 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12420 sect_offset dup_sect_off = dup_cu->sect_off;
12422 complaint (&symfile_complaints,
12423 _("debug cu entry at offset %s is duplicate to"
12424 " the entry at offset %s, signature %s"),
12425 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
12426 hex_string (dwo_unit->signature));
12428 *slot = (void *)dwo_unit;
12432 /* DWP file .debug_{cu,tu}_index section format:
12433 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12437 Both index sections have the same format, and serve to map a 64-bit
12438 signature to a set of section numbers. Each section begins with a header,
12439 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12440 indexes, and a pool of 32-bit section numbers. The index sections will be
12441 aligned at 8-byte boundaries in the file.
12443 The index section header consists of:
12445 V, 32 bit version number
12447 N, 32 bit number of compilation units or type units in the index
12448 M, 32 bit number of slots in the hash table
12450 Numbers are recorded using the byte order of the application binary.
12452 The hash table begins at offset 16 in the section, and consists of an array
12453 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12454 order of the application binary). Unused slots in the hash table are 0.
12455 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12457 The parallel table begins immediately after the hash table
12458 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12459 array of 32-bit indexes (using the byte order of the application binary),
12460 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12461 table contains a 32-bit index into the pool of section numbers. For unused
12462 hash table slots, the corresponding entry in the parallel table will be 0.
12464 The pool of section numbers begins immediately following the hash table
12465 (at offset 16 + 12 * M from the beginning of the section). The pool of
12466 section numbers consists of an array of 32-bit words (using the byte order
12467 of the application binary). Each item in the array is indexed starting
12468 from 0. The hash table entry provides the index of the first section
12469 number in the set. Additional section numbers in the set follow, and the
12470 set is terminated by a 0 entry (section number 0 is not used in ELF).
12472 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12473 section must be the first entry in the set, and the .debug_abbrev.dwo must
12474 be the second entry. Other members of the set may follow in any order.
12480 DWP Version 2 combines all the .debug_info, etc. sections into one,
12481 and the entries in the index tables are now offsets into these sections.
12482 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12485 Index Section Contents:
12487 Hash Table of Signatures dwp_hash_table.hash_table
12488 Parallel Table of Indices dwp_hash_table.unit_table
12489 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12490 Table of Section Sizes dwp_hash_table.v2.sizes
12492 The index section header consists of:
12494 V, 32 bit version number
12495 L, 32 bit number of columns in the table of section offsets
12496 N, 32 bit number of compilation units or type units in the index
12497 M, 32 bit number of slots in the hash table
12499 Numbers are recorded using the byte order of the application binary.
12501 The hash table has the same format as version 1.
12502 The parallel table of indices has the same format as version 1,
12503 except that the entries are origin-1 indices into the table of sections
12504 offsets and the table of section sizes.
12506 The table of offsets begins immediately following the parallel table
12507 (at offset 16 + 12 * M from the beginning of the section). The table is
12508 a two-dimensional array of 32-bit words (using the byte order of the
12509 application binary), with L columns and N+1 rows, in row-major order.
12510 Each row in the array is indexed starting from 0. The first row provides
12511 a key to the remaining rows: each column in this row provides an identifier
12512 for a debug section, and the offsets in the same column of subsequent rows
12513 refer to that section. The section identifiers are:
12515 DW_SECT_INFO 1 .debug_info.dwo
12516 DW_SECT_TYPES 2 .debug_types.dwo
12517 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12518 DW_SECT_LINE 4 .debug_line.dwo
12519 DW_SECT_LOC 5 .debug_loc.dwo
12520 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12521 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12522 DW_SECT_MACRO 8 .debug_macro.dwo
12524 The offsets provided by the CU and TU index sections are the base offsets
12525 for the contributions made by each CU or TU to the corresponding section
12526 in the package file. Each CU and TU header contains an abbrev_offset
12527 field, used to find the abbreviations table for that CU or TU within the
12528 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12529 be interpreted as relative to the base offset given in the index section.
12530 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12531 should be interpreted as relative to the base offset for .debug_line.dwo,
12532 and offsets into other debug sections obtained from DWARF attributes should
12533 also be interpreted as relative to the corresponding base offset.
12535 The table of sizes begins immediately following the table of offsets.
12536 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12537 with L columns and N rows, in row-major order. Each row in the array is
12538 indexed starting from 1 (row 0 is shared by the two tables).
12542 Hash table lookup is handled the same in version 1 and 2:
12544 We assume that N and M will not exceed 2^32 - 1.
12545 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12547 Given a 64-bit compilation unit signature or a type signature S, an entry
12548 in the hash table is located as follows:
12550 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12551 the low-order k bits all set to 1.
12553 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12555 3) If the hash table entry at index H matches the signature, use that
12556 entry. If the hash table entry at index H is unused (all zeroes),
12557 terminate the search: the signature is not present in the table.
12559 4) Let H = (H + H') modulo M. Repeat at Step 3.
12561 Because M > N and H' and M are relatively prime, the search is guaranteed
12562 to stop at an unused slot or find the match. */
12564 /* Create a hash table to map DWO IDs to their CU/TU entry in
12565 .debug_{info,types}.dwo in DWP_FILE.
12566 Returns NULL if there isn't one.
12567 Note: This function processes DWP files only, not DWO files. */
12569 static struct dwp_hash_table *
12570 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12571 struct dwp_file *dwp_file, int is_debug_types)
12573 struct objfile *objfile = dwarf2_per_objfile->objfile;
12574 bfd *dbfd = dwp_file->dbfd;
12575 const gdb_byte *index_ptr, *index_end;
12576 struct dwarf2_section_info *index;
12577 uint32_t version, nr_columns, nr_units, nr_slots;
12578 struct dwp_hash_table *htab;
12580 if (is_debug_types)
12581 index = &dwp_file->sections.tu_index;
12583 index = &dwp_file->sections.cu_index;
12585 if (dwarf2_section_empty_p (index))
12587 dwarf2_read_section (objfile, index);
12589 index_ptr = index->buffer;
12590 index_end = index_ptr + index->size;
12592 version = read_4_bytes (dbfd, index_ptr);
12595 nr_columns = read_4_bytes (dbfd, index_ptr);
12599 nr_units = read_4_bytes (dbfd, index_ptr);
12601 nr_slots = read_4_bytes (dbfd, index_ptr);
12604 if (version != 1 && version != 2)
12606 error (_("Dwarf Error: unsupported DWP file version (%s)"
12607 " [in module %s]"),
12608 pulongest (version), dwp_file->name);
12610 if (nr_slots != (nr_slots & -nr_slots))
12612 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12613 " is not power of 2 [in module %s]"),
12614 pulongest (nr_slots), dwp_file->name);
12617 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12618 htab->version = version;
12619 htab->nr_columns = nr_columns;
12620 htab->nr_units = nr_units;
12621 htab->nr_slots = nr_slots;
12622 htab->hash_table = index_ptr;
12623 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12625 /* Exit early if the table is empty. */
12626 if (nr_slots == 0 || nr_units == 0
12627 || (version == 2 && nr_columns == 0))
12629 /* All must be zero. */
12630 if (nr_slots != 0 || nr_units != 0
12631 || (version == 2 && nr_columns != 0))
12633 complaint (&symfile_complaints,
12634 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12635 " all zero [in modules %s]"),
12643 htab->section_pool.v1.indices =
12644 htab->unit_table + sizeof (uint32_t) * nr_slots;
12645 /* It's harder to decide whether the section is too small in v1.
12646 V1 is deprecated anyway so we punt. */
12650 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12651 int *ids = htab->section_pool.v2.section_ids;
12652 /* Reverse map for error checking. */
12653 int ids_seen[DW_SECT_MAX + 1];
12656 if (nr_columns < 2)
12658 error (_("Dwarf Error: bad DWP hash table, too few columns"
12659 " in section table [in module %s]"),
12662 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12664 error (_("Dwarf Error: bad DWP hash table, too many columns"
12665 " in section table [in module %s]"),
12668 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12669 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12670 for (i = 0; i < nr_columns; ++i)
12672 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12674 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12676 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12677 " in section table [in module %s]"),
12678 id, dwp_file->name);
12680 if (ids_seen[id] != -1)
12682 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12683 " id %d in section table [in module %s]"),
12684 id, dwp_file->name);
12689 /* Must have exactly one info or types section. */
12690 if (((ids_seen[DW_SECT_INFO] != -1)
12691 + (ids_seen[DW_SECT_TYPES] != -1))
12694 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12695 " DWO info/types section [in module %s]"),
12698 /* Must have an abbrev section. */
12699 if (ids_seen[DW_SECT_ABBREV] == -1)
12701 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12702 " section [in module %s]"),
12705 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12706 htab->section_pool.v2.sizes =
12707 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12708 * nr_units * nr_columns);
12709 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12710 * nr_units * nr_columns))
12713 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12714 " [in module %s]"),
12722 /* Update SECTIONS with the data from SECTP.
12724 This function is like the other "locate" section routines that are
12725 passed to bfd_map_over_sections, but in this context the sections to
12726 read comes from the DWP V1 hash table, not the full ELF section table.
12728 The result is non-zero for success, or zero if an error was found. */
12731 locate_v1_virtual_dwo_sections (asection *sectp,
12732 struct virtual_v1_dwo_sections *sections)
12734 const struct dwop_section_names *names = &dwop_section_names;
12736 if (section_is_p (sectp->name, &names->abbrev_dwo))
12738 /* There can be only one. */
12739 if (sections->abbrev.s.section != NULL)
12741 sections->abbrev.s.section = sectp;
12742 sections->abbrev.size = bfd_get_section_size (sectp);
12744 else if (section_is_p (sectp->name, &names->info_dwo)
12745 || section_is_p (sectp->name, &names->types_dwo))
12747 /* There can be only one. */
12748 if (sections->info_or_types.s.section != NULL)
12750 sections->info_or_types.s.section = sectp;
12751 sections->info_or_types.size = bfd_get_section_size (sectp);
12753 else if (section_is_p (sectp->name, &names->line_dwo))
12755 /* There can be only one. */
12756 if (sections->line.s.section != NULL)
12758 sections->line.s.section = sectp;
12759 sections->line.size = bfd_get_section_size (sectp);
12761 else if (section_is_p (sectp->name, &names->loc_dwo))
12763 /* There can be only one. */
12764 if (sections->loc.s.section != NULL)
12766 sections->loc.s.section = sectp;
12767 sections->loc.size = bfd_get_section_size (sectp);
12769 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12771 /* There can be only one. */
12772 if (sections->macinfo.s.section != NULL)
12774 sections->macinfo.s.section = sectp;
12775 sections->macinfo.size = bfd_get_section_size (sectp);
12777 else if (section_is_p (sectp->name, &names->macro_dwo))
12779 /* There can be only one. */
12780 if (sections->macro.s.section != NULL)
12782 sections->macro.s.section = sectp;
12783 sections->macro.size = bfd_get_section_size (sectp);
12785 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12787 /* There can be only one. */
12788 if (sections->str_offsets.s.section != NULL)
12790 sections->str_offsets.s.section = sectp;
12791 sections->str_offsets.size = bfd_get_section_size (sectp);
12795 /* No other kind of section is valid. */
12802 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12803 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12804 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12805 This is for DWP version 1 files. */
12807 static struct dwo_unit *
12808 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12809 struct dwp_file *dwp_file,
12810 uint32_t unit_index,
12811 const char *comp_dir,
12812 ULONGEST signature, int is_debug_types)
12814 struct objfile *objfile = dwarf2_per_objfile->objfile;
12815 const struct dwp_hash_table *dwp_htab =
12816 is_debug_types ? dwp_file->tus : dwp_file->cus;
12817 bfd *dbfd = dwp_file->dbfd;
12818 const char *kind = is_debug_types ? "TU" : "CU";
12819 struct dwo_file *dwo_file;
12820 struct dwo_unit *dwo_unit;
12821 struct virtual_v1_dwo_sections sections;
12822 void **dwo_file_slot;
12825 gdb_assert (dwp_file->version == 1);
12827 if (dwarf_read_debug)
12829 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12831 pulongest (unit_index), hex_string (signature),
12835 /* Fetch the sections of this DWO unit.
12836 Put a limit on the number of sections we look for so that bad data
12837 doesn't cause us to loop forever. */
12839 #define MAX_NR_V1_DWO_SECTIONS \
12840 (1 /* .debug_info or .debug_types */ \
12841 + 1 /* .debug_abbrev */ \
12842 + 1 /* .debug_line */ \
12843 + 1 /* .debug_loc */ \
12844 + 1 /* .debug_str_offsets */ \
12845 + 1 /* .debug_macro or .debug_macinfo */ \
12846 + 1 /* trailing zero */)
12848 memset (§ions, 0, sizeof (sections));
12850 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12853 uint32_t section_nr =
12854 read_4_bytes (dbfd,
12855 dwp_htab->section_pool.v1.indices
12856 + (unit_index + i) * sizeof (uint32_t));
12858 if (section_nr == 0)
12860 if (section_nr >= dwp_file->num_sections)
12862 error (_("Dwarf Error: bad DWP hash table, section number too large"
12863 " [in module %s]"),
12867 sectp = dwp_file->elf_sections[section_nr];
12868 if (! locate_v1_virtual_dwo_sections (sectp, §ions))
12870 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12871 " [in module %s]"),
12877 || dwarf2_section_empty_p (§ions.info_or_types)
12878 || dwarf2_section_empty_p (§ions.abbrev))
12880 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12881 " [in module %s]"),
12884 if (i == MAX_NR_V1_DWO_SECTIONS)
12886 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12887 " [in module %s]"),
12891 /* It's easier for the rest of the code if we fake a struct dwo_file and
12892 have dwo_unit "live" in that. At least for now.
12894 The DWP file can be made up of a random collection of CUs and TUs.
12895 However, for each CU + set of TUs that came from the same original DWO
12896 file, we can combine them back into a virtual DWO file to save space
12897 (fewer struct dwo_file objects to allocate). Remember that for really
12898 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12900 std::string virtual_dwo_name =
12901 string_printf ("virtual-dwo/%d-%d-%d-%d",
12902 get_section_id (§ions.abbrev),
12903 get_section_id (§ions.line),
12904 get_section_id (§ions.loc),
12905 get_section_id (§ions.str_offsets));
12906 /* Can we use an existing virtual DWO file? */
12907 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12908 virtual_dwo_name.c_str (),
12910 /* Create one if necessary. */
12911 if (*dwo_file_slot == NULL)
12913 if (dwarf_read_debug)
12915 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12916 virtual_dwo_name.c_str ());
12918 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12920 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12921 virtual_dwo_name.c_str (),
12922 virtual_dwo_name.size ());
12923 dwo_file->comp_dir = comp_dir;
12924 dwo_file->sections.abbrev = sections.abbrev;
12925 dwo_file->sections.line = sections.line;
12926 dwo_file->sections.loc = sections.loc;
12927 dwo_file->sections.macinfo = sections.macinfo;
12928 dwo_file->sections.macro = sections.macro;
12929 dwo_file->sections.str_offsets = sections.str_offsets;
12930 /* The "str" section is global to the entire DWP file. */
12931 dwo_file->sections.str = dwp_file->sections.str;
12932 /* The info or types section is assigned below to dwo_unit,
12933 there's no need to record it in dwo_file.
12934 Also, we can't simply record type sections in dwo_file because
12935 we record a pointer into the vector in dwo_unit. As we collect more
12936 types we'll grow the vector and eventually have to reallocate space
12937 for it, invalidating all copies of pointers into the previous
12939 *dwo_file_slot = dwo_file;
12943 if (dwarf_read_debug)
12945 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12946 virtual_dwo_name.c_str ());
12948 dwo_file = (struct dwo_file *) *dwo_file_slot;
12951 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12952 dwo_unit->dwo_file = dwo_file;
12953 dwo_unit->signature = signature;
12954 dwo_unit->section =
12955 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12956 *dwo_unit->section = sections.info_or_types;
12957 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12962 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12963 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12964 piece within that section used by a TU/CU, return a virtual section
12965 of just that piece. */
12967 static struct dwarf2_section_info
12968 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12969 struct dwarf2_section_info *section,
12970 bfd_size_type offset, bfd_size_type size)
12972 struct dwarf2_section_info result;
12975 gdb_assert (section != NULL);
12976 gdb_assert (!section->is_virtual);
12978 memset (&result, 0, sizeof (result));
12979 result.s.containing_section = section;
12980 result.is_virtual = 1;
12985 sectp = get_section_bfd_section (section);
12987 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12988 bounds of the real section. This is a pretty-rare event, so just
12989 flag an error (easier) instead of a warning and trying to cope. */
12991 || offset + size > bfd_get_section_size (sectp))
12993 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12994 " in section %s [in module %s]"),
12995 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12996 objfile_name (dwarf2_per_objfile->objfile));
12999 result.virtual_offset = offset;
13000 result.size = size;
13004 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
13005 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
13006 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
13007 This is for DWP version 2 files. */
13009 static struct dwo_unit *
13010 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
13011 struct dwp_file *dwp_file,
13012 uint32_t unit_index,
13013 const char *comp_dir,
13014 ULONGEST signature, int is_debug_types)
13016 struct objfile *objfile = dwarf2_per_objfile->objfile;
13017 const struct dwp_hash_table *dwp_htab =
13018 is_debug_types ? dwp_file->tus : dwp_file->cus;
13019 bfd *dbfd = dwp_file->dbfd;
13020 const char *kind = is_debug_types ? "TU" : "CU";
13021 struct dwo_file *dwo_file;
13022 struct dwo_unit *dwo_unit;
13023 struct virtual_v2_dwo_sections sections;
13024 void **dwo_file_slot;
13027 gdb_assert (dwp_file->version == 2);
13029 if (dwarf_read_debug)
13031 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
13033 pulongest (unit_index), hex_string (signature),
13037 /* Fetch the section offsets of this DWO unit. */
13039 memset (§ions, 0, sizeof (sections));
13041 for (i = 0; i < dwp_htab->nr_columns; ++i)
13043 uint32_t offset = read_4_bytes (dbfd,
13044 dwp_htab->section_pool.v2.offsets
13045 + (((unit_index - 1) * dwp_htab->nr_columns
13047 * sizeof (uint32_t)));
13048 uint32_t size = read_4_bytes (dbfd,
13049 dwp_htab->section_pool.v2.sizes
13050 + (((unit_index - 1) * dwp_htab->nr_columns
13052 * sizeof (uint32_t)));
13054 switch (dwp_htab->section_pool.v2.section_ids[i])
13057 case DW_SECT_TYPES:
13058 sections.info_or_types_offset = offset;
13059 sections.info_or_types_size = size;
13061 case DW_SECT_ABBREV:
13062 sections.abbrev_offset = offset;
13063 sections.abbrev_size = size;
13066 sections.line_offset = offset;
13067 sections.line_size = size;
13070 sections.loc_offset = offset;
13071 sections.loc_size = size;
13073 case DW_SECT_STR_OFFSETS:
13074 sections.str_offsets_offset = offset;
13075 sections.str_offsets_size = size;
13077 case DW_SECT_MACINFO:
13078 sections.macinfo_offset = offset;
13079 sections.macinfo_size = size;
13081 case DW_SECT_MACRO:
13082 sections.macro_offset = offset;
13083 sections.macro_size = size;
13088 /* It's easier for the rest of the code if we fake a struct dwo_file and
13089 have dwo_unit "live" in that. At least for now.
13091 The DWP file can be made up of a random collection of CUs and TUs.
13092 However, for each CU + set of TUs that came from the same original DWO
13093 file, we can combine them back into a virtual DWO file to save space
13094 (fewer struct dwo_file objects to allocate). Remember that for really
13095 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
13097 std::string virtual_dwo_name =
13098 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
13099 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
13100 (long) (sections.line_size ? sections.line_offset : 0),
13101 (long) (sections.loc_size ? sections.loc_offset : 0),
13102 (long) (sections.str_offsets_size
13103 ? sections.str_offsets_offset : 0));
13104 /* Can we use an existing virtual DWO file? */
13105 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13106 virtual_dwo_name.c_str (),
13108 /* Create one if necessary. */
13109 if (*dwo_file_slot == NULL)
13111 if (dwarf_read_debug)
13113 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
13114 virtual_dwo_name.c_str ());
13116 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
13118 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
13119 virtual_dwo_name.c_str (),
13120 virtual_dwo_name.size ());
13121 dwo_file->comp_dir = comp_dir;
13122 dwo_file->sections.abbrev =
13123 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
13124 sections.abbrev_offset, sections.abbrev_size);
13125 dwo_file->sections.line =
13126 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
13127 sections.line_offset, sections.line_size);
13128 dwo_file->sections.loc =
13129 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
13130 sections.loc_offset, sections.loc_size);
13131 dwo_file->sections.macinfo =
13132 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
13133 sections.macinfo_offset, sections.macinfo_size);
13134 dwo_file->sections.macro =
13135 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
13136 sections.macro_offset, sections.macro_size);
13137 dwo_file->sections.str_offsets =
13138 create_dwp_v2_section (dwarf2_per_objfile,
13139 &dwp_file->sections.str_offsets,
13140 sections.str_offsets_offset,
13141 sections.str_offsets_size);
13142 /* The "str" section is global to the entire DWP file. */
13143 dwo_file->sections.str = dwp_file->sections.str;
13144 /* The info or types section is assigned below to dwo_unit,
13145 there's no need to record it in dwo_file.
13146 Also, we can't simply record type sections in dwo_file because
13147 we record a pointer into the vector in dwo_unit. As we collect more
13148 types we'll grow the vector and eventually have to reallocate space
13149 for it, invalidating all copies of pointers into the previous
13151 *dwo_file_slot = dwo_file;
13155 if (dwarf_read_debug)
13157 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
13158 virtual_dwo_name.c_str ());
13160 dwo_file = (struct dwo_file *) *dwo_file_slot;
13163 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
13164 dwo_unit->dwo_file = dwo_file;
13165 dwo_unit->signature = signature;
13166 dwo_unit->section =
13167 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
13168 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
13170 ? &dwp_file->sections.types
13171 : &dwp_file->sections.info,
13172 sections.info_or_types_offset,
13173 sections.info_or_types_size);
13174 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
13179 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
13180 Returns NULL if the signature isn't found. */
13182 static struct dwo_unit *
13183 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
13184 struct dwp_file *dwp_file, const char *comp_dir,
13185 ULONGEST signature, int is_debug_types)
13187 const struct dwp_hash_table *dwp_htab =
13188 is_debug_types ? dwp_file->tus : dwp_file->cus;
13189 bfd *dbfd = dwp_file->dbfd;
13190 uint32_t mask = dwp_htab->nr_slots - 1;
13191 uint32_t hash = signature & mask;
13192 uint32_t hash2 = ((signature >> 32) & mask) | 1;
13195 struct dwo_unit find_dwo_cu;
13197 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
13198 find_dwo_cu.signature = signature;
13199 slot = htab_find_slot (is_debug_types
13200 ? dwp_file->loaded_tus
13201 : dwp_file->loaded_cus,
13202 &find_dwo_cu, INSERT);
13205 return (struct dwo_unit *) *slot;
13207 /* Use a for loop so that we don't loop forever on bad debug info. */
13208 for (i = 0; i < dwp_htab->nr_slots; ++i)
13210 ULONGEST signature_in_table;
13212 signature_in_table =
13213 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
13214 if (signature_in_table == signature)
13216 uint32_t unit_index =
13217 read_4_bytes (dbfd,
13218 dwp_htab->unit_table + hash * sizeof (uint32_t));
13220 if (dwp_file->version == 1)
13222 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
13223 dwp_file, unit_index,
13224 comp_dir, signature,
13229 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
13230 dwp_file, unit_index,
13231 comp_dir, signature,
13234 return (struct dwo_unit *) *slot;
13236 if (signature_in_table == 0)
13238 hash = (hash + hash2) & mask;
13241 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
13242 " [in module %s]"),
13246 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
13247 Open the file specified by FILE_NAME and hand it off to BFD for
13248 preliminary analysis. Return a newly initialized bfd *, which
13249 includes a canonicalized copy of FILE_NAME.
13250 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
13251 SEARCH_CWD is true if the current directory is to be searched.
13252 It will be searched before debug-file-directory.
13253 If successful, the file is added to the bfd include table of the
13254 objfile's bfd (see gdb_bfd_record_inclusion).
13255 If unable to find/open the file, return NULL.
13256 NOTE: This function is derived from symfile_bfd_open. */
13258 static gdb_bfd_ref_ptr
13259 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13260 const char *file_name, int is_dwp, int search_cwd)
13263 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
13264 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
13265 to debug_file_directory. */
13266 const char *search_path;
13267 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
13269 gdb::unique_xmalloc_ptr<char> search_path_holder;
13272 if (*debug_file_directory != '\0')
13274 search_path_holder.reset (concat (".", dirname_separator_string,
13275 debug_file_directory,
13277 search_path = search_path_holder.get ();
13283 search_path = debug_file_directory;
13285 openp_flags flags = OPF_RETURN_REALPATH;
13287 flags |= OPF_SEARCH_IN_PATH;
13289 gdb::unique_xmalloc_ptr<char> absolute_name;
13290 desc = openp (search_path, flags, file_name,
13291 O_RDONLY | O_BINARY, &absolute_name);
13295 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
13297 if (sym_bfd == NULL)
13299 bfd_set_cacheable (sym_bfd.get (), 1);
13301 if (!bfd_check_format (sym_bfd.get (), bfd_object))
13304 /* Success. Record the bfd as having been included by the objfile's bfd.
13305 This is important because things like demangled_names_hash lives in the
13306 objfile's per_bfd space and may have references to things like symbol
13307 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
13308 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13313 /* Try to open DWO file FILE_NAME.
13314 COMP_DIR is the DW_AT_comp_dir attribute.
13315 The result is the bfd handle of the file.
13316 If there is a problem finding or opening the file, return NULL.
13317 Upon success, the canonicalized path of the file is stored in the bfd,
13318 same as symfile_bfd_open. */
13320 static gdb_bfd_ref_ptr
13321 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13322 const char *file_name, const char *comp_dir)
13324 if (IS_ABSOLUTE_PATH (file_name))
13325 return try_open_dwop_file (dwarf2_per_objfile, file_name,
13326 0 /*is_dwp*/, 0 /*search_cwd*/);
13328 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
13330 if (comp_dir != NULL)
13332 char *path_to_try = concat (comp_dir, SLASH_STRING,
13333 file_name, (char *) NULL);
13335 /* NOTE: If comp_dir is a relative path, this will also try the
13336 search path, which seems useful. */
13337 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
13340 1 /*search_cwd*/));
13341 xfree (path_to_try);
13346 /* That didn't work, try debug-file-directory, which, despite its name,
13347 is a list of paths. */
13349 if (*debug_file_directory == '\0')
13352 return try_open_dwop_file (dwarf2_per_objfile, file_name,
13353 0 /*is_dwp*/, 1 /*search_cwd*/);
13356 /* This function is mapped across the sections and remembers the offset and
13357 size of each of the DWO debugging sections we are interested in. */
13360 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
13362 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
13363 const struct dwop_section_names *names = &dwop_section_names;
13365 if (section_is_p (sectp->name, &names->abbrev_dwo))
13367 dwo_sections->abbrev.s.section = sectp;
13368 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
13370 else if (section_is_p (sectp->name, &names->info_dwo))
13372 dwo_sections->info.s.section = sectp;
13373 dwo_sections->info.size = bfd_get_section_size (sectp);
13375 else if (section_is_p (sectp->name, &names->line_dwo))
13377 dwo_sections->line.s.section = sectp;
13378 dwo_sections->line.size = bfd_get_section_size (sectp);
13380 else if (section_is_p (sectp->name, &names->loc_dwo))
13382 dwo_sections->loc.s.section = sectp;
13383 dwo_sections->loc.size = bfd_get_section_size (sectp);
13385 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13387 dwo_sections->macinfo.s.section = sectp;
13388 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
13390 else if (section_is_p (sectp->name, &names->macro_dwo))
13392 dwo_sections->macro.s.section = sectp;
13393 dwo_sections->macro.size = bfd_get_section_size (sectp);
13395 else if (section_is_p (sectp->name, &names->str_dwo))
13397 dwo_sections->str.s.section = sectp;
13398 dwo_sections->str.size = bfd_get_section_size (sectp);
13400 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13402 dwo_sections->str_offsets.s.section = sectp;
13403 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
13405 else if (section_is_p (sectp->name, &names->types_dwo))
13407 struct dwarf2_section_info type_section;
13409 memset (&type_section, 0, sizeof (type_section));
13410 type_section.s.section = sectp;
13411 type_section.size = bfd_get_section_size (sectp);
13412 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
13417 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
13418 by PER_CU. This is for the non-DWP case.
13419 The result is NULL if DWO_NAME can't be found. */
13421 static struct dwo_file *
13422 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13423 const char *dwo_name, const char *comp_dir)
13425 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
13426 struct objfile *objfile = dwarf2_per_objfile->objfile;
13427 struct dwo_file *dwo_file;
13428 struct cleanup *cleanups;
13430 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
13433 if (dwarf_read_debug)
13434 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13437 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
13438 dwo_file->dwo_name = dwo_name;
13439 dwo_file->comp_dir = comp_dir;
13440 dwo_file->dbfd = dbfd.release ();
13442 free_dwo_file_cleanup_data *cleanup_data = XNEW (free_dwo_file_cleanup_data);
13443 cleanup_data->dwo_file = dwo_file;
13444 cleanup_data->dwarf2_per_objfile = dwarf2_per_objfile;
13446 cleanups = make_cleanup (free_dwo_file_cleanup, cleanup_data);
13448 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
13449 &dwo_file->sections);
13451 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13454 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file,
13455 dwo_file->sections.types, dwo_file->tus);
13457 discard_cleanups (cleanups);
13459 if (dwarf_read_debug)
13460 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13465 /* This function is mapped across the sections and remembers the offset and
13466 size of each of the DWP debugging sections common to version 1 and 2 that
13467 we are interested in. */
13470 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13471 void *dwp_file_ptr)
13473 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13474 const struct dwop_section_names *names = &dwop_section_names;
13475 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13477 /* Record the ELF section number for later lookup: this is what the
13478 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13479 gdb_assert (elf_section_nr < dwp_file->num_sections);
13480 dwp_file->elf_sections[elf_section_nr] = sectp;
13482 /* Look for specific sections that we need. */
13483 if (section_is_p (sectp->name, &names->str_dwo))
13485 dwp_file->sections.str.s.section = sectp;
13486 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13488 else if (section_is_p (sectp->name, &names->cu_index))
13490 dwp_file->sections.cu_index.s.section = sectp;
13491 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13493 else if (section_is_p (sectp->name, &names->tu_index))
13495 dwp_file->sections.tu_index.s.section = sectp;
13496 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13500 /* This function is mapped across the sections and remembers the offset and
13501 size of each of the DWP version 2 debugging sections that we are interested
13502 in. This is split into a separate function because we don't know if we
13503 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13506 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13508 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13509 const struct dwop_section_names *names = &dwop_section_names;
13510 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13512 /* Record the ELF section number for later lookup: this is what the
13513 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13514 gdb_assert (elf_section_nr < dwp_file->num_sections);
13515 dwp_file->elf_sections[elf_section_nr] = sectp;
13517 /* Look for specific sections that we need. */
13518 if (section_is_p (sectp->name, &names->abbrev_dwo))
13520 dwp_file->sections.abbrev.s.section = sectp;
13521 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13523 else if (section_is_p (sectp->name, &names->info_dwo))
13525 dwp_file->sections.info.s.section = sectp;
13526 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13528 else if (section_is_p (sectp->name, &names->line_dwo))
13530 dwp_file->sections.line.s.section = sectp;
13531 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13533 else if (section_is_p (sectp->name, &names->loc_dwo))
13535 dwp_file->sections.loc.s.section = sectp;
13536 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13538 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13540 dwp_file->sections.macinfo.s.section = sectp;
13541 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13543 else if (section_is_p (sectp->name, &names->macro_dwo))
13545 dwp_file->sections.macro.s.section = sectp;
13546 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13548 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13550 dwp_file->sections.str_offsets.s.section = sectp;
13551 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13553 else if (section_is_p (sectp->name, &names->types_dwo))
13555 dwp_file->sections.types.s.section = sectp;
13556 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13560 /* Hash function for dwp_file loaded CUs/TUs. */
13563 hash_dwp_loaded_cutus (const void *item)
13565 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13567 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13568 return dwo_unit->signature;
13571 /* Equality function for dwp_file loaded CUs/TUs. */
13574 eq_dwp_loaded_cutus (const void *a, const void *b)
13576 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13577 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13579 return dua->signature == dub->signature;
13582 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13585 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13587 return htab_create_alloc_ex (3,
13588 hash_dwp_loaded_cutus,
13589 eq_dwp_loaded_cutus,
13591 &objfile->objfile_obstack,
13592 hashtab_obstack_allocate,
13593 dummy_obstack_deallocate);
13596 /* Try to open DWP file FILE_NAME.
13597 The result is the bfd handle of the file.
13598 If there is a problem finding or opening the file, return NULL.
13599 Upon success, the canonicalized path of the file is stored in the bfd,
13600 same as symfile_bfd_open. */
13602 static gdb_bfd_ref_ptr
13603 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13604 const char *file_name)
13606 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13608 1 /*search_cwd*/));
13612 /* Work around upstream bug 15652.
13613 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13614 [Whether that's a "bug" is debatable, but it is getting in our way.]
13615 We have no real idea where the dwp file is, because gdb's realpath-ing
13616 of the executable's path may have discarded the needed info.
13617 [IWBN if the dwp file name was recorded in the executable, akin to
13618 .gnu_debuglink, but that doesn't exist yet.]
13619 Strip the directory from FILE_NAME and search again. */
13620 if (*debug_file_directory != '\0')
13622 /* Don't implicitly search the current directory here.
13623 If the user wants to search "." to handle this case,
13624 it must be added to debug-file-directory. */
13625 return try_open_dwop_file (dwarf2_per_objfile,
13626 lbasename (file_name), 1 /*is_dwp*/,
13633 /* Initialize the use of the DWP file for the current objfile.
13634 By convention the name of the DWP file is ${objfile}.dwp.
13635 The result is NULL if it can't be found. */
13637 static struct dwp_file *
13638 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13640 struct objfile *objfile = dwarf2_per_objfile->objfile;
13641 struct dwp_file *dwp_file;
13643 /* Try to find first .dwp for the binary file before any symbolic links
13646 /* If the objfile is a debug file, find the name of the real binary
13647 file and get the name of dwp file from there. */
13648 std::string dwp_name;
13649 if (objfile->separate_debug_objfile_backlink != NULL)
13651 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13652 const char *backlink_basename = lbasename (backlink->original_name);
13654 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13657 dwp_name = objfile->original_name;
13659 dwp_name += ".dwp";
13661 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13663 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13665 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13666 dwp_name = objfile_name (objfile);
13667 dwp_name += ".dwp";
13668 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13673 if (dwarf_read_debug)
13674 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13677 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
13678 dwp_file->name = bfd_get_filename (dbfd.get ());
13679 dwp_file->dbfd = dbfd.release ();
13681 /* +1: section 0 is unused */
13682 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
13683 dwp_file->elf_sections =
13684 OBSTACK_CALLOC (&objfile->objfile_obstack,
13685 dwp_file->num_sections, asection *);
13687 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13690 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
13692 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 1);
13694 /* The DWP file version is stored in the hash table. Oh well. */
13695 if (dwp_file->cus && dwp_file->tus
13696 && dwp_file->cus->version != dwp_file->tus->version)
13698 /* Technically speaking, we should try to limp along, but this is
13699 pretty bizarre. We use pulongest here because that's the established
13700 portability solution (e.g, we cannot use %u for uint32_t). */
13701 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13702 " TU version %s [in DWP file %s]"),
13703 pulongest (dwp_file->cus->version),
13704 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13708 dwp_file->version = dwp_file->cus->version;
13709 else if (dwp_file->tus)
13710 dwp_file->version = dwp_file->tus->version;
13712 dwp_file->version = 2;
13714 if (dwp_file->version == 2)
13715 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13718 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13719 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13721 if (dwarf_read_debug)
13723 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13724 fprintf_unfiltered (gdb_stdlog,
13725 " %s CUs, %s TUs\n",
13726 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13727 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13733 /* Wrapper around open_and_init_dwp_file, only open it once. */
13735 static struct dwp_file *
13736 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13738 if (! dwarf2_per_objfile->dwp_checked)
13740 dwarf2_per_objfile->dwp_file
13741 = open_and_init_dwp_file (dwarf2_per_objfile);
13742 dwarf2_per_objfile->dwp_checked = 1;
13744 return dwarf2_per_objfile->dwp_file;
13747 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13748 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13749 or in the DWP file for the objfile, referenced by THIS_UNIT.
13750 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13751 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13753 This is called, for example, when wanting to read a variable with a
13754 complex location. Therefore we don't want to do file i/o for every call.
13755 Therefore we don't want to look for a DWO file on every call.
13756 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13757 then we check if we've already seen DWO_NAME, and only THEN do we check
13760 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13761 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13763 static struct dwo_unit *
13764 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13765 const char *dwo_name, const char *comp_dir,
13766 ULONGEST signature, int is_debug_types)
13768 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13769 struct objfile *objfile = dwarf2_per_objfile->objfile;
13770 const char *kind = is_debug_types ? "TU" : "CU";
13771 void **dwo_file_slot;
13772 struct dwo_file *dwo_file;
13773 struct dwp_file *dwp_file;
13775 /* First see if there's a DWP file.
13776 If we have a DWP file but didn't find the DWO inside it, don't
13777 look for the original DWO file. It makes gdb behave differently
13778 depending on whether one is debugging in the build tree. */
13780 dwp_file = get_dwp_file (dwarf2_per_objfile);
13781 if (dwp_file != NULL)
13783 const struct dwp_hash_table *dwp_htab =
13784 is_debug_types ? dwp_file->tus : dwp_file->cus;
13786 if (dwp_htab != NULL)
13788 struct dwo_unit *dwo_cutu =
13789 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13790 signature, is_debug_types);
13792 if (dwo_cutu != NULL)
13794 if (dwarf_read_debug)
13796 fprintf_unfiltered (gdb_stdlog,
13797 "Virtual DWO %s %s found: @%s\n",
13798 kind, hex_string (signature),
13799 host_address_to_string (dwo_cutu));
13807 /* No DWP file, look for the DWO file. */
13809 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13810 dwo_name, comp_dir);
13811 if (*dwo_file_slot == NULL)
13813 /* Read in the file and build a table of the CUs/TUs it contains. */
13814 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13816 /* NOTE: This will be NULL if unable to open the file. */
13817 dwo_file = (struct dwo_file *) *dwo_file_slot;
13819 if (dwo_file != NULL)
13821 struct dwo_unit *dwo_cutu = NULL;
13823 if (is_debug_types && dwo_file->tus)
13825 struct dwo_unit find_dwo_cutu;
13827 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13828 find_dwo_cutu.signature = signature;
13830 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13832 else if (!is_debug_types && dwo_file->cus)
13834 struct dwo_unit find_dwo_cutu;
13836 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13837 find_dwo_cutu.signature = signature;
13838 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13842 if (dwo_cutu != NULL)
13844 if (dwarf_read_debug)
13846 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13847 kind, dwo_name, hex_string (signature),
13848 host_address_to_string (dwo_cutu));
13855 /* We didn't find it. This could mean a dwo_id mismatch, or
13856 someone deleted the DWO/DWP file, or the search path isn't set up
13857 correctly to find the file. */
13859 if (dwarf_read_debug)
13861 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13862 kind, dwo_name, hex_string (signature));
13865 /* This is a warning and not a complaint because it can be caused by
13866 pilot error (e.g., user accidentally deleting the DWO). */
13868 /* Print the name of the DWP file if we looked there, helps the user
13869 better diagnose the problem. */
13870 std::string dwp_text;
13872 if (dwp_file != NULL)
13873 dwp_text = string_printf (" [in DWP file %s]",
13874 lbasename (dwp_file->name));
13876 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13877 " [in module %s]"),
13878 kind, dwo_name, hex_string (signature),
13880 this_unit->is_debug_types ? "TU" : "CU",
13881 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13886 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13887 See lookup_dwo_cutu_unit for details. */
13889 static struct dwo_unit *
13890 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13891 const char *dwo_name, const char *comp_dir,
13892 ULONGEST signature)
13894 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13897 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13898 See lookup_dwo_cutu_unit for details. */
13900 static struct dwo_unit *
13901 lookup_dwo_type_unit (struct signatured_type *this_tu,
13902 const char *dwo_name, const char *comp_dir)
13904 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13907 /* Traversal function for queue_and_load_all_dwo_tus. */
13910 queue_and_load_dwo_tu (void **slot, void *info)
13912 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13913 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13914 ULONGEST signature = dwo_unit->signature;
13915 struct signatured_type *sig_type =
13916 lookup_dwo_signatured_type (per_cu->cu, signature);
13918 if (sig_type != NULL)
13920 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13922 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13923 a real dependency of PER_CU on SIG_TYPE. That is detected later
13924 while processing PER_CU. */
13925 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13926 load_full_type_unit (sig_cu);
13927 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13933 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13934 The DWO may have the only definition of the type, though it may not be
13935 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13936 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13939 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13941 struct dwo_unit *dwo_unit;
13942 struct dwo_file *dwo_file;
13944 gdb_assert (!per_cu->is_debug_types);
13945 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13946 gdb_assert (per_cu->cu != NULL);
13948 dwo_unit = per_cu->cu->dwo_unit;
13949 gdb_assert (dwo_unit != NULL);
13951 dwo_file = dwo_unit->dwo_file;
13952 if (dwo_file->tus != NULL)
13953 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13956 /* Free all resources associated with DWO_FILE.
13957 Close the DWO file and munmap the sections.
13958 All memory should be on the objfile obstack. */
13961 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
13964 /* Note: dbfd is NULL for virtual DWO files. */
13965 gdb_bfd_unref (dwo_file->dbfd);
13967 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13970 /* Wrapper for free_dwo_file for use in cleanups. */
13973 free_dwo_file_cleanup (void *arg)
13975 struct free_dwo_file_cleanup_data *data
13976 = (struct free_dwo_file_cleanup_data *) arg;
13977 struct objfile *objfile = data->dwarf2_per_objfile->objfile;
13979 free_dwo_file (data->dwo_file, objfile);
13984 /* Traversal function for free_dwo_files. */
13987 free_dwo_file_from_slot (void **slot, void *info)
13989 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13990 struct objfile *objfile = (struct objfile *) info;
13992 free_dwo_file (dwo_file, objfile);
13997 /* Free all resources associated with DWO_FILES. */
14000 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
14002 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
14005 /* Read in various DIEs. */
14007 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
14008 Inherit only the children of the DW_AT_abstract_origin DIE not being
14009 already referenced by DW_AT_abstract_origin from the children of the
14013 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
14015 struct die_info *child_die;
14016 sect_offset *offsetp;
14017 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
14018 struct die_info *origin_die;
14019 /* Iterator of the ORIGIN_DIE children. */
14020 struct die_info *origin_child_die;
14021 struct attribute *attr;
14022 struct dwarf2_cu *origin_cu;
14023 struct pending **origin_previous_list_in_scope;
14025 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14029 /* Note that following die references may follow to a die in a
14033 origin_die = follow_die_ref (die, attr, &origin_cu);
14035 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
14037 origin_previous_list_in_scope = origin_cu->list_in_scope;
14038 origin_cu->list_in_scope = cu->list_in_scope;
14040 if (die->tag != origin_die->tag
14041 && !(die->tag == DW_TAG_inlined_subroutine
14042 && origin_die->tag == DW_TAG_subprogram))
14043 complaint (&symfile_complaints,
14044 _("DIE %s and its abstract origin %s have different tags"),
14045 sect_offset_str (die->sect_off),
14046 sect_offset_str (origin_die->sect_off));
14048 std::vector<sect_offset> offsets;
14050 for (child_die = die->child;
14051 child_die && child_die->tag;
14052 child_die = sibling_die (child_die))
14054 struct die_info *child_origin_die;
14055 struct dwarf2_cu *child_origin_cu;
14057 /* We are trying to process concrete instance entries:
14058 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
14059 it's not relevant to our analysis here. i.e. detecting DIEs that are
14060 present in the abstract instance but not referenced in the concrete
14062 if (child_die->tag == DW_TAG_call_site
14063 || child_die->tag == DW_TAG_GNU_call_site)
14066 /* For each CHILD_DIE, find the corresponding child of
14067 ORIGIN_DIE. If there is more than one layer of
14068 DW_AT_abstract_origin, follow them all; there shouldn't be,
14069 but GCC versions at least through 4.4 generate this (GCC PR
14071 child_origin_die = child_die;
14072 child_origin_cu = cu;
14075 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
14079 child_origin_die = follow_die_ref (child_origin_die, attr,
14083 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
14084 counterpart may exist. */
14085 if (child_origin_die != child_die)
14087 if (child_die->tag != child_origin_die->tag
14088 && !(child_die->tag == DW_TAG_inlined_subroutine
14089 && child_origin_die->tag == DW_TAG_subprogram))
14090 complaint (&symfile_complaints,
14091 _("Child DIE %s and its abstract origin %s have "
14093 sect_offset_str (child_die->sect_off),
14094 sect_offset_str (child_origin_die->sect_off));
14095 if (child_origin_die->parent != origin_die)
14096 complaint (&symfile_complaints,
14097 _("Child DIE %s and its abstract origin %s have "
14098 "different parents"),
14099 sect_offset_str (child_die->sect_off),
14100 sect_offset_str (child_origin_die->sect_off));
14102 offsets.push_back (child_origin_die->sect_off);
14105 std::sort (offsets.begin (), offsets.end ());
14106 sect_offset *offsets_end = offsets.data () + offsets.size ();
14107 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
14108 if (offsetp[-1] == *offsetp)
14109 complaint (&symfile_complaints,
14110 _("Multiple children of DIE %s refer "
14111 "to DIE %s as their abstract origin"),
14112 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
14114 offsetp = offsets.data ();
14115 origin_child_die = origin_die->child;
14116 while (origin_child_die && origin_child_die->tag)
14118 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
14119 while (offsetp < offsets_end
14120 && *offsetp < origin_child_die->sect_off)
14122 if (offsetp >= offsets_end
14123 || *offsetp > origin_child_die->sect_off)
14125 /* Found that ORIGIN_CHILD_DIE is really not referenced.
14126 Check whether we're already processing ORIGIN_CHILD_DIE.
14127 This can happen with mutually referenced abstract_origins.
14129 if (!origin_child_die->in_process)
14130 process_die (origin_child_die, origin_cu);
14132 origin_child_die = sibling_die (origin_child_die);
14134 origin_cu->list_in_scope = origin_previous_list_in_scope;
14138 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
14140 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14141 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14142 struct context_stack *newobj;
14145 struct die_info *child_die;
14146 struct attribute *attr, *call_line, *call_file;
14148 CORE_ADDR baseaddr;
14149 struct block *block;
14150 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
14151 std::vector<struct symbol *> template_args;
14152 struct template_symbol *templ_func = NULL;
14156 /* If we do not have call site information, we can't show the
14157 caller of this inlined function. That's too confusing, so
14158 only use the scope for local variables. */
14159 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
14160 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
14161 if (call_line == NULL || call_file == NULL)
14163 read_lexical_block_scope (die, cu);
14168 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14170 name = dwarf2_name (die, cu);
14172 /* Ignore functions with missing or empty names. These are actually
14173 illegal according to the DWARF standard. */
14176 complaint (&symfile_complaints,
14177 _("missing name for subprogram DIE at %s"),
14178 sect_offset_str (die->sect_off));
14182 /* Ignore functions with missing or invalid low and high pc attributes. */
14183 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
14184 <= PC_BOUNDS_INVALID)
14186 attr = dwarf2_attr (die, DW_AT_external, cu);
14187 if (!attr || !DW_UNSND (attr))
14188 complaint (&symfile_complaints,
14189 _("cannot get low and high bounds "
14190 "for subprogram DIE at %s"),
14191 sect_offset_str (die->sect_off));
14195 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14196 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
14198 /* If we have any template arguments, then we must allocate a
14199 different sort of symbol. */
14200 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
14202 if (child_die->tag == DW_TAG_template_type_param
14203 || child_die->tag == DW_TAG_template_value_param)
14205 templ_func = allocate_template_symbol (objfile);
14206 templ_func->subclass = SYMBOL_TEMPLATE;
14211 newobj = push_context (0, lowpc);
14212 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
14213 (struct symbol *) templ_func);
14215 /* If there is a location expression for DW_AT_frame_base, record
14217 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
14219 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
14221 /* If there is a location for the static link, record it. */
14222 newobj->static_link = NULL;
14223 attr = dwarf2_attr (die, DW_AT_static_link, cu);
14226 newobj->static_link
14227 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
14228 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
14231 cu->list_in_scope = &local_symbols;
14233 if (die->child != NULL)
14235 child_die = die->child;
14236 while (child_die && child_die->tag)
14238 if (child_die->tag == DW_TAG_template_type_param
14239 || child_die->tag == DW_TAG_template_value_param)
14241 struct symbol *arg = new_symbol (child_die, NULL, cu);
14244 template_args.push_back (arg);
14247 process_die (child_die, cu);
14248 child_die = sibling_die (child_die);
14252 inherit_abstract_dies (die, cu);
14254 /* If we have a DW_AT_specification, we might need to import using
14255 directives from the context of the specification DIE. See the
14256 comment in determine_prefix. */
14257 if (cu->language == language_cplus
14258 && dwarf2_attr (die, DW_AT_specification, cu))
14260 struct dwarf2_cu *spec_cu = cu;
14261 struct die_info *spec_die = die_specification (die, &spec_cu);
14265 child_die = spec_die->child;
14266 while (child_die && child_die->tag)
14268 if (child_die->tag == DW_TAG_imported_module)
14269 process_die (child_die, spec_cu);
14270 child_die = sibling_die (child_die);
14273 /* In some cases, GCC generates specification DIEs that
14274 themselves contain DW_AT_specification attributes. */
14275 spec_die = die_specification (spec_die, &spec_cu);
14279 newobj = pop_context ();
14280 /* Make a block for the local symbols within. */
14281 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
14282 newobj->static_link, lowpc, highpc);
14284 /* For C++, set the block's scope. */
14285 if ((cu->language == language_cplus
14286 || cu->language == language_fortran
14287 || cu->language == language_d
14288 || cu->language == language_rust)
14289 && cu->processing_has_namespace_info)
14290 block_set_scope (block, determine_prefix (die, cu),
14291 &objfile->objfile_obstack);
14293 /* If we have address ranges, record them. */
14294 dwarf2_record_block_ranges (die, block, baseaddr, cu);
14296 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
14298 /* Attach template arguments to function. */
14299 if (!template_args.empty ())
14301 gdb_assert (templ_func != NULL);
14303 templ_func->n_template_arguments = template_args.size ();
14304 templ_func->template_arguments
14305 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
14306 templ_func->n_template_arguments);
14307 memcpy (templ_func->template_arguments,
14308 template_args.data (),
14309 (templ_func->n_template_arguments * sizeof (struct symbol *)));
14312 /* In C++, we can have functions nested inside functions (e.g., when
14313 a function declares a class that has methods). This means that
14314 when we finish processing a function scope, we may need to go
14315 back to building a containing block's symbol lists. */
14316 local_symbols = newobj->locals;
14317 local_using_directives = newobj->local_using_directives;
14319 /* If we've finished processing a top-level function, subsequent
14320 symbols go in the file symbol list. */
14321 if (outermost_context_p ())
14322 cu->list_in_scope = &file_symbols;
14325 /* Process all the DIES contained within a lexical block scope. Start
14326 a new scope, process the dies, and then close the scope. */
14329 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
14331 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14332 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14333 struct context_stack *newobj;
14334 CORE_ADDR lowpc, highpc;
14335 struct die_info *child_die;
14336 CORE_ADDR baseaddr;
14338 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14340 /* Ignore blocks with missing or invalid low and high pc attributes. */
14341 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
14342 as multiple lexical blocks? Handling children in a sane way would
14343 be nasty. Might be easier to properly extend generic blocks to
14344 describe ranges. */
14345 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
14347 case PC_BOUNDS_NOT_PRESENT:
14348 /* DW_TAG_lexical_block has no attributes, process its children as if
14349 there was no wrapping by that DW_TAG_lexical_block.
14350 GCC does no longer produces such DWARF since GCC r224161. */
14351 for (child_die = die->child;
14352 child_die != NULL && child_die->tag;
14353 child_die = sibling_die (child_die))
14354 process_die (child_die, cu);
14356 case PC_BOUNDS_INVALID:
14359 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14360 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
14362 push_context (0, lowpc);
14363 if (die->child != NULL)
14365 child_die = die->child;
14366 while (child_die && child_die->tag)
14368 process_die (child_die, cu);
14369 child_die = sibling_die (child_die);
14372 inherit_abstract_dies (die, cu);
14373 newobj = pop_context ();
14375 if (local_symbols != NULL || local_using_directives != NULL)
14377 struct block *block
14378 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
14379 newobj->start_addr, highpc);
14381 /* Note that recording ranges after traversing children, as we
14382 do here, means that recording a parent's ranges entails
14383 walking across all its children's ranges as they appear in
14384 the address map, which is quadratic behavior.
14386 It would be nicer to record the parent's ranges before
14387 traversing its children, simply overriding whatever you find
14388 there. But since we don't even decide whether to create a
14389 block until after we've traversed its children, that's hard
14391 dwarf2_record_block_ranges (die, block, baseaddr, cu);
14393 local_symbols = newobj->locals;
14394 local_using_directives = newobj->local_using_directives;
14397 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
14400 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
14402 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14403 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14404 CORE_ADDR pc, baseaddr;
14405 struct attribute *attr;
14406 struct call_site *call_site, call_site_local;
14409 struct die_info *child_die;
14411 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14413 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
14416 /* This was a pre-DWARF-5 GNU extension alias
14417 for DW_AT_call_return_pc. */
14418 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14422 complaint (&symfile_complaints,
14423 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
14424 "DIE %s [in module %s]"),
14425 sect_offset_str (die->sect_off), objfile_name (objfile));
14428 pc = attr_value_as_address (attr) + baseaddr;
14429 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
14431 if (cu->call_site_htab == NULL)
14432 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14433 NULL, &objfile->objfile_obstack,
14434 hashtab_obstack_allocate, NULL);
14435 call_site_local.pc = pc;
14436 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14439 complaint (&symfile_complaints,
14440 _("Duplicate PC %s for DW_TAG_call_site "
14441 "DIE %s [in module %s]"),
14442 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
14443 objfile_name (objfile));
14447 /* Count parameters at the caller. */
14450 for (child_die = die->child; child_die && child_die->tag;
14451 child_die = sibling_die (child_die))
14453 if (child_die->tag != DW_TAG_call_site_parameter
14454 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14456 complaint (&symfile_complaints,
14457 _("Tag %d is not DW_TAG_call_site_parameter in "
14458 "DW_TAG_call_site child DIE %s [in module %s]"),
14459 child_die->tag, sect_offset_str (child_die->sect_off),
14460 objfile_name (objfile));
14468 = ((struct call_site *)
14469 obstack_alloc (&objfile->objfile_obstack,
14470 sizeof (*call_site)
14471 + (sizeof (*call_site->parameter) * (nparams - 1))));
14473 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14474 call_site->pc = pc;
14476 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14477 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14479 struct die_info *func_die;
14481 /* Skip also over DW_TAG_inlined_subroutine. */
14482 for (func_die = die->parent;
14483 func_die && func_die->tag != DW_TAG_subprogram
14484 && func_die->tag != DW_TAG_subroutine_type;
14485 func_die = func_die->parent);
14487 /* DW_AT_call_all_calls is a superset
14488 of DW_AT_call_all_tail_calls. */
14490 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14491 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14492 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14493 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14495 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14496 not complete. But keep CALL_SITE for look ups via call_site_htab,
14497 both the initial caller containing the real return address PC and
14498 the final callee containing the current PC of a chain of tail
14499 calls do not need to have the tail call list complete. But any
14500 function candidate for a virtual tail call frame searched via
14501 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14502 determined unambiguously. */
14506 struct type *func_type = NULL;
14509 func_type = get_die_type (func_die, cu);
14510 if (func_type != NULL)
14512 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14514 /* Enlist this call site to the function. */
14515 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14516 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14519 complaint (&symfile_complaints,
14520 _("Cannot find function owning DW_TAG_call_site "
14521 "DIE %s [in module %s]"),
14522 sect_offset_str (die->sect_off), objfile_name (objfile));
14526 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14528 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14530 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14533 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14534 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14536 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14537 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14538 /* Keep NULL DWARF_BLOCK. */;
14539 else if (attr_form_is_block (attr))
14541 struct dwarf2_locexpr_baton *dlbaton;
14543 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14544 dlbaton->data = DW_BLOCK (attr)->data;
14545 dlbaton->size = DW_BLOCK (attr)->size;
14546 dlbaton->per_cu = cu->per_cu;
14548 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14550 else if (attr_form_is_ref (attr))
14552 struct dwarf2_cu *target_cu = cu;
14553 struct die_info *target_die;
14555 target_die = follow_die_ref (die, attr, &target_cu);
14556 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14557 if (die_is_declaration (target_die, target_cu))
14559 const char *target_physname;
14561 /* Prefer the mangled name; otherwise compute the demangled one. */
14562 target_physname = dw2_linkage_name (target_die, target_cu);
14563 if (target_physname == NULL)
14564 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14565 if (target_physname == NULL)
14566 complaint (&symfile_complaints,
14567 _("DW_AT_call_target target DIE has invalid "
14568 "physname, for referencing DIE %s [in module %s]"),
14569 sect_offset_str (die->sect_off), objfile_name (objfile));
14571 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14577 /* DW_AT_entry_pc should be preferred. */
14578 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14579 <= PC_BOUNDS_INVALID)
14580 complaint (&symfile_complaints,
14581 _("DW_AT_call_target target DIE has invalid "
14582 "low pc, for referencing DIE %s [in module %s]"),
14583 sect_offset_str (die->sect_off), objfile_name (objfile));
14586 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14587 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14592 complaint (&symfile_complaints,
14593 _("DW_TAG_call_site DW_AT_call_target is neither "
14594 "block nor reference, for DIE %s [in module %s]"),
14595 sect_offset_str (die->sect_off), objfile_name (objfile));
14597 call_site->per_cu = cu->per_cu;
14599 for (child_die = die->child;
14600 child_die && child_die->tag;
14601 child_die = sibling_die (child_die))
14603 struct call_site_parameter *parameter;
14604 struct attribute *loc, *origin;
14606 if (child_die->tag != DW_TAG_call_site_parameter
14607 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14609 /* Already printed the complaint above. */
14613 gdb_assert (call_site->parameter_count < nparams);
14614 parameter = &call_site->parameter[call_site->parameter_count];
14616 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14617 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14618 register is contained in DW_AT_call_value. */
14620 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14621 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14622 if (origin == NULL)
14624 /* This was a pre-DWARF-5 GNU extension alias
14625 for DW_AT_call_parameter. */
14626 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14628 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14630 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14632 sect_offset sect_off
14633 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14634 if (!offset_in_cu_p (&cu->header, sect_off))
14636 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14637 binding can be done only inside one CU. Such referenced DIE
14638 therefore cannot be even moved to DW_TAG_partial_unit. */
14639 complaint (&symfile_complaints,
14640 _("DW_AT_call_parameter offset is not in CU for "
14641 "DW_TAG_call_site child DIE %s [in module %s]"),
14642 sect_offset_str (child_die->sect_off),
14643 objfile_name (objfile));
14646 parameter->u.param_cu_off
14647 = (cu_offset) (sect_off - cu->header.sect_off);
14649 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14651 complaint (&symfile_complaints,
14652 _("No DW_FORM_block* DW_AT_location for "
14653 "DW_TAG_call_site child DIE %s [in module %s]"),
14654 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14659 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14660 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14661 if (parameter->u.dwarf_reg != -1)
14662 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14663 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14664 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14665 ¶meter->u.fb_offset))
14666 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14669 complaint (&symfile_complaints,
14670 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14671 "for DW_FORM_block* DW_AT_location is supported for "
14672 "DW_TAG_call_site child DIE %s "
14674 sect_offset_str (child_die->sect_off),
14675 objfile_name (objfile));
14680 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14682 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14683 if (!attr_form_is_block (attr))
14685 complaint (&symfile_complaints,
14686 _("No DW_FORM_block* DW_AT_call_value for "
14687 "DW_TAG_call_site child DIE %s [in module %s]"),
14688 sect_offset_str (child_die->sect_off),
14689 objfile_name (objfile));
14692 parameter->value = DW_BLOCK (attr)->data;
14693 parameter->value_size = DW_BLOCK (attr)->size;
14695 /* Parameters are not pre-cleared by memset above. */
14696 parameter->data_value = NULL;
14697 parameter->data_value_size = 0;
14698 call_site->parameter_count++;
14700 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14702 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14705 if (!attr_form_is_block (attr))
14706 complaint (&symfile_complaints,
14707 _("No DW_FORM_block* DW_AT_call_data_value for "
14708 "DW_TAG_call_site child DIE %s [in module %s]"),
14709 sect_offset_str (child_die->sect_off),
14710 objfile_name (objfile));
14713 parameter->data_value = DW_BLOCK (attr)->data;
14714 parameter->data_value_size = DW_BLOCK (attr)->size;
14720 /* Helper function for read_variable. If DIE represents a virtual
14721 table, then return the type of the concrete object that is
14722 associated with the virtual table. Otherwise, return NULL. */
14724 static struct type *
14725 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14727 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14731 /* Find the type DIE. */
14732 struct die_info *type_die = NULL;
14733 struct dwarf2_cu *type_cu = cu;
14735 if (attr_form_is_ref (attr))
14736 type_die = follow_die_ref (die, attr, &type_cu);
14737 if (type_die == NULL)
14740 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14742 return die_containing_type (type_die, type_cu);
14745 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14748 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14750 struct rust_vtable_symbol *storage = NULL;
14752 if (cu->language == language_rust)
14754 struct type *containing_type = rust_containing_type (die, cu);
14756 if (containing_type != NULL)
14758 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14760 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14761 struct rust_vtable_symbol);
14762 initialize_objfile_symbol (storage);
14763 storage->concrete_type = containing_type;
14764 storage->subclass = SYMBOL_RUST_VTABLE;
14768 new_symbol (die, NULL, cu, storage);
14771 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14772 reading .debug_rnglists.
14773 Callback's type should be:
14774 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14775 Return true if the attributes are present and valid, otherwise,
14778 template <typename Callback>
14780 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14781 Callback &&callback)
14783 struct dwarf2_per_objfile *dwarf2_per_objfile
14784 = cu->per_cu->dwarf2_per_objfile;
14785 struct objfile *objfile = dwarf2_per_objfile->objfile;
14786 bfd *obfd = objfile->obfd;
14787 /* Base address selection entry. */
14790 const gdb_byte *buffer;
14791 CORE_ADDR baseaddr;
14792 bool overflow = false;
14794 found_base = cu->base_known;
14795 base = cu->base_address;
14797 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14798 if (offset >= dwarf2_per_objfile->rnglists.size)
14800 complaint (&symfile_complaints,
14801 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14805 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14807 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14811 /* Initialize it due to a false compiler warning. */
14812 CORE_ADDR range_beginning = 0, range_end = 0;
14813 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14814 + dwarf2_per_objfile->rnglists.size);
14815 unsigned int bytes_read;
14817 if (buffer == buf_end)
14822 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14825 case DW_RLE_end_of_list:
14827 case DW_RLE_base_address:
14828 if (buffer + cu->header.addr_size > buf_end)
14833 base = read_address (obfd, buffer, cu, &bytes_read);
14835 buffer += bytes_read;
14837 case DW_RLE_start_length:
14838 if (buffer + cu->header.addr_size > buf_end)
14843 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14844 buffer += bytes_read;
14845 range_end = (range_beginning
14846 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14847 buffer += bytes_read;
14848 if (buffer > buf_end)
14854 case DW_RLE_offset_pair:
14855 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14856 buffer += bytes_read;
14857 if (buffer > buf_end)
14862 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14863 buffer += bytes_read;
14864 if (buffer > buf_end)
14870 case DW_RLE_start_end:
14871 if (buffer + 2 * cu->header.addr_size > buf_end)
14876 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14877 buffer += bytes_read;
14878 range_end = read_address (obfd, buffer, cu, &bytes_read);
14879 buffer += bytes_read;
14882 complaint (&symfile_complaints,
14883 _("Invalid .debug_rnglists data (no base address)"));
14886 if (rlet == DW_RLE_end_of_list || overflow)
14888 if (rlet == DW_RLE_base_address)
14893 /* We have no valid base address for the ranges
14895 complaint (&symfile_complaints,
14896 _("Invalid .debug_rnglists data (no base address)"));
14900 if (range_beginning > range_end)
14902 /* Inverted range entries are invalid. */
14903 complaint (&symfile_complaints,
14904 _("Invalid .debug_rnglists data (inverted range)"));
14908 /* Empty range entries have no effect. */
14909 if (range_beginning == range_end)
14912 range_beginning += base;
14915 /* A not-uncommon case of bad debug info.
14916 Don't pollute the addrmap with bad data. */
14917 if (range_beginning + baseaddr == 0
14918 && !dwarf2_per_objfile->has_section_at_zero)
14920 complaint (&symfile_complaints,
14921 _(".debug_rnglists entry has start address of zero"
14922 " [in module %s]"), objfile_name (objfile));
14926 callback (range_beginning, range_end);
14931 complaint (&symfile_complaints,
14932 _("Offset %d is not terminated "
14933 "for DW_AT_ranges attribute"),
14941 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14942 Callback's type should be:
14943 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14944 Return 1 if the attributes are present and valid, otherwise, return 0. */
14946 template <typename Callback>
14948 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14949 Callback &&callback)
14951 struct dwarf2_per_objfile *dwarf2_per_objfile
14952 = cu->per_cu->dwarf2_per_objfile;
14953 struct objfile *objfile = dwarf2_per_objfile->objfile;
14954 struct comp_unit_head *cu_header = &cu->header;
14955 bfd *obfd = objfile->obfd;
14956 unsigned int addr_size = cu_header->addr_size;
14957 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14958 /* Base address selection entry. */
14961 unsigned int dummy;
14962 const gdb_byte *buffer;
14963 CORE_ADDR baseaddr;
14965 if (cu_header->version >= 5)
14966 return dwarf2_rnglists_process (offset, cu, callback);
14968 found_base = cu->base_known;
14969 base = cu->base_address;
14971 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14972 if (offset >= dwarf2_per_objfile->ranges.size)
14974 complaint (&symfile_complaints,
14975 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14979 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14981 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14985 CORE_ADDR range_beginning, range_end;
14987 range_beginning = read_address (obfd, buffer, cu, &dummy);
14988 buffer += addr_size;
14989 range_end = read_address (obfd, buffer, cu, &dummy);
14990 buffer += addr_size;
14991 offset += 2 * addr_size;
14993 /* An end of list marker is a pair of zero addresses. */
14994 if (range_beginning == 0 && range_end == 0)
14995 /* Found the end of list entry. */
14998 /* Each base address selection entry is a pair of 2 values.
14999 The first is the largest possible address, the second is
15000 the base address. Check for a base address here. */
15001 if ((range_beginning & mask) == mask)
15003 /* If we found the largest possible address, then we already
15004 have the base address in range_end. */
15012 /* We have no valid base address for the ranges
15014 complaint (&symfile_complaints,
15015 _("Invalid .debug_ranges data (no base address)"));
15019 if (range_beginning > range_end)
15021 /* Inverted range entries are invalid. */
15022 complaint (&symfile_complaints,
15023 _("Invalid .debug_ranges data (inverted range)"));
15027 /* Empty range entries have no effect. */
15028 if (range_beginning == range_end)
15031 range_beginning += base;
15034 /* A not-uncommon case of bad debug info.
15035 Don't pollute the addrmap with bad data. */
15036 if (range_beginning + baseaddr == 0
15037 && !dwarf2_per_objfile->has_section_at_zero)
15039 complaint (&symfile_complaints,
15040 _(".debug_ranges entry has start address of zero"
15041 " [in module %s]"), objfile_name (objfile));
15045 callback (range_beginning, range_end);
15051 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
15052 Return 1 if the attributes are present and valid, otherwise, return 0.
15053 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
15056 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
15057 CORE_ADDR *high_return, struct dwarf2_cu *cu,
15058 struct partial_symtab *ranges_pst)
15060 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15061 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15062 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
15063 SECT_OFF_TEXT (objfile));
15066 CORE_ADDR high = 0;
15069 retval = dwarf2_ranges_process (offset, cu,
15070 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
15072 if (ranges_pst != NULL)
15077 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
15078 range_beginning + baseaddr);
15079 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
15080 range_end + baseaddr);
15081 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
15085 /* FIXME: This is recording everything as a low-high
15086 segment of consecutive addresses. We should have a
15087 data structure for discontiguous block ranges
15091 low = range_beginning;
15097 if (range_beginning < low)
15098 low = range_beginning;
15099 if (range_end > high)
15107 /* If the first entry is an end-of-list marker, the range
15108 describes an empty scope, i.e. no instructions. */
15114 *high_return = high;
15118 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
15119 definition for the return value. *LOWPC and *HIGHPC are set iff
15120 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
15122 static enum pc_bounds_kind
15123 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
15124 CORE_ADDR *highpc, struct dwarf2_cu *cu,
15125 struct partial_symtab *pst)
15127 struct dwarf2_per_objfile *dwarf2_per_objfile
15128 = cu->per_cu->dwarf2_per_objfile;
15129 struct attribute *attr;
15130 struct attribute *attr_high;
15132 CORE_ADDR high = 0;
15133 enum pc_bounds_kind ret;
15135 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
15138 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15141 low = attr_value_as_address (attr);
15142 high = attr_value_as_address (attr_high);
15143 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
15147 /* Found high w/o low attribute. */
15148 return PC_BOUNDS_INVALID;
15150 /* Found consecutive range of addresses. */
15151 ret = PC_BOUNDS_HIGH_LOW;
15155 attr = dwarf2_attr (die, DW_AT_ranges, cu);
15158 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
15159 We take advantage of the fact that DW_AT_ranges does not appear
15160 in DW_TAG_compile_unit of DWO files. */
15161 int need_ranges_base = die->tag != DW_TAG_compile_unit;
15162 unsigned int ranges_offset = (DW_UNSND (attr)
15163 + (need_ranges_base
15167 /* Value of the DW_AT_ranges attribute is the offset in the
15168 .debug_ranges section. */
15169 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
15170 return PC_BOUNDS_INVALID;
15171 /* Found discontinuous range of addresses. */
15172 ret = PC_BOUNDS_RANGES;
15175 return PC_BOUNDS_NOT_PRESENT;
15178 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
15180 return PC_BOUNDS_INVALID;
15182 /* When using the GNU linker, .gnu.linkonce. sections are used to
15183 eliminate duplicate copies of functions and vtables and such.
15184 The linker will arbitrarily choose one and discard the others.
15185 The AT_*_pc values for such functions refer to local labels in
15186 these sections. If the section from that file was discarded, the
15187 labels are not in the output, so the relocs get a value of 0.
15188 If this is a discarded function, mark the pc bounds as invalid,
15189 so that GDB will ignore it. */
15190 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
15191 return PC_BOUNDS_INVALID;
15199 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
15200 its low and high PC addresses. Do nothing if these addresses could not
15201 be determined. Otherwise, set LOWPC to the low address if it is smaller,
15202 and HIGHPC to the high address if greater than HIGHPC. */
15205 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
15206 CORE_ADDR *lowpc, CORE_ADDR *highpc,
15207 struct dwarf2_cu *cu)
15209 CORE_ADDR low, high;
15210 struct die_info *child = die->child;
15212 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
15214 *lowpc = std::min (*lowpc, low);
15215 *highpc = std::max (*highpc, high);
15218 /* If the language does not allow nested subprograms (either inside
15219 subprograms or lexical blocks), we're done. */
15220 if (cu->language != language_ada)
15223 /* Check all the children of the given DIE. If it contains nested
15224 subprograms, then check their pc bounds. Likewise, we need to
15225 check lexical blocks as well, as they may also contain subprogram
15227 while (child && child->tag)
15229 if (child->tag == DW_TAG_subprogram
15230 || child->tag == DW_TAG_lexical_block)
15231 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
15232 child = sibling_die (child);
15236 /* Get the low and high pc's represented by the scope DIE, and store
15237 them in *LOWPC and *HIGHPC. If the correct values can't be
15238 determined, set *LOWPC to -1 and *HIGHPC to 0. */
15241 get_scope_pc_bounds (struct die_info *die,
15242 CORE_ADDR *lowpc, CORE_ADDR *highpc,
15243 struct dwarf2_cu *cu)
15245 CORE_ADDR best_low = (CORE_ADDR) -1;
15246 CORE_ADDR best_high = (CORE_ADDR) 0;
15247 CORE_ADDR current_low, current_high;
15249 if (dwarf2_get_pc_bounds (die, ¤t_low, ¤t_high, cu, NULL)
15250 >= PC_BOUNDS_RANGES)
15252 best_low = current_low;
15253 best_high = current_high;
15257 struct die_info *child = die->child;
15259 while (child && child->tag)
15261 switch (child->tag) {
15262 case DW_TAG_subprogram:
15263 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
15265 case DW_TAG_namespace:
15266 case DW_TAG_module:
15267 /* FIXME: carlton/2004-01-16: Should we do this for
15268 DW_TAG_class_type/DW_TAG_structure_type, too? I think
15269 that current GCC's always emit the DIEs corresponding
15270 to definitions of methods of classes as children of a
15271 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
15272 the DIEs giving the declarations, which could be
15273 anywhere). But I don't see any reason why the
15274 standards says that they have to be there. */
15275 get_scope_pc_bounds (child, ¤t_low, ¤t_high, cu);
15277 if (current_low != ((CORE_ADDR) -1))
15279 best_low = std::min (best_low, current_low);
15280 best_high = std::max (best_high, current_high);
15288 child = sibling_die (child);
15293 *highpc = best_high;
15296 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
15300 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
15301 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
15303 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15304 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15305 struct attribute *attr;
15306 struct attribute *attr_high;
15308 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
15311 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15314 CORE_ADDR low = attr_value_as_address (attr);
15315 CORE_ADDR high = attr_value_as_address (attr_high);
15317 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
15320 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
15321 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
15322 record_block_range (block, low, high - 1);
15326 attr = dwarf2_attr (die, DW_AT_ranges, cu);
15329 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
15330 We take advantage of the fact that DW_AT_ranges does not appear
15331 in DW_TAG_compile_unit of DWO files. */
15332 int need_ranges_base = die->tag != DW_TAG_compile_unit;
15334 /* The value of the DW_AT_ranges attribute is the offset of the
15335 address range list in the .debug_ranges section. */
15336 unsigned long offset = (DW_UNSND (attr)
15337 + (need_ranges_base ? cu->ranges_base : 0));
15338 const gdb_byte *buffer;
15340 /* For some target architectures, but not others, the
15341 read_address function sign-extends the addresses it returns.
15342 To recognize base address selection entries, we need a
15344 unsigned int addr_size = cu->header.addr_size;
15345 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
15347 /* The base address, to which the next pair is relative. Note
15348 that this 'base' is a DWARF concept: most entries in a range
15349 list are relative, to reduce the number of relocs against the
15350 debugging information. This is separate from this function's
15351 'baseaddr' argument, which GDB uses to relocate debugging
15352 information from a shared library based on the address at
15353 which the library was loaded. */
15354 CORE_ADDR base = cu->base_address;
15355 int base_known = cu->base_known;
15357 dwarf2_ranges_process (offset, cu,
15358 [&] (CORE_ADDR start, CORE_ADDR end)
15362 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
15363 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
15364 record_block_range (block, start, end - 1);
15369 /* Check whether the producer field indicates either of GCC < 4.6, or the
15370 Intel C/C++ compiler, and cache the result in CU. */
15373 check_producer (struct dwarf2_cu *cu)
15377 if (cu->producer == NULL)
15379 /* For unknown compilers expect their behavior is DWARF version
15382 GCC started to support .debug_types sections by -gdwarf-4 since
15383 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
15384 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
15385 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
15386 interpreted incorrectly by GDB now - GCC PR debug/48229. */
15388 else if (producer_is_gcc (cu->producer, &major, &minor))
15390 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
15391 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
15393 else if (producer_is_icc (cu->producer, &major, &minor))
15394 cu->producer_is_icc_lt_14 = major < 14;
15397 /* For other non-GCC compilers, expect their behavior is DWARF version
15401 cu->checked_producer = 1;
15404 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
15405 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
15406 during 4.6.0 experimental. */
15409 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
15411 if (!cu->checked_producer)
15412 check_producer (cu);
15414 return cu->producer_is_gxx_lt_4_6;
15417 /* Return the default accessibility type if it is not overriden by
15418 DW_AT_accessibility. */
15420 static enum dwarf_access_attribute
15421 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15423 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15425 /* The default DWARF 2 accessibility for members is public, the default
15426 accessibility for inheritance is private. */
15428 if (die->tag != DW_TAG_inheritance)
15429 return DW_ACCESS_public;
15431 return DW_ACCESS_private;
15435 /* DWARF 3+ defines the default accessibility a different way. The same
15436 rules apply now for DW_TAG_inheritance as for the members and it only
15437 depends on the container kind. */
15439 if (die->parent->tag == DW_TAG_class_type)
15440 return DW_ACCESS_private;
15442 return DW_ACCESS_public;
15446 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15447 offset. If the attribute was not found return 0, otherwise return
15448 1. If it was found but could not properly be handled, set *OFFSET
15452 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15455 struct attribute *attr;
15457 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15462 /* Note that we do not check for a section offset first here.
15463 This is because DW_AT_data_member_location is new in DWARF 4,
15464 so if we see it, we can assume that a constant form is really
15465 a constant and not a section offset. */
15466 if (attr_form_is_constant (attr))
15467 *offset = dwarf2_get_attr_constant_value (attr, 0);
15468 else if (attr_form_is_section_offset (attr))
15469 dwarf2_complex_location_expr_complaint ();
15470 else if (attr_form_is_block (attr))
15471 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15473 dwarf2_complex_location_expr_complaint ();
15481 /* Add an aggregate field to the field list. */
15484 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15485 struct dwarf2_cu *cu)
15487 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15488 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15489 struct nextfield *new_field;
15490 struct attribute *attr;
15492 const char *fieldname = "";
15494 /* Allocate a new field list entry and link it in. */
15495 new_field = XNEW (struct nextfield);
15496 make_cleanup (xfree, new_field);
15497 memset (new_field, 0, sizeof (struct nextfield));
15499 if (die->tag == DW_TAG_inheritance)
15501 new_field->next = fip->baseclasses;
15502 fip->baseclasses = new_field;
15506 new_field->next = fip->fields;
15507 fip->fields = new_field;
15511 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15513 new_field->accessibility = DW_UNSND (attr);
15515 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15516 if (new_field->accessibility != DW_ACCESS_public)
15517 fip->non_public_fields = 1;
15519 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15521 new_field->virtuality = DW_UNSND (attr);
15523 new_field->virtuality = DW_VIRTUALITY_none;
15525 fp = &new_field->field;
15527 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15531 /* Data member other than a C++ static data member. */
15533 /* Get type of field. */
15534 fp->type = die_type (die, cu);
15536 SET_FIELD_BITPOS (*fp, 0);
15538 /* Get bit size of field (zero if none). */
15539 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15542 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15546 FIELD_BITSIZE (*fp) = 0;
15549 /* Get bit offset of field. */
15550 if (handle_data_member_location (die, cu, &offset))
15551 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15552 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15555 if (gdbarch_bits_big_endian (gdbarch))
15557 /* For big endian bits, the DW_AT_bit_offset gives the
15558 additional bit offset from the MSB of the containing
15559 anonymous object to the MSB of the field. We don't
15560 have to do anything special since we don't need to
15561 know the size of the anonymous object. */
15562 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15566 /* For little endian bits, compute the bit offset to the
15567 MSB of the anonymous object, subtract off the number of
15568 bits from the MSB of the field to the MSB of the
15569 object, and then subtract off the number of bits of
15570 the field itself. The result is the bit offset of
15571 the LSB of the field. */
15572 int anonymous_size;
15573 int bit_offset = DW_UNSND (attr);
15575 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15578 /* The size of the anonymous object containing
15579 the bit field is explicit, so use the
15580 indicated size (in bytes). */
15581 anonymous_size = DW_UNSND (attr);
15585 /* The size of the anonymous object containing
15586 the bit field must be inferred from the type
15587 attribute of the data member containing the
15589 anonymous_size = TYPE_LENGTH (fp->type);
15591 SET_FIELD_BITPOS (*fp,
15592 (FIELD_BITPOS (*fp)
15593 + anonymous_size * bits_per_byte
15594 - bit_offset - FIELD_BITSIZE (*fp)));
15597 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15599 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15600 + dwarf2_get_attr_constant_value (attr, 0)));
15602 /* Get name of field. */
15603 fieldname = dwarf2_name (die, cu);
15604 if (fieldname == NULL)
15607 /* The name is already allocated along with this objfile, so we don't
15608 need to duplicate it for the type. */
15609 fp->name = fieldname;
15611 /* Change accessibility for artificial fields (e.g. virtual table
15612 pointer or virtual base class pointer) to private. */
15613 if (dwarf2_attr (die, DW_AT_artificial, cu))
15615 FIELD_ARTIFICIAL (*fp) = 1;
15616 new_field->accessibility = DW_ACCESS_private;
15617 fip->non_public_fields = 1;
15620 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15622 /* C++ static member. */
15624 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15625 is a declaration, but all versions of G++ as of this writing
15626 (so through at least 3.2.1) incorrectly generate
15627 DW_TAG_variable tags. */
15629 const char *physname;
15631 /* Get name of field. */
15632 fieldname = dwarf2_name (die, cu);
15633 if (fieldname == NULL)
15636 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15638 /* Only create a symbol if this is an external value.
15639 new_symbol checks this and puts the value in the global symbol
15640 table, which we want. If it is not external, new_symbol
15641 will try to put the value in cu->list_in_scope which is wrong. */
15642 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15644 /* A static const member, not much different than an enum as far as
15645 we're concerned, except that we can support more types. */
15646 new_symbol (die, NULL, cu);
15649 /* Get physical name. */
15650 physname = dwarf2_physname (fieldname, die, cu);
15652 /* The name is already allocated along with this objfile, so we don't
15653 need to duplicate it for the type. */
15654 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15655 FIELD_TYPE (*fp) = die_type (die, cu);
15656 FIELD_NAME (*fp) = fieldname;
15658 else if (die->tag == DW_TAG_inheritance)
15662 /* C++ base class field. */
15663 if (handle_data_member_location (die, cu, &offset))
15664 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15665 FIELD_BITSIZE (*fp) = 0;
15666 FIELD_TYPE (*fp) = die_type (die, cu);
15667 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15668 fip->nbaseclasses++;
15670 else if (die->tag == DW_TAG_variant_part)
15672 /* process_structure_scope will treat this DIE as a union. */
15673 process_structure_scope (die, cu);
15675 /* The variant part is relative to the start of the enclosing
15677 SET_FIELD_BITPOS (*fp, 0);
15678 fp->type = get_die_type (die, cu);
15679 fp->artificial = 1;
15680 fp->name = "<<variant>>";
15683 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15686 /* Can the type given by DIE define another type? */
15689 type_can_define_types (const struct die_info *die)
15693 case DW_TAG_typedef:
15694 case DW_TAG_class_type:
15695 case DW_TAG_structure_type:
15696 case DW_TAG_union_type:
15697 case DW_TAG_enumeration_type:
15705 /* Add a type definition defined in the scope of the FIP's class. */
15708 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15709 struct dwarf2_cu *cu)
15711 struct decl_field_list *new_field;
15712 struct decl_field *fp;
15714 /* Allocate a new field list entry and link it in. */
15715 new_field = XCNEW (struct decl_field_list);
15716 make_cleanup (xfree, new_field);
15718 gdb_assert (type_can_define_types (die));
15720 fp = &new_field->field;
15722 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15723 fp->name = dwarf2_name (die, cu);
15724 fp->type = read_type_die (die, cu);
15726 /* Save accessibility. */
15727 enum dwarf_access_attribute accessibility;
15728 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15730 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15732 accessibility = dwarf2_default_access_attribute (die, cu);
15733 switch (accessibility)
15735 case DW_ACCESS_public:
15736 /* The assumed value if neither private nor protected. */
15738 case DW_ACCESS_private:
15739 fp->is_private = 1;
15741 case DW_ACCESS_protected:
15742 fp->is_protected = 1;
15745 complaint (&symfile_complaints,
15746 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15749 if (die->tag == DW_TAG_typedef)
15751 new_field->next = fip->typedef_field_list;
15752 fip->typedef_field_list = new_field;
15753 fip->typedef_field_list_count++;
15757 new_field->next = fip->nested_types_list;
15758 fip->nested_types_list = new_field;
15759 fip->nested_types_list_count++;
15763 /* Create the vector of fields, and attach it to the type. */
15766 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15767 struct dwarf2_cu *cu)
15769 int nfields = fip->nfields;
15771 /* Record the field count, allocate space for the array of fields,
15772 and create blank accessibility bitfields if necessary. */
15773 TYPE_NFIELDS (type) = nfields;
15774 TYPE_FIELDS (type) = (struct field *)
15775 TYPE_ALLOC (type, sizeof (struct field) * nfields);
15776 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
15778 if (fip->non_public_fields && cu->language != language_ada)
15780 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15782 TYPE_FIELD_PRIVATE_BITS (type) =
15783 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15784 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15786 TYPE_FIELD_PROTECTED_BITS (type) =
15787 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15788 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15790 TYPE_FIELD_IGNORE_BITS (type) =
15791 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15792 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15795 /* If the type has baseclasses, allocate and clear a bit vector for
15796 TYPE_FIELD_VIRTUAL_BITS. */
15797 if (fip->nbaseclasses && cu->language != language_ada)
15799 int num_bytes = B_BYTES (fip->nbaseclasses);
15800 unsigned char *pointer;
15802 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15803 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15804 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15805 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
15806 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
15809 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15811 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15813 int index = nfields - 1;
15814 struct nextfield *field = fip->fields;
15818 if (field->variant.is_discriminant)
15819 di->discriminant_index = index;
15820 else if (field->variant.default_branch)
15821 di->default_index = index;
15823 di->discriminants[index] = field->variant.discriminant_value;
15826 field = field->next;
15830 /* Copy the saved-up fields into the field vector. Start from the head of
15831 the list, adding to the tail of the field array, so that they end up in
15832 the same order in the array in which they were added to the list. */
15833 while (nfields-- > 0)
15835 struct nextfield *fieldp;
15839 fieldp = fip->fields;
15840 fip->fields = fieldp->next;
15844 fieldp = fip->baseclasses;
15845 fip->baseclasses = fieldp->next;
15848 TYPE_FIELD (type, nfields) = fieldp->field;
15849 switch (fieldp->accessibility)
15851 case DW_ACCESS_private:
15852 if (cu->language != language_ada)
15853 SET_TYPE_FIELD_PRIVATE (type, nfields);
15856 case DW_ACCESS_protected:
15857 if (cu->language != language_ada)
15858 SET_TYPE_FIELD_PROTECTED (type, nfields);
15861 case DW_ACCESS_public:
15865 /* Unknown accessibility. Complain and treat it as public. */
15867 complaint (&symfile_complaints, _("unsupported accessibility %d"),
15868 fieldp->accessibility);
15872 if (nfields < fip->nbaseclasses)
15874 switch (fieldp->virtuality)
15876 case DW_VIRTUALITY_virtual:
15877 case DW_VIRTUALITY_pure_virtual:
15878 if (cu->language == language_ada)
15879 error (_("unexpected virtuality in component of Ada type"));
15880 SET_TYPE_FIELD_VIRTUAL (type, nfields);
15887 /* Return true if this member function is a constructor, false
15891 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15893 const char *fieldname;
15894 const char *type_name;
15897 if (die->parent == NULL)
15900 if (die->parent->tag != DW_TAG_structure_type
15901 && die->parent->tag != DW_TAG_union_type
15902 && die->parent->tag != DW_TAG_class_type)
15905 fieldname = dwarf2_name (die, cu);
15906 type_name = dwarf2_name (die->parent, cu);
15907 if (fieldname == NULL || type_name == NULL)
15910 len = strlen (fieldname);
15911 return (strncmp (fieldname, type_name, len) == 0
15912 && (type_name[len] == '\0' || type_name[len] == '<'));
15915 /* Add a member function to the proper fieldlist. */
15918 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15919 struct type *type, struct dwarf2_cu *cu)
15921 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15922 struct attribute *attr;
15923 struct fnfieldlist *flp;
15925 struct fn_field *fnp;
15926 const char *fieldname;
15927 struct nextfnfield *new_fnfield;
15928 struct type *this_type;
15929 enum dwarf_access_attribute accessibility;
15931 if (cu->language == language_ada)
15932 error (_("unexpected member function in Ada type"));
15934 /* Get name of member function. */
15935 fieldname = dwarf2_name (die, cu);
15936 if (fieldname == NULL)
15939 /* Look up member function name in fieldlist. */
15940 for (i = 0; i < fip->nfnfields; i++)
15942 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15946 /* Create new list element if necessary. */
15947 if (i < fip->nfnfields)
15948 flp = &fip->fnfieldlists[i];
15951 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
15953 fip->fnfieldlists = (struct fnfieldlist *)
15954 xrealloc (fip->fnfieldlists,
15955 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
15956 * sizeof (struct fnfieldlist));
15957 if (fip->nfnfields == 0)
15958 make_cleanup (free_current_contents, &fip->fnfieldlists);
15960 flp = &fip->fnfieldlists[fip->nfnfields];
15961 flp->name = fieldname;
15964 i = fip->nfnfields++;
15967 /* Create a new member function field and chain it to the field list
15969 new_fnfield = XNEW (struct nextfnfield);
15970 make_cleanup (xfree, new_fnfield);
15971 memset (new_fnfield, 0, sizeof (struct nextfnfield));
15972 new_fnfield->next = flp->head;
15973 flp->head = new_fnfield;
15976 /* Fill in the member function field info. */
15977 fnp = &new_fnfield->fnfield;
15979 /* Delay processing of the physname until later. */
15980 if (cu->language == language_cplus)
15982 add_to_method_list (type, i, flp->length - 1, fieldname,
15987 const char *physname = dwarf2_physname (fieldname, die, cu);
15988 fnp->physname = physname ? physname : "";
15991 fnp->type = alloc_type (objfile);
15992 this_type = read_type_die (die, cu);
15993 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15995 int nparams = TYPE_NFIELDS (this_type);
15997 /* TYPE is the domain of this method, and THIS_TYPE is the type
15998 of the method itself (TYPE_CODE_METHOD). */
15999 smash_to_method_type (fnp->type, type,
16000 TYPE_TARGET_TYPE (this_type),
16001 TYPE_FIELDS (this_type),
16002 TYPE_NFIELDS (this_type),
16003 TYPE_VARARGS (this_type));
16005 /* Handle static member functions.
16006 Dwarf2 has no clean way to discern C++ static and non-static
16007 member functions. G++ helps GDB by marking the first
16008 parameter for non-static member functions (which is the this
16009 pointer) as artificial. We obtain this information from
16010 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
16011 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
16012 fnp->voffset = VOFFSET_STATIC;
16015 complaint (&symfile_complaints, _("member function type missing for '%s'"),
16016 dwarf2_full_name (fieldname, die, cu));
16018 /* Get fcontext from DW_AT_containing_type if present. */
16019 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16020 fnp->fcontext = die_containing_type (die, cu);
16022 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
16023 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
16025 /* Get accessibility. */
16026 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
16028 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
16030 accessibility = dwarf2_default_access_attribute (die, cu);
16031 switch (accessibility)
16033 case DW_ACCESS_private:
16034 fnp->is_private = 1;
16036 case DW_ACCESS_protected:
16037 fnp->is_protected = 1;
16041 /* Check for artificial methods. */
16042 attr = dwarf2_attr (die, DW_AT_artificial, cu);
16043 if (attr && DW_UNSND (attr) != 0)
16044 fnp->is_artificial = 1;
16046 fnp->is_constructor = dwarf2_is_constructor (die, cu);
16048 /* Get index in virtual function table if it is a virtual member
16049 function. For older versions of GCC, this is an offset in the
16050 appropriate virtual table, as specified by DW_AT_containing_type.
16051 For everyone else, it is an expression to be evaluated relative
16052 to the object address. */
16054 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
16057 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
16059 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
16061 /* Old-style GCC. */
16062 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
16064 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
16065 || (DW_BLOCK (attr)->size > 1
16066 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
16067 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
16069 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
16070 if ((fnp->voffset % cu->header.addr_size) != 0)
16071 dwarf2_complex_location_expr_complaint ();
16073 fnp->voffset /= cu->header.addr_size;
16077 dwarf2_complex_location_expr_complaint ();
16079 if (!fnp->fcontext)
16081 /* If there is no `this' field and no DW_AT_containing_type,
16082 we cannot actually find a base class context for the
16084 if (TYPE_NFIELDS (this_type) == 0
16085 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
16087 complaint (&symfile_complaints,
16088 _("cannot determine context for virtual member "
16089 "function \"%s\" (offset %s)"),
16090 fieldname, sect_offset_str (die->sect_off));
16095 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
16099 else if (attr_form_is_section_offset (attr))
16101 dwarf2_complex_location_expr_complaint ();
16105 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
16111 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
16112 if (attr && DW_UNSND (attr))
16114 /* GCC does this, as of 2008-08-25; PR debug/37237. */
16115 complaint (&symfile_complaints,
16116 _("Member function \"%s\" (offset %s) is virtual "
16117 "but the vtable offset is not specified"),
16118 fieldname, sect_offset_str (die->sect_off));
16119 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16120 TYPE_CPLUS_DYNAMIC (type) = 1;
16125 /* Create the vector of member function fields, and attach it to the type. */
16128 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
16129 struct dwarf2_cu *cu)
16131 struct fnfieldlist *flp;
16134 if (cu->language == language_ada)
16135 error (_("unexpected member functions in Ada type"));
16137 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16138 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
16139 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
16141 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
16143 struct nextfnfield *nfp = flp->head;
16144 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
16147 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
16148 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
16149 fn_flp->fn_fields = (struct fn_field *)
16150 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
16151 for (k = flp->length; (k--, nfp); nfp = nfp->next)
16152 fn_flp->fn_fields[k] = nfp->fnfield;
16155 TYPE_NFN_FIELDS (type) = fip->nfnfields;
16158 /* Returns non-zero if NAME is the name of a vtable member in CU's
16159 language, zero otherwise. */
16161 is_vtable_name (const char *name, struct dwarf2_cu *cu)
16163 static const char vptr[] = "_vptr";
16165 /* Look for the C++ form of the vtable. */
16166 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
16172 /* GCC outputs unnamed structures that are really pointers to member
16173 functions, with the ABI-specified layout. If TYPE describes
16174 such a structure, smash it into a member function type.
16176 GCC shouldn't do this; it should just output pointer to member DIEs.
16177 This is GCC PR debug/28767. */
16180 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
16182 struct type *pfn_type, *self_type, *new_type;
16184 /* Check for a structure with no name and two children. */
16185 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
16188 /* Check for __pfn and __delta members. */
16189 if (TYPE_FIELD_NAME (type, 0) == NULL
16190 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
16191 || TYPE_FIELD_NAME (type, 1) == NULL
16192 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
16195 /* Find the type of the method. */
16196 pfn_type = TYPE_FIELD_TYPE (type, 0);
16197 if (pfn_type == NULL
16198 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
16199 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
16202 /* Look for the "this" argument. */
16203 pfn_type = TYPE_TARGET_TYPE (pfn_type);
16204 if (TYPE_NFIELDS (pfn_type) == 0
16205 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
16206 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
16209 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
16210 new_type = alloc_type (objfile);
16211 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
16212 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
16213 TYPE_VARARGS (pfn_type));
16214 smash_to_methodptr_type (type, new_type);
16218 /* Called when we find the DIE that starts a structure or union scope
16219 (definition) to create a type for the structure or union. Fill in
16220 the type's name and general properties; the members will not be
16221 processed until process_structure_scope. A symbol table entry for
16222 the type will also not be done until process_structure_scope (assuming
16223 the type has a name).
16225 NOTE: we need to call these functions regardless of whether or not the
16226 DIE has a DW_AT_name attribute, since it might be an anonymous
16227 structure or union. This gets the type entered into our set of
16228 user defined types. */
16230 static struct type *
16231 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
16233 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16235 struct attribute *attr;
16238 /* If the definition of this type lives in .debug_types, read that type.
16239 Don't follow DW_AT_specification though, that will take us back up
16240 the chain and we want to go down. */
16241 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16244 type = get_DW_AT_signature_type (die, attr, cu);
16246 /* The type's CU may not be the same as CU.
16247 Ensure TYPE is recorded with CU in die_type_hash. */
16248 return set_die_type (die, type, cu);
16251 type = alloc_type (objfile);
16252 INIT_CPLUS_SPECIFIC (type);
16254 name = dwarf2_name (die, cu);
16257 if (cu->language == language_cplus
16258 || cu->language == language_d
16259 || cu->language == language_rust)
16261 const char *full_name = dwarf2_full_name (name, die, cu);
16263 /* dwarf2_full_name might have already finished building the DIE's
16264 type. If so, there is no need to continue. */
16265 if (get_die_type (die, cu) != NULL)
16266 return get_die_type (die, cu);
16268 TYPE_TAG_NAME (type) = full_name;
16269 if (die->tag == DW_TAG_structure_type
16270 || die->tag == DW_TAG_class_type)
16271 TYPE_NAME (type) = TYPE_TAG_NAME (type);
16275 /* The name is already allocated along with this objfile, so
16276 we don't need to duplicate it for the type. */
16277 TYPE_TAG_NAME (type) = name;
16278 if (die->tag == DW_TAG_class_type)
16279 TYPE_NAME (type) = TYPE_TAG_NAME (type);
16283 if (die->tag == DW_TAG_structure_type)
16285 TYPE_CODE (type) = TYPE_CODE_STRUCT;
16287 else if (die->tag == DW_TAG_union_type)
16289 TYPE_CODE (type) = TYPE_CODE_UNION;
16291 else if (die->tag == DW_TAG_variant_part)
16293 TYPE_CODE (type) = TYPE_CODE_UNION;
16294 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
16298 TYPE_CODE (type) = TYPE_CODE_STRUCT;
16301 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16302 TYPE_DECLARED_CLASS (type) = 1;
16304 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16307 if (attr_form_is_constant (attr))
16308 TYPE_LENGTH (type) = DW_UNSND (attr);
16311 /* For the moment, dynamic type sizes are not supported
16312 by GDB's struct type. The actual size is determined
16313 on-demand when resolving the type of a given object,
16314 so set the type's length to zero for now. Otherwise,
16315 we record an expression as the length, and that expression
16316 could lead to a very large value, which could eventually
16317 lead to us trying to allocate that much memory when creating
16318 a value of that type. */
16319 TYPE_LENGTH (type) = 0;
16324 TYPE_LENGTH (type) = 0;
16327 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16329 /* ICC<14 does not output the required DW_AT_declaration on
16330 incomplete types, but gives them a size of zero. */
16331 TYPE_STUB (type) = 1;
16334 TYPE_STUB_SUPPORTED (type) = 1;
16336 if (die_is_declaration (die, cu))
16337 TYPE_STUB (type) = 1;
16338 else if (attr == NULL && die->child == NULL
16339 && producer_is_realview (cu->producer))
16340 /* RealView does not output the required DW_AT_declaration
16341 on incomplete types. */
16342 TYPE_STUB (type) = 1;
16344 /* We need to add the type field to the die immediately so we don't
16345 infinitely recurse when dealing with pointers to the structure
16346 type within the structure itself. */
16347 set_die_type (die, type, cu);
16349 /* set_die_type should be already done. */
16350 set_descriptive_type (type, die, cu);
16355 /* A helper for process_structure_scope that handles a single member
16359 handle_struct_member_die (struct die_info *child_die, struct type *type,
16360 struct field_info *fi,
16361 std::vector<struct symbol *> *template_args,
16362 struct dwarf2_cu *cu)
16364 if (child_die->tag == DW_TAG_member
16365 || child_die->tag == DW_TAG_variable
16366 || child_die->tag == DW_TAG_variant_part)
16368 /* NOTE: carlton/2002-11-05: A C++ static data member
16369 should be a DW_TAG_member that is a declaration, but
16370 all versions of G++ as of this writing (so through at
16371 least 3.2.1) incorrectly generate DW_TAG_variable
16372 tags for them instead. */
16373 dwarf2_add_field (fi, child_die, cu);
16375 else if (child_die->tag == DW_TAG_subprogram)
16377 /* Rust doesn't have member functions in the C++ sense.
16378 However, it does emit ordinary functions as children
16379 of a struct DIE. */
16380 if (cu->language == language_rust)
16381 read_func_scope (child_die, cu);
16384 /* C++ member function. */
16385 dwarf2_add_member_fn (fi, child_die, type, cu);
16388 else if (child_die->tag == DW_TAG_inheritance)
16390 /* C++ base class field. */
16391 dwarf2_add_field (fi, child_die, cu);
16393 else if (type_can_define_types (child_die))
16394 dwarf2_add_type_defn (fi, child_die, cu);
16395 else if (child_die->tag == DW_TAG_template_type_param
16396 || child_die->tag == DW_TAG_template_value_param)
16398 struct symbol *arg = new_symbol (child_die, NULL, cu);
16401 template_args->push_back (arg);
16403 else if (child_die->tag == DW_TAG_variant)
16405 /* In a variant we want to get the discriminant and also add a
16406 field for our sole member child. */
16407 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16409 for (struct die_info *variant_child = child_die->child;
16410 variant_child != NULL;
16411 variant_child = sibling_die (variant_child))
16413 if (variant_child->tag == DW_TAG_member)
16415 handle_struct_member_die (variant_child, type, fi,
16416 template_args, cu);
16417 /* Only handle the one. */
16422 /* We don't handle this but we might as well report it if we see
16424 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
16425 complaint (&symfile_complaints,
16426 _("DW_AT_discr_list is not supported yet"
16427 " - DIE at %s [in module %s]"),
16428 sect_offset_str (child_die->sect_off),
16429 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16431 /* The first field was just added, so we can stash the
16432 discriminant there. */
16433 gdb_assert (fi->fields != NULL);
16435 fi->fields->variant.default_branch = true;
16437 fi->fields->variant.discriminant_value = DW_UNSND (discr);
16441 /* Finish creating a structure or union type, including filling in
16442 its members and creating a symbol for it. */
16445 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16447 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16448 struct die_info *child_die;
16451 type = get_die_type (die, cu);
16453 type = read_structure_type (die, cu);
16455 /* When reading a DW_TAG_variant_part, we need to notice when we
16456 read the discriminant member, so we can record it later in the
16457 discriminant_info. */
16458 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16459 sect_offset discr_offset;
16461 if (is_variant_part)
16463 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16466 /* Maybe it's a univariant form, an extension we support.
16467 In this case arrange not to check the offset. */
16468 is_variant_part = false;
16470 else if (attr_form_is_ref (discr))
16472 struct dwarf2_cu *target_cu = cu;
16473 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16475 discr_offset = target_die->sect_off;
16479 complaint (&symfile_complaints,
16480 _("DW_AT_discr does not have DIE reference form"
16481 " - DIE at %s [in module %s]"),
16482 sect_offset_str (die->sect_off),
16483 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16484 is_variant_part = false;
16488 if (die->child != NULL && ! die_is_declaration (die, cu))
16490 struct field_info fi;
16491 std::vector<struct symbol *> template_args;
16492 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
16494 memset (&fi, 0, sizeof (struct field_info));
16496 child_die = die->child;
16498 while (child_die && child_die->tag)
16500 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16502 if (is_variant_part && discr_offset == child_die->sect_off)
16503 fi.fields->variant.is_discriminant = true;
16505 child_die = sibling_die (child_die);
16508 /* Attach template arguments to type. */
16509 if (!template_args.empty ())
16511 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16512 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16513 TYPE_TEMPLATE_ARGUMENTS (type)
16514 = XOBNEWVEC (&objfile->objfile_obstack,
16516 TYPE_N_TEMPLATE_ARGUMENTS (type));
16517 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16518 template_args.data (),
16519 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16520 * sizeof (struct symbol *)));
16523 /* Attach fields and member functions to the type. */
16525 dwarf2_attach_fields_to_type (&fi, type, cu);
16528 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16530 /* Get the type which refers to the base class (possibly this
16531 class itself) which contains the vtable pointer for the current
16532 class from the DW_AT_containing_type attribute. This use of
16533 DW_AT_containing_type is a GNU extension. */
16535 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16537 struct type *t = die_containing_type (die, cu);
16539 set_type_vptr_basetype (type, t);
16544 /* Our own class provides vtbl ptr. */
16545 for (i = TYPE_NFIELDS (t) - 1;
16546 i >= TYPE_N_BASECLASSES (t);
16549 const char *fieldname = TYPE_FIELD_NAME (t, i);
16551 if (is_vtable_name (fieldname, cu))
16553 set_type_vptr_fieldno (type, i);
16558 /* Complain if virtual function table field not found. */
16559 if (i < TYPE_N_BASECLASSES (t))
16560 complaint (&symfile_complaints,
16561 _("virtual function table pointer "
16562 "not found when defining class '%s'"),
16563 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16568 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16571 else if (cu->producer
16572 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16574 /* The IBM XLC compiler does not provide direct indication
16575 of the containing type, but the vtable pointer is
16576 always named __vfp. */
16580 for (i = TYPE_NFIELDS (type) - 1;
16581 i >= TYPE_N_BASECLASSES (type);
16584 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16586 set_type_vptr_fieldno (type, i);
16587 set_type_vptr_basetype (type, type);
16594 /* Copy fi.typedef_field_list linked list elements content into the
16595 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16596 if (fi.typedef_field_list)
16598 int i = fi.typedef_field_list_count;
16600 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16601 TYPE_TYPEDEF_FIELD_ARRAY (type)
16602 = ((struct decl_field *)
16603 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
16604 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
16606 /* Reverse the list order to keep the debug info elements order. */
16609 struct decl_field *dest, *src;
16611 dest = &TYPE_TYPEDEF_FIELD (type, i);
16612 src = &fi.typedef_field_list->field;
16613 fi.typedef_field_list = fi.typedef_field_list->next;
16618 /* Copy fi.nested_types_list linked list elements content into the
16619 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16620 if (fi.nested_types_list != NULL && cu->language != language_ada)
16622 int i = fi.nested_types_list_count;
16624 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16625 TYPE_NESTED_TYPES_ARRAY (type)
16626 = ((struct decl_field *)
16627 TYPE_ALLOC (type, sizeof (struct decl_field) * i));
16628 TYPE_NESTED_TYPES_COUNT (type) = i;
16630 /* Reverse the list order to keep the debug info elements order. */
16633 struct decl_field *dest, *src;
16635 dest = &TYPE_NESTED_TYPES_FIELD (type, i);
16636 src = &fi.nested_types_list->field;
16637 fi.nested_types_list = fi.nested_types_list->next;
16642 do_cleanups (back_to);
16645 quirk_gcc_member_function_pointer (type, objfile);
16646 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16647 cu->rust_unions.push_back (type);
16649 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16650 snapshots) has been known to create a die giving a declaration
16651 for a class that has, as a child, a die giving a definition for a
16652 nested class. So we have to process our children even if the
16653 current die is a declaration. Normally, of course, a declaration
16654 won't have any children at all. */
16656 child_die = die->child;
16658 while (child_die != NULL && child_die->tag)
16660 if (child_die->tag == DW_TAG_member
16661 || child_die->tag == DW_TAG_variable
16662 || child_die->tag == DW_TAG_inheritance
16663 || child_die->tag == DW_TAG_template_value_param
16664 || child_die->tag == DW_TAG_template_type_param)
16669 process_die (child_die, cu);
16671 child_die = sibling_die (child_die);
16674 /* Do not consider external references. According to the DWARF standard,
16675 these DIEs are identified by the fact that they have no byte_size
16676 attribute, and a declaration attribute. */
16677 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16678 || !die_is_declaration (die, cu))
16679 new_symbol (die, type, cu);
16682 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16683 update TYPE using some information only available in DIE's children. */
16686 update_enumeration_type_from_children (struct die_info *die,
16688 struct dwarf2_cu *cu)
16690 struct die_info *child_die;
16691 int unsigned_enum = 1;
16695 auto_obstack obstack;
16697 for (child_die = die->child;
16698 child_die != NULL && child_die->tag;
16699 child_die = sibling_die (child_die))
16701 struct attribute *attr;
16703 const gdb_byte *bytes;
16704 struct dwarf2_locexpr_baton *baton;
16707 if (child_die->tag != DW_TAG_enumerator)
16710 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16714 name = dwarf2_name (child_die, cu);
16716 name = "<anonymous enumerator>";
16718 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16719 &value, &bytes, &baton);
16725 else if ((mask & value) != 0)
16730 /* If we already know that the enum type is neither unsigned, nor
16731 a flag type, no need to look at the rest of the enumerates. */
16732 if (!unsigned_enum && !flag_enum)
16737 TYPE_UNSIGNED (type) = 1;
16739 TYPE_FLAG_ENUM (type) = 1;
16742 /* Given a DW_AT_enumeration_type die, set its type. We do not
16743 complete the type's fields yet, or create any symbols. */
16745 static struct type *
16746 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16748 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16750 struct attribute *attr;
16753 /* If the definition of this type lives in .debug_types, read that type.
16754 Don't follow DW_AT_specification though, that will take us back up
16755 the chain and we want to go down. */
16756 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16759 type = get_DW_AT_signature_type (die, attr, cu);
16761 /* The type's CU may not be the same as CU.
16762 Ensure TYPE is recorded with CU in die_type_hash. */
16763 return set_die_type (die, type, cu);
16766 type = alloc_type (objfile);
16768 TYPE_CODE (type) = TYPE_CODE_ENUM;
16769 name = dwarf2_full_name (NULL, die, cu);
16771 TYPE_TAG_NAME (type) = name;
16773 attr = dwarf2_attr (die, DW_AT_type, cu);
16776 struct type *underlying_type = die_type (die, cu);
16778 TYPE_TARGET_TYPE (type) = underlying_type;
16781 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16784 TYPE_LENGTH (type) = DW_UNSND (attr);
16788 TYPE_LENGTH (type) = 0;
16791 /* The enumeration DIE can be incomplete. In Ada, any type can be
16792 declared as private in the package spec, and then defined only
16793 inside the package body. Such types are known as Taft Amendment
16794 Types. When another package uses such a type, an incomplete DIE
16795 may be generated by the compiler. */
16796 if (die_is_declaration (die, cu))
16797 TYPE_STUB (type) = 1;
16799 /* Finish the creation of this type by using the enum's children.
16800 We must call this even when the underlying type has been provided
16801 so that we can determine if we're looking at a "flag" enum. */
16802 update_enumeration_type_from_children (die, type, cu);
16804 /* If this type has an underlying type that is not a stub, then we
16805 may use its attributes. We always use the "unsigned" attribute
16806 in this situation, because ordinarily we guess whether the type
16807 is unsigned -- but the guess can be wrong and the underlying type
16808 can tell us the reality. However, we defer to a local size
16809 attribute if one exists, because this lets the compiler override
16810 the underlying type if needed. */
16811 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16813 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16814 if (TYPE_LENGTH (type) == 0)
16815 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16818 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16820 return set_die_type (die, type, cu);
16823 /* Given a pointer to a die which begins an enumeration, process all
16824 the dies that define the members of the enumeration, and create the
16825 symbol for the enumeration type.
16827 NOTE: We reverse the order of the element list. */
16830 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16832 struct type *this_type;
16834 this_type = get_die_type (die, cu);
16835 if (this_type == NULL)
16836 this_type = read_enumeration_type (die, cu);
16838 if (die->child != NULL)
16840 struct die_info *child_die;
16841 struct symbol *sym;
16842 struct field *fields = NULL;
16843 int num_fields = 0;
16846 child_die = die->child;
16847 while (child_die && child_die->tag)
16849 if (child_die->tag != DW_TAG_enumerator)
16851 process_die (child_die, cu);
16855 name = dwarf2_name (child_die, cu);
16858 sym = new_symbol (child_die, this_type, cu);
16860 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16862 fields = (struct field *)
16864 (num_fields + DW_FIELD_ALLOC_CHUNK)
16865 * sizeof (struct field));
16868 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16869 FIELD_TYPE (fields[num_fields]) = NULL;
16870 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16871 FIELD_BITSIZE (fields[num_fields]) = 0;
16877 child_die = sibling_die (child_die);
16882 TYPE_NFIELDS (this_type) = num_fields;
16883 TYPE_FIELDS (this_type) = (struct field *)
16884 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16885 memcpy (TYPE_FIELDS (this_type), fields,
16886 sizeof (struct field) * num_fields);
16891 /* If we are reading an enum from a .debug_types unit, and the enum
16892 is a declaration, and the enum is not the signatured type in the
16893 unit, then we do not want to add a symbol for it. Adding a
16894 symbol would in some cases obscure the true definition of the
16895 enum, giving users an incomplete type when the definition is
16896 actually available. Note that we do not want to do this for all
16897 enums which are just declarations, because C++0x allows forward
16898 enum declarations. */
16899 if (cu->per_cu->is_debug_types
16900 && die_is_declaration (die, cu))
16902 struct signatured_type *sig_type;
16904 sig_type = (struct signatured_type *) cu->per_cu;
16905 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16906 if (sig_type->type_offset_in_section != die->sect_off)
16910 new_symbol (die, this_type, cu);
16913 /* Extract all information from a DW_TAG_array_type DIE and put it in
16914 the DIE's type field. For now, this only handles one dimensional
16917 static struct type *
16918 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16920 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16921 struct die_info *child_die;
16923 struct type *element_type, *range_type, *index_type;
16924 struct attribute *attr;
16926 struct dynamic_prop *byte_stride_prop = NULL;
16927 unsigned int bit_stride = 0;
16929 element_type = die_type (die, cu);
16931 /* The die_type call above may have already set the type for this DIE. */
16932 type = get_die_type (die, cu);
16936 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16942 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16943 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16946 complaint (&symfile_complaints,
16947 _("unable to read array DW_AT_byte_stride "
16948 " - DIE at %s [in module %s]"),
16949 sect_offset_str (die->sect_off),
16950 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16951 /* Ignore this attribute. We will likely not be able to print
16952 arrays of this type correctly, but there is little we can do
16953 to help if we cannot read the attribute's value. */
16954 byte_stride_prop = NULL;
16958 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16960 bit_stride = DW_UNSND (attr);
16962 /* Irix 6.2 native cc creates array types without children for
16963 arrays with unspecified length. */
16964 if (die->child == NULL)
16966 index_type = objfile_type (objfile)->builtin_int;
16967 range_type = create_static_range_type (NULL, index_type, 0, -1);
16968 type = create_array_type_with_stride (NULL, element_type, range_type,
16969 byte_stride_prop, bit_stride);
16970 return set_die_type (die, type, cu);
16973 std::vector<struct type *> range_types;
16974 child_die = die->child;
16975 while (child_die && child_die->tag)
16977 if (child_die->tag == DW_TAG_subrange_type)
16979 struct type *child_type = read_type_die (child_die, cu);
16981 if (child_type != NULL)
16983 /* The range type was succesfully read. Save it for the
16984 array type creation. */
16985 range_types.push_back (child_type);
16988 child_die = sibling_die (child_die);
16991 /* Dwarf2 dimensions are output from left to right, create the
16992 necessary array types in backwards order. */
16994 type = element_type;
16996 if (read_array_order (die, cu) == DW_ORD_col_major)
17000 while (i < range_types.size ())
17001 type = create_array_type_with_stride (NULL, type, range_types[i++],
17002 byte_stride_prop, bit_stride);
17006 size_t ndim = range_types.size ();
17008 type = create_array_type_with_stride (NULL, type, range_types[ndim],
17009 byte_stride_prop, bit_stride);
17012 /* Understand Dwarf2 support for vector types (like they occur on
17013 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17014 array type. This is not part of the Dwarf2/3 standard yet, but a
17015 custom vendor extension. The main difference between a regular
17016 array and the vector variant is that vectors are passed by value
17018 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
17020 make_vector_type (type);
17022 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17023 implementation may choose to implement triple vectors using this
17025 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17028 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
17029 TYPE_LENGTH (type) = DW_UNSND (attr);
17031 complaint (&symfile_complaints,
17032 _("DW_AT_byte_size for array type smaller "
17033 "than the total size of elements"));
17036 name = dwarf2_name (die, cu);
17038 TYPE_NAME (type) = name;
17040 /* Install the type in the die. */
17041 set_die_type (die, type, cu);
17043 /* set_die_type should be already done. */
17044 set_descriptive_type (type, die, cu);
17049 static enum dwarf_array_dim_ordering
17050 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
17052 struct attribute *attr;
17054 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17057 return (enum dwarf_array_dim_ordering) DW_SND (attr);
17059 /* GNU F77 is a special case, as at 08/2004 array type info is the
17060 opposite order to the dwarf2 specification, but data is still
17061 laid out as per normal fortran.
17063 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17064 version checking. */
17066 if (cu->language == language_fortran
17067 && cu->producer && strstr (cu->producer, "GNU F77"))
17069 return DW_ORD_row_major;
17072 switch (cu->language_defn->la_array_ordering)
17074 case array_column_major:
17075 return DW_ORD_col_major;
17076 case array_row_major:
17078 return DW_ORD_row_major;
17082 /* Extract all information from a DW_TAG_set_type DIE and put it in
17083 the DIE's type field. */
17085 static struct type *
17086 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17088 struct type *domain_type, *set_type;
17089 struct attribute *attr;
17091 domain_type = die_type (die, cu);
17093 /* The die_type call above may have already set the type for this DIE. */
17094 set_type = get_die_type (die, cu);
17098 set_type = create_set_type (NULL, domain_type);
17100 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17102 TYPE_LENGTH (set_type) = DW_UNSND (attr);
17104 return set_die_type (die, set_type, cu);
17107 /* A helper for read_common_block that creates a locexpr baton.
17108 SYM is the symbol which we are marking as computed.
17109 COMMON_DIE is the DIE for the common block.
17110 COMMON_LOC is the location expression attribute for the common
17112 MEMBER_LOC is the location expression attribute for the particular
17113 member of the common block that we are processing.
17114 CU is the CU from which the above come. */
17117 mark_common_block_symbol_computed (struct symbol *sym,
17118 struct die_info *common_die,
17119 struct attribute *common_loc,
17120 struct attribute *member_loc,
17121 struct dwarf2_cu *cu)
17123 struct dwarf2_per_objfile *dwarf2_per_objfile
17124 = cu->per_cu->dwarf2_per_objfile;
17125 struct objfile *objfile = dwarf2_per_objfile->objfile;
17126 struct dwarf2_locexpr_baton *baton;
17128 unsigned int cu_off;
17129 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
17130 LONGEST offset = 0;
17132 gdb_assert (common_loc && member_loc);
17133 gdb_assert (attr_form_is_block (common_loc));
17134 gdb_assert (attr_form_is_block (member_loc)
17135 || attr_form_is_constant (member_loc));
17137 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
17138 baton->per_cu = cu->per_cu;
17139 gdb_assert (baton->per_cu);
17141 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17143 if (attr_form_is_constant (member_loc))
17145 offset = dwarf2_get_attr_constant_value (member_loc, 0);
17146 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17149 baton->size += DW_BLOCK (member_loc)->size;
17151 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
17154 *ptr++ = DW_OP_call4;
17155 cu_off = common_die->sect_off - cu->per_cu->sect_off;
17156 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17159 if (attr_form_is_constant (member_loc))
17161 *ptr++ = DW_OP_addr;
17162 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17163 ptr += cu->header.addr_size;
17167 /* We have to copy the data here, because DW_OP_call4 will only
17168 use a DW_AT_location attribute. */
17169 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
17170 ptr += DW_BLOCK (member_loc)->size;
17173 *ptr++ = DW_OP_plus;
17174 gdb_assert (ptr - baton->data == baton->size);
17176 SYMBOL_LOCATION_BATON (sym) = baton;
17177 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
17180 /* Create appropriate locally-scoped variables for all the
17181 DW_TAG_common_block entries. Also create a struct common_block
17182 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17183 is used to sepate the common blocks name namespace from regular
17187 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
17189 struct attribute *attr;
17191 attr = dwarf2_attr (die, DW_AT_location, cu);
17194 /* Support the .debug_loc offsets. */
17195 if (attr_form_is_block (attr))
17199 else if (attr_form_is_section_offset (attr))
17201 dwarf2_complex_location_expr_complaint ();
17206 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17207 "common block member");
17212 if (die->child != NULL)
17214 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17215 struct die_info *child_die;
17216 size_t n_entries = 0, size;
17217 struct common_block *common_block;
17218 struct symbol *sym;
17220 for (child_die = die->child;
17221 child_die && child_die->tag;
17222 child_die = sibling_die (child_die))
17225 size = (sizeof (struct common_block)
17226 + (n_entries - 1) * sizeof (struct symbol *));
17228 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17230 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17231 common_block->n_entries = 0;
17233 for (child_die = die->child;
17234 child_die && child_die->tag;
17235 child_die = sibling_die (child_die))
17237 /* Create the symbol in the DW_TAG_common_block block in the current
17239 sym = new_symbol (child_die, NULL, cu);
17242 struct attribute *member_loc;
17244 common_block->contents[common_block->n_entries++] = sym;
17246 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17250 /* GDB has handled this for a long time, but it is
17251 not specified by DWARF. It seems to have been
17252 emitted by gfortran at least as recently as:
17253 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17254 complaint (&symfile_complaints,
17255 _("Variable in common block has "
17256 "DW_AT_data_member_location "
17257 "- DIE at %s [in module %s]"),
17258 sect_offset_str (child_die->sect_off),
17259 objfile_name (objfile));
17261 if (attr_form_is_section_offset (member_loc))
17262 dwarf2_complex_location_expr_complaint ();
17263 else if (attr_form_is_constant (member_loc)
17264 || attr_form_is_block (member_loc))
17267 mark_common_block_symbol_computed (sym, die, attr,
17271 dwarf2_complex_location_expr_complaint ();
17276 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17277 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17281 /* Create a type for a C++ namespace. */
17283 static struct type *
17284 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17286 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17287 const char *previous_prefix, *name;
17291 /* For extensions, reuse the type of the original namespace. */
17292 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17294 struct die_info *ext_die;
17295 struct dwarf2_cu *ext_cu = cu;
17297 ext_die = dwarf2_extension (die, &ext_cu);
17298 type = read_type_die (ext_die, ext_cu);
17300 /* EXT_CU may not be the same as CU.
17301 Ensure TYPE is recorded with CU in die_type_hash. */
17302 return set_die_type (die, type, cu);
17305 name = namespace_name (die, &is_anonymous, cu);
17307 /* Now build the name of the current namespace. */
17309 previous_prefix = determine_prefix (die, cu);
17310 if (previous_prefix[0] != '\0')
17311 name = typename_concat (&objfile->objfile_obstack,
17312 previous_prefix, name, 0, cu);
17314 /* Create the type. */
17315 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17316 TYPE_TAG_NAME (type) = TYPE_NAME (type);
17318 return set_die_type (die, type, cu);
17321 /* Read a namespace scope. */
17324 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17326 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17329 /* Add a symbol associated to this if we haven't seen the namespace
17330 before. Also, add a using directive if it's an anonymous
17333 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17337 type = read_type_die (die, cu);
17338 new_symbol (die, type, cu);
17340 namespace_name (die, &is_anonymous, cu);
17343 const char *previous_prefix = determine_prefix (die, cu);
17345 std::vector<const char *> excludes;
17346 add_using_directive (using_directives (cu->language),
17347 previous_prefix, TYPE_NAME (type), NULL,
17348 NULL, excludes, 0, &objfile->objfile_obstack);
17352 if (die->child != NULL)
17354 struct die_info *child_die = die->child;
17356 while (child_die && child_die->tag)
17358 process_die (child_die, cu);
17359 child_die = sibling_die (child_die);
17364 /* Read a Fortran module as type. This DIE can be only a declaration used for
17365 imported module. Still we need that type as local Fortran "use ... only"
17366 declaration imports depend on the created type in determine_prefix. */
17368 static struct type *
17369 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17371 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17372 const char *module_name;
17375 module_name = dwarf2_name (die, cu);
17377 complaint (&symfile_complaints,
17378 _("DW_TAG_module has no name, offset %s"),
17379 sect_offset_str (die->sect_off));
17380 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17382 /* determine_prefix uses TYPE_TAG_NAME. */
17383 TYPE_TAG_NAME (type) = TYPE_NAME (type);
17385 return set_die_type (die, type, cu);
17388 /* Read a Fortran module. */
17391 read_module (struct die_info *die, struct dwarf2_cu *cu)
17393 struct die_info *child_die = die->child;
17396 type = read_type_die (die, cu);
17397 new_symbol (die, type, cu);
17399 while (child_die && child_die->tag)
17401 process_die (child_die, cu);
17402 child_die = sibling_die (child_die);
17406 /* Return the name of the namespace represented by DIE. Set
17407 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17410 static const char *
17411 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17413 struct die_info *current_die;
17414 const char *name = NULL;
17416 /* Loop through the extensions until we find a name. */
17418 for (current_die = die;
17419 current_die != NULL;
17420 current_die = dwarf2_extension (die, &cu))
17422 /* We don't use dwarf2_name here so that we can detect the absence
17423 of a name -> anonymous namespace. */
17424 name = dwarf2_string_attr (die, DW_AT_name, cu);
17430 /* Is it an anonymous namespace? */
17432 *is_anonymous = (name == NULL);
17434 name = CP_ANONYMOUS_NAMESPACE_STR;
17439 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17440 the user defined type vector. */
17442 static struct type *
17443 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17445 struct gdbarch *gdbarch
17446 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
17447 struct comp_unit_head *cu_header = &cu->header;
17449 struct attribute *attr_byte_size;
17450 struct attribute *attr_address_class;
17451 int byte_size, addr_class;
17452 struct type *target_type;
17454 target_type = die_type (die, cu);
17456 /* The die_type call above may have already set the type for this DIE. */
17457 type = get_die_type (die, cu);
17461 type = lookup_pointer_type (target_type);
17463 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17464 if (attr_byte_size)
17465 byte_size = DW_UNSND (attr_byte_size);
17467 byte_size = cu_header->addr_size;
17469 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17470 if (attr_address_class)
17471 addr_class = DW_UNSND (attr_address_class);
17473 addr_class = DW_ADDR_none;
17475 /* If the pointer size or address class is different than the
17476 default, create a type variant marked as such and set the
17477 length accordingly. */
17478 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
17480 if (gdbarch_address_class_type_flags_p (gdbarch))
17484 type_flags = gdbarch_address_class_type_flags
17485 (gdbarch, byte_size, addr_class);
17486 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17488 type = make_type_with_address_space (type, type_flags);
17490 else if (TYPE_LENGTH (type) != byte_size)
17492 complaint (&symfile_complaints,
17493 _("invalid pointer size %d"), byte_size);
17497 /* Should we also complain about unhandled address classes? */
17501 TYPE_LENGTH (type) = byte_size;
17502 return set_die_type (die, type, cu);
17505 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17506 the user defined type vector. */
17508 static struct type *
17509 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17512 struct type *to_type;
17513 struct type *domain;
17515 to_type = die_type (die, cu);
17516 domain = die_containing_type (die, cu);
17518 /* The calls above may have already set the type for this DIE. */
17519 type = get_die_type (die, cu);
17523 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17524 type = lookup_methodptr_type (to_type);
17525 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17527 struct type *new_type
17528 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17530 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17531 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17532 TYPE_VARARGS (to_type));
17533 type = lookup_methodptr_type (new_type);
17536 type = lookup_memberptr_type (to_type, domain);
17538 return set_die_type (die, type, cu);
17541 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17542 the user defined type vector. */
17544 static struct type *
17545 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17546 enum type_code refcode)
17548 struct comp_unit_head *cu_header = &cu->header;
17549 struct type *type, *target_type;
17550 struct attribute *attr;
17552 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17554 target_type = die_type (die, cu);
17556 /* The die_type call above may have already set the type for this DIE. */
17557 type = get_die_type (die, cu);
17561 type = lookup_reference_type (target_type, refcode);
17562 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17565 TYPE_LENGTH (type) = DW_UNSND (attr);
17569 TYPE_LENGTH (type) = cu_header->addr_size;
17571 return set_die_type (die, type, cu);
17574 /* Add the given cv-qualifiers to the element type of the array. GCC
17575 outputs DWARF type qualifiers that apply to an array, not the
17576 element type. But GDB relies on the array element type to carry
17577 the cv-qualifiers. This mimics section 6.7.3 of the C99
17580 static struct type *
17581 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17582 struct type *base_type, int cnst, int voltl)
17584 struct type *el_type, *inner_array;
17586 base_type = copy_type (base_type);
17587 inner_array = base_type;
17589 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17591 TYPE_TARGET_TYPE (inner_array) =
17592 copy_type (TYPE_TARGET_TYPE (inner_array));
17593 inner_array = TYPE_TARGET_TYPE (inner_array);
17596 el_type = TYPE_TARGET_TYPE (inner_array);
17597 cnst |= TYPE_CONST (el_type);
17598 voltl |= TYPE_VOLATILE (el_type);
17599 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17601 return set_die_type (die, base_type, cu);
17604 static struct type *
17605 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17607 struct type *base_type, *cv_type;
17609 base_type = die_type (die, cu);
17611 /* The die_type call above may have already set the type for this DIE. */
17612 cv_type = get_die_type (die, cu);
17616 /* In case the const qualifier is applied to an array type, the element type
17617 is so qualified, not the array type (section 6.7.3 of C99). */
17618 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17619 return add_array_cv_type (die, cu, base_type, 1, 0);
17621 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17622 return set_die_type (die, cv_type, cu);
17625 static struct type *
17626 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17628 struct type *base_type, *cv_type;
17630 base_type = die_type (die, cu);
17632 /* The die_type call above may have already set the type for this DIE. */
17633 cv_type = get_die_type (die, cu);
17637 /* In case the volatile qualifier is applied to an array type, the
17638 element type is so qualified, not the array type (section 6.7.3
17640 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17641 return add_array_cv_type (die, cu, base_type, 0, 1);
17643 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17644 return set_die_type (die, cv_type, cu);
17647 /* Handle DW_TAG_restrict_type. */
17649 static struct type *
17650 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17652 struct type *base_type, *cv_type;
17654 base_type = die_type (die, cu);
17656 /* The die_type call above may have already set the type for this DIE. */
17657 cv_type = get_die_type (die, cu);
17661 cv_type = make_restrict_type (base_type);
17662 return set_die_type (die, cv_type, cu);
17665 /* Handle DW_TAG_atomic_type. */
17667 static struct type *
17668 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17670 struct type *base_type, *cv_type;
17672 base_type = die_type (die, cu);
17674 /* The die_type call above may have already set the type for this DIE. */
17675 cv_type = get_die_type (die, cu);
17679 cv_type = make_atomic_type (base_type);
17680 return set_die_type (die, cv_type, cu);
17683 /* Extract all information from a DW_TAG_string_type DIE and add to
17684 the user defined type vector. It isn't really a user defined type,
17685 but it behaves like one, with other DIE's using an AT_user_def_type
17686 attribute to reference it. */
17688 static struct type *
17689 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17691 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17692 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17693 struct type *type, *range_type, *index_type, *char_type;
17694 struct attribute *attr;
17695 unsigned int length;
17697 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17700 length = DW_UNSND (attr);
17704 /* Check for the DW_AT_byte_size attribute. */
17705 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17708 length = DW_UNSND (attr);
17716 index_type = objfile_type (objfile)->builtin_int;
17717 range_type = create_static_range_type (NULL, index_type, 1, length);
17718 char_type = language_string_char_type (cu->language_defn, gdbarch);
17719 type = create_string_type (NULL, char_type, range_type);
17721 return set_die_type (die, type, cu);
17724 /* Assuming that DIE corresponds to a function, returns nonzero
17725 if the function is prototyped. */
17728 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17730 struct attribute *attr;
17732 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17733 if (attr && (DW_UNSND (attr) != 0))
17736 /* The DWARF standard implies that the DW_AT_prototyped attribute
17737 is only meaninful for C, but the concept also extends to other
17738 languages that allow unprototyped functions (Eg: Objective C).
17739 For all other languages, assume that functions are always
17741 if (cu->language != language_c
17742 && cu->language != language_objc
17743 && cu->language != language_opencl)
17746 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17747 prototyped and unprototyped functions; default to prototyped,
17748 since that is more common in modern code (and RealView warns
17749 about unprototyped functions). */
17750 if (producer_is_realview (cu->producer))
17756 /* Handle DIES due to C code like:
17760 int (*funcp)(int a, long l);
17764 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17766 static struct type *
17767 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17769 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17770 struct type *type; /* Type that this function returns. */
17771 struct type *ftype; /* Function that returns above type. */
17772 struct attribute *attr;
17774 type = die_type (die, cu);
17776 /* The die_type call above may have already set the type for this DIE. */
17777 ftype = get_die_type (die, cu);
17781 ftype = lookup_function_type (type);
17783 if (prototyped_function_p (die, cu))
17784 TYPE_PROTOTYPED (ftype) = 1;
17786 /* Store the calling convention in the type if it's available in
17787 the subroutine die. Otherwise set the calling convention to
17788 the default value DW_CC_normal. */
17789 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17791 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17792 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17793 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17795 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17797 /* Record whether the function returns normally to its caller or not
17798 if the DWARF producer set that information. */
17799 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17800 if (attr && (DW_UNSND (attr) != 0))
17801 TYPE_NO_RETURN (ftype) = 1;
17803 /* We need to add the subroutine type to the die immediately so
17804 we don't infinitely recurse when dealing with parameters
17805 declared as the same subroutine type. */
17806 set_die_type (die, ftype, cu);
17808 if (die->child != NULL)
17810 struct type *void_type = objfile_type (objfile)->builtin_void;
17811 struct die_info *child_die;
17812 int nparams, iparams;
17814 /* Count the number of parameters.
17815 FIXME: GDB currently ignores vararg functions, but knows about
17816 vararg member functions. */
17818 child_die = die->child;
17819 while (child_die && child_die->tag)
17821 if (child_die->tag == DW_TAG_formal_parameter)
17823 else if (child_die->tag == DW_TAG_unspecified_parameters)
17824 TYPE_VARARGS (ftype) = 1;
17825 child_die = sibling_die (child_die);
17828 /* Allocate storage for parameters and fill them in. */
17829 TYPE_NFIELDS (ftype) = nparams;
17830 TYPE_FIELDS (ftype) = (struct field *)
17831 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17833 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17834 even if we error out during the parameters reading below. */
17835 for (iparams = 0; iparams < nparams; iparams++)
17836 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17839 child_die = die->child;
17840 while (child_die && child_die->tag)
17842 if (child_die->tag == DW_TAG_formal_parameter)
17844 struct type *arg_type;
17846 /* DWARF version 2 has no clean way to discern C++
17847 static and non-static member functions. G++ helps
17848 GDB by marking the first parameter for non-static
17849 member functions (which is the this pointer) as
17850 artificial. We pass this information to
17851 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17853 DWARF version 3 added DW_AT_object_pointer, which GCC
17854 4.5 does not yet generate. */
17855 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17857 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17859 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17860 arg_type = die_type (child_die, cu);
17862 /* RealView does not mark THIS as const, which the testsuite
17863 expects. GCC marks THIS as const in method definitions,
17864 but not in the class specifications (GCC PR 43053). */
17865 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17866 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17869 struct dwarf2_cu *arg_cu = cu;
17870 const char *name = dwarf2_name (child_die, cu);
17872 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17875 /* If the compiler emits this, use it. */
17876 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17879 else if (name && strcmp (name, "this") == 0)
17880 /* Function definitions will have the argument names. */
17882 else if (name == NULL && iparams == 0)
17883 /* Declarations may not have the names, so like
17884 elsewhere in GDB, assume an artificial first
17885 argument is "this". */
17889 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17893 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17896 child_die = sibling_die (child_die);
17903 static struct type *
17904 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17906 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17907 const char *name = NULL;
17908 struct type *this_type, *target_type;
17910 name = dwarf2_full_name (NULL, die, cu);
17911 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17912 TYPE_TARGET_STUB (this_type) = 1;
17913 set_die_type (die, this_type, cu);
17914 target_type = die_type (die, cu);
17915 if (target_type != this_type)
17916 TYPE_TARGET_TYPE (this_type) = target_type;
17919 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17920 spec and cause infinite loops in GDB. */
17921 complaint (&symfile_complaints,
17922 _("Self-referential DW_TAG_typedef "
17923 "- DIE at %s [in module %s]"),
17924 sect_offset_str (die->sect_off), objfile_name (objfile));
17925 TYPE_TARGET_TYPE (this_type) = NULL;
17930 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17931 (which may be different from NAME) to the architecture back-end to allow
17932 it to guess the correct format if necessary. */
17934 static struct type *
17935 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17936 const char *name_hint)
17938 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17939 const struct floatformat **format;
17942 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17944 type = init_float_type (objfile, bits, name, format);
17946 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17951 /* Find a representation of a given base type and install
17952 it in the TYPE field of the die. */
17954 static struct type *
17955 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17957 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17959 struct attribute *attr;
17960 int encoding = 0, bits = 0;
17963 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17966 encoding = DW_UNSND (attr);
17968 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17971 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17973 name = dwarf2_name (die, cu);
17976 complaint (&symfile_complaints,
17977 _("DW_AT_name missing from DW_TAG_base_type"));
17982 case DW_ATE_address:
17983 /* Turn DW_ATE_address into a void * pointer. */
17984 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17985 type = init_pointer_type (objfile, bits, name, type);
17987 case DW_ATE_boolean:
17988 type = init_boolean_type (objfile, bits, 1, name);
17990 case DW_ATE_complex_float:
17991 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
17992 type = init_complex_type (objfile, name, type);
17994 case DW_ATE_decimal_float:
17995 type = init_decfloat_type (objfile, bits, name);
17998 type = dwarf2_init_float_type (objfile, bits, name, name);
18000 case DW_ATE_signed:
18001 type = init_integer_type (objfile, bits, 0, name);
18003 case DW_ATE_unsigned:
18004 if (cu->language == language_fortran
18006 && startswith (name, "character("))
18007 type = init_character_type (objfile, bits, 1, name);
18009 type = init_integer_type (objfile, bits, 1, name);
18011 case DW_ATE_signed_char:
18012 if (cu->language == language_ada || cu->language == language_m2
18013 || cu->language == language_pascal
18014 || cu->language == language_fortran)
18015 type = init_character_type (objfile, bits, 0, name);
18017 type = init_integer_type (objfile, bits, 0, name);
18019 case DW_ATE_unsigned_char:
18020 if (cu->language == language_ada || cu->language == language_m2
18021 || cu->language == language_pascal
18022 || cu->language == language_fortran
18023 || cu->language == language_rust)
18024 type = init_character_type (objfile, bits, 1, name);
18026 type = init_integer_type (objfile, bits, 1, name);
18030 gdbarch *arch = get_objfile_arch (objfile);
18033 type = builtin_type (arch)->builtin_char16;
18034 else if (bits == 32)
18035 type = builtin_type (arch)->builtin_char32;
18038 complaint (&symfile_complaints,
18039 _("unsupported DW_ATE_UTF bit size: '%d'"),
18041 type = init_integer_type (objfile, bits, 1, name);
18043 return set_die_type (die, type, cu);
18048 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
18049 dwarf_type_encoding_name (encoding));
18050 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18054 if (name && strcmp (name, "char") == 0)
18055 TYPE_NOSIGN (type) = 1;
18057 return set_die_type (die, type, cu);
18060 /* Parse dwarf attribute if it's a block, reference or constant and put the
18061 resulting value of the attribute into struct bound_prop.
18062 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18065 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18066 struct dwarf2_cu *cu, struct dynamic_prop *prop)
18068 struct dwarf2_property_baton *baton;
18069 struct obstack *obstack
18070 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
18072 if (attr == NULL || prop == NULL)
18075 if (attr_form_is_block (attr))
18077 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18078 baton->referenced_type = NULL;
18079 baton->locexpr.per_cu = cu->per_cu;
18080 baton->locexpr.size = DW_BLOCK (attr)->size;
18081 baton->locexpr.data = DW_BLOCK (attr)->data;
18082 prop->data.baton = baton;
18083 prop->kind = PROP_LOCEXPR;
18084 gdb_assert (prop->data.baton != NULL);
18086 else if (attr_form_is_ref (attr))
18088 struct dwarf2_cu *target_cu = cu;
18089 struct die_info *target_die;
18090 struct attribute *target_attr;
18092 target_die = follow_die_ref (die, attr, &target_cu);
18093 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18094 if (target_attr == NULL)
18095 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18097 if (target_attr == NULL)
18100 switch (target_attr->name)
18102 case DW_AT_location:
18103 if (attr_form_is_section_offset (target_attr))
18105 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18106 baton->referenced_type = die_type (target_die, target_cu);
18107 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18108 prop->data.baton = baton;
18109 prop->kind = PROP_LOCLIST;
18110 gdb_assert (prop->data.baton != NULL);
18112 else if (attr_form_is_block (target_attr))
18114 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18115 baton->referenced_type = die_type (target_die, target_cu);
18116 baton->locexpr.per_cu = cu->per_cu;
18117 baton->locexpr.size = DW_BLOCK (target_attr)->size;
18118 baton->locexpr.data = DW_BLOCK (target_attr)->data;
18119 prop->data.baton = baton;
18120 prop->kind = PROP_LOCEXPR;
18121 gdb_assert (prop->data.baton != NULL);
18125 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18126 "dynamic property");
18130 case DW_AT_data_member_location:
18134 if (!handle_data_member_location (target_die, target_cu,
18138 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18139 baton->referenced_type = read_type_die (target_die->parent,
18141 baton->offset_info.offset = offset;
18142 baton->offset_info.type = die_type (target_die, target_cu);
18143 prop->data.baton = baton;
18144 prop->kind = PROP_ADDR_OFFSET;
18149 else if (attr_form_is_constant (attr))
18151 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
18152 prop->kind = PROP_CONST;
18156 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18157 dwarf2_name (die, cu));
18164 /* Read the given DW_AT_subrange DIE. */
18166 static struct type *
18167 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18169 struct type *base_type, *orig_base_type;
18170 struct type *range_type;
18171 struct attribute *attr;
18172 struct dynamic_prop low, high;
18173 int low_default_is_valid;
18174 int high_bound_is_count = 0;
18176 LONGEST negative_mask;
18178 orig_base_type = die_type (die, cu);
18179 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18180 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18181 creating the range type, but we use the result of check_typedef
18182 when examining properties of the type. */
18183 base_type = check_typedef (orig_base_type);
18185 /* The die_type call above may have already set the type for this DIE. */
18186 range_type = get_die_type (die, cu);
18190 low.kind = PROP_CONST;
18191 high.kind = PROP_CONST;
18192 high.data.const_val = 0;
18194 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18195 omitting DW_AT_lower_bound. */
18196 switch (cu->language)
18199 case language_cplus:
18200 low.data.const_val = 0;
18201 low_default_is_valid = 1;
18203 case language_fortran:
18204 low.data.const_val = 1;
18205 low_default_is_valid = 1;
18208 case language_objc:
18209 case language_rust:
18210 low.data.const_val = 0;
18211 low_default_is_valid = (cu->header.version >= 4);
18215 case language_pascal:
18216 low.data.const_val = 1;
18217 low_default_is_valid = (cu->header.version >= 4);
18220 low.data.const_val = 0;
18221 low_default_is_valid = 0;
18225 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18227 attr_to_dynamic_prop (attr, die, cu, &low);
18228 else if (!low_default_is_valid)
18229 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
18230 "- DIE at %s [in module %s]"),
18231 sect_offset_str (die->sect_off),
18232 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18234 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
18235 if (!attr_to_dynamic_prop (attr, die, cu, &high))
18237 attr = dwarf2_attr (die, DW_AT_count, cu);
18238 if (attr_to_dynamic_prop (attr, die, cu, &high))
18240 /* If bounds are constant do the final calculation here. */
18241 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18242 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18244 high_bound_is_count = 1;
18248 /* Dwarf-2 specifications explicitly allows to create subrange types
18249 without specifying a base type.
18250 In that case, the base type must be set to the type of
18251 the lower bound, upper bound or count, in that order, if any of these
18252 three attributes references an object that has a type.
18253 If no base type is found, the Dwarf-2 specifications say that
18254 a signed integer type of size equal to the size of an address should
18256 For the following C code: `extern char gdb_int [];'
18257 GCC produces an empty range DIE.
18258 FIXME: muller/2010-05-28: Possible references to object for low bound,
18259 high bound or count are not yet handled by this code. */
18260 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
18262 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18263 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18264 int addr_size = gdbarch_addr_bit (gdbarch) /8;
18265 struct type *int_type = objfile_type (objfile)->builtin_int;
18267 /* Test "int", "long int", and "long long int" objfile types,
18268 and select the first one having a size above or equal to the
18269 architecture address size. */
18270 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
18271 base_type = int_type;
18274 int_type = objfile_type (objfile)->builtin_long;
18275 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
18276 base_type = int_type;
18279 int_type = objfile_type (objfile)->builtin_long_long;
18280 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
18281 base_type = int_type;
18286 /* Normally, the DWARF producers are expected to use a signed
18287 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18288 But this is unfortunately not always the case, as witnessed
18289 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18290 is used instead. To work around that ambiguity, we treat
18291 the bounds as signed, and thus sign-extend their values, when
18292 the base type is signed. */
18294 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18295 if (low.kind == PROP_CONST
18296 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18297 low.data.const_val |= negative_mask;
18298 if (high.kind == PROP_CONST
18299 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18300 high.data.const_val |= negative_mask;
18302 range_type = create_range_type (NULL, orig_base_type, &low, &high);
18304 if (high_bound_is_count)
18305 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18307 /* Ada expects an empty array on no boundary attributes. */
18308 if (attr == NULL && cu->language != language_ada)
18309 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
18311 name = dwarf2_name (die, cu);
18313 TYPE_NAME (range_type) = name;
18315 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18317 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18319 set_die_type (die, range_type, cu);
18321 /* set_die_type should be already done. */
18322 set_descriptive_type (range_type, die, cu);
18327 static struct type *
18328 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18332 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18334 TYPE_NAME (type) = dwarf2_name (die, cu);
18336 /* In Ada, an unspecified type is typically used when the description
18337 of the type is defered to a different unit. When encountering
18338 such a type, we treat it as a stub, and try to resolve it later on,
18340 if (cu->language == language_ada)
18341 TYPE_STUB (type) = 1;
18343 return set_die_type (die, type, cu);
18346 /* Read a single die and all its descendents. Set the die's sibling
18347 field to NULL; set other fields in the die correctly, and set all
18348 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18349 location of the info_ptr after reading all of those dies. PARENT
18350 is the parent of the die in question. */
18352 static struct die_info *
18353 read_die_and_children (const struct die_reader_specs *reader,
18354 const gdb_byte *info_ptr,
18355 const gdb_byte **new_info_ptr,
18356 struct die_info *parent)
18358 struct die_info *die;
18359 const gdb_byte *cur_ptr;
18362 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
18365 *new_info_ptr = cur_ptr;
18368 store_in_ref_table (die, reader->cu);
18371 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18375 *new_info_ptr = cur_ptr;
18378 die->sibling = NULL;
18379 die->parent = parent;
18383 /* Read a die, all of its descendents, and all of its siblings; set
18384 all of the fields of all of the dies correctly. Arguments are as
18385 in read_die_and_children. */
18387 static struct die_info *
18388 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18389 const gdb_byte *info_ptr,
18390 const gdb_byte **new_info_ptr,
18391 struct die_info *parent)
18393 struct die_info *first_die, *last_sibling;
18394 const gdb_byte *cur_ptr;
18396 cur_ptr = info_ptr;
18397 first_die = last_sibling = NULL;
18401 struct die_info *die
18402 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18406 *new_info_ptr = cur_ptr;
18413 last_sibling->sibling = die;
18415 last_sibling = die;
18419 /* Read a die, all of its descendents, and all of its siblings; set
18420 all of the fields of all of the dies correctly. Arguments are as
18421 in read_die_and_children.
18422 This the main entry point for reading a DIE and all its children. */
18424 static struct die_info *
18425 read_die_and_siblings (const struct die_reader_specs *reader,
18426 const gdb_byte *info_ptr,
18427 const gdb_byte **new_info_ptr,
18428 struct die_info *parent)
18430 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18431 new_info_ptr, parent);
18433 if (dwarf_die_debug)
18435 fprintf_unfiltered (gdb_stdlog,
18436 "Read die from %s@0x%x of %s:\n",
18437 get_section_name (reader->die_section),
18438 (unsigned) (info_ptr - reader->die_section->buffer),
18439 bfd_get_filename (reader->abfd));
18440 dump_die (die, dwarf_die_debug);
18446 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18448 The caller is responsible for filling in the extra attributes
18449 and updating (*DIEP)->num_attrs.
18450 Set DIEP to point to a newly allocated die with its information,
18451 except for its child, sibling, and parent fields.
18452 Set HAS_CHILDREN to tell whether the die has children or not. */
18454 static const gdb_byte *
18455 read_full_die_1 (const struct die_reader_specs *reader,
18456 struct die_info **diep, const gdb_byte *info_ptr,
18457 int *has_children, int num_extra_attrs)
18459 unsigned int abbrev_number, bytes_read, i;
18460 struct abbrev_info *abbrev;
18461 struct die_info *die;
18462 struct dwarf2_cu *cu = reader->cu;
18463 bfd *abfd = reader->abfd;
18465 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18466 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18467 info_ptr += bytes_read;
18468 if (!abbrev_number)
18475 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18477 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18479 bfd_get_filename (abfd));
18481 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18482 die->sect_off = sect_off;
18483 die->tag = abbrev->tag;
18484 die->abbrev = abbrev_number;
18486 /* Make the result usable.
18487 The caller needs to update num_attrs after adding the extra
18489 die->num_attrs = abbrev->num_attrs;
18491 for (i = 0; i < abbrev->num_attrs; ++i)
18492 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18496 *has_children = abbrev->has_children;
18500 /* Read a die and all its attributes.
18501 Set DIEP to point to a newly allocated die with its information,
18502 except for its child, sibling, and parent fields.
18503 Set HAS_CHILDREN to tell whether the die has children or not. */
18505 static const gdb_byte *
18506 read_full_die (const struct die_reader_specs *reader,
18507 struct die_info **diep, const gdb_byte *info_ptr,
18510 const gdb_byte *result;
18512 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18514 if (dwarf_die_debug)
18516 fprintf_unfiltered (gdb_stdlog,
18517 "Read die from %s@0x%x of %s:\n",
18518 get_section_name (reader->die_section),
18519 (unsigned) (info_ptr - reader->die_section->buffer),
18520 bfd_get_filename (reader->abfd));
18521 dump_die (*diep, dwarf_die_debug);
18527 /* Abbreviation tables.
18529 In DWARF version 2, the description of the debugging information is
18530 stored in a separate .debug_abbrev section. Before we read any
18531 dies from a section we read in all abbreviations and install them
18532 in a hash table. */
18534 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18536 struct abbrev_info *
18537 abbrev_table::alloc_abbrev ()
18539 struct abbrev_info *abbrev;
18541 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18542 memset (abbrev, 0, sizeof (struct abbrev_info));
18547 /* Add an abbreviation to the table. */
18550 abbrev_table::add_abbrev (unsigned int abbrev_number,
18551 struct abbrev_info *abbrev)
18553 unsigned int hash_number;
18555 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18556 abbrev->next = m_abbrevs[hash_number];
18557 m_abbrevs[hash_number] = abbrev;
18560 /* Look up an abbrev in the table.
18561 Returns NULL if the abbrev is not found. */
18563 struct abbrev_info *
18564 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18566 unsigned int hash_number;
18567 struct abbrev_info *abbrev;
18569 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18570 abbrev = m_abbrevs[hash_number];
18574 if (abbrev->number == abbrev_number)
18576 abbrev = abbrev->next;
18581 /* Read in an abbrev table. */
18583 static abbrev_table_up
18584 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18585 struct dwarf2_section_info *section,
18586 sect_offset sect_off)
18588 struct objfile *objfile = dwarf2_per_objfile->objfile;
18589 bfd *abfd = get_section_bfd_owner (section);
18590 const gdb_byte *abbrev_ptr;
18591 struct abbrev_info *cur_abbrev;
18592 unsigned int abbrev_number, bytes_read, abbrev_name;
18593 unsigned int abbrev_form;
18594 struct attr_abbrev *cur_attrs;
18595 unsigned int allocated_attrs;
18597 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18599 dwarf2_read_section (objfile, section);
18600 abbrev_ptr = section->buffer + to_underlying (sect_off);
18601 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18602 abbrev_ptr += bytes_read;
18604 allocated_attrs = ATTR_ALLOC_CHUNK;
18605 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18607 /* Loop until we reach an abbrev number of 0. */
18608 while (abbrev_number)
18610 cur_abbrev = abbrev_table->alloc_abbrev ();
18612 /* read in abbrev header */
18613 cur_abbrev->number = abbrev_number;
18615 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18616 abbrev_ptr += bytes_read;
18617 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18620 /* now read in declarations */
18623 LONGEST implicit_const;
18625 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18626 abbrev_ptr += bytes_read;
18627 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18628 abbrev_ptr += bytes_read;
18629 if (abbrev_form == DW_FORM_implicit_const)
18631 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18633 abbrev_ptr += bytes_read;
18637 /* Initialize it due to a false compiler warning. */
18638 implicit_const = -1;
18641 if (abbrev_name == 0)
18644 if (cur_abbrev->num_attrs == allocated_attrs)
18646 allocated_attrs += ATTR_ALLOC_CHUNK;
18648 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18651 cur_attrs[cur_abbrev->num_attrs].name
18652 = (enum dwarf_attribute) abbrev_name;
18653 cur_attrs[cur_abbrev->num_attrs].form
18654 = (enum dwarf_form) abbrev_form;
18655 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18656 ++cur_abbrev->num_attrs;
18659 cur_abbrev->attrs =
18660 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18661 cur_abbrev->num_attrs);
18662 memcpy (cur_abbrev->attrs, cur_attrs,
18663 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18665 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18667 /* Get next abbreviation.
18668 Under Irix6 the abbreviations for a compilation unit are not
18669 always properly terminated with an abbrev number of 0.
18670 Exit loop if we encounter an abbreviation which we have
18671 already read (which means we are about to read the abbreviations
18672 for the next compile unit) or if the end of the abbreviation
18673 table is reached. */
18674 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18676 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18677 abbrev_ptr += bytes_read;
18678 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18683 return abbrev_table;
18686 /* Returns nonzero if TAG represents a type that we might generate a partial
18690 is_type_tag_for_partial (int tag)
18695 /* Some types that would be reasonable to generate partial symbols for,
18696 that we don't at present. */
18697 case DW_TAG_array_type:
18698 case DW_TAG_file_type:
18699 case DW_TAG_ptr_to_member_type:
18700 case DW_TAG_set_type:
18701 case DW_TAG_string_type:
18702 case DW_TAG_subroutine_type:
18704 case DW_TAG_base_type:
18705 case DW_TAG_class_type:
18706 case DW_TAG_interface_type:
18707 case DW_TAG_enumeration_type:
18708 case DW_TAG_structure_type:
18709 case DW_TAG_subrange_type:
18710 case DW_TAG_typedef:
18711 case DW_TAG_union_type:
18718 /* Load all DIEs that are interesting for partial symbols into memory. */
18720 static struct partial_die_info *
18721 load_partial_dies (const struct die_reader_specs *reader,
18722 const gdb_byte *info_ptr, int building_psymtab)
18724 struct dwarf2_cu *cu = reader->cu;
18725 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18726 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18727 unsigned int bytes_read;
18728 unsigned int load_all = 0;
18729 int nesting_level = 1;
18734 gdb_assert (cu->per_cu != NULL);
18735 if (cu->per_cu->load_all_dies)
18739 = htab_create_alloc_ex (cu->header.length / 12,
18743 &cu->comp_unit_obstack,
18744 hashtab_obstack_allocate,
18745 dummy_obstack_deallocate);
18749 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18751 /* A NULL abbrev means the end of a series of children. */
18752 if (abbrev == NULL)
18754 if (--nesting_level == 0)
18757 info_ptr += bytes_read;
18758 last_die = parent_die;
18759 parent_die = parent_die->die_parent;
18763 /* Check for template arguments. We never save these; if
18764 they're seen, we just mark the parent, and go on our way. */
18765 if (parent_die != NULL
18766 && cu->language == language_cplus
18767 && (abbrev->tag == DW_TAG_template_type_param
18768 || abbrev->tag == DW_TAG_template_value_param))
18770 parent_die->has_template_arguments = 1;
18774 /* We don't need a partial DIE for the template argument. */
18775 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18780 /* We only recurse into c++ subprograms looking for template arguments.
18781 Skip their other children. */
18783 && cu->language == language_cplus
18784 && parent_die != NULL
18785 && parent_die->tag == DW_TAG_subprogram)
18787 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18791 /* Check whether this DIE is interesting enough to save. Normally
18792 we would not be interested in members here, but there may be
18793 later variables referencing them via DW_AT_specification (for
18794 static members). */
18796 && !is_type_tag_for_partial (abbrev->tag)
18797 && abbrev->tag != DW_TAG_constant
18798 && abbrev->tag != DW_TAG_enumerator
18799 && abbrev->tag != DW_TAG_subprogram
18800 && abbrev->tag != DW_TAG_inlined_subroutine
18801 && abbrev->tag != DW_TAG_lexical_block
18802 && abbrev->tag != DW_TAG_variable
18803 && abbrev->tag != DW_TAG_namespace
18804 && abbrev->tag != DW_TAG_module
18805 && abbrev->tag != DW_TAG_member
18806 && abbrev->tag != DW_TAG_imported_unit
18807 && abbrev->tag != DW_TAG_imported_declaration)
18809 /* Otherwise we skip to the next sibling, if any. */
18810 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18814 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18817 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18819 /* This two-pass algorithm for processing partial symbols has a
18820 high cost in cache pressure. Thus, handle some simple cases
18821 here which cover the majority of C partial symbols. DIEs
18822 which neither have specification tags in them, nor could have
18823 specification tags elsewhere pointing at them, can simply be
18824 processed and discarded.
18826 This segment is also optional; scan_partial_symbols and
18827 add_partial_symbol will handle these DIEs if we chain
18828 them in normally. When compilers which do not emit large
18829 quantities of duplicate debug information are more common,
18830 this code can probably be removed. */
18832 /* Any complete simple types at the top level (pretty much all
18833 of them, for a language without namespaces), can be processed
18835 if (parent_die == NULL
18836 && pdi.has_specification == 0
18837 && pdi.is_declaration == 0
18838 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18839 || pdi.tag == DW_TAG_base_type
18840 || pdi.tag == DW_TAG_subrange_type))
18842 if (building_psymtab && pdi.name != NULL)
18843 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18844 VAR_DOMAIN, LOC_TYPEDEF,
18845 &objfile->static_psymbols,
18846 0, cu->language, objfile);
18847 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18851 /* The exception for DW_TAG_typedef with has_children above is
18852 a workaround of GCC PR debug/47510. In the case of this complaint
18853 type_name_no_tag_or_error will error on such types later.
18855 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18856 it could not find the child DIEs referenced later, this is checked
18857 above. In correct DWARF DW_TAG_typedef should have no children. */
18859 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18860 complaint (&symfile_complaints,
18861 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18862 "- DIE at %s [in module %s]"),
18863 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18865 /* If we're at the second level, and we're an enumerator, and
18866 our parent has no specification (meaning possibly lives in a
18867 namespace elsewhere), then we can add the partial symbol now
18868 instead of queueing it. */
18869 if (pdi.tag == DW_TAG_enumerator
18870 && parent_die != NULL
18871 && parent_die->die_parent == NULL
18872 && parent_die->tag == DW_TAG_enumeration_type
18873 && parent_die->has_specification == 0)
18875 if (pdi.name == NULL)
18876 complaint (&symfile_complaints,
18877 _("malformed enumerator DIE ignored"));
18878 else if (building_psymtab)
18879 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18880 VAR_DOMAIN, LOC_CONST,
18881 cu->language == language_cplus
18882 ? &objfile->global_psymbols
18883 : &objfile->static_psymbols,
18884 0, cu->language, objfile);
18886 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18890 struct partial_die_info *part_die
18891 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18893 /* We'll save this DIE so link it in. */
18894 part_die->die_parent = parent_die;
18895 part_die->die_sibling = NULL;
18896 part_die->die_child = NULL;
18898 if (last_die && last_die == parent_die)
18899 last_die->die_child = part_die;
18901 last_die->die_sibling = part_die;
18903 last_die = part_die;
18905 if (first_die == NULL)
18906 first_die = part_die;
18908 /* Maybe add the DIE to the hash table. Not all DIEs that we
18909 find interesting need to be in the hash table, because we
18910 also have the parent/sibling/child chains; only those that we
18911 might refer to by offset later during partial symbol reading.
18913 For now this means things that might have be the target of a
18914 DW_AT_specification, DW_AT_abstract_origin, or
18915 DW_AT_extension. DW_AT_extension will refer only to
18916 namespaces; DW_AT_abstract_origin refers to functions (and
18917 many things under the function DIE, but we do not recurse
18918 into function DIEs during partial symbol reading) and
18919 possibly variables as well; DW_AT_specification refers to
18920 declarations. Declarations ought to have the DW_AT_declaration
18921 flag. It happens that GCC forgets to put it in sometimes, but
18922 only for functions, not for types.
18924 Adding more things than necessary to the hash table is harmless
18925 except for the performance cost. Adding too few will result in
18926 wasted time in find_partial_die, when we reread the compilation
18927 unit with load_all_dies set. */
18930 || abbrev->tag == DW_TAG_constant
18931 || abbrev->tag == DW_TAG_subprogram
18932 || abbrev->tag == DW_TAG_variable
18933 || abbrev->tag == DW_TAG_namespace
18934 || part_die->is_declaration)
18938 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18939 to_underlying (part_die->sect_off),
18944 /* For some DIEs we want to follow their children (if any). For C
18945 we have no reason to follow the children of structures; for other
18946 languages we have to, so that we can get at method physnames
18947 to infer fully qualified class names, for DW_AT_specification,
18948 and for C++ template arguments. For C++, we also look one level
18949 inside functions to find template arguments (if the name of the
18950 function does not already contain the template arguments).
18952 For Ada, we need to scan the children of subprograms and lexical
18953 blocks as well because Ada allows the definition of nested
18954 entities that could be interesting for the debugger, such as
18955 nested subprograms for instance. */
18956 if (last_die->has_children
18958 || last_die->tag == DW_TAG_namespace
18959 || last_die->tag == DW_TAG_module
18960 || last_die->tag == DW_TAG_enumeration_type
18961 || (cu->language == language_cplus
18962 && last_die->tag == DW_TAG_subprogram
18963 && (last_die->name == NULL
18964 || strchr (last_die->name, '<') == NULL))
18965 || (cu->language != language_c
18966 && (last_die->tag == DW_TAG_class_type
18967 || last_die->tag == DW_TAG_interface_type
18968 || last_die->tag == DW_TAG_structure_type
18969 || last_die->tag == DW_TAG_union_type))
18970 || (cu->language == language_ada
18971 && (last_die->tag == DW_TAG_subprogram
18972 || last_die->tag == DW_TAG_lexical_block))))
18975 parent_die = last_die;
18979 /* Otherwise we skip to the next sibling, if any. */
18980 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18982 /* Back to the top, do it again. */
18986 partial_die_info::partial_die_info (sect_offset sect_off_,
18987 struct abbrev_info *abbrev)
18988 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18992 /* Read a minimal amount of information into the minimal die structure.
18993 INFO_PTR should point just after the initial uleb128 of a DIE. */
18996 partial_die_info::read (const struct die_reader_specs *reader,
18997 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18999 struct dwarf2_cu *cu = reader->cu;
19000 struct dwarf2_per_objfile *dwarf2_per_objfile
19001 = cu->per_cu->dwarf2_per_objfile;
19003 int has_low_pc_attr = 0;
19004 int has_high_pc_attr = 0;
19005 int high_pc_relative = 0;
19007 for (i = 0; i < abbrev.num_attrs; ++i)
19009 struct attribute attr;
19011 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19013 /* Store the data if it is of an attribute we want to keep in a
19014 partial symbol table. */
19020 case DW_TAG_compile_unit:
19021 case DW_TAG_partial_unit:
19022 case DW_TAG_type_unit:
19023 /* Compilation units have a DW_AT_name that is a filename, not
19024 a source language identifier. */
19025 case DW_TAG_enumeration_type:
19026 case DW_TAG_enumerator:
19027 /* These tags always have simple identifiers already; no need
19028 to canonicalize them. */
19029 name = DW_STRING (&attr);
19033 struct objfile *objfile = dwarf2_per_objfile->objfile;
19036 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
19037 &objfile->per_bfd->storage_obstack);
19042 case DW_AT_linkage_name:
19043 case DW_AT_MIPS_linkage_name:
19044 /* Note that both forms of linkage name might appear. We
19045 assume they will be the same, and we only store the last
19047 if (cu->language == language_ada)
19048 name = DW_STRING (&attr);
19049 linkage_name = DW_STRING (&attr);
19052 has_low_pc_attr = 1;
19053 lowpc = attr_value_as_address (&attr);
19055 case DW_AT_high_pc:
19056 has_high_pc_attr = 1;
19057 highpc = attr_value_as_address (&attr);
19058 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
19059 high_pc_relative = 1;
19061 case DW_AT_location:
19062 /* Support the .debug_loc offsets. */
19063 if (attr_form_is_block (&attr))
19065 d.locdesc = DW_BLOCK (&attr);
19067 else if (attr_form_is_section_offset (&attr))
19069 dwarf2_complex_location_expr_complaint ();
19073 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19074 "partial symbol information");
19077 case DW_AT_external:
19078 is_external = DW_UNSND (&attr);
19080 case DW_AT_declaration:
19081 is_declaration = DW_UNSND (&attr);
19086 case DW_AT_abstract_origin:
19087 case DW_AT_specification:
19088 case DW_AT_extension:
19089 has_specification = 1;
19090 spec_offset = dwarf2_get_ref_die_offset (&attr);
19091 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19092 || cu->per_cu->is_dwz);
19094 case DW_AT_sibling:
19095 /* Ignore absolute siblings, they might point outside of
19096 the current compile unit. */
19097 if (attr.form == DW_FORM_ref_addr)
19098 complaint (&symfile_complaints,
19099 _("ignoring absolute DW_AT_sibling"));
19102 const gdb_byte *buffer = reader->buffer;
19103 sect_offset off = dwarf2_get_ref_die_offset (&attr);
19104 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19106 if (sibling_ptr < info_ptr)
19107 complaint (&symfile_complaints,
19108 _("DW_AT_sibling points backwards"));
19109 else if (sibling_ptr > reader->buffer_end)
19110 dwarf2_section_buffer_overflow_complaint (reader->die_section);
19112 sibling = sibling_ptr;
19115 case DW_AT_byte_size:
19118 case DW_AT_const_value:
19119 has_const_value = 1;
19121 case DW_AT_calling_convention:
19122 /* DWARF doesn't provide a way to identify a program's source-level
19123 entry point. DW_AT_calling_convention attributes are only meant
19124 to describe functions' calling conventions.
19126 However, because it's a necessary piece of information in
19127 Fortran, and before DWARF 4 DW_CC_program was the only
19128 piece of debugging information whose definition refers to
19129 a 'main program' at all, several compilers marked Fortran
19130 main programs with DW_CC_program --- even when those
19131 functions use the standard calling conventions.
19133 Although DWARF now specifies a way to provide this
19134 information, we support this practice for backward
19136 if (DW_UNSND (&attr) == DW_CC_program
19137 && cu->language == language_fortran)
19138 main_subprogram = 1;
19141 if (DW_UNSND (&attr) == DW_INL_inlined
19142 || DW_UNSND (&attr) == DW_INL_declared_inlined)
19143 may_be_inlined = 1;
19147 if (tag == DW_TAG_imported_unit)
19149 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19150 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19151 || cu->per_cu->is_dwz);
19155 case DW_AT_main_subprogram:
19156 main_subprogram = DW_UNSND (&attr);
19164 if (high_pc_relative)
19167 if (has_low_pc_attr && has_high_pc_attr)
19169 /* When using the GNU linker, .gnu.linkonce. sections are used to
19170 eliminate duplicate copies of functions and vtables and such.
19171 The linker will arbitrarily choose one and discard the others.
19172 The AT_*_pc values for such functions refer to local labels in
19173 these sections. If the section from that file was discarded, the
19174 labels are not in the output, so the relocs get a value of 0.
19175 If this is a discarded function, mark the pc bounds as invalid,
19176 so that GDB will ignore it. */
19177 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
19179 struct objfile *objfile = dwarf2_per_objfile->objfile;
19180 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19182 complaint (&symfile_complaints,
19183 _("DW_AT_low_pc %s is zero "
19184 "for DIE at %s [in module %s]"),
19185 paddress (gdbarch, lowpc),
19186 sect_offset_str (sect_off),
19187 objfile_name (objfile));
19189 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19190 else if (lowpc >= highpc)
19192 struct objfile *objfile = dwarf2_per_objfile->objfile;
19193 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19195 complaint (&symfile_complaints,
19196 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19197 "for DIE at %s [in module %s]"),
19198 paddress (gdbarch, lowpc),
19199 paddress (gdbarch, highpc),
19200 sect_offset_str (sect_off),
19201 objfile_name (objfile));
19210 /* Find a cached partial DIE at OFFSET in CU. */
19212 struct partial_die_info *
19213 dwarf2_cu::find_partial_die (sect_offset sect_off)
19215 struct partial_die_info *lookup_die = NULL;
19216 struct partial_die_info part_die (sect_off);
19218 lookup_die = ((struct partial_die_info *)
19219 htab_find_with_hash (partial_dies, &part_die,
19220 to_underlying (sect_off)));
19225 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19226 except in the case of .debug_types DIEs which do not reference
19227 outside their CU (they do however referencing other types via
19228 DW_FORM_ref_sig8). */
19230 static struct partial_die_info *
19231 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19233 struct dwarf2_per_objfile *dwarf2_per_objfile
19234 = cu->per_cu->dwarf2_per_objfile;
19235 struct objfile *objfile = dwarf2_per_objfile->objfile;
19236 struct dwarf2_per_cu_data *per_cu = NULL;
19237 struct partial_die_info *pd = NULL;
19239 if (offset_in_dwz == cu->per_cu->is_dwz
19240 && offset_in_cu_p (&cu->header, sect_off))
19242 pd = cu->find_partial_die (sect_off);
19245 /* We missed recording what we needed.
19246 Load all dies and try again. */
19247 per_cu = cu->per_cu;
19251 /* TUs don't reference other CUs/TUs (except via type signatures). */
19252 if (cu->per_cu->is_debug_types)
19254 error (_("Dwarf Error: Type Unit at offset %s contains"
19255 " external reference to offset %s [in module %s].\n"),
19256 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19257 bfd_get_filename (objfile->obfd));
19259 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19260 dwarf2_per_objfile);
19262 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19263 load_partial_comp_unit (per_cu);
19265 per_cu->cu->last_used = 0;
19266 pd = per_cu->cu->find_partial_die (sect_off);
19269 /* If we didn't find it, and not all dies have been loaded,
19270 load them all and try again. */
19272 if (pd == NULL && per_cu->load_all_dies == 0)
19274 per_cu->load_all_dies = 1;
19276 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19277 THIS_CU->cu may already be in use. So we can't just free it and
19278 replace its DIEs with the ones we read in. Instead, we leave those
19279 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19280 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19282 load_partial_comp_unit (per_cu);
19284 pd = per_cu->cu->find_partial_die (sect_off);
19288 internal_error (__FILE__, __LINE__,
19289 _("could not find partial DIE %s "
19290 "in cache [from module %s]\n"),
19291 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19295 /* See if we can figure out if the class lives in a namespace. We do
19296 this by looking for a member function; its demangled name will
19297 contain namespace info, if there is any. */
19300 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19301 struct dwarf2_cu *cu)
19303 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19304 what template types look like, because the demangler
19305 frequently doesn't give the same name as the debug info. We
19306 could fix this by only using the demangled name to get the
19307 prefix (but see comment in read_structure_type). */
19309 struct partial_die_info *real_pdi;
19310 struct partial_die_info *child_pdi;
19312 /* If this DIE (this DIE's specification, if any) has a parent, then
19313 we should not do this. We'll prepend the parent's fully qualified
19314 name when we create the partial symbol. */
19316 real_pdi = struct_pdi;
19317 while (real_pdi->has_specification)
19318 real_pdi = find_partial_die (real_pdi->spec_offset,
19319 real_pdi->spec_is_dwz, cu);
19321 if (real_pdi->die_parent != NULL)
19324 for (child_pdi = struct_pdi->die_child;
19326 child_pdi = child_pdi->die_sibling)
19328 if (child_pdi->tag == DW_TAG_subprogram
19329 && child_pdi->linkage_name != NULL)
19331 char *actual_class_name
19332 = language_class_name_from_physname (cu->language_defn,
19333 child_pdi->linkage_name);
19334 if (actual_class_name != NULL)
19336 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19339 obstack_copy0 (&objfile->per_bfd->storage_obstack,
19341 strlen (actual_class_name)));
19342 xfree (actual_class_name);
19350 partial_die_info::fixup (struct dwarf2_cu *cu)
19352 /* Once we've fixed up a die, there's no point in doing so again.
19353 This also avoids a memory leak if we were to call
19354 guess_partial_die_structure_name multiple times. */
19358 /* If we found a reference attribute and the DIE has no name, try
19359 to find a name in the referred to DIE. */
19361 if (name == NULL && has_specification)
19363 struct partial_die_info *spec_die;
19365 spec_die = find_partial_die (spec_offset, spec_is_dwz, cu);
19367 spec_die->fixup (cu);
19369 if (spec_die->name)
19371 name = spec_die->name;
19373 /* Copy DW_AT_external attribute if it is set. */
19374 if (spec_die->is_external)
19375 is_external = spec_die->is_external;
19379 /* Set default names for some unnamed DIEs. */
19381 if (name == NULL && tag == DW_TAG_namespace)
19382 name = CP_ANONYMOUS_NAMESPACE_STR;
19384 /* If there is no parent die to provide a namespace, and there are
19385 children, see if we can determine the namespace from their linkage
19387 if (cu->language == language_cplus
19388 && !VEC_empty (dwarf2_section_info_def,
19389 cu->per_cu->dwarf2_per_objfile->types)
19390 && die_parent == NULL
19392 && (tag == DW_TAG_class_type
19393 || tag == DW_TAG_structure_type
19394 || tag == DW_TAG_union_type))
19395 guess_partial_die_structure_name (this, cu);
19397 /* GCC might emit a nameless struct or union that has a linkage
19398 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19400 && (tag == DW_TAG_class_type
19401 || tag == DW_TAG_interface_type
19402 || tag == DW_TAG_structure_type
19403 || tag == DW_TAG_union_type)
19404 && linkage_name != NULL)
19408 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
19413 /* Strip any leading namespaces/classes, keep only the base name.
19414 DW_AT_name for named DIEs does not contain the prefixes. */
19415 base = strrchr (demangled, ':');
19416 if (base && base > demangled && base[-1] == ':')
19421 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19424 obstack_copy0 (&objfile->per_bfd->storage_obstack,
19425 base, strlen (base)));
19433 /* Read an attribute value described by an attribute form. */
19435 static const gdb_byte *
19436 read_attribute_value (const struct die_reader_specs *reader,
19437 struct attribute *attr, unsigned form,
19438 LONGEST implicit_const, const gdb_byte *info_ptr)
19440 struct dwarf2_cu *cu = reader->cu;
19441 struct dwarf2_per_objfile *dwarf2_per_objfile
19442 = cu->per_cu->dwarf2_per_objfile;
19443 struct objfile *objfile = dwarf2_per_objfile->objfile;
19444 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19445 bfd *abfd = reader->abfd;
19446 struct comp_unit_head *cu_header = &cu->header;
19447 unsigned int bytes_read;
19448 struct dwarf_block *blk;
19450 attr->form = (enum dwarf_form) form;
19453 case DW_FORM_ref_addr:
19454 if (cu->header.version == 2)
19455 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19457 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19458 &cu->header, &bytes_read);
19459 info_ptr += bytes_read;
19461 case DW_FORM_GNU_ref_alt:
19462 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19463 info_ptr += bytes_read;
19466 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19467 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19468 info_ptr += bytes_read;
19470 case DW_FORM_block2:
19471 blk = dwarf_alloc_block (cu);
19472 blk->size = read_2_bytes (abfd, info_ptr);
19474 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19475 info_ptr += blk->size;
19476 DW_BLOCK (attr) = blk;
19478 case DW_FORM_block4:
19479 blk = dwarf_alloc_block (cu);
19480 blk->size = read_4_bytes (abfd, info_ptr);
19482 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19483 info_ptr += blk->size;
19484 DW_BLOCK (attr) = blk;
19486 case DW_FORM_data2:
19487 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19490 case DW_FORM_data4:
19491 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19494 case DW_FORM_data8:
19495 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19498 case DW_FORM_data16:
19499 blk = dwarf_alloc_block (cu);
19501 blk->data = read_n_bytes (abfd, info_ptr, 16);
19503 DW_BLOCK (attr) = blk;
19505 case DW_FORM_sec_offset:
19506 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19507 info_ptr += bytes_read;
19509 case DW_FORM_string:
19510 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19511 DW_STRING_IS_CANONICAL (attr) = 0;
19512 info_ptr += bytes_read;
19515 if (!cu->per_cu->is_dwz)
19517 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19518 abfd, info_ptr, cu_header,
19520 DW_STRING_IS_CANONICAL (attr) = 0;
19521 info_ptr += bytes_read;
19525 case DW_FORM_line_strp:
19526 if (!cu->per_cu->is_dwz)
19528 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19530 cu_header, &bytes_read);
19531 DW_STRING_IS_CANONICAL (attr) = 0;
19532 info_ptr += bytes_read;
19536 case DW_FORM_GNU_strp_alt:
19538 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19539 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19542 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19544 DW_STRING_IS_CANONICAL (attr) = 0;
19545 info_ptr += bytes_read;
19548 case DW_FORM_exprloc:
19549 case DW_FORM_block:
19550 blk = dwarf_alloc_block (cu);
19551 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19552 info_ptr += bytes_read;
19553 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19554 info_ptr += blk->size;
19555 DW_BLOCK (attr) = blk;
19557 case DW_FORM_block1:
19558 blk = dwarf_alloc_block (cu);
19559 blk->size = read_1_byte (abfd, info_ptr);
19561 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19562 info_ptr += blk->size;
19563 DW_BLOCK (attr) = blk;
19565 case DW_FORM_data1:
19566 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19570 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19573 case DW_FORM_flag_present:
19574 DW_UNSND (attr) = 1;
19576 case DW_FORM_sdata:
19577 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19578 info_ptr += bytes_read;
19580 case DW_FORM_udata:
19581 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19582 info_ptr += bytes_read;
19585 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19586 + read_1_byte (abfd, info_ptr));
19590 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19591 + read_2_bytes (abfd, info_ptr));
19595 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19596 + read_4_bytes (abfd, info_ptr));
19600 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19601 + read_8_bytes (abfd, info_ptr));
19604 case DW_FORM_ref_sig8:
19605 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19608 case DW_FORM_ref_udata:
19609 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19610 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19611 info_ptr += bytes_read;
19613 case DW_FORM_indirect:
19614 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19615 info_ptr += bytes_read;
19616 if (form == DW_FORM_implicit_const)
19618 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19619 info_ptr += bytes_read;
19621 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19624 case DW_FORM_implicit_const:
19625 DW_SND (attr) = implicit_const;
19627 case DW_FORM_GNU_addr_index:
19628 if (reader->dwo_file == NULL)
19630 /* For now flag a hard error.
19631 Later we can turn this into a complaint. */
19632 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19633 dwarf_form_name (form),
19634 bfd_get_filename (abfd));
19636 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19637 info_ptr += bytes_read;
19639 case DW_FORM_GNU_str_index:
19640 if (reader->dwo_file == NULL)
19642 /* For now flag a hard error.
19643 Later we can turn this into a complaint if warranted. */
19644 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19645 dwarf_form_name (form),
19646 bfd_get_filename (abfd));
19649 ULONGEST str_index =
19650 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19652 DW_STRING (attr) = read_str_index (reader, str_index);
19653 DW_STRING_IS_CANONICAL (attr) = 0;
19654 info_ptr += bytes_read;
19658 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19659 dwarf_form_name (form),
19660 bfd_get_filename (abfd));
19664 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19665 attr->form = DW_FORM_GNU_ref_alt;
19667 /* We have seen instances where the compiler tried to emit a byte
19668 size attribute of -1 which ended up being encoded as an unsigned
19669 0xffffffff. Although 0xffffffff is technically a valid size value,
19670 an object of this size seems pretty unlikely so we can relatively
19671 safely treat these cases as if the size attribute was invalid and
19672 treat them as zero by default. */
19673 if (attr->name == DW_AT_byte_size
19674 && form == DW_FORM_data4
19675 && DW_UNSND (attr) >= 0xffffffff)
19678 (&symfile_complaints,
19679 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19680 hex_string (DW_UNSND (attr)));
19681 DW_UNSND (attr) = 0;
19687 /* Read an attribute described by an abbreviated attribute. */
19689 static const gdb_byte *
19690 read_attribute (const struct die_reader_specs *reader,
19691 struct attribute *attr, struct attr_abbrev *abbrev,
19692 const gdb_byte *info_ptr)
19694 attr->name = abbrev->name;
19695 return read_attribute_value (reader, attr, abbrev->form,
19696 abbrev->implicit_const, info_ptr);
19699 /* Read dwarf information from a buffer. */
19701 static unsigned int
19702 read_1_byte (bfd *abfd, const gdb_byte *buf)
19704 return bfd_get_8 (abfd, buf);
19708 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19710 return bfd_get_signed_8 (abfd, buf);
19713 static unsigned int
19714 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19716 return bfd_get_16 (abfd, buf);
19720 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19722 return bfd_get_signed_16 (abfd, buf);
19725 static unsigned int
19726 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19728 return bfd_get_32 (abfd, buf);
19732 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19734 return bfd_get_signed_32 (abfd, buf);
19738 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19740 return bfd_get_64 (abfd, buf);
19744 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19745 unsigned int *bytes_read)
19747 struct comp_unit_head *cu_header = &cu->header;
19748 CORE_ADDR retval = 0;
19750 if (cu_header->signed_addr_p)
19752 switch (cu_header->addr_size)
19755 retval = bfd_get_signed_16 (abfd, buf);
19758 retval = bfd_get_signed_32 (abfd, buf);
19761 retval = bfd_get_signed_64 (abfd, buf);
19764 internal_error (__FILE__, __LINE__,
19765 _("read_address: bad switch, signed [in module %s]"),
19766 bfd_get_filename (abfd));
19771 switch (cu_header->addr_size)
19774 retval = bfd_get_16 (abfd, buf);
19777 retval = bfd_get_32 (abfd, buf);
19780 retval = bfd_get_64 (abfd, buf);
19783 internal_error (__FILE__, __LINE__,
19784 _("read_address: bad switch, "
19785 "unsigned [in module %s]"),
19786 bfd_get_filename (abfd));
19790 *bytes_read = cu_header->addr_size;
19794 /* Read the initial length from a section. The (draft) DWARF 3
19795 specification allows the initial length to take up either 4 bytes
19796 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19797 bytes describe the length and all offsets will be 8 bytes in length
19800 An older, non-standard 64-bit format is also handled by this
19801 function. The older format in question stores the initial length
19802 as an 8-byte quantity without an escape value. Lengths greater
19803 than 2^32 aren't very common which means that the initial 4 bytes
19804 is almost always zero. Since a length value of zero doesn't make
19805 sense for the 32-bit format, this initial zero can be considered to
19806 be an escape value which indicates the presence of the older 64-bit
19807 format. As written, the code can't detect (old format) lengths
19808 greater than 4GB. If it becomes necessary to handle lengths
19809 somewhat larger than 4GB, we could allow other small values (such
19810 as the non-sensical values of 1, 2, and 3) to also be used as
19811 escape values indicating the presence of the old format.
19813 The value returned via bytes_read should be used to increment the
19814 relevant pointer after calling read_initial_length().
19816 [ Note: read_initial_length() and read_offset() are based on the
19817 document entitled "DWARF Debugging Information Format", revision
19818 3, draft 8, dated November 19, 2001. This document was obtained
19821 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19823 This document is only a draft and is subject to change. (So beware.)
19825 Details regarding the older, non-standard 64-bit format were
19826 determined empirically by examining 64-bit ELF files produced by
19827 the SGI toolchain on an IRIX 6.5 machine.
19829 - Kevin, July 16, 2002
19833 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19835 LONGEST length = bfd_get_32 (abfd, buf);
19837 if (length == 0xffffffff)
19839 length = bfd_get_64 (abfd, buf + 4);
19842 else if (length == 0)
19844 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19845 length = bfd_get_64 (abfd, buf);
19856 /* Cover function for read_initial_length.
19857 Returns the length of the object at BUF, and stores the size of the
19858 initial length in *BYTES_READ and stores the size that offsets will be in
19860 If the initial length size is not equivalent to that specified in
19861 CU_HEADER then issue a complaint.
19862 This is useful when reading non-comp-unit headers. */
19865 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19866 const struct comp_unit_head *cu_header,
19867 unsigned int *bytes_read,
19868 unsigned int *offset_size)
19870 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19872 gdb_assert (cu_header->initial_length_size == 4
19873 || cu_header->initial_length_size == 8
19874 || cu_header->initial_length_size == 12);
19876 if (cu_header->initial_length_size != *bytes_read)
19877 complaint (&symfile_complaints,
19878 _("intermixed 32-bit and 64-bit DWARF sections"));
19880 *offset_size = (*bytes_read == 4) ? 4 : 8;
19884 /* Read an offset from the data stream. The size of the offset is
19885 given by cu_header->offset_size. */
19888 read_offset (bfd *abfd, const gdb_byte *buf,
19889 const struct comp_unit_head *cu_header,
19890 unsigned int *bytes_read)
19892 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19894 *bytes_read = cu_header->offset_size;
19898 /* Read an offset from the data stream. */
19901 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19903 LONGEST retval = 0;
19905 switch (offset_size)
19908 retval = bfd_get_32 (abfd, buf);
19911 retval = bfd_get_64 (abfd, buf);
19914 internal_error (__FILE__, __LINE__,
19915 _("read_offset_1: bad switch [in module %s]"),
19916 bfd_get_filename (abfd));
19922 static const gdb_byte *
19923 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19925 /* If the size of a host char is 8 bits, we can return a pointer
19926 to the buffer, otherwise we have to copy the data to a buffer
19927 allocated on the temporary obstack. */
19928 gdb_assert (HOST_CHAR_BIT == 8);
19932 static const char *
19933 read_direct_string (bfd *abfd, const gdb_byte *buf,
19934 unsigned int *bytes_read_ptr)
19936 /* If the size of a host char is 8 bits, we can return a pointer
19937 to the string, otherwise we have to copy the string to a buffer
19938 allocated on the temporary obstack. */
19939 gdb_assert (HOST_CHAR_BIT == 8);
19942 *bytes_read_ptr = 1;
19945 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19946 return (const char *) buf;
19949 /* Return pointer to string at section SECT offset STR_OFFSET with error
19950 reporting strings FORM_NAME and SECT_NAME. */
19952 static const char *
19953 read_indirect_string_at_offset_from (struct objfile *objfile,
19954 bfd *abfd, LONGEST str_offset,
19955 struct dwarf2_section_info *sect,
19956 const char *form_name,
19957 const char *sect_name)
19959 dwarf2_read_section (objfile, sect);
19960 if (sect->buffer == NULL)
19961 error (_("%s used without %s section [in module %s]"),
19962 form_name, sect_name, bfd_get_filename (abfd));
19963 if (str_offset >= sect->size)
19964 error (_("%s pointing outside of %s section [in module %s]"),
19965 form_name, sect_name, bfd_get_filename (abfd));
19966 gdb_assert (HOST_CHAR_BIT == 8);
19967 if (sect->buffer[str_offset] == '\0')
19969 return (const char *) (sect->buffer + str_offset);
19972 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19974 static const char *
19975 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19976 bfd *abfd, LONGEST str_offset)
19978 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19980 &dwarf2_per_objfile->str,
19981 "DW_FORM_strp", ".debug_str");
19984 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19986 static const char *
19987 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19988 bfd *abfd, LONGEST str_offset)
19990 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19992 &dwarf2_per_objfile->line_str,
19993 "DW_FORM_line_strp",
19994 ".debug_line_str");
19997 /* Read a string at offset STR_OFFSET in the .debug_str section from
19998 the .dwz file DWZ. Throw an error if the offset is too large. If
19999 the string consists of a single NUL byte, return NULL; otherwise
20000 return a pointer to the string. */
20002 static const char *
20003 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
20004 LONGEST str_offset)
20006 dwarf2_read_section (objfile, &dwz->str);
20008 if (dwz->str.buffer == NULL)
20009 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
20010 "section [in module %s]"),
20011 bfd_get_filename (dwz->dwz_bfd));
20012 if (str_offset >= dwz->str.size)
20013 error (_("DW_FORM_GNU_strp_alt pointing outside of "
20014 ".debug_str section [in module %s]"),
20015 bfd_get_filename (dwz->dwz_bfd));
20016 gdb_assert (HOST_CHAR_BIT == 8);
20017 if (dwz->str.buffer[str_offset] == '\0')
20019 return (const char *) (dwz->str.buffer + str_offset);
20022 /* Return pointer to string at .debug_str offset as read from BUF.
20023 BUF is assumed to be in a compilation unit described by CU_HEADER.
20024 Return *BYTES_READ_PTR count of bytes read from BUF. */
20026 static const char *
20027 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
20028 const gdb_byte *buf,
20029 const struct comp_unit_head *cu_header,
20030 unsigned int *bytes_read_ptr)
20032 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20034 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
20037 /* Return pointer to string at .debug_line_str offset as read from BUF.
20038 BUF is assumed to be in a compilation unit described by CU_HEADER.
20039 Return *BYTES_READ_PTR count of bytes read from BUF. */
20041 static const char *
20042 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
20043 bfd *abfd, const gdb_byte *buf,
20044 const struct comp_unit_head *cu_header,
20045 unsigned int *bytes_read_ptr)
20047 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20049 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
20054 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
20055 unsigned int *bytes_read_ptr)
20058 unsigned int num_read;
20060 unsigned char byte;
20067 byte = bfd_get_8 (abfd, buf);
20070 result |= ((ULONGEST) (byte & 127) << shift);
20071 if ((byte & 128) == 0)
20077 *bytes_read_ptr = num_read;
20082 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
20083 unsigned int *bytes_read_ptr)
20086 int shift, num_read;
20087 unsigned char byte;
20094 byte = bfd_get_8 (abfd, buf);
20097 result |= ((LONGEST) (byte & 127) << shift);
20099 if ((byte & 128) == 0)
20104 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
20105 result |= -(((LONGEST) 1) << shift);
20106 *bytes_read_ptr = num_read;
20110 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20111 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
20112 ADDR_SIZE is the size of addresses from the CU header. */
20115 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
20116 unsigned int addr_index, ULONGEST addr_base, int addr_size)
20118 struct objfile *objfile = dwarf2_per_objfile->objfile;
20119 bfd *abfd = objfile->obfd;
20120 const gdb_byte *info_ptr;
20122 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20123 if (dwarf2_per_objfile->addr.buffer == NULL)
20124 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20125 objfile_name (objfile));
20126 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
20127 error (_("DW_FORM_addr_index pointing outside of "
20128 ".debug_addr section [in module %s]"),
20129 objfile_name (objfile));
20130 info_ptr = (dwarf2_per_objfile->addr.buffer
20131 + addr_base + addr_index * addr_size);
20132 if (addr_size == 4)
20133 return bfd_get_32 (abfd, info_ptr);
20135 return bfd_get_64 (abfd, info_ptr);
20138 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20141 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20143 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20144 cu->addr_base, cu->header.addr_size);
20147 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20150 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20151 unsigned int *bytes_read)
20153 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
20154 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20156 return read_addr_index (cu, addr_index);
20159 /* Data structure to pass results from dwarf2_read_addr_index_reader
20160 back to dwarf2_read_addr_index. */
20162 struct dwarf2_read_addr_index_data
20164 ULONGEST addr_base;
20168 /* die_reader_func for dwarf2_read_addr_index. */
20171 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
20172 const gdb_byte *info_ptr,
20173 struct die_info *comp_unit_die,
20177 struct dwarf2_cu *cu = reader->cu;
20178 struct dwarf2_read_addr_index_data *aidata =
20179 (struct dwarf2_read_addr_index_data *) data;
20181 aidata->addr_base = cu->addr_base;
20182 aidata->addr_size = cu->header.addr_size;
20185 /* Given an index in .debug_addr, fetch the value.
20186 NOTE: This can be called during dwarf expression evaluation,
20187 long after the debug information has been read, and thus per_cu->cu
20188 may no longer exist. */
20191 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20192 unsigned int addr_index)
20194 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
20195 struct objfile *objfile = dwarf2_per_objfile->objfile;
20196 struct dwarf2_cu *cu = per_cu->cu;
20197 ULONGEST addr_base;
20200 /* We need addr_base and addr_size.
20201 If we don't have PER_CU->cu, we have to get it.
20202 Nasty, but the alternative is storing the needed info in PER_CU,
20203 which at this point doesn't seem justified: it's not clear how frequently
20204 it would get used and it would increase the size of every PER_CU.
20205 Entry points like dwarf2_per_cu_addr_size do a similar thing
20206 so we're not in uncharted territory here.
20207 Alas we need to be a bit more complicated as addr_base is contained
20210 We don't need to read the entire CU(/TU).
20211 We just need the header and top level die.
20213 IWBN to use the aging mechanism to let us lazily later discard the CU.
20214 For now we skip this optimization. */
20218 addr_base = cu->addr_base;
20219 addr_size = cu->header.addr_size;
20223 struct dwarf2_read_addr_index_data aidata;
20225 /* Note: We can't use init_cutu_and_read_dies_simple here,
20226 we need addr_base. */
20227 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
20228 dwarf2_read_addr_index_reader, &aidata);
20229 addr_base = aidata.addr_base;
20230 addr_size = aidata.addr_size;
20233 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20237 /* Given a DW_FORM_GNU_str_index, fetch the string.
20238 This is only used by the Fission support. */
20240 static const char *
20241 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20243 struct dwarf2_cu *cu = reader->cu;
20244 struct dwarf2_per_objfile *dwarf2_per_objfile
20245 = cu->per_cu->dwarf2_per_objfile;
20246 struct objfile *objfile = dwarf2_per_objfile->objfile;
20247 const char *objf_name = objfile_name (objfile);
20248 bfd *abfd = objfile->obfd;
20249 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20250 struct dwarf2_section_info *str_offsets_section =
20251 &reader->dwo_file->sections.str_offsets;
20252 const gdb_byte *info_ptr;
20253 ULONGEST str_offset;
20254 static const char form_name[] = "DW_FORM_GNU_str_index";
20256 dwarf2_read_section (objfile, str_section);
20257 dwarf2_read_section (objfile, str_offsets_section);
20258 if (str_section->buffer == NULL)
20259 error (_("%s used without .debug_str.dwo section"
20260 " in CU at offset %s [in module %s]"),
20261 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20262 if (str_offsets_section->buffer == NULL)
20263 error (_("%s used without .debug_str_offsets.dwo section"
20264 " in CU at offset %s [in module %s]"),
20265 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20266 if (str_index * cu->header.offset_size >= str_offsets_section->size)
20267 error (_("%s pointing outside of .debug_str_offsets.dwo"
20268 " section in CU at offset %s [in module %s]"),
20269 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20270 info_ptr = (str_offsets_section->buffer
20271 + str_index * cu->header.offset_size);
20272 if (cu->header.offset_size == 4)
20273 str_offset = bfd_get_32 (abfd, info_ptr);
20275 str_offset = bfd_get_64 (abfd, info_ptr);
20276 if (str_offset >= str_section->size)
20277 error (_("Offset from %s pointing outside of"
20278 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20279 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20280 return (const char *) (str_section->buffer + str_offset);
20283 /* Return the length of an LEB128 number in BUF. */
20286 leb128_size (const gdb_byte *buf)
20288 const gdb_byte *begin = buf;
20294 if ((byte & 128) == 0)
20295 return buf - begin;
20300 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20309 cu->language = language_c;
20312 case DW_LANG_C_plus_plus:
20313 case DW_LANG_C_plus_plus_11:
20314 case DW_LANG_C_plus_plus_14:
20315 cu->language = language_cplus;
20318 cu->language = language_d;
20320 case DW_LANG_Fortran77:
20321 case DW_LANG_Fortran90:
20322 case DW_LANG_Fortran95:
20323 case DW_LANG_Fortran03:
20324 case DW_LANG_Fortran08:
20325 cu->language = language_fortran;
20328 cu->language = language_go;
20330 case DW_LANG_Mips_Assembler:
20331 cu->language = language_asm;
20333 case DW_LANG_Ada83:
20334 case DW_LANG_Ada95:
20335 cu->language = language_ada;
20337 case DW_LANG_Modula2:
20338 cu->language = language_m2;
20340 case DW_LANG_Pascal83:
20341 cu->language = language_pascal;
20344 cu->language = language_objc;
20347 case DW_LANG_Rust_old:
20348 cu->language = language_rust;
20350 case DW_LANG_Cobol74:
20351 case DW_LANG_Cobol85:
20353 cu->language = language_minimal;
20356 cu->language_defn = language_def (cu->language);
20359 /* Return the named attribute or NULL if not there. */
20361 static struct attribute *
20362 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20367 struct attribute *spec = NULL;
20369 for (i = 0; i < die->num_attrs; ++i)
20371 if (die->attrs[i].name == name)
20372 return &die->attrs[i];
20373 if (die->attrs[i].name == DW_AT_specification
20374 || die->attrs[i].name == DW_AT_abstract_origin)
20375 spec = &die->attrs[i];
20381 die = follow_die_ref (die, spec, &cu);
20387 /* Return the named attribute or NULL if not there,
20388 but do not follow DW_AT_specification, etc.
20389 This is for use in contexts where we're reading .debug_types dies.
20390 Following DW_AT_specification, DW_AT_abstract_origin will take us
20391 back up the chain, and we want to go down. */
20393 static struct attribute *
20394 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
20398 for (i = 0; i < die->num_attrs; ++i)
20399 if (die->attrs[i].name == name)
20400 return &die->attrs[i];
20405 /* Return the string associated with a string-typed attribute, or NULL if it
20406 is either not found or is of an incorrect type. */
20408 static const char *
20409 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20411 struct attribute *attr;
20412 const char *str = NULL;
20414 attr = dwarf2_attr (die, name, cu);
20418 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20419 || attr->form == DW_FORM_string
20420 || attr->form == DW_FORM_GNU_str_index
20421 || attr->form == DW_FORM_GNU_strp_alt)
20422 str = DW_STRING (attr);
20424 complaint (&symfile_complaints,
20425 _("string type expected for attribute %s for "
20426 "DIE at %s in module %s"),
20427 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20428 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
20434 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20435 and holds a non-zero value. This function should only be used for
20436 DW_FORM_flag or DW_FORM_flag_present attributes. */
20439 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20441 struct attribute *attr = dwarf2_attr (die, name, cu);
20443 return (attr && DW_UNSND (attr));
20447 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20449 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20450 which value is non-zero. However, we have to be careful with
20451 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20452 (via dwarf2_flag_true_p) follows this attribute. So we may
20453 end up accidently finding a declaration attribute that belongs
20454 to a different DIE referenced by the specification attribute,
20455 even though the given DIE does not have a declaration attribute. */
20456 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20457 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20460 /* Return the die giving the specification for DIE, if there is
20461 one. *SPEC_CU is the CU containing DIE on input, and the CU
20462 containing the return value on output. If there is no
20463 specification, but there is an abstract origin, that is
20466 static struct die_info *
20467 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20469 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20472 if (spec_attr == NULL)
20473 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20475 if (spec_attr == NULL)
20478 return follow_die_ref (die, spec_attr, spec_cu);
20481 /* Stub for free_line_header to match void * callback types. */
20484 free_line_header_voidp (void *arg)
20486 struct line_header *lh = (struct line_header *) arg;
20492 line_header::add_include_dir (const char *include_dir)
20494 if (dwarf_line_debug >= 2)
20495 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20496 include_dirs.size () + 1, include_dir);
20498 include_dirs.push_back (include_dir);
20502 line_header::add_file_name (const char *name,
20504 unsigned int mod_time,
20505 unsigned int length)
20507 if (dwarf_line_debug >= 2)
20508 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
20509 (unsigned) file_names.size () + 1, name);
20511 file_names.emplace_back (name, d_index, mod_time, length);
20514 /* A convenience function to find the proper .debug_line section for a CU. */
20516 static struct dwarf2_section_info *
20517 get_debug_line_section (struct dwarf2_cu *cu)
20519 struct dwarf2_section_info *section;
20520 struct dwarf2_per_objfile *dwarf2_per_objfile
20521 = cu->per_cu->dwarf2_per_objfile;
20523 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20525 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20526 section = &cu->dwo_unit->dwo_file->sections.line;
20527 else if (cu->per_cu->is_dwz)
20529 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20531 section = &dwz->line;
20534 section = &dwarf2_per_objfile->line;
20539 /* Read directory or file name entry format, starting with byte of
20540 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20541 entries count and the entries themselves in the described entry
20545 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20546 bfd *abfd, const gdb_byte **bufp,
20547 struct line_header *lh,
20548 const struct comp_unit_head *cu_header,
20549 void (*callback) (struct line_header *lh,
20552 unsigned int mod_time,
20553 unsigned int length))
20555 gdb_byte format_count, formati;
20556 ULONGEST data_count, datai;
20557 const gdb_byte *buf = *bufp;
20558 const gdb_byte *format_header_data;
20559 unsigned int bytes_read;
20561 format_count = read_1_byte (abfd, buf);
20563 format_header_data = buf;
20564 for (formati = 0; formati < format_count; formati++)
20566 read_unsigned_leb128 (abfd, buf, &bytes_read);
20568 read_unsigned_leb128 (abfd, buf, &bytes_read);
20572 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20574 for (datai = 0; datai < data_count; datai++)
20576 const gdb_byte *format = format_header_data;
20577 struct file_entry fe;
20579 for (formati = 0; formati < format_count; formati++)
20581 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20582 format += bytes_read;
20584 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20585 format += bytes_read;
20587 gdb::optional<const char *> string;
20588 gdb::optional<unsigned int> uint;
20592 case DW_FORM_string:
20593 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20597 case DW_FORM_line_strp:
20598 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20605 case DW_FORM_data1:
20606 uint.emplace (read_1_byte (abfd, buf));
20610 case DW_FORM_data2:
20611 uint.emplace (read_2_bytes (abfd, buf));
20615 case DW_FORM_data4:
20616 uint.emplace (read_4_bytes (abfd, buf));
20620 case DW_FORM_data8:
20621 uint.emplace (read_8_bytes (abfd, buf));
20625 case DW_FORM_udata:
20626 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20630 case DW_FORM_block:
20631 /* It is valid only for DW_LNCT_timestamp which is ignored by
20636 switch (content_type)
20639 if (string.has_value ())
20642 case DW_LNCT_directory_index:
20643 if (uint.has_value ())
20644 fe.d_index = (dir_index) *uint;
20646 case DW_LNCT_timestamp:
20647 if (uint.has_value ())
20648 fe.mod_time = *uint;
20651 if (uint.has_value ())
20657 complaint (&symfile_complaints,
20658 _("Unknown format content type %s"),
20659 pulongest (content_type));
20663 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20669 /* Read the statement program header starting at OFFSET in
20670 .debug_line, or .debug_line.dwo. Return a pointer
20671 to a struct line_header, allocated using xmalloc.
20672 Returns NULL if there is a problem reading the header, e.g., if it
20673 has a version we don't understand.
20675 NOTE: the strings in the include directory and file name tables of
20676 the returned object point into the dwarf line section buffer,
20677 and must not be freed. */
20679 static line_header_up
20680 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20682 const gdb_byte *line_ptr;
20683 unsigned int bytes_read, offset_size;
20685 const char *cur_dir, *cur_file;
20686 struct dwarf2_section_info *section;
20688 struct dwarf2_per_objfile *dwarf2_per_objfile
20689 = cu->per_cu->dwarf2_per_objfile;
20691 section = get_debug_line_section (cu);
20692 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20693 if (section->buffer == NULL)
20695 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20696 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20698 complaint (&symfile_complaints, _("missing .debug_line section"));
20702 /* We can't do this until we know the section is non-empty.
20703 Only then do we know we have such a section. */
20704 abfd = get_section_bfd_owner (section);
20706 /* Make sure that at least there's room for the total_length field.
20707 That could be 12 bytes long, but we're just going to fudge that. */
20708 if (to_underlying (sect_off) + 4 >= section->size)
20710 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20714 line_header_up lh (new line_header ());
20716 lh->sect_off = sect_off;
20717 lh->offset_in_dwz = cu->per_cu->is_dwz;
20719 line_ptr = section->buffer + to_underlying (sect_off);
20721 /* Read in the header. */
20723 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20724 &bytes_read, &offset_size);
20725 line_ptr += bytes_read;
20726 if (line_ptr + lh->total_length > (section->buffer + section->size))
20728 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20731 lh->statement_program_end = line_ptr + lh->total_length;
20732 lh->version = read_2_bytes (abfd, line_ptr);
20734 if (lh->version > 5)
20736 /* This is a version we don't understand. The format could have
20737 changed in ways we don't handle properly so just punt. */
20738 complaint (&symfile_complaints,
20739 _("unsupported version in .debug_line section"));
20742 if (lh->version >= 5)
20744 gdb_byte segment_selector_size;
20746 /* Skip address size. */
20747 read_1_byte (abfd, line_ptr);
20750 segment_selector_size = read_1_byte (abfd, line_ptr);
20752 if (segment_selector_size != 0)
20754 complaint (&symfile_complaints,
20755 _("unsupported segment selector size %u "
20756 "in .debug_line section"),
20757 segment_selector_size);
20761 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20762 line_ptr += offset_size;
20763 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20765 if (lh->version >= 4)
20767 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20771 lh->maximum_ops_per_instruction = 1;
20773 if (lh->maximum_ops_per_instruction == 0)
20775 lh->maximum_ops_per_instruction = 1;
20776 complaint (&symfile_complaints,
20777 _("invalid maximum_ops_per_instruction "
20778 "in `.debug_line' section"));
20781 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20783 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20785 lh->line_range = read_1_byte (abfd, line_ptr);
20787 lh->opcode_base = read_1_byte (abfd, line_ptr);
20789 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20791 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20792 for (i = 1; i < lh->opcode_base; ++i)
20794 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20798 if (lh->version >= 5)
20800 /* Read directory table. */
20801 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20803 [] (struct line_header *lh, const char *name,
20804 dir_index d_index, unsigned int mod_time,
20805 unsigned int length)
20807 lh->add_include_dir (name);
20810 /* Read file name table. */
20811 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20813 [] (struct line_header *lh, const char *name,
20814 dir_index d_index, unsigned int mod_time,
20815 unsigned int length)
20817 lh->add_file_name (name, d_index, mod_time, length);
20822 /* Read directory table. */
20823 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20825 line_ptr += bytes_read;
20826 lh->add_include_dir (cur_dir);
20828 line_ptr += bytes_read;
20830 /* Read file name table. */
20831 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20833 unsigned int mod_time, length;
20836 line_ptr += bytes_read;
20837 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20838 line_ptr += bytes_read;
20839 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20840 line_ptr += bytes_read;
20841 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20842 line_ptr += bytes_read;
20844 lh->add_file_name (cur_file, d_index, mod_time, length);
20846 line_ptr += bytes_read;
20848 lh->statement_program_start = line_ptr;
20850 if (line_ptr > (section->buffer + section->size))
20851 complaint (&symfile_complaints,
20852 _("line number info header doesn't "
20853 "fit in `.debug_line' section"));
20858 /* Subroutine of dwarf_decode_lines to simplify it.
20859 Return the file name of the psymtab for included file FILE_INDEX
20860 in line header LH of PST.
20861 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20862 If space for the result is malloc'd, *NAME_HOLDER will be set.
20863 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20865 static const char *
20866 psymtab_include_file_name (const struct line_header *lh, int file_index,
20867 const struct partial_symtab *pst,
20868 const char *comp_dir,
20869 gdb::unique_xmalloc_ptr<char> *name_holder)
20871 const file_entry &fe = lh->file_names[file_index];
20872 const char *include_name = fe.name;
20873 const char *include_name_to_compare = include_name;
20874 const char *pst_filename;
20877 const char *dir_name = fe.include_dir (lh);
20879 gdb::unique_xmalloc_ptr<char> hold_compare;
20880 if (!IS_ABSOLUTE_PATH (include_name)
20881 && (dir_name != NULL || comp_dir != NULL))
20883 /* Avoid creating a duplicate psymtab for PST.
20884 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20885 Before we do the comparison, however, we need to account
20886 for DIR_NAME and COMP_DIR.
20887 First prepend dir_name (if non-NULL). If we still don't
20888 have an absolute path prepend comp_dir (if non-NULL).
20889 However, the directory we record in the include-file's
20890 psymtab does not contain COMP_DIR (to match the
20891 corresponding symtab(s)).
20896 bash$ gcc -g ./hello.c
20897 include_name = "hello.c"
20899 DW_AT_comp_dir = comp_dir = "/tmp"
20900 DW_AT_name = "./hello.c"
20904 if (dir_name != NULL)
20906 name_holder->reset (concat (dir_name, SLASH_STRING,
20907 include_name, (char *) NULL));
20908 include_name = name_holder->get ();
20909 include_name_to_compare = include_name;
20911 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20913 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20914 include_name, (char *) NULL));
20915 include_name_to_compare = hold_compare.get ();
20919 pst_filename = pst->filename;
20920 gdb::unique_xmalloc_ptr<char> copied_name;
20921 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20923 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20924 pst_filename, (char *) NULL));
20925 pst_filename = copied_name.get ();
20928 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20932 return include_name;
20935 /* State machine to track the state of the line number program. */
20937 class lnp_state_machine
20940 /* Initialize a machine state for the start of a line number
20942 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20944 file_entry *current_file ()
20946 /* lh->file_names is 0-based, but the file name numbers in the
20947 statement program are 1-based. */
20948 return m_line_header->file_name_at (m_file);
20951 /* Record the line in the state machine. END_SEQUENCE is true if
20952 we're processing the end of a sequence. */
20953 void record_line (bool end_sequence);
20955 /* Check address and if invalid nop-out the rest of the lines in this
20957 void check_line_address (struct dwarf2_cu *cu,
20958 const gdb_byte *line_ptr,
20959 CORE_ADDR lowpc, CORE_ADDR address);
20961 void handle_set_discriminator (unsigned int discriminator)
20963 m_discriminator = discriminator;
20964 m_line_has_non_zero_discriminator |= discriminator != 0;
20967 /* Handle DW_LNE_set_address. */
20968 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20971 address += baseaddr;
20972 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20975 /* Handle DW_LNS_advance_pc. */
20976 void handle_advance_pc (CORE_ADDR adjust);
20978 /* Handle a special opcode. */
20979 void handle_special_opcode (unsigned char op_code);
20981 /* Handle DW_LNS_advance_line. */
20982 void handle_advance_line (int line_delta)
20984 advance_line (line_delta);
20987 /* Handle DW_LNS_set_file. */
20988 void handle_set_file (file_name_index file);
20990 /* Handle DW_LNS_negate_stmt. */
20991 void handle_negate_stmt ()
20993 m_is_stmt = !m_is_stmt;
20996 /* Handle DW_LNS_const_add_pc. */
20997 void handle_const_add_pc ();
20999 /* Handle DW_LNS_fixed_advance_pc. */
21000 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21002 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21006 /* Handle DW_LNS_copy. */
21007 void handle_copy ()
21009 record_line (false);
21010 m_discriminator = 0;
21013 /* Handle DW_LNE_end_sequence. */
21014 void handle_end_sequence ()
21016 m_record_line_callback = ::record_line;
21020 /* Advance the line by LINE_DELTA. */
21021 void advance_line (int line_delta)
21023 m_line += line_delta;
21025 if (line_delta != 0)
21026 m_line_has_non_zero_discriminator = m_discriminator != 0;
21029 gdbarch *m_gdbarch;
21031 /* True if we're recording lines.
21032 Otherwise we're building partial symtabs and are just interested in
21033 finding include files mentioned by the line number program. */
21034 bool m_record_lines_p;
21036 /* The line number header. */
21037 line_header *m_line_header;
21039 /* These are part of the standard DWARF line number state machine,
21040 and initialized according to the DWARF spec. */
21042 unsigned char m_op_index = 0;
21043 /* The line table index (1-based) of the current file. */
21044 file_name_index m_file = (file_name_index) 1;
21045 unsigned int m_line = 1;
21047 /* These are initialized in the constructor. */
21049 CORE_ADDR m_address;
21051 unsigned int m_discriminator;
21053 /* Additional bits of state we need to track. */
21055 /* The last file that we called dwarf2_start_subfile for.
21056 This is only used for TLLs. */
21057 unsigned int m_last_file = 0;
21058 /* The last file a line number was recorded for. */
21059 struct subfile *m_last_subfile = NULL;
21061 /* The function to call to record a line. */
21062 record_line_ftype *m_record_line_callback = NULL;
21064 /* The last line number that was recorded, used to coalesce
21065 consecutive entries for the same line. This can happen, for
21066 example, when discriminators are present. PR 17276. */
21067 unsigned int m_last_line = 0;
21068 bool m_line_has_non_zero_discriminator = false;
21072 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21074 CORE_ADDR addr_adj = (((m_op_index + adjust)
21075 / m_line_header->maximum_ops_per_instruction)
21076 * m_line_header->minimum_instruction_length);
21077 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21078 m_op_index = ((m_op_index + adjust)
21079 % m_line_header->maximum_ops_per_instruction);
21083 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21085 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21086 CORE_ADDR addr_adj = (((m_op_index
21087 + (adj_opcode / m_line_header->line_range))
21088 / m_line_header->maximum_ops_per_instruction)
21089 * m_line_header->minimum_instruction_length);
21090 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21091 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
21092 % m_line_header->maximum_ops_per_instruction);
21094 int line_delta = (m_line_header->line_base
21095 + (adj_opcode % m_line_header->line_range));
21096 advance_line (line_delta);
21097 record_line (false);
21098 m_discriminator = 0;
21102 lnp_state_machine::handle_set_file (file_name_index file)
21106 const file_entry *fe = current_file ();
21108 dwarf2_debug_line_missing_file_complaint ();
21109 else if (m_record_lines_p)
21111 const char *dir = fe->include_dir (m_line_header);
21113 m_last_subfile = current_subfile;
21114 m_line_has_non_zero_discriminator = m_discriminator != 0;
21115 dwarf2_start_subfile (fe->name, dir);
21120 lnp_state_machine::handle_const_add_pc ()
21123 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21126 = (((m_op_index + adjust)
21127 / m_line_header->maximum_ops_per_instruction)
21128 * m_line_header->minimum_instruction_length);
21130 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21131 m_op_index = ((m_op_index + adjust)
21132 % m_line_header->maximum_ops_per_instruction);
21135 /* Ignore this record_line request. */
21138 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
21143 /* Return non-zero if we should add LINE to the line number table.
21144 LINE is the line to add, LAST_LINE is the last line that was added,
21145 LAST_SUBFILE is the subfile for LAST_LINE.
21146 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21147 had a non-zero discriminator.
21149 We have to be careful in the presence of discriminators.
21150 E.g., for this line:
21152 for (i = 0; i < 100000; i++);
21154 clang can emit four line number entries for that one line,
21155 each with a different discriminator.
21156 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21158 However, we want gdb to coalesce all four entries into one.
21159 Otherwise the user could stepi into the middle of the line and
21160 gdb would get confused about whether the pc really was in the
21161 middle of the line.
21163 Things are further complicated by the fact that two consecutive
21164 line number entries for the same line is a heuristic used by gcc
21165 to denote the end of the prologue. So we can't just discard duplicate
21166 entries, we have to be selective about it. The heuristic we use is
21167 that we only collapse consecutive entries for the same line if at least
21168 one of those entries has a non-zero discriminator. PR 17276.
21170 Note: Addresses in the line number state machine can never go backwards
21171 within one sequence, thus this coalescing is ok. */
21174 dwarf_record_line_p (unsigned int line, unsigned int last_line,
21175 int line_has_non_zero_discriminator,
21176 struct subfile *last_subfile)
21178 if (current_subfile != last_subfile)
21180 if (line != last_line)
21182 /* Same line for the same file that we've seen already.
21183 As a last check, for pr 17276, only record the line if the line
21184 has never had a non-zero discriminator. */
21185 if (!line_has_non_zero_discriminator)
21190 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
21191 in the line table of subfile SUBFILE. */
21194 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21195 unsigned int line, CORE_ADDR address,
21196 record_line_ftype p_record_line)
21198 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21200 if (dwarf_line_debug)
21202 fprintf_unfiltered (gdb_stdlog,
21203 "Recording line %u, file %s, address %s\n",
21204 line, lbasename (subfile->name),
21205 paddress (gdbarch, address));
21208 (*p_record_line) (subfile, line, addr);
21211 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21212 Mark the end of a set of line number records.
21213 The arguments are the same as for dwarf_record_line_1.
21214 If SUBFILE is NULL the request is ignored. */
21217 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21218 CORE_ADDR address, record_line_ftype p_record_line)
21220 if (subfile == NULL)
21223 if (dwarf_line_debug)
21225 fprintf_unfiltered (gdb_stdlog,
21226 "Finishing current line, file %s, address %s\n",
21227 lbasename (subfile->name),
21228 paddress (gdbarch, address));
21231 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
21235 lnp_state_machine::record_line (bool end_sequence)
21237 if (dwarf_line_debug)
21239 fprintf_unfiltered (gdb_stdlog,
21240 "Processing actual line %u: file %u,"
21241 " address %s, is_stmt %u, discrim %u\n",
21242 m_line, to_underlying (m_file),
21243 paddress (m_gdbarch, m_address),
21244 m_is_stmt, m_discriminator);
21247 file_entry *fe = current_file ();
21250 dwarf2_debug_line_missing_file_complaint ();
21251 /* For now we ignore lines not starting on an instruction boundary.
21252 But not when processing end_sequence for compatibility with the
21253 previous version of the code. */
21254 else if (m_op_index == 0 || end_sequence)
21256 fe->included_p = 1;
21257 if (m_record_lines_p && m_is_stmt)
21259 if (m_last_subfile != current_subfile || end_sequence)
21261 dwarf_finish_line (m_gdbarch, m_last_subfile,
21262 m_address, m_record_line_callback);
21267 if (dwarf_record_line_p (m_line, m_last_line,
21268 m_line_has_non_zero_discriminator,
21271 dwarf_record_line_1 (m_gdbarch, current_subfile,
21273 m_record_line_callback);
21275 m_last_subfile = current_subfile;
21276 m_last_line = m_line;
21282 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
21283 bool record_lines_p)
21286 m_record_lines_p = record_lines_p;
21287 m_line_header = lh;
21289 m_record_line_callback = ::record_line;
21291 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21292 was a line entry for it so that the backend has a chance to adjust it
21293 and also record it in case it needs it. This is currently used by MIPS
21294 code, cf. `mips_adjust_dwarf2_line'. */
21295 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21296 m_is_stmt = lh->default_is_stmt;
21297 m_discriminator = 0;
21301 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21302 const gdb_byte *line_ptr,
21303 CORE_ADDR lowpc, CORE_ADDR address)
21305 /* If address < lowpc then it's not a usable value, it's outside the
21306 pc range of the CU. However, we restrict the test to only address
21307 values of zero to preserve GDB's previous behaviour which is to
21308 handle the specific case of a function being GC'd by the linker. */
21310 if (address == 0 && address < lowpc)
21312 /* This line table is for a function which has been
21313 GCd by the linker. Ignore it. PR gdb/12528 */
21315 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21316 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21318 complaint (&symfile_complaints,
21319 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21320 line_offset, objfile_name (objfile));
21321 m_record_line_callback = noop_record_line;
21322 /* Note: record_line_callback is left as noop_record_line until
21323 we see DW_LNE_end_sequence. */
21327 /* Subroutine of dwarf_decode_lines to simplify it.
21328 Process the line number information in LH.
21329 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21330 program in order to set included_p for every referenced header. */
21333 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21334 const int decode_for_pst_p, CORE_ADDR lowpc)
21336 const gdb_byte *line_ptr, *extended_end;
21337 const gdb_byte *line_end;
21338 unsigned int bytes_read, extended_len;
21339 unsigned char op_code, extended_op;
21340 CORE_ADDR baseaddr;
21341 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21342 bfd *abfd = objfile->obfd;
21343 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21344 /* True if we're recording line info (as opposed to building partial
21345 symtabs and just interested in finding include files mentioned by
21346 the line number program). */
21347 bool record_lines_p = !decode_for_pst_p;
21349 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21351 line_ptr = lh->statement_program_start;
21352 line_end = lh->statement_program_end;
21354 /* Read the statement sequences until there's nothing left. */
21355 while (line_ptr < line_end)
21357 /* The DWARF line number program state machine. Reset the state
21358 machine at the start of each sequence. */
21359 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
21360 bool end_sequence = false;
21362 if (record_lines_p)
21364 /* Start a subfile for the current file of the state
21366 const file_entry *fe = state_machine.current_file ();
21369 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
21372 /* Decode the table. */
21373 while (line_ptr < line_end && !end_sequence)
21375 op_code = read_1_byte (abfd, line_ptr);
21378 if (op_code >= lh->opcode_base)
21380 /* Special opcode. */
21381 state_machine.handle_special_opcode (op_code);
21383 else switch (op_code)
21385 case DW_LNS_extended_op:
21386 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21388 line_ptr += bytes_read;
21389 extended_end = line_ptr + extended_len;
21390 extended_op = read_1_byte (abfd, line_ptr);
21392 switch (extended_op)
21394 case DW_LNE_end_sequence:
21395 state_machine.handle_end_sequence ();
21396 end_sequence = true;
21398 case DW_LNE_set_address:
21401 = read_address (abfd, line_ptr, cu, &bytes_read);
21402 line_ptr += bytes_read;
21404 state_machine.check_line_address (cu, line_ptr,
21406 state_machine.handle_set_address (baseaddr, address);
21409 case DW_LNE_define_file:
21411 const char *cur_file;
21412 unsigned int mod_time, length;
21415 cur_file = read_direct_string (abfd, line_ptr,
21417 line_ptr += bytes_read;
21418 dindex = (dir_index)
21419 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21420 line_ptr += bytes_read;
21422 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21423 line_ptr += bytes_read;
21425 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21426 line_ptr += bytes_read;
21427 lh->add_file_name (cur_file, dindex, mod_time, length);
21430 case DW_LNE_set_discriminator:
21432 /* The discriminator is not interesting to the
21433 debugger; just ignore it. We still need to
21434 check its value though:
21435 if there are consecutive entries for the same
21436 (non-prologue) line we want to coalesce them.
21439 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21440 line_ptr += bytes_read;
21442 state_machine.handle_set_discriminator (discr);
21446 complaint (&symfile_complaints,
21447 _("mangled .debug_line section"));
21450 /* Make sure that we parsed the extended op correctly. If e.g.
21451 we expected a different address size than the producer used,
21452 we may have read the wrong number of bytes. */
21453 if (line_ptr != extended_end)
21455 complaint (&symfile_complaints,
21456 _("mangled .debug_line section"));
21461 state_machine.handle_copy ();
21463 case DW_LNS_advance_pc:
21466 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21467 line_ptr += bytes_read;
21469 state_machine.handle_advance_pc (adjust);
21472 case DW_LNS_advance_line:
21475 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21476 line_ptr += bytes_read;
21478 state_machine.handle_advance_line (line_delta);
21481 case DW_LNS_set_file:
21483 file_name_index file
21484 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21486 line_ptr += bytes_read;
21488 state_machine.handle_set_file (file);
21491 case DW_LNS_set_column:
21492 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21493 line_ptr += bytes_read;
21495 case DW_LNS_negate_stmt:
21496 state_machine.handle_negate_stmt ();
21498 case DW_LNS_set_basic_block:
21500 /* Add to the address register of the state machine the
21501 address increment value corresponding to special opcode
21502 255. I.e., this value is scaled by the minimum
21503 instruction length since special opcode 255 would have
21504 scaled the increment. */
21505 case DW_LNS_const_add_pc:
21506 state_machine.handle_const_add_pc ();
21508 case DW_LNS_fixed_advance_pc:
21510 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21513 state_machine.handle_fixed_advance_pc (addr_adj);
21518 /* Unknown standard opcode, ignore it. */
21521 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21523 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21524 line_ptr += bytes_read;
21531 dwarf2_debug_line_missing_end_sequence_complaint ();
21533 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21534 in which case we still finish recording the last line). */
21535 state_machine.record_line (true);
21539 /* Decode the Line Number Program (LNP) for the given line_header
21540 structure and CU. The actual information extracted and the type
21541 of structures created from the LNP depends on the value of PST.
21543 1. If PST is NULL, then this procedure uses the data from the program
21544 to create all necessary symbol tables, and their linetables.
21546 2. If PST is not NULL, this procedure reads the program to determine
21547 the list of files included by the unit represented by PST, and
21548 builds all the associated partial symbol tables.
21550 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21551 It is used for relative paths in the line table.
21552 NOTE: When processing partial symtabs (pst != NULL),
21553 comp_dir == pst->dirname.
21555 NOTE: It is important that psymtabs have the same file name (via strcmp)
21556 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21557 symtab we don't use it in the name of the psymtabs we create.
21558 E.g. expand_line_sal requires this when finding psymtabs to expand.
21559 A good testcase for this is mb-inline.exp.
21561 LOWPC is the lowest address in CU (or 0 if not known).
21563 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21564 for its PC<->lines mapping information. Otherwise only the filename
21565 table is read in. */
21568 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21569 struct dwarf2_cu *cu, struct partial_symtab *pst,
21570 CORE_ADDR lowpc, int decode_mapping)
21572 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21573 const int decode_for_pst_p = (pst != NULL);
21575 if (decode_mapping)
21576 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21578 if (decode_for_pst_p)
21582 /* Now that we're done scanning the Line Header Program, we can
21583 create the psymtab of each included file. */
21584 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21585 if (lh->file_names[file_index].included_p == 1)
21587 gdb::unique_xmalloc_ptr<char> name_holder;
21588 const char *include_name =
21589 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21591 if (include_name != NULL)
21592 dwarf2_create_include_psymtab (include_name, pst, objfile);
21597 /* Make sure a symtab is created for every file, even files
21598 which contain only variables (i.e. no code with associated
21600 struct compunit_symtab *cust = buildsym_compunit_symtab ();
21603 for (i = 0; i < lh->file_names.size (); i++)
21605 file_entry &fe = lh->file_names[i];
21607 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
21609 if (current_subfile->symtab == NULL)
21611 current_subfile->symtab
21612 = allocate_symtab (cust, current_subfile->name);
21614 fe.symtab = current_subfile->symtab;
21619 /* Start a subfile for DWARF. FILENAME is the name of the file and
21620 DIRNAME the name of the source directory which contains FILENAME
21621 or NULL if not known.
21622 This routine tries to keep line numbers from identical absolute and
21623 relative file names in a common subfile.
21625 Using the `list' example from the GDB testsuite, which resides in
21626 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21627 of /srcdir/list0.c yields the following debugging information for list0.c:
21629 DW_AT_name: /srcdir/list0.c
21630 DW_AT_comp_dir: /compdir
21631 files.files[0].name: list0.h
21632 files.files[0].dir: /srcdir
21633 files.files[1].name: list0.c
21634 files.files[1].dir: /srcdir
21636 The line number information for list0.c has to end up in a single
21637 subfile, so that `break /srcdir/list0.c:1' works as expected.
21638 start_subfile will ensure that this happens provided that we pass the
21639 concatenation of files.files[1].dir and files.files[1].name as the
21643 dwarf2_start_subfile (const char *filename, const char *dirname)
21647 /* In order not to lose the line information directory,
21648 we concatenate it to the filename when it makes sense.
21649 Note that the Dwarf3 standard says (speaking of filenames in line
21650 information): ``The directory index is ignored for file names
21651 that represent full path names''. Thus ignoring dirname in the
21652 `else' branch below isn't an issue. */
21654 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21656 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21660 start_subfile (filename);
21666 /* Start a symtab for DWARF.
21667 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21669 static struct compunit_symtab *
21670 dwarf2_start_symtab (struct dwarf2_cu *cu,
21671 const char *name, const char *comp_dir, CORE_ADDR low_pc)
21673 struct compunit_symtab *cust
21674 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21675 low_pc, cu->language);
21677 record_debugformat ("DWARF 2");
21678 record_producer (cu->producer);
21680 /* We assume that we're processing GCC output. */
21681 processing_gcc_compilation = 2;
21683 cu->processing_has_namespace_info = 0;
21689 var_decode_location (struct attribute *attr, struct symbol *sym,
21690 struct dwarf2_cu *cu)
21692 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21693 struct comp_unit_head *cu_header = &cu->header;
21695 /* NOTE drow/2003-01-30: There used to be a comment and some special
21696 code here to turn a symbol with DW_AT_external and a
21697 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21698 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21699 with some versions of binutils) where shared libraries could have
21700 relocations against symbols in their debug information - the
21701 minimal symbol would have the right address, but the debug info
21702 would not. It's no longer necessary, because we will explicitly
21703 apply relocations when we read in the debug information now. */
21705 /* A DW_AT_location attribute with no contents indicates that a
21706 variable has been optimized away. */
21707 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21709 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21713 /* Handle one degenerate form of location expression specially, to
21714 preserve GDB's previous behavior when section offsets are
21715 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21716 then mark this symbol as LOC_STATIC. */
21718 if (attr_form_is_block (attr)
21719 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21720 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21721 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21722 && (DW_BLOCK (attr)->size
21723 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21725 unsigned int dummy;
21727 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21728 SYMBOL_VALUE_ADDRESS (sym) =
21729 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21731 SYMBOL_VALUE_ADDRESS (sym) =
21732 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21733 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21734 fixup_symbol_section (sym, objfile);
21735 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21736 SYMBOL_SECTION (sym));
21740 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21741 expression evaluator, and use LOC_COMPUTED only when necessary
21742 (i.e. when the value of a register or memory location is
21743 referenced, or a thread-local block, etc.). Then again, it might
21744 not be worthwhile. I'm assuming that it isn't unless performance
21745 or memory numbers show me otherwise. */
21747 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21749 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21750 cu->has_loclist = 1;
21753 /* Given a pointer to a DWARF information entry, figure out if we need
21754 to make a symbol table entry for it, and if so, create a new entry
21755 and return a pointer to it.
21756 If TYPE is NULL, determine symbol type from the die, otherwise
21757 used the passed type.
21758 If SPACE is not NULL, use it to hold the new symbol. If it is
21759 NULL, allocate a new symbol on the objfile's obstack. */
21761 static struct symbol *
21762 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21763 struct symbol *space)
21765 struct dwarf2_per_objfile *dwarf2_per_objfile
21766 = cu->per_cu->dwarf2_per_objfile;
21767 struct objfile *objfile = dwarf2_per_objfile->objfile;
21768 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21769 struct symbol *sym = NULL;
21771 struct attribute *attr = NULL;
21772 struct attribute *attr2 = NULL;
21773 CORE_ADDR baseaddr;
21774 struct pending **list_to_add = NULL;
21776 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21778 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21780 name = dwarf2_name (die, cu);
21783 const char *linkagename;
21784 int suppress_add = 0;
21789 sym = allocate_symbol (objfile);
21790 OBJSTAT (objfile, n_syms++);
21792 /* Cache this symbol's name and the name's demangled form (if any). */
21793 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21794 linkagename = dwarf2_physname (name, die, cu);
21795 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21797 /* Fortran does not have mangling standard and the mangling does differ
21798 between gfortran, iFort etc. */
21799 if (cu->language == language_fortran
21800 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21801 symbol_set_demangled_name (&(sym->ginfo),
21802 dwarf2_full_name (name, die, cu),
21805 /* Default assumptions.
21806 Use the passed type or decode it from the die. */
21807 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21808 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21810 SYMBOL_TYPE (sym) = type;
21812 SYMBOL_TYPE (sym) = die_type (die, cu);
21813 attr = dwarf2_attr (die,
21814 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21818 SYMBOL_LINE (sym) = DW_UNSND (attr);
21821 attr = dwarf2_attr (die,
21822 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21826 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21827 struct file_entry *fe;
21829 if (cu->line_header != NULL)
21830 fe = cu->line_header->file_name_at (file_index);
21835 complaint (&symfile_complaints,
21836 _("file index out of range"));
21838 symbol_set_symtab (sym, fe->symtab);
21844 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21849 addr = attr_value_as_address (attr);
21850 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21851 SYMBOL_VALUE_ADDRESS (sym) = addr;
21853 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21854 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21855 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21856 add_symbol_to_list (sym, cu->list_in_scope);
21858 case DW_TAG_subprogram:
21859 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21861 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21862 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21863 if ((attr2 && (DW_UNSND (attr2) != 0))
21864 || cu->language == language_ada)
21866 /* Subprograms marked external are stored as a global symbol.
21867 Ada subprograms, whether marked external or not, are always
21868 stored as a global symbol, because we want to be able to
21869 access them globally. For instance, we want to be able
21870 to break on a nested subprogram without having to
21871 specify the context. */
21872 list_to_add = &global_symbols;
21876 list_to_add = cu->list_in_scope;
21879 case DW_TAG_inlined_subroutine:
21880 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21882 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21883 SYMBOL_INLINED (sym) = 1;
21884 list_to_add = cu->list_in_scope;
21886 case DW_TAG_template_value_param:
21888 /* Fall through. */
21889 case DW_TAG_constant:
21890 case DW_TAG_variable:
21891 case DW_TAG_member:
21892 /* Compilation with minimal debug info may result in
21893 variables with missing type entries. Change the
21894 misleading `void' type to something sensible. */
21895 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21896 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21898 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21899 /* In the case of DW_TAG_member, we should only be called for
21900 static const members. */
21901 if (die->tag == DW_TAG_member)
21903 /* dwarf2_add_field uses die_is_declaration,
21904 so we do the same. */
21905 gdb_assert (die_is_declaration (die, cu));
21910 dwarf2_const_value (attr, sym, cu);
21911 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21914 if (attr2 && (DW_UNSND (attr2) != 0))
21915 list_to_add = &global_symbols;
21917 list_to_add = cu->list_in_scope;
21921 attr = dwarf2_attr (die, DW_AT_location, cu);
21924 var_decode_location (attr, sym, cu);
21925 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21927 /* Fortran explicitly imports any global symbols to the local
21928 scope by DW_TAG_common_block. */
21929 if (cu->language == language_fortran && die->parent
21930 && die->parent->tag == DW_TAG_common_block)
21933 if (SYMBOL_CLASS (sym) == LOC_STATIC
21934 && SYMBOL_VALUE_ADDRESS (sym) == 0
21935 && !dwarf2_per_objfile->has_section_at_zero)
21937 /* When a static variable is eliminated by the linker,
21938 the corresponding debug information is not stripped
21939 out, but the variable address is set to null;
21940 do not add such variables into symbol table. */
21942 else if (attr2 && (DW_UNSND (attr2) != 0))
21944 /* Workaround gfortran PR debug/40040 - it uses
21945 DW_AT_location for variables in -fPIC libraries which may
21946 get overriden by other libraries/executable and get
21947 a different address. Resolve it by the minimal symbol
21948 which may come from inferior's executable using copy
21949 relocation. Make this workaround only for gfortran as for
21950 other compilers GDB cannot guess the minimal symbol
21951 Fortran mangling kind. */
21952 if (cu->language == language_fortran && die->parent
21953 && die->parent->tag == DW_TAG_module
21955 && startswith (cu->producer, "GNU Fortran"))
21956 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21958 /* A variable with DW_AT_external is never static,
21959 but it may be block-scoped. */
21960 list_to_add = (cu->list_in_scope == &file_symbols
21961 ? &global_symbols : cu->list_in_scope);
21964 list_to_add = cu->list_in_scope;
21968 /* We do not know the address of this symbol.
21969 If it is an external symbol and we have type information
21970 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21971 The address of the variable will then be determined from
21972 the minimal symbol table whenever the variable is
21974 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21976 /* Fortran explicitly imports any global symbols to the local
21977 scope by DW_TAG_common_block. */
21978 if (cu->language == language_fortran && die->parent
21979 && die->parent->tag == DW_TAG_common_block)
21981 /* SYMBOL_CLASS doesn't matter here because
21982 read_common_block is going to reset it. */
21984 list_to_add = cu->list_in_scope;
21986 else if (attr2 && (DW_UNSND (attr2) != 0)
21987 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21989 /* A variable with DW_AT_external is never static, but it
21990 may be block-scoped. */
21991 list_to_add = (cu->list_in_scope == &file_symbols
21992 ? &global_symbols : cu->list_in_scope);
21994 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21996 else if (!die_is_declaration (die, cu))
21998 /* Use the default LOC_OPTIMIZED_OUT class. */
21999 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22001 list_to_add = cu->list_in_scope;
22005 case DW_TAG_formal_parameter:
22006 /* If we are inside a function, mark this as an argument. If
22007 not, we might be looking at an argument to an inlined function
22008 when we do not have enough information to show inlined frames;
22009 pretend it's a local variable in that case so that the user can
22011 if (context_stack_depth > 0
22012 && context_stack[context_stack_depth - 1].name != NULL)
22013 SYMBOL_IS_ARGUMENT (sym) = 1;
22014 attr = dwarf2_attr (die, DW_AT_location, cu);
22017 var_decode_location (attr, sym, cu);
22019 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22022 dwarf2_const_value (attr, sym, cu);
22025 list_to_add = cu->list_in_scope;
22027 case DW_TAG_unspecified_parameters:
22028 /* From varargs functions; gdb doesn't seem to have any
22029 interest in this information, so just ignore it for now.
22032 case DW_TAG_template_type_param:
22034 /* Fall through. */
22035 case DW_TAG_class_type:
22036 case DW_TAG_interface_type:
22037 case DW_TAG_structure_type:
22038 case DW_TAG_union_type:
22039 case DW_TAG_set_type:
22040 case DW_TAG_enumeration_type:
22041 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22042 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22045 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22046 really ever be static objects: otherwise, if you try
22047 to, say, break of a class's method and you're in a file
22048 which doesn't mention that class, it won't work unless
22049 the check for all static symbols in lookup_symbol_aux
22050 saves you. See the OtherFileClass tests in
22051 gdb.c++/namespace.exp. */
22055 list_to_add = (cu->list_in_scope == &file_symbols
22056 && cu->language == language_cplus
22057 ? &global_symbols : cu->list_in_scope);
22059 /* The semantics of C++ state that "struct foo {
22060 ... }" also defines a typedef for "foo". */
22061 if (cu->language == language_cplus
22062 || cu->language == language_ada
22063 || cu->language == language_d
22064 || cu->language == language_rust)
22066 /* The symbol's name is already allocated along
22067 with this objfile, so we don't need to
22068 duplicate it for the type. */
22069 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
22070 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
22075 case DW_TAG_typedef:
22076 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22077 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22078 list_to_add = cu->list_in_scope;
22080 case DW_TAG_base_type:
22081 case DW_TAG_subrange_type:
22082 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22083 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22084 list_to_add = cu->list_in_scope;
22086 case DW_TAG_enumerator:
22087 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22090 dwarf2_const_value (attr, sym, cu);
22093 /* NOTE: carlton/2003-11-10: See comment above in the
22094 DW_TAG_class_type, etc. block. */
22096 list_to_add = (cu->list_in_scope == &file_symbols
22097 && cu->language == language_cplus
22098 ? &global_symbols : cu->list_in_scope);
22101 case DW_TAG_imported_declaration:
22102 case DW_TAG_namespace:
22103 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22104 list_to_add = &global_symbols;
22106 case DW_TAG_module:
22107 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22108 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22109 list_to_add = &global_symbols;
22111 case DW_TAG_common_block:
22112 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22113 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22114 add_symbol_to_list (sym, cu->list_in_scope);
22117 /* Not a tag we recognize. Hopefully we aren't processing
22118 trash data, but since we must specifically ignore things
22119 we don't recognize, there is nothing else we should do at
22121 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
22122 dwarf_tag_name (die->tag));
22128 sym->hash_next = objfile->template_symbols;
22129 objfile->template_symbols = sym;
22130 list_to_add = NULL;
22133 if (list_to_add != NULL)
22134 add_symbol_to_list (sym, list_to_add);
22136 /* For the benefit of old versions of GCC, check for anonymous
22137 namespaces based on the demangled name. */
22138 if (!cu->processing_has_namespace_info
22139 && cu->language == language_cplus)
22140 cp_scan_for_anonymous_namespaces (sym, objfile);
22145 /* Given an attr with a DW_FORM_dataN value in host byte order,
22146 zero-extend it as appropriate for the symbol's type. The DWARF
22147 standard (v4) is not entirely clear about the meaning of using
22148 DW_FORM_dataN for a constant with a signed type, where the type is
22149 wider than the data. The conclusion of a discussion on the DWARF
22150 list was that this is unspecified. We choose to always zero-extend
22151 because that is the interpretation long in use by GCC. */
22154 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22155 struct dwarf2_cu *cu, LONGEST *value, int bits)
22157 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22158 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22159 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22160 LONGEST l = DW_UNSND (attr);
22162 if (bits < sizeof (*value) * 8)
22164 l &= ((LONGEST) 1 << bits) - 1;
22167 else if (bits == sizeof (*value) * 8)
22171 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22172 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22179 /* Read a constant value from an attribute. Either set *VALUE, or if
22180 the value does not fit in *VALUE, set *BYTES - either already
22181 allocated on the objfile obstack, or newly allocated on OBSTACK,
22182 or, set *BATON, if we translated the constant to a location
22186 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22187 const char *name, struct obstack *obstack,
22188 struct dwarf2_cu *cu,
22189 LONGEST *value, const gdb_byte **bytes,
22190 struct dwarf2_locexpr_baton **baton)
22192 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22193 struct comp_unit_head *cu_header = &cu->header;
22194 struct dwarf_block *blk;
22195 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22196 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22202 switch (attr->form)
22205 case DW_FORM_GNU_addr_index:
22209 if (TYPE_LENGTH (type) != cu_header->addr_size)
22210 dwarf2_const_value_length_mismatch_complaint (name,
22211 cu_header->addr_size,
22212 TYPE_LENGTH (type));
22213 /* Symbols of this form are reasonably rare, so we just
22214 piggyback on the existing location code rather than writing
22215 a new implementation of symbol_computed_ops. */
22216 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22217 (*baton)->per_cu = cu->per_cu;
22218 gdb_assert ((*baton)->per_cu);
22220 (*baton)->size = 2 + cu_header->addr_size;
22221 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22222 (*baton)->data = data;
22224 data[0] = DW_OP_addr;
22225 store_unsigned_integer (&data[1], cu_header->addr_size,
22226 byte_order, DW_ADDR (attr));
22227 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22230 case DW_FORM_string:
22232 case DW_FORM_GNU_str_index:
22233 case DW_FORM_GNU_strp_alt:
22234 /* DW_STRING is already allocated on the objfile obstack, point
22236 *bytes = (const gdb_byte *) DW_STRING (attr);
22238 case DW_FORM_block1:
22239 case DW_FORM_block2:
22240 case DW_FORM_block4:
22241 case DW_FORM_block:
22242 case DW_FORM_exprloc:
22243 case DW_FORM_data16:
22244 blk = DW_BLOCK (attr);
22245 if (TYPE_LENGTH (type) != blk->size)
22246 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22247 TYPE_LENGTH (type));
22248 *bytes = blk->data;
22251 /* The DW_AT_const_value attributes are supposed to carry the
22252 symbol's value "represented as it would be on the target
22253 architecture." By the time we get here, it's already been
22254 converted to host endianness, so we just need to sign- or
22255 zero-extend it as appropriate. */
22256 case DW_FORM_data1:
22257 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22259 case DW_FORM_data2:
22260 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22262 case DW_FORM_data4:
22263 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22265 case DW_FORM_data8:
22266 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22269 case DW_FORM_sdata:
22270 case DW_FORM_implicit_const:
22271 *value = DW_SND (attr);
22274 case DW_FORM_udata:
22275 *value = DW_UNSND (attr);
22279 complaint (&symfile_complaints,
22280 _("unsupported const value attribute form: '%s'"),
22281 dwarf_form_name (attr->form));
22288 /* Copy constant value from an attribute to a symbol. */
22291 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22292 struct dwarf2_cu *cu)
22294 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22296 const gdb_byte *bytes;
22297 struct dwarf2_locexpr_baton *baton;
22299 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22300 SYMBOL_PRINT_NAME (sym),
22301 &objfile->objfile_obstack, cu,
22302 &value, &bytes, &baton);
22306 SYMBOL_LOCATION_BATON (sym) = baton;
22307 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22309 else if (bytes != NULL)
22311 SYMBOL_VALUE_BYTES (sym) = bytes;
22312 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22316 SYMBOL_VALUE (sym) = value;
22317 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22321 /* Return the type of the die in question using its DW_AT_type attribute. */
22323 static struct type *
22324 die_type (struct die_info *die, struct dwarf2_cu *cu)
22326 struct attribute *type_attr;
22328 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22331 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22332 /* A missing DW_AT_type represents a void type. */
22333 return objfile_type (objfile)->builtin_void;
22336 return lookup_die_type (die, type_attr, cu);
22339 /* True iff CU's producer generates GNAT Ada auxiliary information
22340 that allows to find parallel types through that information instead
22341 of having to do expensive parallel lookups by type name. */
22344 need_gnat_info (struct dwarf2_cu *cu)
22346 /* Assume that the Ada compiler was GNAT, which always produces
22347 the auxiliary information. */
22348 return (cu->language == language_ada);
22351 /* Return the auxiliary type of the die in question using its
22352 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22353 attribute is not present. */
22355 static struct type *
22356 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22358 struct attribute *type_attr;
22360 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22364 return lookup_die_type (die, type_attr, cu);
22367 /* If DIE has a descriptive_type attribute, then set the TYPE's
22368 descriptive type accordingly. */
22371 set_descriptive_type (struct type *type, struct die_info *die,
22372 struct dwarf2_cu *cu)
22374 struct type *descriptive_type = die_descriptive_type (die, cu);
22376 if (descriptive_type)
22378 ALLOCATE_GNAT_AUX_TYPE (type);
22379 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22383 /* Return the containing type of the die in question using its
22384 DW_AT_containing_type attribute. */
22386 static struct type *
22387 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22389 struct attribute *type_attr;
22390 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22392 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22394 error (_("Dwarf Error: Problem turning containing type into gdb type "
22395 "[in module %s]"), objfile_name (objfile));
22397 return lookup_die_type (die, type_attr, cu);
22400 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22402 static struct type *
22403 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22405 struct dwarf2_per_objfile *dwarf2_per_objfile
22406 = cu->per_cu->dwarf2_per_objfile;
22407 struct objfile *objfile = dwarf2_per_objfile->objfile;
22408 char *message, *saved;
22410 message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
22411 objfile_name (objfile),
22412 sect_offset_str (cu->header.sect_off),
22413 sect_offset_str (die->sect_off));
22414 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
22415 message, strlen (message));
22418 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22421 /* Look up the type of DIE in CU using its type attribute ATTR.
22422 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22423 DW_AT_containing_type.
22424 If there is no type substitute an error marker. */
22426 static struct type *
22427 lookup_die_type (struct die_info *die, const struct attribute *attr,
22428 struct dwarf2_cu *cu)
22430 struct dwarf2_per_objfile *dwarf2_per_objfile
22431 = cu->per_cu->dwarf2_per_objfile;
22432 struct objfile *objfile = dwarf2_per_objfile->objfile;
22433 struct type *this_type;
22435 gdb_assert (attr->name == DW_AT_type
22436 || attr->name == DW_AT_GNAT_descriptive_type
22437 || attr->name == DW_AT_containing_type);
22439 /* First see if we have it cached. */
22441 if (attr->form == DW_FORM_GNU_ref_alt)
22443 struct dwarf2_per_cu_data *per_cu;
22444 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22446 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22447 dwarf2_per_objfile);
22448 this_type = get_die_type_at_offset (sect_off, per_cu);
22450 else if (attr_form_is_ref (attr))
22452 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22454 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22456 else if (attr->form == DW_FORM_ref_sig8)
22458 ULONGEST signature = DW_SIGNATURE (attr);
22460 return get_signatured_type (die, signature, cu);
22464 complaint (&symfile_complaints,
22465 _("Dwarf Error: Bad type attribute %s in DIE"
22466 " at %s [in module %s]"),
22467 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22468 objfile_name (objfile));
22469 return build_error_marker_type (cu, die);
22472 /* If not cached we need to read it in. */
22474 if (this_type == NULL)
22476 struct die_info *type_die = NULL;
22477 struct dwarf2_cu *type_cu = cu;
22479 if (attr_form_is_ref (attr))
22480 type_die = follow_die_ref (die, attr, &type_cu);
22481 if (type_die == NULL)
22482 return build_error_marker_type (cu, die);
22483 /* If we find the type now, it's probably because the type came
22484 from an inter-CU reference and the type's CU got expanded before
22486 this_type = read_type_die (type_die, type_cu);
22489 /* If we still don't have a type use an error marker. */
22491 if (this_type == NULL)
22492 return build_error_marker_type (cu, die);
22497 /* Return the type in DIE, CU.
22498 Returns NULL for invalid types.
22500 This first does a lookup in die_type_hash,
22501 and only reads the die in if necessary.
22503 NOTE: This can be called when reading in partial or full symbols. */
22505 static struct type *
22506 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22508 struct type *this_type;
22510 this_type = get_die_type (die, cu);
22514 return read_type_die_1 (die, cu);
22517 /* Read the type in DIE, CU.
22518 Returns NULL for invalid types. */
22520 static struct type *
22521 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22523 struct type *this_type = NULL;
22527 case DW_TAG_class_type:
22528 case DW_TAG_interface_type:
22529 case DW_TAG_structure_type:
22530 case DW_TAG_union_type:
22531 this_type = read_structure_type (die, cu);
22533 case DW_TAG_enumeration_type:
22534 this_type = read_enumeration_type (die, cu);
22536 case DW_TAG_subprogram:
22537 case DW_TAG_subroutine_type:
22538 case DW_TAG_inlined_subroutine:
22539 this_type = read_subroutine_type (die, cu);
22541 case DW_TAG_array_type:
22542 this_type = read_array_type (die, cu);
22544 case DW_TAG_set_type:
22545 this_type = read_set_type (die, cu);
22547 case DW_TAG_pointer_type:
22548 this_type = read_tag_pointer_type (die, cu);
22550 case DW_TAG_ptr_to_member_type:
22551 this_type = read_tag_ptr_to_member_type (die, cu);
22553 case DW_TAG_reference_type:
22554 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22556 case DW_TAG_rvalue_reference_type:
22557 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22559 case DW_TAG_const_type:
22560 this_type = read_tag_const_type (die, cu);
22562 case DW_TAG_volatile_type:
22563 this_type = read_tag_volatile_type (die, cu);
22565 case DW_TAG_restrict_type:
22566 this_type = read_tag_restrict_type (die, cu);
22568 case DW_TAG_string_type:
22569 this_type = read_tag_string_type (die, cu);
22571 case DW_TAG_typedef:
22572 this_type = read_typedef (die, cu);
22574 case DW_TAG_subrange_type:
22575 this_type = read_subrange_type (die, cu);
22577 case DW_TAG_base_type:
22578 this_type = read_base_type (die, cu);
22580 case DW_TAG_unspecified_type:
22581 this_type = read_unspecified_type (die, cu);
22583 case DW_TAG_namespace:
22584 this_type = read_namespace_type (die, cu);
22586 case DW_TAG_module:
22587 this_type = read_module_type (die, cu);
22589 case DW_TAG_atomic_type:
22590 this_type = read_tag_atomic_type (die, cu);
22593 complaint (&symfile_complaints,
22594 _("unexpected tag in read_type_die: '%s'"),
22595 dwarf_tag_name (die->tag));
22602 /* See if we can figure out if the class lives in a namespace. We do
22603 this by looking for a member function; its demangled name will
22604 contain namespace info, if there is any.
22605 Return the computed name or NULL.
22606 Space for the result is allocated on the objfile's obstack.
22607 This is the full-die version of guess_partial_die_structure_name.
22608 In this case we know DIE has no useful parent. */
22611 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22613 struct die_info *spec_die;
22614 struct dwarf2_cu *spec_cu;
22615 struct die_info *child;
22616 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22619 spec_die = die_specification (die, &spec_cu);
22620 if (spec_die != NULL)
22626 for (child = die->child;
22628 child = child->sibling)
22630 if (child->tag == DW_TAG_subprogram)
22632 const char *linkage_name = dw2_linkage_name (child, cu);
22634 if (linkage_name != NULL)
22637 = language_class_name_from_physname (cu->language_defn,
22641 if (actual_name != NULL)
22643 const char *die_name = dwarf2_name (die, cu);
22645 if (die_name != NULL
22646 && strcmp (die_name, actual_name) != 0)
22648 /* Strip off the class name from the full name.
22649 We want the prefix. */
22650 int die_name_len = strlen (die_name);
22651 int actual_name_len = strlen (actual_name);
22653 /* Test for '::' as a sanity check. */
22654 if (actual_name_len > die_name_len + 2
22655 && actual_name[actual_name_len
22656 - die_name_len - 1] == ':')
22657 name = (char *) obstack_copy0 (
22658 &objfile->per_bfd->storage_obstack,
22659 actual_name, actual_name_len - die_name_len - 2);
22662 xfree (actual_name);
22671 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22672 prefix part in such case. See
22673 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22675 static const char *
22676 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22678 struct attribute *attr;
22681 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22682 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22685 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22688 attr = dw2_linkage_name_attr (die, cu);
22689 if (attr == NULL || DW_STRING (attr) == NULL)
22692 /* dwarf2_name had to be already called. */
22693 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22695 /* Strip the base name, keep any leading namespaces/classes. */
22696 base = strrchr (DW_STRING (attr), ':');
22697 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22700 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22701 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
22703 &base[-1] - DW_STRING (attr));
22706 /* Return the name of the namespace/class that DIE is defined within,
22707 or "" if we can't tell. The caller should not xfree the result.
22709 For example, if we're within the method foo() in the following
22719 then determine_prefix on foo's die will return "N::C". */
22721 static const char *
22722 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22724 struct dwarf2_per_objfile *dwarf2_per_objfile
22725 = cu->per_cu->dwarf2_per_objfile;
22726 struct die_info *parent, *spec_die;
22727 struct dwarf2_cu *spec_cu;
22728 struct type *parent_type;
22729 const char *retval;
22731 if (cu->language != language_cplus
22732 && cu->language != language_fortran && cu->language != language_d
22733 && cu->language != language_rust)
22736 retval = anonymous_struct_prefix (die, cu);
22740 /* We have to be careful in the presence of DW_AT_specification.
22741 For example, with GCC 3.4, given the code
22745 // Definition of N::foo.
22749 then we'll have a tree of DIEs like this:
22751 1: DW_TAG_compile_unit
22752 2: DW_TAG_namespace // N
22753 3: DW_TAG_subprogram // declaration of N::foo
22754 4: DW_TAG_subprogram // definition of N::foo
22755 DW_AT_specification // refers to die #3
22757 Thus, when processing die #4, we have to pretend that we're in
22758 the context of its DW_AT_specification, namely the contex of die
22761 spec_die = die_specification (die, &spec_cu);
22762 if (spec_die == NULL)
22763 parent = die->parent;
22766 parent = spec_die->parent;
22770 if (parent == NULL)
22772 else if (parent->building_fullname)
22775 const char *parent_name;
22777 /* It has been seen on RealView 2.2 built binaries,
22778 DW_TAG_template_type_param types actually _defined_ as
22779 children of the parent class:
22782 template class <class Enum> Class{};
22783 Class<enum E> class_e;
22785 1: DW_TAG_class_type (Class)
22786 2: DW_TAG_enumeration_type (E)
22787 3: DW_TAG_enumerator (enum1:0)
22788 3: DW_TAG_enumerator (enum2:1)
22790 2: DW_TAG_template_type_param
22791 DW_AT_type DW_FORM_ref_udata (E)
22793 Besides being broken debug info, it can put GDB into an
22794 infinite loop. Consider:
22796 When we're building the full name for Class<E>, we'll start
22797 at Class, and go look over its template type parameters,
22798 finding E. We'll then try to build the full name of E, and
22799 reach here. We're now trying to build the full name of E,
22800 and look over the parent DIE for containing scope. In the
22801 broken case, if we followed the parent DIE of E, we'd again
22802 find Class, and once again go look at its template type
22803 arguments, etc., etc. Simply don't consider such parent die
22804 as source-level parent of this die (it can't be, the language
22805 doesn't allow it), and break the loop here. */
22806 name = dwarf2_name (die, cu);
22807 parent_name = dwarf2_name (parent, cu);
22808 complaint (&symfile_complaints,
22809 _("template param type '%s' defined within parent '%s'"),
22810 name ? name : "<unknown>",
22811 parent_name ? parent_name : "<unknown>");
22815 switch (parent->tag)
22817 case DW_TAG_namespace:
22818 parent_type = read_type_die (parent, cu);
22819 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22820 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22821 Work around this problem here. */
22822 if (cu->language == language_cplus
22823 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22825 /* We give a name to even anonymous namespaces. */
22826 return TYPE_TAG_NAME (parent_type);
22827 case DW_TAG_class_type:
22828 case DW_TAG_interface_type:
22829 case DW_TAG_structure_type:
22830 case DW_TAG_union_type:
22831 case DW_TAG_module:
22832 parent_type = read_type_die (parent, cu);
22833 if (TYPE_TAG_NAME (parent_type) != NULL)
22834 return TYPE_TAG_NAME (parent_type);
22836 /* An anonymous structure is only allowed non-static data
22837 members; no typedefs, no member functions, et cetera.
22838 So it does not need a prefix. */
22840 case DW_TAG_compile_unit:
22841 case DW_TAG_partial_unit:
22842 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22843 if (cu->language == language_cplus
22844 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22845 && die->child != NULL
22846 && (die->tag == DW_TAG_class_type
22847 || die->tag == DW_TAG_structure_type
22848 || die->tag == DW_TAG_union_type))
22850 char *name = guess_full_die_structure_name (die, cu);
22855 case DW_TAG_enumeration_type:
22856 parent_type = read_type_die (parent, cu);
22857 if (TYPE_DECLARED_CLASS (parent_type))
22859 if (TYPE_TAG_NAME (parent_type) != NULL)
22860 return TYPE_TAG_NAME (parent_type);
22863 /* Fall through. */
22865 return determine_prefix (parent, cu);
22869 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22870 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22871 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22872 an obconcat, otherwise allocate storage for the result. The CU argument is
22873 used to determine the language and hence, the appropriate separator. */
22875 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22878 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22879 int physname, struct dwarf2_cu *cu)
22881 const char *lead = "";
22884 if (suffix == NULL || suffix[0] == '\0'
22885 || prefix == NULL || prefix[0] == '\0')
22887 else if (cu->language == language_d)
22889 /* For D, the 'main' function could be defined in any module, but it
22890 should never be prefixed. */
22891 if (strcmp (suffix, "D main") == 0)
22899 else if (cu->language == language_fortran && physname)
22901 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22902 DW_AT_MIPS_linkage_name is preferred and used instead. */
22910 if (prefix == NULL)
22912 if (suffix == NULL)
22919 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22921 strcpy (retval, lead);
22922 strcat (retval, prefix);
22923 strcat (retval, sep);
22924 strcat (retval, suffix);
22929 /* We have an obstack. */
22930 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22934 /* Return sibling of die, NULL if no sibling. */
22936 static struct die_info *
22937 sibling_die (struct die_info *die)
22939 return die->sibling;
22942 /* Get name of a die, return NULL if not found. */
22944 static const char *
22945 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22946 struct obstack *obstack)
22948 if (name && cu->language == language_cplus)
22950 std::string canon_name = cp_canonicalize_string (name);
22952 if (!canon_name.empty ())
22954 if (canon_name != name)
22955 name = (const char *) obstack_copy0 (obstack,
22956 canon_name.c_str (),
22957 canon_name.length ());
22964 /* Get name of a die, return NULL if not found.
22965 Anonymous namespaces are converted to their magic string. */
22967 static const char *
22968 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22970 struct attribute *attr;
22971 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22973 attr = dwarf2_attr (die, DW_AT_name, cu);
22974 if ((!attr || !DW_STRING (attr))
22975 && die->tag != DW_TAG_namespace
22976 && die->tag != DW_TAG_class_type
22977 && die->tag != DW_TAG_interface_type
22978 && die->tag != DW_TAG_structure_type
22979 && die->tag != DW_TAG_union_type)
22984 case DW_TAG_compile_unit:
22985 case DW_TAG_partial_unit:
22986 /* Compilation units have a DW_AT_name that is a filename, not
22987 a source language identifier. */
22988 case DW_TAG_enumeration_type:
22989 case DW_TAG_enumerator:
22990 /* These tags always have simple identifiers already; no need
22991 to canonicalize them. */
22992 return DW_STRING (attr);
22994 case DW_TAG_namespace:
22995 if (attr != NULL && DW_STRING (attr) != NULL)
22996 return DW_STRING (attr);
22997 return CP_ANONYMOUS_NAMESPACE_STR;
22999 case DW_TAG_class_type:
23000 case DW_TAG_interface_type:
23001 case DW_TAG_structure_type:
23002 case DW_TAG_union_type:
23003 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23004 structures or unions. These were of the form "._%d" in GCC 4.1,
23005 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23006 and GCC 4.4. We work around this problem by ignoring these. */
23007 if (attr && DW_STRING (attr)
23008 && (startswith (DW_STRING (attr), "._")
23009 || startswith (DW_STRING (attr), "<anonymous")))
23012 /* GCC might emit a nameless typedef that has a linkage name. See
23013 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23014 if (!attr || DW_STRING (attr) == NULL)
23016 char *demangled = NULL;
23018 attr = dw2_linkage_name_attr (die, cu);
23019 if (attr == NULL || DW_STRING (attr) == NULL)
23022 /* Avoid demangling DW_STRING (attr) the second time on a second
23023 call for the same DIE. */
23024 if (!DW_STRING_IS_CANONICAL (attr))
23025 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
23031 /* FIXME: we already did this for the partial symbol... */
23034 obstack_copy0 (&objfile->per_bfd->storage_obstack,
23035 demangled, strlen (demangled)));
23036 DW_STRING_IS_CANONICAL (attr) = 1;
23039 /* Strip any leading namespaces/classes, keep only the base name.
23040 DW_AT_name for named DIEs does not contain the prefixes. */
23041 base = strrchr (DW_STRING (attr), ':');
23042 if (base && base > DW_STRING (attr) && base[-1] == ':')
23045 return DW_STRING (attr);
23054 if (!DW_STRING_IS_CANONICAL (attr))
23057 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
23058 &objfile->per_bfd->storage_obstack);
23059 DW_STRING_IS_CANONICAL (attr) = 1;
23061 return DW_STRING (attr);
23064 /* Return the die that this die in an extension of, or NULL if there
23065 is none. *EXT_CU is the CU containing DIE on input, and the CU
23066 containing the return value on output. */
23068 static struct die_info *
23069 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23071 struct attribute *attr;
23073 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23077 return follow_die_ref (die, attr, ext_cu);
23080 /* Convert a DIE tag into its string name. */
23082 static const char *
23083 dwarf_tag_name (unsigned tag)
23085 const char *name = get_DW_TAG_name (tag);
23088 return "DW_TAG_<unknown>";
23093 /* Convert a DWARF attribute code into its string name. */
23095 static const char *
23096 dwarf_attr_name (unsigned attr)
23100 #ifdef MIPS /* collides with DW_AT_HP_block_index */
23101 if (attr == DW_AT_MIPS_fde)
23102 return "DW_AT_MIPS_fde";
23104 if (attr == DW_AT_HP_block_index)
23105 return "DW_AT_HP_block_index";
23108 name = get_DW_AT_name (attr);
23111 return "DW_AT_<unknown>";
23116 /* Convert a DWARF value form code into its string name. */
23118 static const char *
23119 dwarf_form_name (unsigned form)
23121 const char *name = get_DW_FORM_name (form);
23124 return "DW_FORM_<unknown>";
23129 static const char *
23130 dwarf_bool_name (unsigned mybool)
23138 /* Convert a DWARF type code into its string name. */
23140 static const char *
23141 dwarf_type_encoding_name (unsigned enc)
23143 const char *name = get_DW_ATE_name (enc);
23146 return "DW_ATE_<unknown>";
23152 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23156 print_spaces (indent, f);
23157 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23158 dwarf_tag_name (die->tag), die->abbrev,
23159 sect_offset_str (die->sect_off));
23161 if (die->parent != NULL)
23163 print_spaces (indent, f);
23164 fprintf_unfiltered (f, " parent at offset: %s\n",
23165 sect_offset_str (die->parent->sect_off));
23168 print_spaces (indent, f);
23169 fprintf_unfiltered (f, " has children: %s\n",
23170 dwarf_bool_name (die->child != NULL));
23172 print_spaces (indent, f);
23173 fprintf_unfiltered (f, " attributes:\n");
23175 for (i = 0; i < die->num_attrs; ++i)
23177 print_spaces (indent, f);
23178 fprintf_unfiltered (f, " %s (%s) ",
23179 dwarf_attr_name (die->attrs[i].name),
23180 dwarf_form_name (die->attrs[i].form));
23182 switch (die->attrs[i].form)
23185 case DW_FORM_GNU_addr_index:
23186 fprintf_unfiltered (f, "address: ");
23187 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
23189 case DW_FORM_block2:
23190 case DW_FORM_block4:
23191 case DW_FORM_block:
23192 case DW_FORM_block1:
23193 fprintf_unfiltered (f, "block: size %s",
23194 pulongest (DW_BLOCK (&die->attrs[i])->size));
23196 case DW_FORM_exprloc:
23197 fprintf_unfiltered (f, "expression: size %s",
23198 pulongest (DW_BLOCK (&die->attrs[i])->size));
23200 case DW_FORM_data16:
23201 fprintf_unfiltered (f, "constant of 16 bytes");
23203 case DW_FORM_ref_addr:
23204 fprintf_unfiltered (f, "ref address: ");
23205 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23207 case DW_FORM_GNU_ref_alt:
23208 fprintf_unfiltered (f, "alt ref address: ");
23209 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23215 case DW_FORM_ref_udata:
23216 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23217 (long) (DW_UNSND (&die->attrs[i])));
23219 case DW_FORM_data1:
23220 case DW_FORM_data2:
23221 case DW_FORM_data4:
23222 case DW_FORM_data8:
23223 case DW_FORM_udata:
23224 case DW_FORM_sdata:
23225 fprintf_unfiltered (f, "constant: %s",
23226 pulongest (DW_UNSND (&die->attrs[i])));
23228 case DW_FORM_sec_offset:
23229 fprintf_unfiltered (f, "section offset: %s",
23230 pulongest (DW_UNSND (&die->attrs[i])));
23232 case DW_FORM_ref_sig8:
23233 fprintf_unfiltered (f, "signature: %s",
23234 hex_string (DW_SIGNATURE (&die->attrs[i])));
23236 case DW_FORM_string:
23238 case DW_FORM_line_strp:
23239 case DW_FORM_GNU_str_index:
23240 case DW_FORM_GNU_strp_alt:
23241 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23242 DW_STRING (&die->attrs[i])
23243 ? DW_STRING (&die->attrs[i]) : "",
23244 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
23247 if (DW_UNSND (&die->attrs[i]))
23248 fprintf_unfiltered (f, "flag: TRUE");
23250 fprintf_unfiltered (f, "flag: FALSE");
23252 case DW_FORM_flag_present:
23253 fprintf_unfiltered (f, "flag: TRUE");
23255 case DW_FORM_indirect:
23256 /* The reader will have reduced the indirect form to
23257 the "base form" so this form should not occur. */
23258 fprintf_unfiltered (f,
23259 "unexpected attribute form: DW_FORM_indirect");
23261 case DW_FORM_implicit_const:
23262 fprintf_unfiltered (f, "constant: %s",
23263 plongest (DW_SND (&die->attrs[i])));
23266 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23267 die->attrs[i].form);
23270 fprintf_unfiltered (f, "\n");
23275 dump_die_for_error (struct die_info *die)
23277 dump_die_shallow (gdb_stderr, 0, die);
23281 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23283 int indent = level * 4;
23285 gdb_assert (die != NULL);
23287 if (level >= max_level)
23290 dump_die_shallow (f, indent, die);
23292 if (die->child != NULL)
23294 print_spaces (indent, f);
23295 fprintf_unfiltered (f, " Children:");
23296 if (level + 1 < max_level)
23298 fprintf_unfiltered (f, "\n");
23299 dump_die_1 (f, level + 1, max_level, die->child);
23303 fprintf_unfiltered (f,
23304 " [not printed, max nesting level reached]\n");
23308 if (die->sibling != NULL && level > 0)
23310 dump_die_1 (f, level, max_level, die->sibling);
23314 /* This is called from the pdie macro in gdbinit.in.
23315 It's not static so gcc will keep a copy callable from gdb. */
23318 dump_die (struct die_info *die, int max_level)
23320 dump_die_1 (gdb_stdlog, 0, max_level, die);
23324 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23328 slot = htab_find_slot_with_hash (cu->die_hash, die,
23329 to_underlying (die->sect_off),
23335 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23339 dwarf2_get_ref_die_offset (const struct attribute *attr)
23341 if (attr_form_is_ref (attr))
23342 return (sect_offset) DW_UNSND (attr);
23344 complaint (&symfile_complaints,
23345 _("unsupported die ref attribute form: '%s'"),
23346 dwarf_form_name (attr->form));
23350 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23351 * the value held by the attribute is not constant. */
23354 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
23356 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
23357 return DW_SND (attr);
23358 else if (attr->form == DW_FORM_udata
23359 || attr->form == DW_FORM_data1
23360 || attr->form == DW_FORM_data2
23361 || attr->form == DW_FORM_data4
23362 || attr->form == DW_FORM_data8)
23363 return DW_UNSND (attr);
23366 /* For DW_FORM_data16 see attr_form_is_constant. */
23367 complaint (&symfile_complaints,
23368 _("Attribute value is not a constant (%s)"),
23369 dwarf_form_name (attr->form));
23370 return default_value;
23374 /* Follow reference or signature attribute ATTR of SRC_DIE.
23375 On entry *REF_CU is the CU of SRC_DIE.
23376 On exit *REF_CU is the CU of the result. */
23378 static struct die_info *
23379 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23380 struct dwarf2_cu **ref_cu)
23382 struct die_info *die;
23384 if (attr_form_is_ref (attr))
23385 die = follow_die_ref (src_die, attr, ref_cu);
23386 else if (attr->form == DW_FORM_ref_sig8)
23387 die = follow_die_sig (src_die, attr, ref_cu);
23390 dump_die_for_error (src_die);
23391 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23392 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23398 /* Follow reference OFFSET.
23399 On entry *REF_CU is the CU of the source die referencing OFFSET.
23400 On exit *REF_CU is the CU of the result.
23401 Returns NULL if OFFSET is invalid. */
23403 static struct die_info *
23404 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23405 struct dwarf2_cu **ref_cu)
23407 struct die_info temp_die;
23408 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23409 struct dwarf2_per_objfile *dwarf2_per_objfile
23410 = cu->per_cu->dwarf2_per_objfile;
23411 struct objfile *objfile = dwarf2_per_objfile->objfile;
23413 gdb_assert (cu->per_cu != NULL);
23417 if (cu->per_cu->is_debug_types)
23419 /* .debug_types CUs cannot reference anything outside their CU.
23420 If they need to, they have to reference a signatured type via
23421 DW_FORM_ref_sig8. */
23422 if (!offset_in_cu_p (&cu->header, sect_off))
23425 else if (offset_in_dwz != cu->per_cu->is_dwz
23426 || !offset_in_cu_p (&cu->header, sect_off))
23428 struct dwarf2_per_cu_data *per_cu;
23430 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23431 dwarf2_per_objfile);
23433 /* If necessary, add it to the queue and load its DIEs. */
23434 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23435 load_full_comp_unit (per_cu, cu->language);
23437 target_cu = per_cu->cu;
23439 else if (cu->dies == NULL)
23441 /* We're loading full DIEs during partial symbol reading. */
23442 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23443 load_full_comp_unit (cu->per_cu, language_minimal);
23446 *ref_cu = target_cu;
23447 temp_die.sect_off = sect_off;
23448 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23450 to_underlying (sect_off));
23453 /* Follow reference attribute ATTR of SRC_DIE.
23454 On entry *REF_CU is the CU of SRC_DIE.
23455 On exit *REF_CU is the CU of the result. */
23457 static struct die_info *
23458 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23459 struct dwarf2_cu **ref_cu)
23461 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23462 struct dwarf2_cu *cu = *ref_cu;
23463 struct die_info *die;
23465 die = follow_die_offset (sect_off,
23466 (attr->form == DW_FORM_GNU_ref_alt
23467 || cu->per_cu->is_dwz),
23470 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23471 "at %s [in module %s]"),
23472 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23473 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23478 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23479 Returned value is intended for DW_OP_call*. Returned
23480 dwarf2_locexpr_baton->data has lifetime of
23481 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23483 struct dwarf2_locexpr_baton
23484 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23485 struct dwarf2_per_cu_data *per_cu,
23486 CORE_ADDR (*get_frame_pc) (void *baton),
23489 struct dwarf2_cu *cu;
23490 struct die_info *die;
23491 struct attribute *attr;
23492 struct dwarf2_locexpr_baton retval;
23493 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23494 struct dwarf2_per_objfile *dwarf2_per_objfile
23495 = get_dwarf2_per_objfile (objfile);
23497 if (per_cu->cu == NULL)
23502 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23503 Instead just throw an error, not much else we can do. */
23504 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23505 sect_offset_str (sect_off), objfile_name (objfile));
23508 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23510 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23511 sect_offset_str (sect_off), objfile_name (objfile));
23513 attr = dwarf2_attr (die, DW_AT_location, cu);
23516 /* DWARF: "If there is no such attribute, then there is no effect.".
23517 DATA is ignored if SIZE is 0. */
23519 retval.data = NULL;
23522 else if (attr_form_is_section_offset (attr))
23524 struct dwarf2_loclist_baton loclist_baton;
23525 CORE_ADDR pc = (*get_frame_pc) (baton);
23528 fill_in_loclist_baton (cu, &loclist_baton, attr);
23530 retval.data = dwarf2_find_location_expression (&loclist_baton,
23532 retval.size = size;
23536 if (!attr_form_is_block (attr))
23537 error (_("Dwarf Error: DIE at %s referenced in module %s "
23538 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23539 sect_offset_str (sect_off), objfile_name (objfile));
23541 retval.data = DW_BLOCK (attr)->data;
23542 retval.size = DW_BLOCK (attr)->size;
23544 retval.per_cu = cu->per_cu;
23546 age_cached_comp_units (dwarf2_per_objfile);
23551 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23554 struct dwarf2_locexpr_baton
23555 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23556 struct dwarf2_per_cu_data *per_cu,
23557 CORE_ADDR (*get_frame_pc) (void *baton),
23560 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23562 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23565 /* Write a constant of a given type as target-ordered bytes into
23568 static const gdb_byte *
23569 write_constant_as_bytes (struct obstack *obstack,
23570 enum bfd_endian byte_order,
23577 *len = TYPE_LENGTH (type);
23578 result = (gdb_byte *) obstack_alloc (obstack, *len);
23579 store_unsigned_integer (result, *len, byte_order, value);
23584 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23585 pointer to the constant bytes and set LEN to the length of the
23586 data. If memory is needed, allocate it on OBSTACK. If the DIE
23587 does not have a DW_AT_const_value, return NULL. */
23590 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23591 struct dwarf2_per_cu_data *per_cu,
23592 struct obstack *obstack,
23595 struct dwarf2_cu *cu;
23596 struct die_info *die;
23597 struct attribute *attr;
23598 const gdb_byte *result = NULL;
23601 enum bfd_endian byte_order;
23602 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23604 if (per_cu->cu == NULL)
23609 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23610 Instead just throw an error, not much else we can do. */
23611 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23612 sect_offset_str (sect_off), objfile_name (objfile));
23615 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23617 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23618 sect_offset_str (sect_off), objfile_name (objfile));
23620 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23624 byte_order = (bfd_big_endian (objfile->obfd)
23625 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23627 switch (attr->form)
23630 case DW_FORM_GNU_addr_index:
23634 *len = cu->header.addr_size;
23635 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23636 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23640 case DW_FORM_string:
23642 case DW_FORM_GNU_str_index:
23643 case DW_FORM_GNU_strp_alt:
23644 /* DW_STRING is already allocated on the objfile obstack, point
23646 result = (const gdb_byte *) DW_STRING (attr);
23647 *len = strlen (DW_STRING (attr));
23649 case DW_FORM_block1:
23650 case DW_FORM_block2:
23651 case DW_FORM_block4:
23652 case DW_FORM_block:
23653 case DW_FORM_exprloc:
23654 case DW_FORM_data16:
23655 result = DW_BLOCK (attr)->data;
23656 *len = DW_BLOCK (attr)->size;
23659 /* The DW_AT_const_value attributes are supposed to carry the
23660 symbol's value "represented as it would be on the target
23661 architecture." By the time we get here, it's already been
23662 converted to host endianness, so we just need to sign- or
23663 zero-extend it as appropriate. */
23664 case DW_FORM_data1:
23665 type = die_type (die, cu);
23666 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23667 if (result == NULL)
23668 result = write_constant_as_bytes (obstack, byte_order,
23671 case DW_FORM_data2:
23672 type = die_type (die, cu);
23673 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23674 if (result == NULL)
23675 result = write_constant_as_bytes (obstack, byte_order,
23678 case DW_FORM_data4:
23679 type = die_type (die, cu);
23680 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23681 if (result == NULL)
23682 result = write_constant_as_bytes (obstack, byte_order,
23685 case DW_FORM_data8:
23686 type = die_type (die, cu);
23687 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23688 if (result == NULL)
23689 result = write_constant_as_bytes (obstack, byte_order,
23693 case DW_FORM_sdata:
23694 case DW_FORM_implicit_const:
23695 type = die_type (die, cu);
23696 result = write_constant_as_bytes (obstack, byte_order,
23697 type, DW_SND (attr), len);
23700 case DW_FORM_udata:
23701 type = die_type (die, cu);
23702 result = write_constant_as_bytes (obstack, byte_order,
23703 type, DW_UNSND (attr), len);
23707 complaint (&symfile_complaints,
23708 _("unsupported const value attribute form: '%s'"),
23709 dwarf_form_name (attr->form));
23716 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23717 valid type for this die is found. */
23720 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23721 struct dwarf2_per_cu_data *per_cu)
23723 struct dwarf2_cu *cu;
23724 struct die_info *die;
23726 if (per_cu->cu == NULL)
23732 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23736 return die_type (die, cu);
23739 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23743 dwarf2_get_die_type (cu_offset die_offset,
23744 struct dwarf2_per_cu_data *per_cu)
23746 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23747 return get_die_type_at_offset (die_offset_sect, per_cu);
23750 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23751 On entry *REF_CU is the CU of SRC_DIE.
23752 On exit *REF_CU is the CU of the result.
23753 Returns NULL if the referenced DIE isn't found. */
23755 static struct die_info *
23756 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23757 struct dwarf2_cu **ref_cu)
23759 struct die_info temp_die;
23760 struct dwarf2_cu *sig_cu;
23761 struct die_info *die;
23763 /* While it might be nice to assert sig_type->type == NULL here,
23764 we can get here for DW_AT_imported_declaration where we need
23765 the DIE not the type. */
23767 /* If necessary, add it to the queue and load its DIEs. */
23769 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23770 read_signatured_type (sig_type);
23772 sig_cu = sig_type->per_cu.cu;
23773 gdb_assert (sig_cu != NULL);
23774 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23775 temp_die.sect_off = sig_type->type_offset_in_section;
23776 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23777 to_underlying (temp_die.sect_off));
23780 struct dwarf2_per_objfile *dwarf2_per_objfile
23781 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23783 /* For .gdb_index version 7 keep track of included TUs.
23784 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23785 if (dwarf2_per_objfile->index_table != NULL
23786 && dwarf2_per_objfile->index_table->version <= 7)
23788 VEC_safe_push (dwarf2_per_cu_ptr,
23789 (*ref_cu)->per_cu->imported_symtabs,
23800 /* Follow signatured type referenced by ATTR in SRC_DIE.
23801 On entry *REF_CU is the CU of SRC_DIE.
23802 On exit *REF_CU is the CU of the result.
23803 The result is the DIE of the type.
23804 If the referenced type cannot be found an error is thrown. */
23806 static struct die_info *
23807 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23808 struct dwarf2_cu **ref_cu)
23810 ULONGEST signature = DW_SIGNATURE (attr);
23811 struct signatured_type *sig_type;
23812 struct die_info *die;
23814 gdb_assert (attr->form == DW_FORM_ref_sig8);
23816 sig_type = lookup_signatured_type (*ref_cu, signature);
23817 /* sig_type will be NULL if the signatured type is missing from
23819 if (sig_type == NULL)
23821 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23822 " from DIE at %s [in module %s]"),
23823 hex_string (signature), sect_offset_str (src_die->sect_off),
23824 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23827 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23830 dump_die_for_error (src_die);
23831 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23832 " from DIE at %s [in module %s]"),
23833 hex_string (signature), sect_offset_str (src_die->sect_off),
23834 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23840 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23841 reading in and processing the type unit if necessary. */
23843 static struct type *
23844 get_signatured_type (struct die_info *die, ULONGEST signature,
23845 struct dwarf2_cu *cu)
23847 struct dwarf2_per_objfile *dwarf2_per_objfile
23848 = cu->per_cu->dwarf2_per_objfile;
23849 struct signatured_type *sig_type;
23850 struct dwarf2_cu *type_cu;
23851 struct die_info *type_die;
23854 sig_type = lookup_signatured_type (cu, signature);
23855 /* sig_type will be NULL if the signatured type is missing from
23857 if (sig_type == NULL)
23859 complaint (&symfile_complaints,
23860 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23861 " from DIE at %s [in module %s]"),
23862 hex_string (signature), sect_offset_str (die->sect_off),
23863 objfile_name (dwarf2_per_objfile->objfile));
23864 return build_error_marker_type (cu, die);
23867 /* If we already know the type we're done. */
23868 if (sig_type->type != NULL)
23869 return sig_type->type;
23872 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23873 if (type_die != NULL)
23875 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23876 is created. This is important, for example, because for c++ classes
23877 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23878 type = read_type_die (type_die, type_cu);
23881 complaint (&symfile_complaints,
23882 _("Dwarf Error: Cannot build signatured type %s"
23883 " referenced from DIE at %s [in module %s]"),
23884 hex_string (signature), sect_offset_str (die->sect_off),
23885 objfile_name (dwarf2_per_objfile->objfile));
23886 type = build_error_marker_type (cu, die);
23891 complaint (&symfile_complaints,
23892 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23893 " from DIE at %s [in module %s]"),
23894 hex_string (signature), sect_offset_str (die->sect_off),
23895 objfile_name (dwarf2_per_objfile->objfile));
23896 type = build_error_marker_type (cu, die);
23898 sig_type->type = type;
23903 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23904 reading in and processing the type unit if necessary. */
23906 static struct type *
23907 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23908 struct dwarf2_cu *cu) /* ARI: editCase function */
23910 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23911 if (attr_form_is_ref (attr))
23913 struct dwarf2_cu *type_cu = cu;
23914 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23916 return read_type_die (type_die, type_cu);
23918 else if (attr->form == DW_FORM_ref_sig8)
23920 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23924 struct dwarf2_per_objfile *dwarf2_per_objfile
23925 = cu->per_cu->dwarf2_per_objfile;
23927 complaint (&symfile_complaints,
23928 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23929 " at %s [in module %s]"),
23930 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23931 objfile_name (dwarf2_per_objfile->objfile));
23932 return build_error_marker_type (cu, die);
23936 /* Load the DIEs associated with type unit PER_CU into memory. */
23939 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23941 struct signatured_type *sig_type;
23943 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23944 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23946 /* We have the per_cu, but we need the signatured_type.
23947 Fortunately this is an easy translation. */
23948 gdb_assert (per_cu->is_debug_types);
23949 sig_type = (struct signatured_type *) per_cu;
23951 gdb_assert (per_cu->cu == NULL);
23953 read_signatured_type (sig_type);
23955 gdb_assert (per_cu->cu != NULL);
23958 /* die_reader_func for read_signatured_type.
23959 This is identical to load_full_comp_unit_reader,
23960 but is kept separate for now. */
23963 read_signatured_type_reader (const struct die_reader_specs *reader,
23964 const gdb_byte *info_ptr,
23965 struct die_info *comp_unit_die,
23969 struct dwarf2_cu *cu = reader->cu;
23971 gdb_assert (cu->die_hash == NULL);
23973 htab_create_alloc_ex (cu->header.length / 12,
23977 &cu->comp_unit_obstack,
23978 hashtab_obstack_allocate,
23979 dummy_obstack_deallocate);
23982 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23983 &info_ptr, comp_unit_die);
23984 cu->dies = comp_unit_die;
23985 /* comp_unit_die is not stored in die_hash, no need. */
23987 /* We try not to read any attributes in this function, because not
23988 all CUs needed for references have been loaded yet, and symbol
23989 table processing isn't initialized. But we have to set the CU language,
23990 or we won't be able to build types correctly.
23991 Similarly, if we do not read the producer, we can not apply
23992 producer-specific interpretation. */
23993 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23996 /* Read in a signatured type and build its CU and DIEs.
23997 If the type is a stub for the real type in a DWO file,
23998 read in the real type from the DWO file as well. */
24001 read_signatured_type (struct signatured_type *sig_type)
24003 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24005 gdb_assert (per_cu->is_debug_types);
24006 gdb_assert (per_cu->cu == NULL);
24008 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
24009 read_signatured_type_reader, NULL);
24010 sig_type->per_cu.tu_read = 1;
24013 /* Decode simple location descriptions.
24014 Given a pointer to a dwarf block that defines a location, compute
24015 the location and return the value.
24017 NOTE drow/2003-11-18: This function is called in two situations
24018 now: for the address of static or global variables (partial symbols
24019 only) and for offsets into structures which are expected to be
24020 (more or less) constant. The partial symbol case should go away,
24021 and only the constant case should remain. That will let this
24022 function complain more accurately. A few special modes are allowed
24023 without complaint for global variables (for instance, global
24024 register values and thread-local values).
24026 A location description containing no operations indicates that the
24027 object is optimized out. The return value is 0 for that case.
24028 FIXME drow/2003-11-16: No callers check for this case any more; soon all
24029 callers will only want a very basic result and this can become a
24032 Note that stack[0] is unused except as a default error return. */
24035 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
24037 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
24039 size_t size = blk->size;
24040 const gdb_byte *data = blk->data;
24041 CORE_ADDR stack[64];
24043 unsigned int bytes_read, unsnd;
24049 stack[++stacki] = 0;
24088 stack[++stacki] = op - DW_OP_lit0;
24123 stack[++stacki] = op - DW_OP_reg0;
24125 dwarf2_complex_location_expr_complaint ();
24129 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24131 stack[++stacki] = unsnd;
24133 dwarf2_complex_location_expr_complaint ();
24137 stack[++stacki] = read_address (objfile->obfd, &data[i],
24142 case DW_OP_const1u:
24143 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24147 case DW_OP_const1s:
24148 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24152 case DW_OP_const2u:
24153 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24157 case DW_OP_const2s:
24158 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24162 case DW_OP_const4u:
24163 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24167 case DW_OP_const4s:
24168 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24172 case DW_OP_const8u:
24173 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24178 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24184 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24189 stack[stacki + 1] = stack[stacki];
24194 stack[stacki - 1] += stack[stacki];
24198 case DW_OP_plus_uconst:
24199 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24205 stack[stacki - 1] -= stack[stacki];
24210 /* If we're not the last op, then we definitely can't encode
24211 this using GDB's address_class enum. This is valid for partial
24212 global symbols, although the variable's address will be bogus
24215 dwarf2_complex_location_expr_complaint ();
24218 case DW_OP_GNU_push_tls_address:
24219 case DW_OP_form_tls_address:
24220 /* The top of the stack has the offset from the beginning
24221 of the thread control block at which the variable is located. */
24222 /* Nothing should follow this operator, so the top of stack would
24224 /* This is valid for partial global symbols, but the variable's
24225 address will be bogus in the psymtab. Make it always at least
24226 non-zero to not look as a variable garbage collected by linker
24227 which have DW_OP_addr 0. */
24229 dwarf2_complex_location_expr_complaint ();
24233 case DW_OP_GNU_uninit:
24236 case DW_OP_GNU_addr_index:
24237 case DW_OP_GNU_const_index:
24238 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24245 const char *name = get_DW_OP_name (op);
24248 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
24251 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
24255 return (stack[stacki]);
24258 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24259 outside of the allocated space. Also enforce minimum>0. */
24260 if (stacki >= ARRAY_SIZE (stack) - 1)
24262 complaint (&symfile_complaints,
24263 _("location description stack overflow"));
24269 complaint (&symfile_complaints,
24270 _("location description stack underflow"));
24274 return (stack[stacki]);
24277 /* memory allocation interface */
24279 static struct dwarf_block *
24280 dwarf_alloc_block (struct dwarf2_cu *cu)
24282 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24285 static struct die_info *
24286 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24288 struct die_info *die;
24289 size_t size = sizeof (struct die_info);
24292 size += (num_attrs - 1) * sizeof (struct attribute);
24294 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24295 memset (die, 0, sizeof (struct die_info));
24300 /* Macro support. */
24302 /* Return file name relative to the compilation directory of file number I in
24303 *LH's file name table. The result is allocated using xmalloc; the caller is
24304 responsible for freeing it. */
24307 file_file_name (int file, struct line_header *lh)
24309 /* Is the file number a valid index into the line header's file name
24310 table? Remember that file numbers start with one, not zero. */
24311 if (1 <= file && file <= lh->file_names.size ())
24313 const file_entry &fe = lh->file_names[file - 1];
24315 if (!IS_ABSOLUTE_PATH (fe.name))
24317 const char *dir = fe.include_dir (lh);
24319 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
24321 return xstrdup (fe.name);
24325 /* The compiler produced a bogus file number. We can at least
24326 record the macro definitions made in the file, even if we
24327 won't be able to find the file by name. */
24328 char fake_name[80];
24330 xsnprintf (fake_name, sizeof (fake_name),
24331 "<bad macro file number %d>", file);
24333 complaint (&symfile_complaints,
24334 _("bad file number in macro information (%d)"),
24337 return xstrdup (fake_name);
24341 /* Return the full name of file number I in *LH's file name table.
24342 Use COMP_DIR as the name of the current directory of the
24343 compilation. The result is allocated using xmalloc; the caller is
24344 responsible for freeing it. */
24346 file_full_name (int file, struct line_header *lh, const char *comp_dir)
24348 /* Is the file number a valid index into the line header's file name
24349 table? Remember that file numbers start with one, not zero. */
24350 if (1 <= file && file <= lh->file_names.size ())
24352 char *relative = file_file_name (file, lh);
24354 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24356 return reconcat (relative, comp_dir, SLASH_STRING,
24357 relative, (char *) NULL);
24360 return file_file_name (file, lh);
24364 static struct macro_source_file *
24365 macro_start_file (int file, int line,
24366 struct macro_source_file *current_file,
24367 struct line_header *lh)
24369 /* File name relative to the compilation directory of this source file. */
24370 char *file_name = file_file_name (file, lh);
24372 if (! current_file)
24374 /* Note: We don't create a macro table for this compilation unit
24375 at all until we actually get a filename. */
24376 struct macro_table *macro_table = get_macro_table ();
24378 /* If we have no current file, then this must be the start_file
24379 directive for the compilation unit's main source file. */
24380 current_file = macro_set_main (macro_table, file_name);
24381 macro_define_special (macro_table);
24384 current_file = macro_include (current_file, line, file_name);
24388 return current_file;
24391 static const char *
24392 consume_improper_spaces (const char *p, const char *body)
24396 complaint (&symfile_complaints,
24397 _("macro definition contains spaces "
24398 "in formal argument list:\n`%s'"),
24410 parse_macro_definition (struct macro_source_file *file, int line,
24415 /* The body string takes one of two forms. For object-like macro
24416 definitions, it should be:
24418 <macro name> " " <definition>
24420 For function-like macro definitions, it should be:
24422 <macro name> "() " <definition>
24424 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24426 Spaces may appear only where explicitly indicated, and in the
24429 The Dwarf 2 spec says that an object-like macro's name is always
24430 followed by a space, but versions of GCC around March 2002 omit
24431 the space when the macro's definition is the empty string.
24433 The Dwarf 2 spec says that there should be no spaces between the
24434 formal arguments in a function-like macro's formal argument list,
24435 but versions of GCC around March 2002 include spaces after the
24439 /* Find the extent of the macro name. The macro name is terminated
24440 by either a space or null character (for an object-like macro) or
24441 an opening paren (for a function-like macro). */
24442 for (p = body; *p; p++)
24443 if (*p == ' ' || *p == '(')
24446 if (*p == ' ' || *p == '\0')
24448 /* It's an object-like macro. */
24449 int name_len = p - body;
24450 char *name = savestring (body, name_len);
24451 const char *replacement;
24454 replacement = body + name_len + 1;
24457 dwarf2_macro_malformed_definition_complaint (body);
24458 replacement = body + name_len;
24461 macro_define_object (file, line, name, replacement);
24465 else if (*p == '(')
24467 /* It's a function-like macro. */
24468 char *name = savestring (body, p - body);
24471 char **argv = XNEWVEC (char *, argv_size);
24475 p = consume_improper_spaces (p, body);
24477 /* Parse the formal argument list. */
24478 while (*p && *p != ')')
24480 /* Find the extent of the current argument name. */
24481 const char *arg_start = p;
24483 while (*p && *p != ',' && *p != ')' && *p != ' ')
24486 if (! *p || p == arg_start)
24487 dwarf2_macro_malformed_definition_complaint (body);
24490 /* Make sure argv has room for the new argument. */
24491 if (argc >= argv_size)
24494 argv = XRESIZEVEC (char *, argv, argv_size);
24497 argv[argc++] = savestring (arg_start, p - arg_start);
24500 p = consume_improper_spaces (p, body);
24502 /* Consume the comma, if present. */
24507 p = consume_improper_spaces (p, body);
24516 /* Perfectly formed definition, no complaints. */
24517 macro_define_function (file, line, name,
24518 argc, (const char **) argv,
24520 else if (*p == '\0')
24522 /* Complain, but do define it. */
24523 dwarf2_macro_malformed_definition_complaint (body);
24524 macro_define_function (file, line, name,
24525 argc, (const char **) argv,
24529 /* Just complain. */
24530 dwarf2_macro_malformed_definition_complaint (body);
24533 /* Just complain. */
24534 dwarf2_macro_malformed_definition_complaint (body);
24540 for (i = 0; i < argc; i++)
24546 dwarf2_macro_malformed_definition_complaint (body);
24549 /* Skip some bytes from BYTES according to the form given in FORM.
24550 Returns the new pointer. */
24552 static const gdb_byte *
24553 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24554 enum dwarf_form form,
24555 unsigned int offset_size,
24556 struct dwarf2_section_info *section)
24558 unsigned int bytes_read;
24562 case DW_FORM_data1:
24567 case DW_FORM_data2:
24571 case DW_FORM_data4:
24575 case DW_FORM_data8:
24579 case DW_FORM_data16:
24583 case DW_FORM_string:
24584 read_direct_string (abfd, bytes, &bytes_read);
24585 bytes += bytes_read;
24588 case DW_FORM_sec_offset:
24590 case DW_FORM_GNU_strp_alt:
24591 bytes += offset_size;
24594 case DW_FORM_block:
24595 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24596 bytes += bytes_read;
24599 case DW_FORM_block1:
24600 bytes += 1 + read_1_byte (abfd, bytes);
24602 case DW_FORM_block2:
24603 bytes += 2 + read_2_bytes (abfd, bytes);
24605 case DW_FORM_block4:
24606 bytes += 4 + read_4_bytes (abfd, bytes);
24609 case DW_FORM_sdata:
24610 case DW_FORM_udata:
24611 case DW_FORM_GNU_addr_index:
24612 case DW_FORM_GNU_str_index:
24613 bytes = gdb_skip_leb128 (bytes, buffer_end);
24616 dwarf2_section_buffer_overflow_complaint (section);
24621 case DW_FORM_implicit_const:
24626 complaint (&symfile_complaints,
24627 _("invalid form 0x%x in `%s'"),
24628 form, get_section_name (section));
24636 /* A helper for dwarf_decode_macros that handles skipping an unknown
24637 opcode. Returns an updated pointer to the macro data buffer; or,
24638 on error, issues a complaint and returns NULL. */
24640 static const gdb_byte *
24641 skip_unknown_opcode (unsigned int opcode,
24642 const gdb_byte **opcode_definitions,
24643 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24645 unsigned int offset_size,
24646 struct dwarf2_section_info *section)
24648 unsigned int bytes_read, i;
24650 const gdb_byte *defn;
24652 if (opcode_definitions[opcode] == NULL)
24654 complaint (&symfile_complaints,
24655 _("unrecognized DW_MACFINO opcode 0x%x"),
24660 defn = opcode_definitions[opcode];
24661 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24662 defn += bytes_read;
24664 for (i = 0; i < arg; ++i)
24666 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24667 (enum dwarf_form) defn[i], offset_size,
24669 if (mac_ptr == NULL)
24671 /* skip_form_bytes already issued the complaint. */
24679 /* A helper function which parses the header of a macro section.
24680 If the macro section is the extended (for now called "GNU") type,
24681 then this updates *OFFSET_SIZE. Returns a pointer to just after
24682 the header, or issues a complaint and returns NULL on error. */
24684 static const gdb_byte *
24685 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24687 const gdb_byte *mac_ptr,
24688 unsigned int *offset_size,
24689 int section_is_gnu)
24691 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24693 if (section_is_gnu)
24695 unsigned int version, flags;
24697 version = read_2_bytes (abfd, mac_ptr);
24698 if (version != 4 && version != 5)
24700 complaint (&symfile_complaints,
24701 _("unrecognized version `%d' in .debug_macro section"),
24707 flags = read_1_byte (abfd, mac_ptr);
24709 *offset_size = (flags & 1) ? 8 : 4;
24711 if ((flags & 2) != 0)
24712 /* We don't need the line table offset. */
24713 mac_ptr += *offset_size;
24715 /* Vendor opcode descriptions. */
24716 if ((flags & 4) != 0)
24718 unsigned int i, count;
24720 count = read_1_byte (abfd, mac_ptr);
24722 for (i = 0; i < count; ++i)
24724 unsigned int opcode, bytes_read;
24727 opcode = read_1_byte (abfd, mac_ptr);
24729 opcode_definitions[opcode] = mac_ptr;
24730 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24731 mac_ptr += bytes_read;
24740 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24741 including DW_MACRO_import. */
24744 dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24746 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24747 struct macro_source_file *current_file,
24748 struct line_header *lh,
24749 struct dwarf2_section_info *section,
24750 int section_is_gnu, int section_is_dwz,
24751 unsigned int offset_size,
24752 htab_t include_hash)
24754 struct objfile *objfile = dwarf2_per_objfile->objfile;
24755 enum dwarf_macro_record_type macinfo_type;
24756 int at_commandline;
24757 const gdb_byte *opcode_definitions[256];
24759 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24760 &offset_size, section_is_gnu);
24761 if (mac_ptr == NULL)
24763 /* We already issued a complaint. */
24767 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24768 GDB is still reading the definitions from command line. First
24769 DW_MACINFO_start_file will need to be ignored as it was already executed
24770 to create CURRENT_FILE for the main source holding also the command line
24771 definitions. On first met DW_MACINFO_start_file this flag is reset to
24772 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24774 at_commandline = 1;
24778 /* Do we at least have room for a macinfo type byte? */
24779 if (mac_ptr >= mac_end)
24781 dwarf2_section_buffer_overflow_complaint (section);
24785 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24788 /* Note that we rely on the fact that the corresponding GNU and
24789 DWARF constants are the same. */
24791 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24792 switch (macinfo_type)
24794 /* A zero macinfo type indicates the end of the macro
24799 case DW_MACRO_define:
24800 case DW_MACRO_undef:
24801 case DW_MACRO_define_strp:
24802 case DW_MACRO_undef_strp:
24803 case DW_MACRO_define_sup:
24804 case DW_MACRO_undef_sup:
24806 unsigned int bytes_read;
24811 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24812 mac_ptr += bytes_read;
24814 if (macinfo_type == DW_MACRO_define
24815 || macinfo_type == DW_MACRO_undef)
24817 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24818 mac_ptr += bytes_read;
24822 LONGEST str_offset;
24824 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24825 mac_ptr += offset_size;
24827 if (macinfo_type == DW_MACRO_define_sup
24828 || macinfo_type == DW_MACRO_undef_sup
24831 struct dwz_file *dwz
24832 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24834 body = read_indirect_string_from_dwz (objfile,
24838 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24842 is_define = (macinfo_type == DW_MACRO_define
24843 || macinfo_type == DW_MACRO_define_strp
24844 || macinfo_type == DW_MACRO_define_sup);
24845 if (! current_file)
24847 /* DWARF violation as no main source is present. */
24848 complaint (&symfile_complaints,
24849 _("debug info with no main source gives macro %s "
24851 is_define ? _("definition") : _("undefinition"),
24855 if ((line == 0 && !at_commandline)
24856 || (line != 0 && at_commandline))
24857 complaint (&symfile_complaints,
24858 _("debug info gives %s macro %s with %s line %d: %s"),
24859 at_commandline ? _("command-line") : _("in-file"),
24860 is_define ? _("definition") : _("undefinition"),
24861 line == 0 ? _("zero") : _("non-zero"), line, body);
24864 parse_macro_definition (current_file, line, body);
24867 gdb_assert (macinfo_type == DW_MACRO_undef
24868 || macinfo_type == DW_MACRO_undef_strp
24869 || macinfo_type == DW_MACRO_undef_sup);
24870 macro_undef (current_file, line, body);
24875 case DW_MACRO_start_file:
24877 unsigned int bytes_read;
24880 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24881 mac_ptr += bytes_read;
24882 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24883 mac_ptr += bytes_read;
24885 if ((line == 0 && !at_commandline)
24886 || (line != 0 && at_commandline))
24887 complaint (&symfile_complaints,
24888 _("debug info gives source %d included "
24889 "from %s at %s line %d"),
24890 file, at_commandline ? _("command-line") : _("file"),
24891 line == 0 ? _("zero") : _("non-zero"), line);
24893 if (at_commandline)
24895 /* This DW_MACRO_start_file was executed in the
24897 at_commandline = 0;
24900 current_file = macro_start_file (file, line, current_file, lh);
24904 case DW_MACRO_end_file:
24905 if (! current_file)
24906 complaint (&symfile_complaints,
24907 _("macro debug info has an unmatched "
24908 "`close_file' directive"));
24911 current_file = current_file->included_by;
24912 if (! current_file)
24914 enum dwarf_macro_record_type next_type;
24916 /* GCC circa March 2002 doesn't produce the zero
24917 type byte marking the end of the compilation
24918 unit. Complain if it's not there, but exit no
24921 /* Do we at least have room for a macinfo type byte? */
24922 if (mac_ptr >= mac_end)
24924 dwarf2_section_buffer_overflow_complaint (section);
24928 /* We don't increment mac_ptr here, so this is just
24931 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24933 if (next_type != 0)
24934 complaint (&symfile_complaints,
24935 _("no terminating 0-type entry for "
24936 "macros in `.debug_macinfo' section"));
24943 case DW_MACRO_import:
24944 case DW_MACRO_import_sup:
24948 bfd *include_bfd = abfd;
24949 struct dwarf2_section_info *include_section = section;
24950 const gdb_byte *include_mac_end = mac_end;
24951 int is_dwz = section_is_dwz;
24952 const gdb_byte *new_mac_ptr;
24954 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24955 mac_ptr += offset_size;
24957 if (macinfo_type == DW_MACRO_import_sup)
24959 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24961 dwarf2_read_section (objfile, &dwz->macro);
24963 include_section = &dwz->macro;
24964 include_bfd = get_section_bfd_owner (include_section);
24965 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24969 new_mac_ptr = include_section->buffer + offset;
24970 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24974 /* This has actually happened; see
24975 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24976 complaint (&symfile_complaints,
24977 _("recursive DW_MACRO_import in "
24978 ".debug_macro section"));
24982 *slot = (void *) new_mac_ptr;
24984 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24985 include_bfd, new_mac_ptr,
24986 include_mac_end, current_file, lh,
24987 section, section_is_gnu, is_dwz,
24988 offset_size, include_hash);
24990 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24995 case DW_MACINFO_vendor_ext:
24996 if (!section_is_gnu)
24998 unsigned int bytes_read;
25000 /* This reads the constant, but since we don't recognize
25001 any vendor extensions, we ignore it. */
25002 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25003 mac_ptr += bytes_read;
25004 read_direct_string (abfd, mac_ptr, &bytes_read);
25005 mac_ptr += bytes_read;
25007 /* We don't recognize any vendor extensions. */
25013 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25014 mac_ptr, mac_end, abfd, offset_size,
25016 if (mac_ptr == NULL)
25021 } while (macinfo_type != 0);
25025 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
25026 int section_is_gnu)
25028 struct dwarf2_per_objfile *dwarf2_per_objfile
25029 = cu->per_cu->dwarf2_per_objfile;
25030 struct objfile *objfile = dwarf2_per_objfile->objfile;
25031 struct line_header *lh = cu->line_header;
25033 const gdb_byte *mac_ptr, *mac_end;
25034 struct macro_source_file *current_file = 0;
25035 enum dwarf_macro_record_type macinfo_type;
25036 unsigned int offset_size = cu->header.offset_size;
25037 const gdb_byte *opcode_definitions[256];
25039 struct dwarf2_section_info *section;
25040 const char *section_name;
25042 if (cu->dwo_unit != NULL)
25044 if (section_is_gnu)
25046 section = &cu->dwo_unit->dwo_file->sections.macro;
25047 section_name = ".debug_macro.dwo";
25051 section = &cu->dwo_unit->dwo_file->sections.macinfo;
25052 section_name = ".debug_macinfo.dwo";
25057 if (section_is_gnu)
25059 section = &dwarf2_per_objfile->macro;
25060 section_name = ".debug_macro";
25064 section = &dwarf2_per_objfile->macinfo;
25065 section_name = ".debug_macinfo";
25069 dwarf2_read_section (objfile, section);
25070 if (section->buffer == NULL)
25072 complaint (&symfile_complaints, _("missing %s section"), section_name);
25075 abfd = get_section_bfd_owner (section);
25077 /* First pass: Find the name of the base filename.
25078 This filename is needed in order to process all macros whose definition
25079 (or undefinition) comes from the command line. These macros are defined
25080 before the first DW_MACINFO_start_file entry, and yet still need to be
25081 associated to the base file.
25083 To determine the base file name, we scan the macro definitions until we
25084 reach the first DW_MACINFO_start_file entry. We then initialize
25085 CURRENT_FILE accordingly so that any macro definition found before the
25086 first DW_MACINFO_start_file can still be associated to the base file. */
25088 mac_ptr = section->buffer + offset;
25089 mac_end = section->buffer + section->size;
25091 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25092 &offset_size, section_is_gnu);
25093 if (mac_ptr == NULL)
25095 /* We already issued a complaint. */
25101 /* Do we at least have room for a macinfo type byte? */
25102 if (mac_ptr >= mac_end)
25104 /* Complaint is printed during the second pass as GDB will probably
25105 stop the first pass earlier upon finding
25106 DW_MACINFO_start_file. */
25110 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
25113 /* Note that we rely on the fact that the corresponding GNU and
25114 DWARF constants are the same. */
25116 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
25117 switch (macinfo_type)
25119 /* A zero macinfo type indicates the end of the macro
25124 case DW_MACRO_define:
25125 case DW_MACRO_undef:
25126 /* Only skip the data by MAC_PTR. */
25128 unsigned int bytes_read;
25130 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25131 mac_ptr += bytes_read;
25132 read_direct_string (abfd, mac_ptr, &bytes_read);
25133 mac_ptr += bytes_read;
25137 case DW_MACRO_start_file:
25139 unsigned int bytes_read;
25142 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25143 mac_ptr += bytes_read;
25144 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25145 mac_ptr += bytes_read;
25147 current_file = macro_start_file (file, line, current_file, lh);
25151 case DW_MACRO_end_file:
25152 /* No data to skip by MAC_PTR. */
25155 case DW_MACRO_define_strp:
25156 case DW_MACRO_undef_strp:
25157 case DW_MACRO_define_sup:
25158 case DW_MACRO_undef_sup:
25160 unsigned int bytes_read;
25162 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25163 mac_ptr += bytes_read;
25164 mac_ptr += offset_size;
25168 case DW_MACRO_import:
25169 case DW_MACRO_import_sup:
25170 /* Note that, according to the spec, a transparent include
25171 chain cannot call DW_MACRO_start_file. So, we can just
25172 skip this opcode. */
25173 mac_ptr += offset_size;
25176 case DW_MACINFO_vendor_ext:
25177 /* Only skip the data by MAC_PTR. */
25178 if (!section_is_gnu)
25180 unsigned int bytes_read;
25182 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25183 mac_ptr += bytes_read;
25184 read_direct_string (abfd, mac_ptr, &bytes_read);
25185 mac_ptr += bytes_read;
25190 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25191 mac_ptr, mac_end, abfd, offset_size,
25193 if (mac_ptr == NULL)
25198 } while (macinfo_type != 0 && current_file == NULL);
25200 /* Second pass: Process all entries.
25202 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25203 command-line macro definitions/undefinitions. This flag is unset when we
25204 reach the first DW_MACINFO_start_file entry. */
25206 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25208 NULL, xcalloc, xfree));
25209 mac_ptr = section->buffer + offset;
25210 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
25211 *slot = (void *) mac_ptr;
25212 dwarf_decode_macro_bytes (dwarf2_per_objfile,
25213 abfd, mac_ptr, mac_end,
25214 current_file, lh, section,
25215 section_is_gnu, 0, offset_size,
25216 include_hash.get ());
25219 /* Check if the attribute's form is a DW_FORM_block*
25220 if so return true else false. */
25223 attr_form_is_block (const struct attribute *attr)
25225 return (attr == NULL ? 0 :
25226 attr->form == DW_FORM_block1
25227 || attr->form == DW_FORM_block2
25228 || attr->form == DW_FORM_block4
25229 || attr->form == DW_FORM_block
25230 || attr->form == DW_FORM_exprloc);
25233 /* Return non-zero if ATTR's value is a section offset --- classes
25234 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25235 You may use DW_UNSND (attr) to retrieve such offsets.
25237 Section 7.5.4, "Attribute Encodings", explains that no attribute
25238 may have a value that belongs to more than one of these classes; it
25239 would be ambiguous if we did, because we use the same forms for all
25243 attr_form_is_section_offset (const struct attribute *attr)
25245 return (attr->form == DW_FORM_data4
25246 || attr->form == DW_FORM_data8
25247 || attr->form == DW_FORM_sec_offset);
25250 /* Return non-zero if ATTR's value falls in the 'constant' class, or
25251 zero otherwise. When this function returns true, you can apply
25252 dwarf2_get_attr_constant_value to it.
25254 However, note that for some attributes you must check
25255 attr_form_is_section_offset before using this test. DW_FORM_data4
25256 and DW_FORM_data8 are members of both the constant class, and of
25257 the classes that contain offsets into other debug sections
25258 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25259 that, if an attribute's can be either a constant or one of the
25260 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
25261 taken as section offsets, not constants.
25263 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25264 cannot handle that. */
25267 attr_form_is_constant (const struct attribute *attr)
25269 switch (attr->form)
25271 case DW_FORM_sdata:
25272 case DW_FORM_udata:
25273 case DW_FORM_data1:
25274 case DW_FORM_data2:
25275 case DW_FORM_data4:
25276 case DW_FORM_data8:
25277 case DW_FORM_implicit_const:
25285 /* DW_ADDR is always stored already as sect_offset; despite for the forms
25286 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25289 attr_form_is_ref (const struct attribute *attr)
25291 switch (attr->form)
25293 case DW_FORM_ref_addr:
25298 case DW_FORM_ref_udata:
25299 case DW_FORM_GNU_ref_alt:
25306 /* Return the .debug_loc section to use for CU.
25307 For DWO files use .debug_loc.dwo. */
25309 static struct dwarf2_section_info *
25310 cu_debug_loc_section (struct dwarf2_cu *cu)
25312 struct dwarf2_per_objfile *dwarf2_per_objfile
25313 = cu->per_cu->dwarf2_per_objfile;
25317 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25319 return cu->header.version >= 5 ? §ions->loclists : §ions->loc;
25321 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25322 : &dwarf2_per_objfile->loc);
25325 /* A helper function that fills in a dwarf2_loclist_baton. */
25328 fill_in_loclist_baton (struct dwarf2_cu *cu,
25329 struct dwarf2_loclist_baton *baton,
25330 const struct attribute *attr)
25332 struct dwarf2_per_objfile *dwarf2_per_objfile
25333 = cu->per_cu->dwarf2_per_objfile;
25334 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25336 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
25338 baton->per_cu = cu->per_cu;
25339 gdb_assert (baton->per_cu);
25340 /* We don't know how long the location list is, but make sure we
25341 don't run off the edge of the section. */
25342 baton->size = section->size - DW_UNSND (attr);
25343 baton->data = section->buffer + DW_UNSND (attr);
25344 baton->base_address = cu->base_address;
25345 baton->from_dwo = cu->dwo_unit != NULL;
25349 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
25350 struct dwarf2_cu *cu, int is_block)
25352 struct dwarf2_per_objfile *dwarf2_per_objfile
25353 = cu->per_cu->dwarf2_per_objfile;
25354 struct objfile *objfile = dwarf2_per_objfile->objfile;
25355 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25357 if (attr_form_is_section_offset (attr)
25358 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
25359 the section. If so, fall through to the complaint in the
25361 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
25363 struct dwarf2_loclist_baton *baton;
25365 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25367 fill_in_loclist_baton (cu, baton, attr);
25369 if (cu->base_known == 0)
25370 complaint (&symfile_complaints,
25371 _("Location list used without "
25372 "specifying the CU base address."));
25374 SYMBOL_ACLASS_INDEX (sym) = (is_block
25375 ? dwarf2_loclist_block_index
25376 : dwarf2_loclist_index);
25377 SYMBOL_LOCATION_BATON (sym) = baton;
25381 struct dwarf2_locexpr_baton *baton;
25383 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25384 baton->per_cu = cu->per_cu;
25385 gdb_assert (baton->per_cu);
25387 if (attr_form_is_block (attr))
25389 /* Note that we're just copying the block's data pointer
25390 here, not the actual data. We're still pointing into the
25391 info_buffer for SYM's objfile; right now we never release
25392 that buffer, but when we do clean up properly this may
25394 baton->size = DW_BLOCK (attr)->size;
25395 baton->data = DW_BLOCK (attr)->data;
25399 dwarf2_invalid_attrib_class_complaint ("location description",
25400 SYMBOL_NATURAL_NAME (sym));
25404 SYMBOL_ACLASS_INDEX (sym) = (is_block
25405 ? dwarf2_locexpr_block_index
25406 : dwarf2_locexpr_index);
25407 SYMBOL_LOCATION_BATON (sym) = baton;
25411 /* Return the OBJFILE associated with the compilation unit CU. If CU
25412 came from a separate debuginfo file, then the master objfile is
25416 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25418 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25420 /* Return the master objfile, so that we can report and look up the
25421 correct file containing this variable. */
25422 if (objfile->separate_debug_objfile_backlink)
25423 objfile = objfile->separate_debug_objfile_backlink;
25428 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25429 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25430 CU_HEADERP first. */
25432 static const struct comp_unit_head *
25433 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25434 struct dwarf2_per_cu_data *per_cu)
25436 const gdb_byte *info_ptr;
25439 return &per_cu->cu->header;
25441 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25443 memset (cu_headerp, 0, sizeof (*cu_headerp));
25444 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25445 rcuh_kind::COMPILE);
25450 /* Return the address size given in the compilation unit header for CU. */
25453 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25455 struct comp_unit_head cu_header_local;
25456 const struct comp_unit_head *cu_headerp;
25458 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25460 return cu_headerp->addr_size;
25463 /* Return the offset size given in the compilation unit header for CU. */
25466 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25468 struct comp_unit_head cu_header_local;
25469 const struct comp_unit_head *cu_headerp;
25471 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25473 return cu_headerp->offset_size;
25476 /* See its dwarf2loc.h declaration. */
25479 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25481 struct comp_unit_head cu_header_local;
25482 const struct comp_unit_head *cu_headerp;
25484 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25486 if (cu_headerp->version == 2)
25487 return cu_headerp->addr_size;
25489 return cu_headerp->offset_size;
25492 /* Return the text offset of the CU. The returned offset comes from
25493 this CU's objfile. If this objfile came from a separate debuginfo
25494 file, then the offset may be different from the corresponding
25495 offset in the parent objfile. */
25498 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25500 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25502 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25505 /* Return DWARF version number of PER_CU. */
25508 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25510 return per_cu->dwarf_version;
25513 /* Locate the .debug_info compilation unit from CU's objfile which contains
25514 the DIE at OFFSET. Raises an error on failure. */
25516 static struct dwarf2_per_cu_data *
25517 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25518 unsigned int offset_in_dwz,
25519 struct dwarf2_per_objfile *dwarf2_per_objfile)
25521 struct dwarf2_per_cu_data *this_cu;
25523 const sect_offset *cu_off;
25526 high = dwarf2_per_objfile->n_comp_units - 1;
25529 struct dwarf2_per_cu_data *mid_cu;
25530 int mid = low + (high - low) / 2;
25532 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25533 cu_off = &mid_cu->sect_off;
25534 if (mid_cu->is_dwz > offset_in_dwz
25535 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
25540 gdb_assert (low == high);
25541 this_cu = dwarf2_per_objfile->all_comp_units[low];
25542 cu_off = &this_cu->sect_off;
25543 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
25545 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25546 error (_("Dwarf Error: could not find partial DIE containing "
25547 "offset %s [in module %s]"),
25548 sect_offset_str (sect_off),
25549 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25551 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25553 return dwarf2_per_objfile->all_comp_units[low-1];
25557 this_cu = dwarf2_per_objfile->all_comp_units[low];
25558 if (low == dwarf2_per_objfile->n_comp_units - 1
25559 && sect_off >= this_cu->sect_off + this_cu->length)
25560 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25561 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25566 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25568 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25569 : per_cu (per_cu_),
25572 checked_producer (0),
25573 producer_is_gxx_lt_4_6 (0),
25574 producer_is_gcc_lt_4_3 (0),
25575 producer_is_icc_lt_14 (0),
25576 processing_has_namespace_info (0)
25581 /* Destroy a dwarf2_cu. */
25583 dwarf2_cu::~dwarf2_cu ()
25588 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25591 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25592 enum language pretend_language)
25594 struct attribute *attr;
25596 /* Set the language we're debugging. */
25597 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25599 set_cu_language (DW_UNSND (attr), cu);
25602 cu->language = pretend_language;
25603 cu->language_defn = language_def (cu->language);
25606 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25609 /* Free all cached compilation units. */
25612 free_cached_comp_units (void *data)
25614 struct dwarf2_per_objfile *dwarf2_per_objfile
25615 = (struct dwarf2_per_objfile *) data;
25617 dwarf2_per_objfile->free_cached_comp_units ();
25620 /* Increase the age counter on each cached compilation unit, and free
25621 any that are too old. */
25624 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25626 struct dwarf2_per_cu_data *per_cu, **last_chain;
25628 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25629 per_cu = dwarf2_per_objfile->read_in_chain;
25630 while (per_cu != NULL)
25632 per_cu->cu->last_used ++;
25633 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25634 dwarf2_mark (per_cu->cu);
25635 per_cu = per_cu->cu->read_in_chain;
25638 per_cu = dwarf2_per_objfile->read_in_chain;
25639 last_chain = &dwarf2_per_objfile->read_in_chain;
25640 while (per_cu != NULL)
25642 struct dwarf2_per_cu_data *next_cu;
25644 next_cu = per_cu->cu->read_in_chain;
25646 if (!per_cu->cu->mark)
25649 *last_chain = next_cu;
25652 last_chain = &per_cu->cu->read_in_chain;
25658 /* Remove a single compilation unit from the cache. */
25661 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25663 struct dwarf2_per_cu_data *per_cu, **last_chain;
25664 struct dwarf2_per_objfile *dwarf2_per_objfile
25665 = target_per_cu->dwarf2_per_objfile;
25667 per_cu = dwarf2_per_objfile->read_in_chain;
25668 last_chain = &dwarf2_per_objfile->read_in_chain;
25669 while (per_cu != NULL)
25671 struct dwarf2_per_cu_data *next_cu;
25673 next_cu = per_cu->cu->read_in_chain;
25675 if (per_cu == target_per_cu)
25679 *last_chain = next_cu;
25683 last_chain = &per_cu->cu->read_in_chain;
25689 /* Release all extra memory associated with OBJFILE. */
25692 dwarf2_free_objfile (struct objfile *objfile)
25694 struct dwarf2_per_objfile *dwarf2_per_objfile
25695 = get_dwarf2_per_objfile (objfile);
25697 delete dwarf2_per_objfile;
25700 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25701 We store these in a hash table separate from the DIEs, and preserve them
25702 when the DIEs are flushed out of cache.
25704 The CU "per_cu" pointer is needed because offset alone is not enough to
25705 uniquely identify the type. A file may have multiple .debug_types sections,
25706 or the type may come from a DWO file. Furthermore, while it's more logical
25707 to use per_cu->section+offset, with Fission the section with the data is in
25708 the DWO file but we don't know that section at the point we need it.
25709 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25710 because we can enter the lookup routine, get_die_type_at_offset, from
25711 outside this file, and thus won't necessarily have PER_CU->cu.
25712 Fortunately, PER_CU is stable for the life of the objfile. */
25714 struct dwarf2_per_cu_offset_and_type
25716 const struct dwarf2_per_cu_data *per_cu;
25717 sect_offset sect_off;
25721 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25724 per_cu_offset_and_type_hash (const void *item)
25726 const struct dwarf2_per_cu_offset_and_type *ofs
25727 = (const struct dwarf2_per_cu_offset_and_type *) item;
25729 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25732 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25735 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25737 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25738 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25739 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25740 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25742 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25743 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25746 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25747 table if necessary. For convenience, return TYPE.
25749 The DIEs reading must have careful ordering to:
25750 * Not cause infite loops trying to read in DIEs as a prerequisite for
25751 reading current DIE.
25752 * Not trying to dereference contents of still incompletely read in types
25753 while reading in other DIEs.
25754 * Enable referencing still incompletely read in types just by a pointer to
25755 the type without accessing its fields.
25757 Therefore caller should follow these rules:
25758 * Try to fetch any prerequisite types we may need to build this DIE type
25759 before building the type and calling set_die_type.
25760 * After building type call set_die_type for current DIE as soon as
25761 possible before fetching more types to complete the current type.
25762 * Make the type as complete as possible before fetching more types. */
25764 static struct type *
25765 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25767 struct dwarf2_per_objfile *dwarf2_per_objfile
25768 = cu->per_cu->dwarf2_per_objfile;
25769 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25770 struct objfile *objfile = dwarf2_per_objfile->objfile;
25771 struct attribute *attr;
25772 struct dynamic_prop prop;
25774 /* For Ada types, make sure that the gnat-specific data is always
25775 initialized (if not already set). There are a few types where
25776 we should not be doing so, because the type-specific area is
25777 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25778 where the type-specific area is used to store the floatformat).
25779 But this is not a problem, because the gnat-specific information
25780 is actually not needed for these types. */
25781 if (need_gnat_info (cu)
25782 && TYPE_CODE (type) != TYPE_CODE_FUNC
25783 && TYPE_CODE (type) != TYPE_CODE_FLT
25784 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25785 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25786 && TYPE_CODE (type) != TYPE_CODE_METHOD
25787 && !HAVE_GNAT_AUX_INFO (type))
25788 INIT_GNAT_SPECIFIC (type);
25790 /* Read DW_AT_allocated and set in type. */
25791 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25792 if (attr_form_is_block (attr))
25794 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25795 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25797 else if (attr != NULL)
25799 complaint (&symfile_complaints,
25800 _("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25801 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25802 sect_offset_str (die->sect_off));
25805 /* Read DW_AT_associated and set in type. */
25806 attr = dwarf2_attr (die, DW_AT_associated, cu);
25807 if (attr_form_is_block (attr))
25809 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25810 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25812 else if (attr != NULL)
25814 complaint (&symfile_complaints,
25815 _("DW_AT_associated has the wrong form (%s) at DIE %s"),
25816 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25817 sect_offset_str (die->sect_off));
25820 /* Read DW_AT_data_location and set in type. */
25821 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25822 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25823 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25825 if (dwarf2_per_objfile->die_type_hash == NULL)
25827 dwarf2_per_objfile->die_type_hash =
25828 htab_create_alloc_ex (127,
25829 per_cu_offset_and_type_hash,
25830 per_cu_offset_and_type_eq,
25832 &objfile->objfile_obstack,
25833 hashtab_obstack_allocate,
25834 dummy_obstack_deallocate);
25837 ofs.per_cu = cu->per_cu;
25838 ofs.sect_off = die->sect_off;
25840 slot = (struct dwarf2_per_cu_offset_and_type **)
25841 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25843 complaint (&symfile_complaints,
25844 _("A problem internal to GDB: DIE %s has type already set"),
25845 sect_offset_str (die->sect_off));
25846 *slot = XOBNEW (&objfile->objfile_obstack,
25847 struct dwarf2_per_cu_offset_and_type);
25852 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25853 or return NULL if the die does not have a saved type. */
25855 static struct type *
25856 get_die_type_at_offset (sect_offset sect_off,
25857 struct dwarf2_per_cu_data *per_cu)
25859 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25860 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25862 if (dwarf2_per_objfile->die_type_hash == NULL)
25865 ofs.per_cu = per_cu;
25866 ofs.sect_off = sect_off;
25867 slot = ((struct dwarf2_per_cu_offset_and_type *)
25868 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25875 /* Look up the type for DIE in CU in die_type_hash,
25876 or return NULL if DIE does not have a saved type. */
25878 static struct type *
25879 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25881 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25884 /* Add a dependence relationship from CU to REF_PER_CU. */
25887 dwarf2_add_dependence (struct dwarf2_cu *cu,
25888 struct dwarf2_per_cu_data *ref_per_cu)
25892 if (cu->dependencies == NULL)
25894 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25895 NULL, &cu->comp_unit_obstack,
25896 hashtab_obstack_allocate,
25897 dummy_obstack_deallocate);
25899 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25901 *slot = ref_per_cu;
25904 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25905 Set the mark field in every compilation unit in the
25906 cache that we must keep because we are keeping CU. */
25909 dwarf2_mark_helper (void **slot, void *data)
25911 struct dwarf2_per_cu_data *per_cu;
25913 per_cu = (struct dwarf2_per_cu_data *) *slot;
25915 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25916 reading of the chain. As such dependencies remain valid it is not much
25917 useful to track and undo them during QUIT cleanups. */
25918 if (per_cu->cu == NULL)
25921 if (per_cu->cu->mark)
25923 per_cu->cu->mark = 1;
25925 if (per_cu->cu->dependencies != NULL)
25926 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25931 /* Set the mark field in CU and in every other compilation unit in the
25932 cache that we must keep because we are keeping CU. */
25935 dwarf2_mark (struct dwarf2_cu *cu)
25940 if (cu->dependencies != NULL)
25941 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25945 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25949 per_cu->cu->mark = 0;
25950 per_cu = per_cu->cu->read_in_chain;
25954 /* Trivial hash function for partial_die_info: the hash value of a DIE
25955 is its offset in .debug_info for this objfile. */
25958 partial_die_hash (const void *item)
25960 const struct partial_die_info *part_die
25961 = (const struct partial_die_info *) item;
25963 return to_underlying (part_die->sect_off);
25966 /* Trivial comparison function for partial_die_info structures: two DIEs
25967 are equal if they have the same offset. */
25970 partial_die_eq (const void *item_lhs, const void *item_rhs)
25972 const struct partial_die_info *part_die_lhs
25973 = (const struct partial_die_info *) item_lhs;
25974 const struct partial_die_info *part_die_rhs
25975 = (const struct partial_die_info *) item_rhs;
25977 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25980 static struct cmd_list_element *set_dwarf_cmdlist;
25981 static struct cmd_list_element *show_dwarf_cmdlist;
25984 set_dwarf_cmd (const char *args, int from_tty)
25986 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25991 show_dwarf_cmd (const char *args, int from_tty)
25993 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25996 /* The "save gdb-index" command. */
25998 /* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
26002 file_write (FILE *file, const void *data, size_t size)
26004 if (fwrite (data, 1, size, file) != size)
26005 error (_("couldn't data write to file"));
26008 /* Write the contents of VEC to FILE, with error checking. */
26010 template<typename Elem, typename Alloc>
26012 file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
26014 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
26017 /* In-memory buffer to prepare data to be written later to a file. */
26021 /* Copy DATA to the end of the buffer. */
26022 template<typename T>
26023 void append_data (const T &data)
26025 std::copy (reinterpret_cast<const gdb_byte *> (&data),
26026 reinterpret_cast<const gdb_byte *> (&data + 1),
26027 grow (sizeof (data)));
26030 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
26031 terminating zero is appended too. */
26032 void append_cstr0 (const char *cstr)
26034 const size_t size = strlen (cstr) + 1;
26035 std::copy (cstr, cstr + size, grow (size));
26038 /* Store INPUT as ULEB128 to the end of buffer. */
26039 void append_unsigned_leb128 (ULONGEST input)
26043 gdb_byte output = input & 0x7f;
26047 append_data (output);
26053 /* Accept a host-format integer in VAL and append it to the buffer
26054 as a target-format integer which is LEN bytes long. */
26055 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
26057 ::store_unsigned_integer (grow (len), len, byte_order, val);
26060 /* Return the size of the buffer. */
26061 size_t size () const
26063 return m_vec.size ();
26066 /* Return true iff the buffer is empty. */
26067 bool empty () const
26069 return m_vec.empty ();
26072 /* Write the buffer to FILE. */
26073 void file_write (FILE *file) const
26075 ::file_write (file, m_vec);
26079 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
26080 the start of the new block. */
26081 gdb_byte *grow (size_t size)
26083 m_vec.resize (m_vec.size () + size);
26084 return &*m_vec.end () - size;
26087 gdb::byte_vector m_vec;
26090 /* An entry in the symbol table. */
26091 struct symtab_index_entry
26093 /* The name of the symbol. */
26095 /* The offset of the name in the constant pool. */
26096 offset_type index_offset;
26097 /* A sorted vector of the indices of all the CUs that hold an object
26099 std::vector<offset_type> cu_indices;
26102 /* The symbol table. This is a power-of-2-sized hash table. */
26103 struct mapped_symtab
26107 data.resize (1024);
26110 offset_type n_elements = 0;
26111 std::vector<symtab_index_entry> data;
26114 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
26117 Function is used only during write_hash_table so no index format backward
26118 compatibility is needed. */
26120 static symtab_index_entry &
26121 find_slot (struct mapped_symtab *symtab, const char *name)
26123 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
26125 index = hash & (symtab->data.size () - 1);
26126 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
26130 if (symtab->data[index].name == NULL
26131 || strcmp (name, symtab->data[index].name) == 0)
26132 return symtab->data[index];
26133 index = (index + step) & (symtab->data.size () - 1);
26137 /* Expand SYMTAB's hash table. */
26140 hash_expand (struct mapped_symtab *symtab)
26142 auto old_entries = std::move (symtab->data);
26144 symtab->data.clear ();
26145 symtab->data.resize (old_entries.size () * 2);
26147 for (auto &it : old_entries)
26148 if (it.name != NULL)
26150 auto &ref = find_slot (symtab, it.name);
26151 ref = std::move (it);
26155 /* Add an entry to SYMTAB. NAME is the name of the symbol.
26156 CU_INDEX is the index of the CU in which the symbol appears.
26157 IS_STATIC is one if the symbol is static, otherwise zero (global). */
26160 add_index_entry (struct mapped_symtab *symtab, const char *name,
26161 int is_static, gdb_index_symbol_kind kind,
26162 offset_type cu_index)
26164 offset_type cu_index_and_attrs;
26166 ++symtab->n_elements;
26167 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
26168 hash_expand (symtab);
26170 symtab_index_entry &slot = find_slot (symtab, name);
26171 if (slot.name == NULL)
26174 /* index_offset is set later. */
26177 cu_index_and_attrs = 0;
26178 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
26179 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
26180 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
26182 /* We don't want to record an index value twice as we want to avoid the
26184 We process all global symbols and then all static symbols
26185 (which would allow us to avoid the duplication by only having to check
26186 the last entry pushed), but a symbol could have multiple kinds in one CU.
26187 To keep things simple we don't worry about the duplication here and
26188 sort and uniqufy the list after we've processed all symbols. */
26189 slot.cu_indices.push_back (cu_index_and_attrs);
26192 /* Sort and remove duplicates of all symbols' cu_indices lists. */
26195 uniquify_cu_indices (struct mapped_symtab *symtab)
26197 for (auto &entry : symtab->data)
26199 if (entry.name != NULL && !entry.cu_indices.empty ())
26201 auto &cu_indices = entry.cu_indices;
26202 std::sort (cu_indices.begin (), cu_indices.end ());
26203 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
26204 cu_indices.erase (from, cu_indices.end ());
26209 /* A form of 'const char *' suitable for container keys. Only the
26210 pointer is stored. The strings themselves are compared, not the
26215 c_str_view (const char *cstr)
26219 bool operator== (const c_str_view &other) const
26221 return strcmp (m_cstr, other.m_cstr) == 0;
26224 /* Return the underlying C string. Note, the returned string is
26225 only a reference with lifetime of this object. */
26226 const char *c_str () const
26232 friend class c_str_view_hasher;
26233 const char *const m_cstr;
26236 /* A std::unordered_map::hasher for c_str_view that uses the right
26237 hash function for strings in a mapped index. */
26238 class c_str_view_hasher
26241 size_t operator () (const c_str_view &x) const
26243 return mapped_index_string_hash (INT_MAX, x.m_cstr);
26247 /* A std::unordered_map::hasher for std::vector<>. */
26248 template<typename T>
26249 class vector_hasher
26252 size_t operator () (const std::vector<T> &key) const
26254 return iterative_hash (key.data (),
26255 sizeof (key.front ()) * key.size (), 0);
26259 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
26260 constant pool entries going into the data buffer CPOOL. */
26263 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
26266 /* Elements are sorted vectors of the indices of all the CUs that
26267 hold an object of this name. */
26268 std::unordered_map<std::vector<offset_type>, offset_type,
26269 vector_hasher<offset_type>>
26272 /* We add all the index vectors to the constant pool first, to
26273 ensure alignment is ok. */
26274 for (symtab_index_entry &entry : symtab->data)
26276 if (entry.name == NULL)
26278 gdb_assert (entry.index_offset == 0);
26280 /* Finding before inserting is faster than always trying to
26281 insert, because inserting always allocates a node, does the
26282 lookup, and then destroys the new node if another node
26283 already had the same key. C++17 try_emplace will avoid
26286 = symbol_hash_table.find (entry.cu_indices);
26287 if (found != symbol_hash_table.end ())
26289 entry.index_offset = found->second;
26293 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
26294 entry.index_offset = cpool.size ();
26295 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
26296 for (const auto index : entry.cu_indices)
26297 cpool.append_data (MAYBE_SWAP (index));
26301 /* Now write out the hash table. */
26302 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
26303 for (const auto &entry : symtab->data)
26305 offset_type str_off, vec_off;
26307 if (entry.name != NULL)
26309 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
26310 if (insertpair.second)
26311 cpool.append_cstr0 (entry.name);
26312 str_off = insertpair.first->second;
26313 vec_off = entry.index_offset;
26317 /* While 0 is a valid constant pool index, it is not valid
26318 to have 0 for both offsets. */
26323 output.append_data (MAYBE_SWAP (str_off));
26324 output.append_data (MAYBE_SWAP (vec_off));
26328 typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
26330 /* Helper struct for building the address table. */
26331 struct addrmap_index_data
26333 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
26334 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
26337 struct objfile *objfile;
26338 data_buf &addr_vec;
26339 psym_index_map &cu_index_htab;
26341 /* Non-zero if the previous_* fields are valid.
26342 We can't write an entry until we see the next entry (since it is only then
26343 that we know the end of the entry). */
26344 int previous_valid;
26345 /* Index of the CU in the table of all CUs in the index file. */
26346 unsigned int previous_cu_index;
26347 /* Start address of the CU. */
26348 CORE_ADDR previous_cu_start;
26351 /* Write an address entry to ADDR_VEC. */
26354 add_address_entry (struct objfile *objfile, data_buf &addr_vec,
26355 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
26357 CORE_ADDR baseaddr;
26359 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
26361 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
26362 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
26363 addr_vec.append_data (MAYBE_SWAP (cu_index));
26366 /* Worker function for traversing an addrmap to build the address table. */
26369 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
26371 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
26372 struct partial_symtab *pst = (struct partial_symtab *) obj;
26374 if (data->previous_valid)
26375 add_address_entry (data->objfile, data->addr_vec,
26376 data->previous_cu_start, start_addr,
26377 data->previous_cu_index);
26379 data->previous_cu_start = start_addr;
26382 const auto it = data->cu_index_htab.find (pst);
26383 gdb_assert (it != data->cu_index_htab.cend ());
26384 data->previous_cu_index = it->second;
26385 data->previous_valid = 1;
26388 data->previous_valid = 0;
26393 /* Write OBJFILE's address map to ADDR_VEC.
26394 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
26395 in the index file. */
26398 write_address_map (struct objfile *objfile, data_buf &addr_vec,
26399 psym_index_map &cu_index_htab)
26401 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
26403 /* When writing the address table, we have to cope with the fact that
26404 the addrmap iterator only provides the start of a region; we have to
26405 wait until the next invocation to get the start of the next region. */
26407 addrmap_index_data.objfile = objfile;
26408 addrmap_index_data.previous_valid = 0;
26410 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
26411 &addrmap_index_data);
26413 /* It's highly unlikely the last entry (end address = 0xff...ff)
26414 is valid, but we should still handle it.
26415 The end address is recorded as the start of the next region, but that
26416 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
26418 if (addrmap_index_data.previous_valid)
26419 add_address_entry (objfile, addr_vec,
26420 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
26421 addrmap_index_data.previous_cu_index);
26424 /* Return the symbol kind of PSYM. */
26426 static gdb_index_symbol_kind
26427 symbol_kind (struct partial_symbol *psym)
26429 domain_enum domain = PSYMBOL_DOMAIN (psym);
26430 enum address_class aclass = PSYMBOL_CLASS (psym);
26438 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
26440 return GDB_INDEX_SYMBOL_KIND_TYPE;
26442 case LOC_CONST_BYTES:
26443 case LOC_OPTIMIZED_OUT:
26445 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
26447 /* Note: It's currently impossible to recognize psyms as enum values
26448 short of reading the type info. For now punt. */
26449 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
26451 /* There are other LOC_FOO values that one might want to classify
26452 as variables, but dwarf2read.c doesn't currently use them. */
26453 return GDB_INDEX_SYMBOL_KIND_OTHER;
26455 case STRUCT_DOMAIN:
26456 return GDB_INDEX_SYMBOL_KIND_TYPE;
26458 return GDB_INDEX_SYMBOL_KIND_OTHER;
26462 /* Add a list of partial symbols to SYMTAB. */
26465 write_psymbols (struct mapped_symtab *symtab,
26466 std::unordered_set<partial_symbol *> &psyms_seen,
26467 struct partial_symbol **psymp,
26469 offset_type cu_index,
26472 for (; count-- > 0; ++psymp)
26474 struct partial_symbol *psym = *psymp;
26476 if (SYMBOL_LANGUAGE (psym) == language_ada)
26477 error (_("Ada is not currently supported by the index"));
26479 /* Only add a given psymbol once. */
26480 if (psyms_seen.insert (psym).second)
26482 gdb_index_symbol_kind kind = symbol_kind (psym);
26484 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
26485 is_static, kind, cu_index);
26490 /* A helper struct used when iterating over debug_types. */
26491 struct signatured_type_index_data
26493 signatured_type_index_data (data_buf &types_list_,
26494 std::unordered_set<partial_symbol *> &psyms_seen_)
26495 : types_list (types_list_), psyms_seen (psyms_seen_)
26498 struct objfile *objfile;
26499 struct mapped_symtab *symtab;
26500 data_buf &types_list;
26501 std::unordered_set<partial_symbol *> &psyms_seen;
26505 /* A helper function that writes a single signatured_type to an
26509 write_one_signatured_type (void **slot, void *d)
26511 struct signatured_type_index_data *info
26512 = (struct signatured_type_index_data *) d;
26513 struct signatured_type *entry = (struct signatured_type *) *slot;
26514 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
26516 write_psymbols (info->symtab,
26518 &info->objfile->global_psymbols[psymtab->globals_offset],
26519 psymtab->n_global_syms, info->cu_index,
26521 write_psymbols (info->symtab,
26523 &info->objfile->static_psymbols[psymtab->statics_offset],
26524 psymtab->n_static_syms, info->cu_index,
26527 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26528 to_underlying (entry->per_cu.sect_off));
26529 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26530 to_underlying (entry->type_offset_in_tu));
26531 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
26538 /* Recurse into all "included" dependencies and count their symbols as
26539 if they appeared in this psymtab. */
26542 recursively_count_psymbols (struct partial_symtab *psymtab,
26543 size_t &psyms_seen)
26545 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26546 if (psymtab->dependencies[i]->user != NULL)
26547 recursively_count_psymbols (psymtab->dependencies[i],
26550 psyms_seen += psymtab->n_global_syms;
26551 psyms_seen += psymtab->n_static_syms;
26554 /* Recurse into all "included" dependencies and write their symbols as
26555 if they appeared in this psymtab. */
26558 recursively_write_psymbols (struct objfile *objfile,
26559 struct partial_symtab *psymtab,
26560 struct mapped_symtab *symtab,
26561 std::unordered_set<partial_symbol *> &psyms_seen,
26562 offset_type cu_index)
26566 for (i = 0; i < psymtab->number_of_dependencies; ++i)
26567 if (psymtab->dependencies[i]->user != NULL)
26568 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26569 symtab, psyms_seen, cu_index);
26571 write_psymbols (symtab,
26573 &objfile->global_psymbols[psymtab->globals_offset],
26574 psymtab->n_global_syms, cu_index,
26576 write_psymbols (symtab,
26578 &objfile->static_psymbols[psymtab->statics_offset],
26579 psymtab->n_static_syms, cu_index,
26583 /* DWARF-5 .debug_names builder. */
26587 debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile, bool is_dwarf64,
26588 bfd_endian dwarf5_byte_order)
26589 : m_dwarf5_byte_order (dwarf5_byte_order),
26590 m_dwarf32 (dwarf5_byte_order),
26591 m_dwarf64 (dwarf5_byte_order),
26592 m_dwarf (is_dwarf64
26593 ? static_cast<dwarf &> (m_dwarf64)
26594 : static_cast<dwarf &> (m_dwarf32)),
26595 m_name_table_string_offs (m_dwarf.name_table_string_offs),
26596 m_name_table_entry_offs (m_dwarf.name_table_entry_offs),
26597 m_debugstrlookup (dwarf2_per_objfile)
26600 int dwarf5_offset_size () const
26602 const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64;
26603 return dwarf5_is_dwarf64 ? 8 : 4;
26606 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
26607 enum class unit_kind { cu, tu };
26609 /* Insert one symbol. */
26610 void insert (const partial_symbol *psym, int cu_index, bool is_static,
26613 const int dwarf_tag = psymbol_tag (psym);
26614 if (dwarf_tag == 0)
26616 const char *const name = SYMBOL_SEARCH_NAME (psym);
26617 const auto insertpair
26618 = m_name_to_value_set.emplace (c_str_view (name),
26619 std::set<symbol_value> ());
26620 std::set<symbol_value> &value_set = insertpair.first->second;
26621 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static, kind));
26624 /* Build all the tables. All symbols must be already inserted.
26625 This function does not call file_write, caller has to do it
26629 /* Verify the build method has not be called twice. */
26630 gdb_assert (m_abbrev_table.empty ());
26631 const size_t name_count = m_name_to_value_set.size ();
26632 m_bucket_table.resize
26633 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
26634 m_hash_table.reserve (name_count);
26635 m_name_table_string_offs.reserve (name_count);
26636 m_name_table_entry_offs.reserve (name_count);
26638 /* Map each hash of symbol to its name and value. */
26639 struct hash_it_pair
26642 decltype (m_name_to_value_set)::const_iterator it;
26644 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
26645 bucket_hash.resize (m_bucket_table.size ());
26646 for (decltype (m_name_to_value_set)::const_iterator it
26647 = m_name_to_value_set.cbegin ();
26648 it != m_name_to_value_set.cend ();
26651 const char *const name = it->first.c_str ();
26652 const uint32_t hash = dwarf5_djb_hash (name);
26653 hash_it_pair hashitpair;
26654 hashitpair.hash = hash;
26655 hashitpair.it = it;
26656 auto &slot = bucket_hash[hash % bucket_hash.size()];
26657 slot.push_front (std::move (hashitpair));
26659 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
26661 const std::forward_list<hash_it_pair> &hashitlist
26662 = bucket_hash[bucket_ix];
26663 if (hashitlist.empty ())
26665 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
26666 /* The hashes array is indexed starting at 1. */
26667 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
26668 sizeof (bucket_slot), m_dwarf5_byte_order,
26669 m_hash_table.size () + 1);
26670 for (const hash_it_pair &hashitpair : hashitlist)
26672 m_hash_table.push_back (0);
26673 store_unsigned_integer (reinterpret_cast<gdb_byte *>
26674 (&m_hash_table.back ()),
26675 sizeof (m_hash_table.back ()),
26676 m_dwarf5_byte_order, hashitpair.hash);
26677 const c_str_view &name = hashitpair.it->first;
26678 const std::set<symbol_value> &value_set = hashitpair.it->second;
26679 m_name_table_string_offs.push_back_reorder
26680 (m_debugstrlookup.lookup (name.c_str ()));
26681 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
26682 gdb_assert (!value_set.empty ());
26683 for (const symbol_value &value : value_set)
26685 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
26690 idx = m_idx_next++;
26691 m_abbrev_table.append_unsigned_leb128 (idx);
26692 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
26693 m_abbrev_table.append_unsigned_leb128
26694 (value.kind == unit_kind::cu ? DW_IDX_compile_unit
26695 : DW_IDX_type_unit);
26696 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
26697 m_abbrev_table.append_unsigned_leb128 (value.is_static
26698 ? DW_IDX_GNU_internal
26699 : DW_IDX_GNU_external);
26700 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
26702 /* Terminate attributes list. */
26703 m_abbrev_table.append_unsigned_leb128 (0);
26704 m_abbrev_table.append_unsigned_leb128 (0);
26707 m_entry_pool.append_unsigned_leb128 (idx);
26708 m_entry_pool.append_unsigned_leb128 (value.cu_index);
26711 /* Terminate the list of CUs. */
26712 m_entry_pool.append_unsigned_leb128 (0);
26715 gdb_assert (m_hash_table.size () == name_count);
26717 /* Terminate tags list. */
26718 m_abbrev_table.append_unsigned_leb128 (0);
26721 /* Return .debug_names bucket count. This must be called only after
26722 calling the build method. */
26723 uint32_t bucket_count () const
26725 /* Verify the build method has been already called. */
26726 gdb_assert (!m_abbrev_table.empty ());
26727 const uint32_t retval = m_bucket_table.size ();
26729 /* Check for overflow. */
26730 gdb_assert (retval == m_bucket_table.size ());
26734 /* Return .debug_names names count. This must be called only after
26735 calling the build method. */
26736 uint32_t name_count () const
26738 /* Verify the build method has been already called. */
26739 gdb_assert (!m_abbrev_table.empty ());
26740 const uint32_t retval = m_hash_table.size ();
26742 /* Check for overflow. */
26743 gdb_assert (retval == m_hash_table.size ());
26747 /* Return number of bytes of .debug_names abbreviation table. This
26748 must be called only after calling the build method. */
26749 uint32_t abbrev_table_bytes () const
26751 gdb_assert (!m_abbrev_table.empty ());
26752 return m_abbrev_table.size ();
26755 /* Recurse into all "included" dependencies and store their symbols
26756 as if they appeared in this psymtab. */
26757 void recursively_write_psymbols
26758 (struct objfile *objfile,
26759 struct partial_symtab *psymtab,
26760 std::unordered_set<partial_symbol *> &psyms_seen,
26763 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26764 if (psymtab->dependencies[i]->user != NULL)
26765 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26766 psyms_seen, cu_index);
26768 write_psymbols (psyms_seen,
26769 &objfile->global_psymbols[psymtab->globals_offset],
26770 psymtab->n_global_syms, cu_index, false, unit_kind::cu);
26771 write_psymbols (psyms_seen,
26772 &objfile->static_psymbols[psymtab->statics_offset],
26773 psymtab->n_static_syms, cu_index, true, unit_kind::cu);
26776 /* Return number of bytes the .debug_names section will have. This
26777 must be called only after calling the build method. */
26778 size_t bytes () const
26780 /* Verify the build method has been already called. */
26781 gdb_assert (!m_abbrev_table.empty ());
26782 size_t expected_bytes = 0;
26783 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
26784 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
26785 expected_bytes += m_name_table_string_offs.bytes ();
26786 expected_bytes += m_name_table_entry_offs.bytes ();
26787 expected_bytes += m_abbrev_table.size ();
26788 expected_bytes += m_entry_pool.size ();
26789 return expected_bytes;
26792 /* Write .debug_names to FILE_NAMES and .debug_str addition to
26793 FILE_STR. This must be called only after calling the build
26795 void file_write (FILE *file_names, FILE *file_str) const
26797 /* Verify the build method has been already called. */
26798 gdb_assert (!m_abbrev_table.empty ());
26799 ::file_write (file_names, m_bucket_table);
26800 ::file_write (file_names, m_hash_table);
26801 m_name_table_string_offs.file_write (file_names);
26802 m_name_table_entry_offs.file_write (file_names);
26803 m_abbrev_table.file_write (file_names);
26804 m_entry_pool.file_write (file_names);
26805 m_debugstrlookup.file_write (file_str);
26808 /* A helper user data for write_one_signatured_type. */
26809 class write_one_signatured_type_data
26812 write_one_signatured_type_data (debug_names &nametable_,
26813 signatured_type_index_data &&info_)
26814 : nametable (nametable_), info (std::move (info_))
26816 debug_names &nametable;
26817 struct signatured_type_index_data info;
26820 /* A helper function to pass write_one_signatured_type to
26821 htab_traverse_noresize. */
26823 write_one_signatured_type (void **slot, void *d)
26825 write_one_signatured_type_data *data = (write_one_signatured_type_data *) d;
26826 struct signatured_type_index_data *info = &data->info;
26827 struct signatured_type *entry = (struct signatured_type *) *slot;
26829 data->nametable.write_one_signatured_type (entry, info);
26836 /* Storage for symbol names mapping them to their .debug_str section
26838 class debug_str_lookup
26842 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
26843 All .debug_str section strings are automatically stored. */
26844 debug_str_lookup (struct dwarf2_per_objfile *dwarf2_per_objfile)
26845 : m_abfd (dwarf2_per_objfile->objfile->obfd),
26846 m_dwarf2_per_objfile (dwarf2_per_objfile)
26848 dwarf2_read_section (dwarf2_per_objfile->objfile,
26849 &dwarf2_per_objfile->str);
26850 if (dwarf2_per_objfile->str.buffer == NULL)
26852 for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
26853 data < (dwarf2_per_objfile->str.buffer
26854 + dwarf2_per_objfile->str.size);)
26856 const char *const s = reinterpret_cast<const char *> (data);
26857 const auto insertpair
26858 = m_str_table.emplace (c_str_view (s),
26859 data - dwarf2_per_objfile->str.buffer);
26860 if (!insertpair.second)
26861 complaint (&symfile_complaints,
26862 _("Duplicate string \"%s\" in "
26863 ".debug_str section [in module %s]"),
26864 s, bfd_get_filename (m_abfd));
26865 data += strlen (s) + 1;
26869 /* Return offset of symbol name S in the .debug_str section. Add
26870 such symbol to the section's end if it does not exist there
26872 size_t lookup (const char *s)
26874 const auto it = m_str_table.find (c_str_view (s));
26875 if (it != m_str_table.end ())
26877 const size_t offset = (m_dwarf2_per_objfile->str.size
26878 + m_str_add_buf.size ());
26879 m_str_table.emplace (c_str_view (s), offset);
26880 m_str_add_buf.append_cstr0 (s);
26884 /* Append the end of the .debug_str section to FILE. */
26885 void file_write (FILE *file) const
26887 m_str_add_buf.file_write (file);
26891 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
26893 struct dwarf2_per_objfile *m_dwarf2_per_objfile;
26895 /* Data to add at the end of .debug_str for new needed symbol names. */
26896 data_buf m_str_add_buf;
26899 /* Container to map used DWARF tags to their .debug_names abbreviation
26904 index_key (int dwarf_tag_, bool is_static_, unit_kind kind_)
26905 : dwarf_tag (dwarf_tag_), is_static (is_static_), kind (kind_)
26910 operator== (const index_key &other) const
26912 return (dwarf_tag == other.dwarf_tag && is_static == other.is_static
26913 && kind == other.kind);
26916 const int dwarf_tag;
26917 const bool is_static;
26918 const unit_kind kind;
26921 /* Provide std::unordered_map::hasher for index_key. */
26922 class index_key_hasher
26926 operator () (const index_key &key) const
26928 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
26932 /* Parameters of one symbol entry. */
26936 const int dwarf_tag, cu_index;
26937 const bool is_static;
26938 const unit_kind kind;
26940 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_,
26942 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_),
26947 operator< (const symbol_value &other) const
26967 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
26972 const bfd_endian dwarf5_byte_order;
26974 explicit offset_vec (bfd_endian dwarf5_byte_order_)
26975 : dwarf5_byte_order (dwarf5_byte_order_)
26978 /* Call std::vector::reserve for NELEM elements. */
26979 virtual void reserve (size_t nelem) = 0;
26981 /* Call std::vector::push_back with store_unsigned_integer byte
26982 reordering for ELEM. */
26983 virtual void push_back_reorder (size_t elem) = 0;
26985 /* Return expected output size in bytes. */
26986 virtual size_t bytes () const = 0;
26988 /* Write name table to FILE. */
26989 virtual void file_write (FILE *file) const = 0;
26992 /* Template to unify DWARF-32 and DWARF-64 output. */
26993 template<typename OffsetSize>
26994 class offset_vec_tmpl : public offset_vec
26997 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
26998 : offset_vec (dwarf5_byte_order_)
27001 /* Implement offset_vec::reserve. */
27002 void reserve (size_t nelem) override
27004 m_vec.reserve (nelem);
27007 /* Implement offset_vec::push_back_reorder. */
27008 void push_back_reorder (size_t elem) override
27010 m_vec.push_back (elem);
27011 /* Check for overflow. */
27012 gdb_assert (m_vec.back () == elem);
27013 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
27014 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
27017 /* Implement offset_vec::bytes. */
27018 size_t bytes () const override
27020 return m_vec.size () * sizeof (m_vec[0]);
27023 /* Implement offset_vec::file_write. */
27024 void file_write (FILE *file) const override
27026 ::file_write (file, m_vec);
27030 std::vector<OffsetSize> m_vec;
27033 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
27034 respecting name table width. */
27038 offset_vec &name_table_string_offs, &name_table_entry_offs;
27040 dwarf (offset_vec &name_table_string_offs_,
27041 offset_vec &name_table_entry_offs_)
27042 : name_table_string_offs (name_table_string_offs_),
27043 name_table_entry_offs (name_table_entry_offs_)
27048 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
27049 respecting name table width. */
27050 template<typename OffsetSize>
27051 class dwarf_tmpl : public dwarf
27054 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
27055 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
27056 m_name_table_string_offs (dwarf5_byte_order_),
27057 m_name_table_entry_offs (dwarf5_byte_order_)
27061 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
27062 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
27065 /* Try to reconstruct original DWARF tag for given partial_symbol.
27066 This function is not DWARF-5 compliant but it is sufficient for
27067 GDB as a DWARF-5 index consumer. */
27068 static int psymbol_tag (const struct partial_symbol *psym)
27070 domain_enum domain = PSYMBOL_DOMAIN (psym);
27071 enum address_class aclass = PSYMBOL_CLASS (psym);
27079 return DW_TAG_subprogram;
27081 return DW_TAG_typedef;
27083 case LOC_CONST_BYTES:
27084 case LOC_OPTIMIZED_OUT:
27086 return DW_TAG_variable;
27088 /* Note: It's currently impossible to recognize psyms as enum values
27089 short of reading the type info. For now punt. */
27090 return DW_TAG_variable;
27092 /* There are other LOC_FOO values that one might want to classify
27093 as variables, but dwarf2read.c doesn't currently use them. */
27094 return DW_TAG_variable;
27096 case STRUCT_DOMAIN:
27097 return DW_TAG_structure_type;
27103 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
27104 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
27105 struct partial_symbol **psymp, int count, int cu_index,
27106 bool is_static, unit_kind kind)
27108 for (; count-- > 0; ++psymp)
27110 struct partial_symbol *psym = *psymp;
27112 if (SYMBOL_LANGUAGE (psym) == language_ada)
27113 error (_("Ada is not currently supported by the index"));
27115 /* Only add a given psymbol once. */
27116 if (psyms_seen.insert (psym).second)
27117 insert (psym, cu_index, is_static, kind);
27121 /* A helper function that writes a single signatured_type
27122 to a debug_names. */
27124 write_one_signatured_type (struct signatured_type *entry,
27125 struct signatured_type_index_data *info)
27127 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
27129 write_psymbols (info->psyms_seen,
27130 &info->objfile->global_psymbols[psymtab->globals_offset],
27131 psymtab->n_global_syms, info->cu_index, false,
27133 write_psymbols (info->psyms_seen,
27134 &info->objfile->static_psymbols[psymtab->statics_offset],
27135 psymtab->n_static_syms, info->cu_index, true,
27138 info->types_list.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order,
27139 to_underlying (entry->per_cu.sect_off));
27144 /* Store value of each symbol. */
27145 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
27146 m_name_to_value_set;
27148 /* Tables of DWARF-5 .debug_names. They are in object file byte
27150 std::vector<uint32_t> m_bucket_table;
27151 std::vector<uint32_t> m_hash_table;
27153 const bfd_endian m_dwarf5_byte_order;
27154 dwarf_tmpl<uint32_t> m_dwarf32;
27155 dwarf_tmpl<uint64_t> m_dwarf64;
27157 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
27158 debug_str_lookup m_debugstrlookup;
27160 /* Map each used .debug_names abbreviation tag parameter to its
27162 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
27164 /* Next unused .debug_names abbreviation tag for
27165 m_indexkey_to_idx. */
27166 int m_idx_next = 1;
27168 /* .debug_names abbreviation table. */
27169 data_buf m_abbrev_table;
27171 /* .debug_names entry pool. */
27172 data_buf m_entry_pool;
27175 /* Return iff any of the needed offsets does not fit into 32-bit
27176 .debug_names section. */
27179 check_dwarf64_offsets (struct dwarf2_per_objfile *dwarf2_per_objfile)
27181 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27183 const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
27185 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
27188 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
27190 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
27191 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
27193 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
27199 /* The psyms_seen set is potentially going to be largish (~40k
27200 elements when indexing a -g3 build of GDB itself). Estimate the
27201 number of elements in order to avoid too many rehashes, which
27202 require rebuilding buckets and thus many trips to
27206 psyms_seen_size (struct dwarf2_per_objfile *dwarf2_per_objfile)
27208 size_t psyms_count = 0;
27209 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27211 struct dwarf2_per_cu_data *per_cu
27212 = dwarf2_per_objfile->all_comp_units[i];
27213 struct partial_symtab *psymtab = per_cu->v.psymtab;
27215 if (psymtab != NULL && psymtab->user == NULL)
27216 recursively_count_psymbols (psymtab, psyms_count);
27218 /* Generating an index for gdb itself shows a ratio of
27219 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
27220 return psyms_count / 4;
27223 /* Write new .gdb_index section for OBJFILE into OUT_FILE.
27224 Return how many bytes were expected to be written into OUT_FILE. */
27227 write_gdbindex (struct dwarf2_per_objfile *dwarf2_per_objfile, FILE *out_file)
27229 struct objfile *objfile = dwarf2_per_objfile->objfile;
27230 mapped_symtab symtab;
27233 /* While we're scanning CU's create a table that maps a psymtab pointer
27234 (which is what addrmap records) to its index (which is what is recorded
27235 in the index file). This will later be needed to write the address
27237 psym_index_map cu_index_htab;
27238 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
27240 /* The CU list is already sorted, so we don't need to do additional
27241 work here. Also, the debug_types entries do not appear in
27242 all_comp_units, but only in their own hash table. */
27244 std::unordered_set<partial_symbol *> psyms_seen
27245 (psyms_seen_size (dwarf2_per_objfile));
27246 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27248 struct dwarf2_per_cu_data *per_cu
27249 = dwarf2_per_objfile->all_comp_units[i];
27250 struct partial_symtab *psymtab = per_cu->v.psymtab;
27252 /* CU of a shared file from 'dwz -m' may be unused by this main file.
27253 It may be referenced from a local scope but in such case it does not
27254 need to be present in .gdb_index. */
27255 if (psymtab == NULL)
27258 if (psymtab->user == NULL)
27259 recursively_write_psymbols (objfile, psymtab, &symtab,
27262 const auto insertpair = cu_index_htab.emplace (psymtab, i);
27263 gdb_assert (insertpair.second);
27265 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
27266 to_underlying (per_cu->sect_off));
27267 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
27270 /* Dump the address map. */
27272 write_address_map (objfile, addr_vec, cu_index_htab);
27274 /* Write out the .debug_type entries, if any. */
27275 data_buf types_cu_list;
27276 if (dwarf2_per_objfile->signatured_types)
27278 signatured_type_index_data sig_data (types_cu_list,
27281 sig_data.objfile = objfile;
27282 sig_data.symtab = &symtab;
27283 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
27284 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
27285 write_one_signatured_type, &sig_data);
27288 /* Now that we've processed all symbols we can shrink their cu_indices
27290 uniquify_cu_indices (&symtab);
27292 data_buf symtab_vec, constant_pool;
27293 write_hash_table (&symtab, symtab_vec, constant_pool);
27296 const offset_type size_of_contents = 6 * sizeof (offset_type);
27297 offset_type total_len = size_of_contents;
27299 /* The version number. */
27300 contents.append_data (MAYBE_SWAP (8));
27302 /* The offset of the CU list from the start of the file. */
27303 contents.append_data (MAYBE_SWAP (total_len));
27304 total_len += cu_list.size ();
27306 /* The offset of the types CU list from the start of the file. */
27307 contents.append_data (MAYBE_SWAP (total_len));
27308 total_len += types_cu_list.size ();
27310 /* The offset of the address table from the start of the file. */
27311 contents.append_data (MAYBE_SWAP (total_len));
27312 total_len += addr_vec.size ();
27314 /* The offset of the symbol table from the start of the file. */
27315 contents.append_data (MAYBE_SWAP (total_len));
27316 total_len += symtab_vec.size ();
27318 /* The offset of the constant pool from the start of the file. */
27319 contents.append_data (MAYBE_SWAP (total_len));
27320 total_len += constant_pool.size ();
27322 gdb_assert (contents.size () == size_of_contents);
27324 contents.file_write (out_file);
27325 cu_list.file_write (out_file);
27326 types_cu_list.file_write (out_file);
27327 addr_vec.file_write (out_file);
27328 symtab_vec.file_write (out_file);
27329 constant_pool.file_write (out_file);
27334 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
27335 static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
27337 /* Write a new .debug_names section for OBJFILE into OUT_FILE, write
27338 needed addition to .debug_str section to OUT_FILE_STR. Return how
27339 many bytes were expected to be written into OUT_FILE. */
27342 write_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
27343 FILE *out_file, FILE *out_file_str)
27345 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets (dwarf2_per_objfile);
27346 struct objfile *objfile = dwarf2_per_objfile->objfile;
27347 const enum bfd_endian dwarf5_byte_order
27348 = gdbarch_byte_order (get_objfile_arch (objfile));
27350 /* The CU list is already sorted, so we don't need to do additional
27351 work here. Also, the debug_types entries do not appear in
27352 all_comp_units, but only in their own hash table. */
27354 debug_names nametable (dwarf2_per_objfile, dwarf5_is_dwarf64,
27355 dwarf5_byte_order);
27356 std::unordered_set<partial_symbol *>
27357 psyms_seen (psyms_seen_size (dwarf2_per_objfile));
27358 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27360 const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
27361 partial_symtab *psymtab = per_cu->v.psymtab;
27363 /* CU of a shared file from 'dwz -m' may be unused by this main
27364 file. It may be referenced from a local scope but in such
27365 case it does not need to be present in .debug_names. */
27366 if (psymtab == NULL)
27369 if (psymtab->user == NULL)
27370 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
27372 cu_list.append_uint (nametable.dwarf5_offset_size (), dwarf5_byte_order,
27373 to_underlying (per_cu->sect_off));
27376 /* Write out the .debug_type entries, if any. */
27377 data_buf types_cu_list;
27378 if (dwarf2_per_objfile->signatured_types)
27380 debug_names::write_one_signatured_type_data sig_data (nametable,
27381 signatured_type_index_data (types_cu_list, psyms_seen));
27383 sig_data.info.objfile = objfile;
27384 /* It is used only for gdb_index. */
27385 sig_data.info.symtab = nullptr;
27386 sig_data.info.cu_index = 0;
27387 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
27388 debug_names::write_one_signatured_type,
27392 nametable.build ();
27394 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
27396 const offset_type bytes_of_header
27397 = ((dwarf5_is_dwarf64 ? 12 : 4)
27399 + sizeof (dwarf5_gdb_augmentation));
27400 size_t expected_bytes = 0;
27401 expected_bytes += bytes_of_header;
27402 expected_bytes += cu_list.size ();
27403 expected_bytes += types_cu_list.size ();
27404 expected_bytes += nametable.bytes ();
27407 if (!dwarf5_is_dwarf64)
27409 const uint64_t size64 = expected_bytes - 4;
27410 gdb_assert (size64 < 0xfffffff0);
27411 header.append_uint (4, dwarf5_byte_order, size64);
27415 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
27416 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
27419 /* The version number. */
27420 header.append_uint (2, dwarf5_byte_order, 5);
27423 header.append_uint (2, dwarf5_byte_order, 0);
27425 /* comp_unit_count - The number of CUs in the CU list. */
27426 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
27428 /* local_type_unit_count - The number of TUs in the local TU
27430 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
27432 /* foreign_type_unit_count - The number of TUs in the foreign TU
27434 header.append_uint (4, dwarf5_byte_order, 0);
27436 /* bucket_count - The number of hash buckets in the hash lookup
27438 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
27440 /* name_count - The number of unique names in the index. */
27441 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
27443 /* abbrev_table_size - The size in bytes of the abbreviations
27445 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
27447 /* augmentation_string_size - The size in bytes of the augmentation
27448 string. This value is rounded up to a multiple of 4. */
27449 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
27450 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
27451 header.append_data (dwarf5_gdb_augmentation);
27453 gdb_assert (header.size () == bytes_of_header);
27455 header.file_write (out_file);
27456 cu_list.file_write (out_file);
27457 types_cu_list.file_write (out_file);
27458 nametable.file_write (out_file, out_file_str);
27460 return expected_bytes;
27463 /* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
27464 position is at the end of the file. */
27467 assert_file_size (FILE *file, const char *filename, size_t expected_size)
27469 const auto file_size = ftell (file);
27470 if (file_size == -1)
27471 error (_("Can't get `%s' size"), filename);
27472 gdb_assert (file_size == expected_size);
27475 /* Create an index file for OBJFILE in the directory DIR. */
27478 write_psymtabs_to_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
27480 dw_index_kind index_kind)
27482 struct objfile *objfile = dwarf2_per_objfile->objfile;
27484 if (dwarf2_per_objfile->using_index)
27485 error (_("Cannot use an index to create the index"));
27487 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
27488 error (_("Cannot make an index when the file has multiple .debug_types sections"));
27490 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
27494 if (stat (objfile_name (objfile), &st) < 0)
27495 perror_with_name (objfile_name (objfile));
27497 std::string filename (std::string (dir) + SLASH_STRING
27498 + lbasename (objfile_name (objfile))
27499 + (index_kind == dw_index_kind::DEBUG_NAMES
27500 ? INDEX5_SUFFIX : INDEX4_SUFFIX));
27502 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
27504 error (_("Can't open `%s' for writing"), filename.c_str ());
27506 /* Order matters here; we want FILE to be closed before FILENAME is
27507 unlinked, because on MS-Windows one cannot delete a file that is
27508 still open. (Don't call anything here that might throw until
27509 file_closer is created.) */
27510 gdb::unlinker unlink_file (filename.c_str ());
27511 gdb_file_up close_out_file (out_file);
27513 if (index_kind == dw_index_kind::DEBUG_NAMES)
27515 std::string filename_str (std::string (dir) + SLASH_STRING
27516 + lbasename (objfile_name (objfile))
27517 + DEBUG_STR_SUFFIX);
27519 = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
27521 error (_("Can't open `%s' for writing"), filename_str.c_str ());
27522 gdb::unlinker unlink_file_str (filename_str.c_str ());
27523 gdb_file_up close_out_file_str (out_file_str);
27525 const size_t total_len
27526 = write_debug_names (dwarf2_per_objfile, out_file, out_file_str);
27527 assert_file_size (out_file, filename.c_str (), total_len);
27529 /* We want to keep the file .debug_str file too. */
27530 unlink_file_str.keep ();
27534 const size_t total_len
27535 = write_gdbindex (dwarf2_per_objfile, out_file);
27536 assert_file_size (out_file, filename.c_str (), total_len);
27539 /* We want to keep the file. */
27540 unlink_file.keep ();
27543 /* Implementation of the `save gdb-index' command.
27545 Note that the .gdb_index file format used by this command is
27546 documented in the GDB manual. Any changes here must be documented
27550 save_gdb_index_command (const char *arg, int from_tty)
27552 struct objfile *objfile;
27553 const char dwarf5space[] = "-dwarf-5 ";
27554 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
27559 arg = skip_spaces (arg);
27560 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
27562 index_kind = dw_index_kind::DEBUG_NAMES;
27563 arg += strlen (dwarf5space);
27564 arg = skip_spaces (arg);
27568 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
27570 ALL_OBJFILES (objfile)
27574 /* If the objfile does not correspond to an actual file, skip it. */
27575 if (stat (objfile_name (objfile), &st) < 0)
27578 struct dwarf2_per_objfile *dwarf2_per_objfile
27579 = get_dwarf2_per_objfile (objfile);
27581 if (dwarf2_per_objfile != NULL)
27585 write_psymtabs_to_index (dwarf2_per_objfile, arg, index_kind);
27587 CATCH (except, RETURN_MASK_ERROR)
27589 exception_fprintf (gdb_stderr, except,
27590 _("Error while writing index for `%s': "),
27591 objfile_name (objfile));
27601 int dwarf_always_disassemble;
27604 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
27605 struct cmd_list_element *c, const char *value)
27607 fprintf_filtered (file,
27608 _("Whether to always disassemble "
27609 "DWARF expressions is %s.\n"),
27614 show_check_physname (struct ui_file *file, int from_tty,
27615 struct cmd_list_element *c, const char *value)
27617 fprintf_filtered (file,
27618 _("Whether to check \"physname\" is %s.\n"),
27623 _initialize_dwarf2_read (void)
27625 struct cmd_list_element *c;
27627 dwarf2_objfile_data_key = register_objfile_data ();
27629 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
27630 Set DWARF specific variables.\n\
27631 Configure DWARF variables such as the cache size"),
27632 &set_dwarf_cmdlist, "maintenance set dwarf ",
27633 0/*allow-unknown*/, &maintenance_set_cmdlist);
27635 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
27636 Show DWARF specific variables\n\
27637 Show DWARF variables such as the cache size"),
27638 &show_dwarf_cmdlist, "maintenance show dwarf ",
27639 0/*allow-unknown*/, &maintenance_show_cmdlist);
27641 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
27642 &dwarf_max_cache_age, _("\
27643 Set the upper bound on the age of cached DWARF compilation units."), _("\
27644 Show the upper bound on the age of cached DWARF compilation units."), _("\
27645 A higher limit means that cached compilation units will be stored\n\
27646 in memory longer, and more total memory will be used. Zero disables\n\
27647 caching, which can slow down startup."),
27649 show_dwarf_max_cache_age,
27650 &set_dwarf_cmdlist,
27651 &show_dwarf_cmdlist);
27653 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
27654 &dwarf_always_disassemble, _("\
27655 Set whether `info address' always disassembles DWARF expressions."), _("\
27656 Show whether `info address' always disassembles DWARF expressions."), _("\
27657 When enabled, DWARF expressions are always printed in an assembly-like\n\
27658 syntax. When disabled, expressions will be printed in a more\n\
27659 conversational style, when possible."),
27661 show_dwarf_always_disassemble,
27662 &set_dwarf_cmdlist,
27663 &show_dwarf_cmdlist);
27665 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
27666 Set debugging of the DWARF reader."), _("\
27667 Show debugging of the DWARF reader."), _("\
27668 When enabled (non-zero), debugging messages are printed during DWARF\n\
27669 reading and symtab expansion. A value of 1 (one) provides basic\n\
27670 information. A value greater than 1 provides more verbose information."),
27673 &setdebuglist, &showdebuglist);
27675 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
27676 Set debugging of the DWARF DIE reader."), _("\
27677 Show debugging of the DWARF DIE reader."), _("\
27678 When enabled (non-zero), DIEs are dumped after they are read in.\n\
27679 The value is the maximum depth to print."),
27682 &setdebuglist, &showdebuglist);
27684 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
27685 Set debugging of the dwarf line reader."), _("\
27686 Show debugging of the dwarf line reader."), _("\
27687 When enabled (non-zero), line number entries are dumped as they are read in.\n\
27688 A value of 1 (one) provides basic information.\n\
27689 A value greater than 1 provides more verbose information."),
27692 &setdebuglist, &showdebuglist);
27694 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
27695 Set cross-checking of \"physname\" code against demangler."), _("\
27696 Show cross-checking of \"physname\" code against demangler."), _("\
27697 When enabled, GDB's internal \"physname\" code is checked against\n\
27699 NULL, show_check_physname,
27700 &setdebuglist, &showdebuglist);
27702 add_setshow_boolean_cmd ("use-deprecated-index-sections",
27703 no_class, &use_deprecated_index_sections, _("\
27704 Set whether to use deprecated gdb_index sections."), _("\
27705 Show whether to use deprecated gdb_index sections."), _("\
27706 When enabled, deprecated .gdb_index sections are used anyway.\n\
27707 Normally they are ignored either because of a missing feature or\n\
27708 performance issue.\n\
27709 Warning: This option must be enabled before gdb reads the file."),
27712 &setlist, &showlist);
27714 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
27716 Save a gdb-index file.\n\
27717 Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
27719 No options create one file with .gdb-index extension for pre-DWARF-5\n\
27720 compatible .gdb_index section. With -dwarf-5 creates two files with\n\
27721 extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
27723 set_cmd_completer (c, filename_completer);
27725 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
27726 &dwarf2_locexpr_funcs);
27727 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
27728 &dwarf2_loclist_funcs);
27730 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
27731 &dwarf2_block_frame_base_locexpr_funcs);
27732 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
27733 &dwarf2_block_frame_base_loclist_funcs);
27736 selftests::register_test ("dw2_expand_symtabs_matching",
27737 selftests::dw2_expand_symtabs_matching::run_test);