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>
90 typedef struct symbol *symbolp;
93 /* When == 1, print basic high level tracing messages.
94 When > 1, be more verbose.
95 This is in contrast to the low level DIE reading of dwarf_die_debug. */
96 static unsigned int dwarf_read_debug = 0;
98 /* When non-zero, dump DIEs after they are read in. */
99 static unsigned int dwarf_die_debug = 0;
101 /* When non-zero, dump line number entries as they are read in. */
102 static unsigned int dwarf_line_debug = 0;
104 /* When non-zero, cross-check physname against demangler. */
105 static int check_physname = 0;
107 /* When non-zero, do not reject deprecated .gdb_index sections. */
108 static int use_deprecated_index_sections = 0;
110 static const struct objfile_data *dwarf2_objfile_data_key;
112 /* The "aclass" indices for various kinds of computed DWARF symbols. */
114 static int dwarf2_locexpr_index;
115 static int dwarf2_loclist_index;
116 static int dwarf2_locexpr_block_index;
117 static int dwarf2_loclist_block_index;
119 /* A descriptor for dwarf sections.
121 S.ASECTION, SIZE are typically initialized when the objfile is first
122 scanned. BUFFER, READIN are filled in later when the section is read.
123 If the section contained compressed data then SIZE is updated to record
124 the uncompressed size of the section.
126 DWP file format V2 introduces a wrinkle that is easiest to handle by
127 creating the concept of virtual sections contained within a real section.
128 In DWP V2 the sections of the input DWO files are concatenated together
129 into one section, but section offsets are kept relative to the original
131 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
132 the real section this "virtual" section is contained in, and BUFFER,SIZE
133 describe the virtual section. */
135 struct dwarf2_section_info
139 /* If this is a real section, the bfd section. */
141 /* If this is a virtual section, pointer to the containing ("real")
143 struct dwarf2_section_info *containing_section;
145 /* Pointer to section data, only valid if readin. */
146 const gdb_byte *buffer;
147 /* The size of the section, real or virtual. */
149 /* If this is a virtual section, the offset in the real section.
150 Only valid if is_virtual. */
151 bfd_size_type virtual_offset;
152 /* True if we have tried to read this section. */
154 /* True if this is a virtual section, False otherwise.
155 This specifies which of s.section and s.containing_section to use. */
159 typedef struct dwarf2_section_info dwarf2_section_info_def;
160 DEF_VEC_O (dwarf2_section_info_def);
162 /* All offsets in the index are of this type. It must be
163 architecture-independent. */
164 typedef uint32_t offset_type;
166 DEF_VEC_I (offset_type);
168 /* Ensure only legit values are used. */
169 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
171 gdb_assert ((unsigned int) (value) <= 1); \
172 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
175 /* Ensure only legit values are used. */
176 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
178 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
179 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
180 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
183 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
184 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
186 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
187 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
192 /* Convert VALUE between big- and little-endian. */
195 byte_swap (offset_type value)
199 result = (value & 0xff) << 24;
200 result |= (value & 0xff00) << 8;
201 result |= (value & 0xff0000) >> 8;
202 result |= (value & 0xff000000) >> 24;
206 #define MAYBE_SWAP(V) byte_swap (V)
209 #define MAYBE_SWAP(V) static_cast<offset_type> (V)
210 #endif /* WORDS_BIGENDIAN */
212 /* An index into a (C++) symbol name component in a symbol name as
213 recorded in the mapped_index's symbol table. For each C++ symbol
214 in the symbol table, we record one entry for the start of each
215 component in the symbol in a table of name components, and then
216 sort the table, in order to be able to binary search symbol names,
217 ignoring leading namespaces, both completion and regular look up.
218 For example, for symbol "A::B::C", we'll have an entry that points
219 to "A::B::C", another that points to "B::C", and another for "C".
220 Note that function symbols in GDB index have no parameter
221 information, just the function/method names. You can convert a
222 name_component to a "const char *" using the
223 'mapped_index::symbol_name_at(offset_type)' method. */
225 struct name_component
227 /* Offset in the symbol name where the component starts. Stored as
228 a (32-bit) offset instead of a pointer to save memory and improve
229 locality on 64-bit architectures. */
230 offset_type name_offset;
232 /* The symbol's index in the symbol and constant pool tables of a
237 /* Base class containing bits shared by both .gdb_index and
238 .debug_name indexes. */
240 struct mapped_index_base
242 /* The name_component table (a sorted vector). See name_component's
243 description above. */
244 std::vector<name_component> name_components;
246 /* How NAME_COMPONENTS is sorted. */
247 enum case_sensitivity name_components_casing;
249 /* Return the number of names in the symbol table. */
250 virtual size_t symbol_name_count () const = 0;
252 /* Get the name of the symbol at IDX in the symbol table. */
253 virtual const char *symbol_name_at (offset_type idx) const = 0;
255 /* Return whether the name at IDX in the symbol table should be
257 virtual bool symbol_name_slot_invalid (offset_type idx) const
262 /* Build the symbol name component sorted vector, if we haven't
264 void build_name_components ();
266 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
267 possible matches for LN_NO_PARAMS in the name component
269 std::pair<std::vector<name_component>::const_iterator,
270 std::vector<name_component>::const_iterator>
271 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
273 /* Prevent deleting/destroying via a base class pointer. */
275 ~mapped_index_base() = default;
278 /* A description of the mapped index. The file format is described in
279 a comment by the code that writes the index. */
280 struct mapped_index final : public mapped_index_base
282 /* A slot/bucket in the symbol table hash. */
283 struct symbol_table_slot
285 const offset_type name;
286 const offset_type vec;
289 /* Index data format version. */
292 /* The total length of the buffer. */
295 /* The address table data. */
296 gdb::array_view<const gdb_byte> address_table;
298 /* The symbol table, implemented as a hash table. */
299 gdb::array_view<symbol_table_slot> symbol_table;
301 /* A pointer to the constant pool. */
302 const char *constant_pool;
304 bool symbol_name_slot_invalid (offset_type idx) const override
306 const auto &bucket = this->symbol_table[idx];
307 return bucket.name == 0 && bucket.vec;
310 /* Convenience method to get at the name of the symbol at IDX in the
312 const char *symbol_name_at (offset_type idx) const override
313 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
315 size_t symbol_name_count () const override
316 { return this->symbol_table.size (); }
319 /* A description of the mapped .debug_names.
320 Uninitialized map has CU_COUNT 0. */
321 struct mapped_debug_names final : public mapped_index_base
323 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
324 : dwarf2_per_objfile (dwarf2_per_objfile_)
327 struct dwarf2_per_objfile *dwarf2_per_objfile;
328 bfd_endian dwarf5_byte_order;
329 bool dwarf5_is_dwarf64;
330 bool augmentation_is_gdb;
332 uint32_t cu_count = 0;
333 uint32_t tu_count, bucket_count, name_count;
334 const gdb_byte *cu_table_reordered, *tu_table_reordered;
335 const uint32_t *bucket_table_reordered, *hash_table_reordered;
336 const gdb_byte *name_table_string_offs_reordered;
337 const gdb_byte *name_table_entry_offs_reordered;
338 const gdb_byte *entry_pool;
345 /* Attribute name DW_IDX_*. */
348 /* Attribute form DW_FORM_*. */
351 /* Value if FORM is DW_FORM_implicit_const. */
352 LONGEST implicit_const;
354 std::vector<attr> attr_vec;
357 std::unordered_map<ULONGEST, index_val> abbrev_map;
359 const char *namei_to_name (uint32_t namei) const;
361 /* Implementation of the mapped_index_base virtual interface, for
362 the name_components cache. */
364 const char *symbol_name_at (offset_type idx) const override
365 { return namei_to_name (idx); }
367 size_t symbol_name_count () const override
368 { return this->name_count; }
371 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
372 DEF_VEC_P (dwarf2_per_cu_ptr);
376 int nr_uniq_abbrev_tables;
378 int nr_symtab_sharers;
379 int nr_stmt_less_type_units;
380 int nr_all_type_units_reallocs;
383 /* Collection of data recorded per objfile.
384 This hangs off of dwarf2_objfile_data_key. */
386 struct dwarf2_per_objfile
388 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
389 dwarf2 section names, or is NULL if the standard ELF names are
391 dwarf2_per_objfile (struct objfile *objfile,
392 const dwarf2_debug_sections *names);
394 ~dwarf2_per_objfile ();
396 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
398 /* Free all cached compilation units. */
399 void free_cached_comp_units ();
401 /* This function is mapped across the sections and remembers the
402 offset and size of each of the debugging sections we are
404 void locate_sections (bfd *abfd, asection *sectp,
405 const dwarf2_debug_sections &names);
408 dwarf2_section_info info {};
409 dwarf2_section_info abbrev {};
410 dwarf2_section_info line {};
411 dwarf2_section_info loc {};
412 dwarf2_section_info loclists {};
413 dwarf2_section_info macinfo {};
414 dwarf2_section_info macro {};
415 dwarf2_section_info str {};
416 dwarf2_section_info line_str {};
417 dwarf2_section_info ranges {};
418 dwarf2_section_info rnglists {};
419 dwarf2_section_info addr {};
420 dwarf2_section_info frame {};
421 dwarf2_section_info eh_frame {};
422 dwarf2_section_info gdb_index {};
423 dwarf2_section_info debug_names {};
424 dwarf2_section_info debug_aranges {};
426 VEC (dwarf2_section_info_def) *types = NULL;
429 struct objfile *objfile = NULL;
431 /* Table of all the compilation units. This is used to locate
432 the target compilation unit of a particular reference. */
433 struct dwarf2_per_cu_data **all_comp_units = NULL;
435 /* The number of compilation units in ALL_COMP_UNITS. */
436 int n_comp_units = 0;
438 /* The number of .debug_types-related CUs. */
439 int n_type_units = 0;
441 /* The number of elements allocated in all_type_units.
442 If there are skeleton-less TUs, we add them to all_type_units lazily. */
443 int n_allocated_type_units = 0;
445 /* The .debug_types-related CUs (TUs).
446 This is stored in malloc space because we may realloc it. */
447 struct signatured_type **all_type_units = NULL;
449 /* Table of struct type_unit_group objects.
450 The hash key is the DW_AT_stmt_list value. */
451 htab_t type_unit_groups {};
453 /* A table mapping .debug_types signatures to its signatured_type entry.
454 This is NULL if the .debug_types section hasn't been read in yet. */
455 htab_t signatured_types {};
457 /* Type unit statistics, to see how well the scaling improvements
459 struct tu_stats tu_stats {};
461 /* A chain of compilation units that are currently read in, so that
462 they can be freed later. */
463 dwarf2_per_cu_data *read_in_chain = NULL;
465 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
466 This is NULL if the table hasn't been allocated yet. */
469 /* True if we've checked for whether there is a DWP file. */
470 bool dwp_checked = false;
472 /* The DWP file if there is one, or NULL. */
473 struct dwp_file *dwp_file = NULL;
475 /* The shared '.dwz' file, if one exists. This is used when the
476 original data was compressed using 'dwz -m'. */
477 struct dwz_file *dwz_file = NULL;
479 /* A flag indicating whether this objfile has a section loaded at a
481 bool has_section_at_zero = false;
483 /* True if we are using the mapped index,
484 or we are faking it for OBJF_READNOW's sake. */
485 bool using_index = false;
487 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
488 mapped_index *index_table = NULL;
490 /* The mapped index, or NULL if .debug_names is missing or not being used. */
491 std::unique_ptr<mapped_debug_names> debug_names_table;
493 /* When using index_table, this keeps track of all quick_file_names entries.
494 TUs typically share line table entries with a CU, so we maintain a
495 separate table of all line table entries to support the sharing.
496 Note that while there can be way more TUs than CUs, we've already
497 sorted all the TUs into "type unit groups", grouped by their
498 DW_AT_stmt_list value. Therefore the only sharing done here is with a
499 CU and its associated TU group if there is one. */
500 htab_t quick_file_names_table {};
502 /* Set during partial symbol reading, to prevent queueing of full
504 bool reading_partial_symbols = false;
506 /* Table mapping type DIEs to their struct type *.
507 This is NULL if not allocated yet.
508 The mapping is done via (CU/TU + DIE offset) -> type. */
509 htab_t die_type_hash {};
511 /* The CUs we recently read. */
512 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
514 /* Table containing line_header indexed by offset and offset_in_dwz. */
515 htab_t line_header_hash {};
517 /* Table containing all filenames. This is an optional because the
518 table is lazily constructed on first access. */
519 gdb::optional<filename_seen_cache> filenames_cache;
522 /* Get the dwarf2_per_objfile associated to OBJFILE. */
524 struct dwarf2_per_objfile *
525 get_dwarf2_per_objfile (struct objfile *objfile)
527 return ((struct dwarf2_per_objfile *)
528 objfile_data (objfile, dwarf2_objfile_data_key));
531 /* Set the dwarf2_per_objfile associated to OBJFILE. */
534 set_dwarf2_per_objfile (struct objfile *objfile,
535 struct dwarf2_per_objfile *dwarf2_per_objfile)
537 gdb_assert (get_dwarf2_per_objfile (objfile) == NULL);
538 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
541 /* Default names of the debugging sections. */
543 /* Note that if the debugging section has been compressed, it might
544 have a name like .zdebug_info. */
546 static const struct dwarf2_debug_sections dwarf2_elf_names =
548 { ".debug_info", ".zdebug_info" },
549 { ".debug_abbrev", ".zdebug_abbrev" },
550 { ".debug_line", ".zdebug_line" },
551 { ".debug_loc", ".zdebug_loc" },
552 { ".debug_loclists", ".zdebug_loclists" },
553 { ".debug_macinfo", ".zdebug_macinfo" },
554 { ".debug_macro", ".zdebug_macro" },
555 { ".debug_str", ".zdebug_str" },
556 { ".debug_line_str", ".zdebug_line_str" },
557 { ".debug_ranges", ".zdebug_ranges" },
558 { ".debug_rnglists", ".zdebug_rnglists" },
559 { ".debug_types", ".zdebug_types" },
560 { ".debug_addr", ".zdebug_addr" },
561 { ".debug_frame", ".zdebug_frame" },
562 { ".eh_frame", NULL },
563 { ".gdb_index", ".zgdb_index" },
564 { ".debug_names", ".zdebug_names" },
565 { ".debug_aranges", ".zdebug_aranges" },
569 /* List of DWO/DWP sections. */
571 static const struct dwop_section_names
573 struct dwarf2_section_names abbrev_dwo;
574 struct dwarf2_section_names info_dwo;
575 struct dwarf2_section_names line_dwo;
576 struct dwarf2_section_names loc_dwo;
577 struct dwarf2_section_names loclists_dwo;
578 struct dwarf2_section_names macinfo_dwo;
579 struct dwarf2_section_names macro_dwo;
580 struct dwarf2_section_names str_dwo;
581 struct dwarf2_section_names str_offsets_dwo;
582 struct dwarf2_section_names types_dwo;
583 struct dwarf2_section_names cu_index;
584 struct dwarf2_section_names tu_index;
588 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
589 { ".debug_info.dwo", ".zdebug_info.dwo" },
590 { ".debug_line.dwo", ".zdebug_line.dwo" },
591 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
592 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
593 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
594 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
595 { ".debug_str.dwo", ".zdebug_str.dwo" },
596 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
597 { ".debug_types.dwo", ".zdebug_types.dwo" },
598 { ".debug_cu_index", ".zdebug_cu_index" },
599 { ".debug_tu_index", ".zdebug_tu_index" },
602 /* local data types */
604 /* The data in a compilation unit header, after target2host
605 translation, looks like this. */
606 struct comp_unit_head
610 unsigned char addr_size;
611 unsigned char signed_addr_p;
612 sect_offset abbrev_sect_off;
614 /* Size of file offsets; either 4 or 8. */
615 unsigned int offset_size;
617 /* Size of the length field; either 4 or 12. */
618 unsigned int initial_length_size;
620 enum dwarf_unit_type unit_type;
622 /* Offset to the first byte of this compilation unit header in the
623 .debug_info section, for resolving relative reference dies. */
624 sect_offset sect_off;
626 /* Offset to first die in this cu from the start of the cu.
627 This will be the first byte following the compilation unit header. */
628 cu_offset first_die_cu_offset;
630 /* 64-bit signature of this type unit - it is valid only for
631 UNIT_TYPE DW_UT_type. */
634 /* For types, offset in the type's DIE of the type defined by this TU. */
635 cu_offset type_cu_offset_in_tu;
638 /* Type used for delaying computation of method physnames.
639 See comments for compute_delayed_physnames. */
640 struct delayed_method_info
642 /* The type to which the method is attached, i.e., its parent class. */
645 /* The index of the method in the type's function fieldlists. */
648 /* The index of the method in the fieldlist. */
651 /* The name of the DIE. */
654 /* The DIE associated with this method. */
655 struct die_info *die;
658 /* Internal state when decoding a particular compilation unit. */
661 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
664 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
666 /* The header of the compilation unit. */
667 struct comp_unit_head header {};
669 /* Base address of this compilation unit. */
670 CORE_ADDR base_address = 0;
672 /* Non-zero if base_address has been set. */
675 /* The language we are debugging. */
676 enum language language = language_unknown;
677 const struct language_defn *language_defn = nullptr;
679 const char *producer = nullptr;
681 /* The generic symbol table building routines have separate lists for
682 file scope symbols and all all other scopes (local scopes). So
683 we need to select the right one to pass to add_symbol_to_list().
684 We do it by keeping a pointer to the correct list in list_in_scope.
686 FIXME: The original dwarf code just treated the file scope as the
687 first local scope, and all other local scopes as nested local
688 scopes, and worked fine. Check to see if we really need to
689 distinguish these in buildsym.c. */
690 struct pending **list_in_scope = nullptr;
692 /* Hash table holding all the loaded partial DIEs
693 with partial_die->offset.SECT_OFF as hash. */
694 htab_t partial_dies = nullptr;
696 /* Storage for things with the same lifetime as this read-in compilation
697 unit, including partial DIEs. */
698 auto_obstack comp_unit_obstack;
700 /* When multiple dwarf2_cu structures are living in memory, this field
701 chains them all together, so that they can be released efficiently.
702 We will probably also want a generation counter so that most-recently-used
703 compilation units are cached... */
704 struct dwarf2_per_cu_data *read_in_chain = nullptr;
706 /* Backlink to our per_cu entry. */
707 struct dwarf2_per_cu_data *per_cu;
709 /* How many compilation units ago was this CU last referenced? */
712 /* A hash table of DIE cu_offset for following references with
713 die_info->offset.sect_off as hash. */
714 htab_t die_hash = nullptr;
716 /* Full DIEs if read in. */
717 struct die_info *dies = nullptr;
719 /* A set of pointers to dwarf2_per_cu_data objects for compilation
720 units referenced by this one. Only set during full symbol processing;
721 partial symbol tables do not have dependencies. */
722 htab_t dependencies = nullptr;
724 /* Header data from the line table, during full symbol processing. */
725 struct line_header *line_header = nullptr;
726 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
727 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
728 this is the DW_TAG_compile_unit die for this CU. We'll hold on
729 to the line header as long as this DIE is being processed. See
730 process_die_scope. */
731 die_info *line_header_die_owner = nullptr;
733 /* A list of methods which need to have physnames computed
734 after all type information has been read. */
735 std::vector<delayed_method_info> method_list;
737 /* To be copied to symtab->call_site_htab. */
738 htab_t call_site_htab = nullptr;
740 /* Non-NULL if this CU came from a DWO file.
741 There is an invariant here that is important to remember:
742 Except for attributes copied from the top level DIE in the "main"
743 (or "stub") file in preparation for reading the DWO file
744 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
745 Either there isn't a DWO file (in which case this is NULL and the point
746 is moot), or there is and either we're not going to read it (in which
747 case this is NULL) or there is and we are reading it (in which case this
749 struct dwo_unit *dwo_unit = nullptr;
751 /* The DW_AT_addr_base attribute if present, zero otherwise
752 (zero is a valid value though).
753 Note this value comes from the Fission stub CU/TU's DIE. */
754 ULONGEST addr_base = 0;
756 /* The DW_AT_ranges_base attribute if present, zero otherwise
757 (zero is a valid value though).
758 Note this value comes from the Fission stub CU/TU's DIE.
759 Also note that the value is zero in the non-DWO case so this value can
760 be used without needing to know whether DWO files are in use or not.
761 N.B. This does not apply to DW_AT_ranges appearing in
762 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
763 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
764 DW_AT_ranges_base *would* have to be applied, and we'd have to care
765 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
766 ULONGEST ranges_base = 0;
768 /* Mark used when releasing cached dies. */
769 unsigned int mark : 1;
771 /* This CU references .debug_loc. See the symtab->locations_valid field.
772 This test is imperfect as there may exist optimized debug code not using
773 any location list and still facing inlining issues if handled as
774 unoptimized code. For a future better test see GCC PR other/32998. */
775 unsigned int has_loclist : 1;
777 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
778 if all the producer_is_* fields are valid. This information is cached
779 because profiling CU expansion showed excessive time spent in
780 producer_is_gxx_lt_4_6. */
781 unsigned int checked_producer : 1;
782 unsigned int producer_is_gxx_lt_4_6 : 1;
783 unsigned int producer_is_gcc_lt_4_3 : 1;
784 unsigned int producer_is_icc_lt_14 : 1;
786 /* When set, the file that we're processing is known to have
787 debugging info for C++ namespaces. GCC 3.3.x did not produce
788 this information, but later versions do. */
790 unsigned int processing_has_namespace_info : 1;
793 /* Persistent data held for a compilation unit, even when not
794 processing it. We put a pointer to this structure in the
795 read_symtab_private field of the psymtab. */
797 struct dwarf2_per_cu_data
799 /* The start offset and length of this compilation unit.
800 NOTE: Unlike comp_unit_head.length, this length includes
802 If the DIE refers to a DWO file, this is always of the original die,
804 sect_offset sect_off;
807 /* DWARF standard version this data has been read from (such as 4 or 5). */
810 /* Flag indicating this compilation unit will be read in before
811 any of the current compilation units are processed. */
812 unsigned int queued : 1;
814 /* This flag will be set when reading partial DIEs if we need to load
815 absolutely all DIEs for this compilation unit, instead of just the ones
816 we think are interesting. It gets set if we look for a DIE in the
817 hash table and don't find it. */
818 unsigned int load_all_dies : 1;
820 /* Non-zero if this CU is from .debug_types.
821 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
823 unsigned int is_debug_types : 1;
825 /* Non-zero if this CU is from the .dwz file. */
826 unsigned int is_dwz : 1;
828 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
829 This flag is only valid if is_debug_types is true.
830 We can't read a CU directly from a DWO file: There are required
831 attributes in the stub. */
832 unsigned int reading_dwo_directly : 1;
834 /* Non-zero if the TU has been read.
835 This is used to assist the "Stay in DWO Optimization" for Fission:
836 When reading a DWO, it's faster to read TUs from the DWO instead of
837 fetching them from random other DWOs (due to comdat folding).
838 If the TU has already been read, the optimization is unnecessary
839 (and unwise - we don't want to change where gdb thinks the TU lives
841 This flag is only valid if is_debug_types is true. */
842 unsigned int tu_read : 1;
844 /* The section this CU/TU lives in.
845 If the DIE refers to a DWO file, this is always the original die,
847 struct dwarf2_section_info *section;
849 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
850 of the CU cache it gets reset to NULL again. This is left as NULL for
851 dummy CUs (a CU header, but nothing else). */
852 struct dwarf2_cu *cu;
854 /* The corresponding dwarf2_per_objfile. */
855 struct dwarf2_per_objfile *dwarf2_per_objfile;
857 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
858 is active. Otherwise, the 'psymtab' field is active. */
861 /* The partial symbol table associated with this compilation unit,
862 or NULL for unread partial units. */
863 struct partial_symtab *psymtab;
865 /* Data needed by the "quick" functions. */
866 struct dwarf2_per_cu_quick_data *quick;
869 /* The CUs we import using DW_TAG_imported_unit. This is filled in
870 while reading psymtabs, used to compute the psymtab dependencies,
871 and then cleared. Then it is filled in again while reading full
872 symbols, and only deleted when the objfile is destroyed.
874 This is also used to work around a difference between the way gold
875 generates .gdb_index version <=7 and the way gdb does. Arguably this
876 is a gold bug. For symbols coming from TUs, gold records in the index
877 the CU that includes the TU instead of the TU itself. This breaks
878 dw2_lookup_symbol: It assumes that if the index says symbol X lives
879 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
880 will find X. Alas TUs live in their own symtab, so after expanding CU Y
881 we need to look in TU Z to find X. Fortunately, this is akin to
882 DW_TAG_imported_unit, so we just use the same mechanism: For
883 .gdb_index version <=7 this also records the TUs that the CU referred
884 to. Concurrently with this change gdb was modified to emit version 8
885 indices so we only pay a price for gold generated indices.
886 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
887 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
890 /* Entry in the signatured_types hash table. */
892 struct signatured_type
894 /* The "per_cu" object of this type.
895 This struct is used iff per_cu.is_debug_types.
896 N.B.: This is the first member so that it's easy to convert pointers
898 struct dwarf2_per_cu_data per_cu;
900 /* The type's signature. */
903 /* Offset in the TU of the type's DIE, as read from the TU header.
904 If this TU is a DWO stub and the definition lives in a DWO file
905 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
906 cu_offset type_offset_in_tu;
908 /* Offset in the section of the type's DIE.
909 If the definition lives in a DWO file, this is the offset in the
910 .debug_types.dwo section.
911 The value is zero until the actual value is known.
912 Zero is otherwise not a valid section offset. */
913 sect_offset type_offset_in_section;
915 /* Type units are grouped by their DW_AT_stmt_list entry so that they
916 can share them. This points to the containing symtab. */
917 struct type_unit_group *type_unit_group;
920 The first time we encounter this type we fully read it in and install it
921 in the symbol tables. Subsequent times we only need the type. */
924 /* Containing DWO unit.
925 This field is valid iff per_cu.reading_dwo_directly. */
926 struct dwo_unit *dwo_unit;
929 typedef struct signatured_type *sig_type_ptr;
930 DEF_VEC_P (sig_type_ptr);
932 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
933 This includes type_unit_group and quick_file_names. */
935 struct stmt_list_hash
937 /* The DWO unit this table is from or NULL if there is none. */
938 struct dwo_unit *dwo_unit;
940 /* Offset in .debug_line or .debug_line.dwo. */
941 sect_offset line_sect_off;
944 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
945 an object of this type. */
947 struct type_unit_group
949 /* dwarf2read.c's main "handle" on a TU symtab.
950 To simplify things we create an artificial CU that "includes" all the
951 type units using this stmt_list so that the rest of the code still has
952 a "per_cu" handle on the symtab.
953 This PER_CU is recognized by having no section. */
954 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
955 struct dwarf2_per_cu_data per_cu;
957 /* The TUs that share this DW_AT_stmt_list entry.
958 This is added to while parsing type units to build partial symtabs,
959 and is deleted afterwards and not used again. */
960 VEC (sig_type_ptr) *tus;
962 /* The compunit symtab.
963 Type units in a group needn't all be defined in the same source file,
964 so we create an essentially anonymous symtab as the compunit symtab. */
965 struct compunit_symtab *compunit_symtab;
967 /* The data used to construct the hash key. */
968 struct stmt_list_hash hash;
970 /* The number of symtabs from the line header.
971 The value here must match line_header.num_file_names. */
972 unsigned int num_symtabs;
974 /* The symbol tables for this TU (obtained from the files listed in
976 WARNING: The order of entries here must match the order of entries
977 in the line header. After the first TU using this type_unit_group, the
978 line header for the subsequent TUs is recreated from this. This is done
979 because we need to use the same symtabs for each TU using the same
980 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
981 there's no guarantee the line header doesn't have duplicate entries. */
982 struct symtab **symtabs;
985 /* These sections are what may appear in a (real or virtual) DWO file. */
989 struct dwarf2_section_info abbrev;
990 struct dwarf2_section_info line;
991 struct dwarf2_section_info loc;
992 struct dwarf2_section_info loclists;
993 struct dwarf2_section_info macinfo;
994 struct dwarf2_section_info macro;
995 struct dwarf2_section_info str;
996 struct dwarf2_section_info str_offsets;
997 /* In the case of a virtual DWO file, these two are unused. */
998 struct dwarf2_section_info info;
999 VEC (dwarf2_section_info_def) *types;
1002 /* CUs/TUs in DWP/DWO files. */
1006 /* Backlink to the containing struct dwo_file. */
1007 struct dwo_file *dwo_file;
1009 /* The "id" that distinguishes this CU/TU.
1010 .debug_info calls this "dwo_id", .debug_types calls this "signature".
1011 Since signatures came first, we stick with it for consistency. */
1014 /* The section this CU/TU lives in, in the DWO file. */
1015 struct dwarf2_section_info *section;
1017 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
1018 sect_offset sect_off;
1019 unsigned int length;
1021 /* For types, offset in the type's DIE of the type defined by this TU. */
1022 cu_offset type_offset_in_tu;
1025 /* include/dwarf2.h defines the DWP section codes.
1026 It defines a max value but it doesn't define a min value, which we
1027 use for error checking, so provide one. */
1029 enum dwp_v2_section_ids
1034 /* Data for one DWO file.
1036 This includes virtual DWO files (a virtual DWO file is a DWO file as it
1037 appears in a DWP file). DWP files don't really have DWO files per se -
1038 comdat folding of types "loses" the DWO file they came from, and from
1039 a high level view DWP files appear to contain a mass of random types.
1040 However, to maintain consistency with the non-DWP case we pretend DWP
1041 files contain virtual DWO files, and we assign each TU with one virtual
1042 DWO file (generally based on the line and abbrev section offsets -
1043 a heuristic that seems to work in practice). */
1047 /* The DW_AT_GNU_dwo_name attribute.
1048 For virtual DWO files the name is constructed from the section offsets
1049 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
1050 from related CU+TUs. */
1051 const char *dwo_name;
1053 /* The DW_AT_comp_dir attribute. */
1054 const char *comp_dir;
1056 /* The bfd, when the file is open. Otherwise this is NULL.
1057 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
1060 /* The sections that make up this DWO file.
1061 Remember that for virtual DWO files in DWP V2, these are virtual
1062 sections (for lack of a better name). */
1063 struct dwo_sections sections;
1065 /* The CUs in the file.
1066 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
1067 an extension to handle LLVM's Link Time Optimization output (where
1068 multiple source files may be compiled into a single object/dwo pair). */
1071 /* Table of TUs in the file.
1072 Each element is a struct dwo_unit. */
1076 /* These sections are what may appear in a DWP file. */
1080 /* These are used by both DWP version 1 and 2. */
1081 struct dwarf2_section_info str;
1082 struct dwarf2_section_info cu_index;
1083 struct dwarf2_section_info tu_index;
1085 /* These are only used by DWP version 2 files.
1086 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
1087 sections are referenced by section number, and are not recorded here.
1088 In DWP version 2 there is at most one copy of all these sections, each
1089 section being (effectively) comprised of the concatenation of all of the
1090 individual sections that exist in the version 1 format.
1091 To keep the code simple we treat each of these concatenated pieces as a
1092 section itself (a virtual section?). */
1093 struct dwarf2_section_info abbrev;
1094 struct dwarf2_section_info info;
1095 struct dwarf2_section_info line;
1096 struct dwarf2_section_info loc;
1097 struct dwarf2_section_info macinfo;
1098 struct dwarf2_section_info macro;
1099 struct dwarf2_section_info str_offsets;
1100 struct dwarf2_section_info types;
1103 /* These sections are what may appear in a virtual DWO file in DWP version 1.
1104 A virtual DWO file is a DWO file as it appears in a DWP file. */
1106 struct virtual_v1_dwo_sections
1108 struct dwarf2_section_info abbrev;
1109 struct dwarf2_section_info line;
1110 struct dwarf2_section_info loc;
1111 struct dwarf2_section_info macinfo;
1112 struct dwarf2_section_info macro;
1113 struct dwarf2_section_info str_offsets;
1114 /* Each DWP hash table entry records one CU or one TU.
1115 That is recorded here, and copied to dwo_unit.section. */
1116 struct dwarf2_section_info info_or_types;
1119 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
1120 In version 2, the sections of the DWO files are concatenated together
1121 and stored in one section of that name. Thus each ELF section contains
1122 several "virtual" sections. */
1124 struct virtual_v2_dwo_sections
1126 bfd_size_type abbrev_offset;
1127 bfd_size_type abbrev_size;
1129 bfd_size_type line_offset;
1130 bfd_size_type line_size;
1132 bfd_size_type loc_offset;
1133 bfd_size_type loc_size;
1135 bfd_size_type macinfo_offset;
1136 bfd_size_type macinfo_size;
1138 bfd_size_type macro_offset;
1139 bfd_size_type macro_size;
1141 bfd_size_type str_offsets_offset;
1142 bfd_size_type str_offsets_size;
1144 /* Each DWP hash table entry records one CU or one TU.
1145 That is recorded here, and copied to dwo_unit.section. */
1146 bfd_size_type info_or_types_offset;
1147 bfd_size_type info_or_types_size;
1150 /* Contents of DWP hash tables. */
1152 struct dwp_hash_table
1154 uint32_t version, nr_columns;
1155 uint32_t nr_units, nr_slots;
1156 const gdb_byte *hash_table, *unit_table;
1161 const gdb_byte *indices;
1165 /* This is indexed by column number and gives the id of the section
1167 #define MAX_NR_V2_DWO_SECTIONS \
1168 (1 /* .debug_info or .debug_types */ \
1169 + 1 /* .debug_abbrev */ \
1170 + 1 /* .debug_line */ \
1171 + 1 /* .debug_loc */ \
1172 + 1 /* .debug_str_offsets */ \
1173 + 1 /* .debug_macro or .debug_macinfo */)
1174 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1175 const gdb_byte *offsets;
1176 const gdb_byte *sizes;
1181 /* Data for one DWP file. */
1185 /* Name of the file. */
1188 /* File format version. */
1194 /* Section info for this file. */
1195 struct dwp_sections sections;
1197 /* Table of CUs in the file. */
1198 const struct dwp_hash_table *cus;
1200 /* Table of TUs in the file. */
1201 const struct dwp_hash_table *tus;
1203 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1207 /* Table to map ELF section numbers to their sections.
1208 This is only needed for the DWP V1 file format. */
1209 unsigned int num_sections;
1210 asection **elf_sections;
1213 /* This represents a '.dwz' file. */
1217 /* A dwz file can only contain a few sections. */
1218 struct dwarf2_section_info abbrev;
1219 struct dwarf2_section_info info;
1220 struct dwarf2_section_info str;
1221 struct dwarf2_section_info line;
1222 struct dwarf2_section_info macro;
1223 struct dwarf2_section_info gdb_index;
1224 struct dwarf2_section_info debug_names;
1226 /* The dwz's BFD. */
1230 /* Struct used to pass misc. parameters to read_die_and_children, et
1231 al. which are used for both .debug_info and .debug_types dies.
1232 All parameters here are unchanging for the life of the call. This
1233 struct exists to abstract away the constant parameters of die reading. */
1235 struct die_reader_specs
1237 /* The bfd of die_section. */
1240 /* The CU of the DIE we are parsing. */
1241 struct dwarf2_cu *cu;
1243 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1244 struct dwo_file *dwo_file;
1246 /* The section the die comes from.
1247 This is either .debug_info or .debug_types, or the .dwo variants. */
1248 struct dwarf2_section_info *die_section;
1250 /* die_section->buffer. */
1251 const gdb_byte *buffer;
1253 /* The end of the buffer. */
1254 const gdb_byte *buffer_end;
1256 /* The value of the DW_AT_comp_dir attribute. */
1257 const char *comp_dir;
1259 /* The abbreviation table to use when reading the DIEs. */
1260 struct abbrev_table *abbrev_table;
1263 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1264 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1265 const gdb_byte *info_ptr,
1266 struct die_info *comp_unit_die,
1270 /* A 1-based directory index. This is a strong typedef to prevent
1271 accidentally using a directory index as a 0-based index into an
1273 enum class dir_index : unsigned int {};
1275 /* Likewise, a 1-based file name index. */
1276 enum class file_name_index : unsigned int {};
1280 file_entry () = default;
1282 file_entry (const char *name_, dir_index d_index_,
1283 unsigned int mod_time_, unsigned int length_)
1286 mod_time (mod_time_),
1290 /* Return the include directory at D_INDEX stored in LH. Returns
1291 NULL if D_INDEX is out of bounds. */
1292 const char *include_dir (const line_header *lh) const;
1294 /* The file name. Note this is an observing pointer. The memory is
1295 owned by debug_line_buffer. */
1296 const char *name {};
1298 /* The directory index (1-based). */
1299 dir_index d_index {};
1301 unsigned int mod_time {};
1303 unsigned int length {};
1305 /* True if referenced by the Line Number Program. */
1308 /* The associated symbol table, if any. */
1309 struct symtab *symtab {};
1312 /* The line number information for a compilation unit (found in the
1313 .debug_line section) begins with a "statement program header",
1314 which contains the following information. */
1321 /* Add an entry to the include directory table. */
1322 void add_include_dir (const char *include_dir);
1324 /* Add an entry to the file name table. */
1325 void add_file_name (const char *name, dir_index d_index,
1326 unsigned int mod_time, unsigned int length);
1328 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
1329 is out of bounds. */
1330 const char *include_dir_at (dir_index index) const
1332 /* Convert directory index number (1-based) to vector index
1334 size_t vec_index = to_underlying (index) - 1;
1336 if (vec_index >= include_dirs.size ())
1338 return include_dirs[vec_index];
1341 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
1342 is out of bounds. */
1343 file_entry *file_name_at (file_name_index index)
1345 /* Convert file name index number (1-based) to vector index
1347 size_t vec_index = to_underlying (index) - 1;
1349 if (vec_index >= file_names.size ())
1351 return &file_names[vec_index];
1354 /* Const version of the above. */
1355 const file_entry *file_name_at (unsigned int index) const
1357 if (index >= file_names.size ())
1359 return &file_names[index];
1362 /* Offset of line number information in .debug_line section. */
1363 sect_offset sect_off {};
1365 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1366 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1368 unsigned int total_length {};
1369 unsigned short version {};
1370 unsigned int header_length {};
1371 unsigned char minimum_instruction_length {};
1372 unsigned char maximum_ops_per_instruction {};
1373 unsigned char default_is_stmt {};
1375 unsigned char line_range {};
1376 unsigned char opcode_base {};
1378 /* standard_opcode_lengths[i] is the number of operands for the
1379 standard opcode whose value is i. This means that
1380 standard_opcode_lengths[0] is unused, and the last meaningful
1381 element is standard_opcode_lengths[opcode_base - 1]. */
1382 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1384 /* The include_directories table. Note these are observing
1385 pointers. The memory is owned by debug_line_buffer. */
1386 std::vector<const char *> include_dirs;
1388 /* The file_names table. */
1389 std::vector<file_entry> file_names;
1391 /* The start and end of the statement program following this
1392 header. These point into dwarf2_per_objfile->line_buffer. */
1393 const gdb_byte *statement_program_start {}, *statement_program_end {};
1396 typedef std::unique_ptr<line_header> line_header_up;
1399 file_entry::include_dir (const line_header *lh) const
1401 return lh->include_dir_at (d_index);
1404 /* When we construct a partial symbol table entry we only
1405 need this much information. */
1406 struct partial_die_info
1408 /* Offset of this DIE. */
1409 sect_offset sect_off;
1411 /* DWARF-2 tag for this DIE. */
1412 ENUM_BITFIELD(dwarf_tag) tag : 16;
1414 /* Assorted flags describing the data found in this DIE. */
1415 unsigned int has_children : 1;
1416 unsigned int is_external : 1;
1417 unsigned int is_declaration : 1;
1418 unsigned int has_type : 1;
1419 unsigned int has_specification : 1;
1420 unsigned int has_pc_info : 1;
1421 unsigned int may_be_inlined : 1;
1423 /* This DIE has been marked DW_AT_main_subprogram. */
1424 unsigned int main_subprogram : 1;
1426 /* Flag set if the SCOPE field of this structure has been
1428 unsigned int scope_set : 1;
1430 /* Flag set if the DIE has a byte_size attribute. */
1431 unsigned int has_byte_size : 1;
1433 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1434 unsigned int has_const_value : 1;
1436 /* Flag set if any of the DIE's children are template arguments. */
1437 unsigned int has_template_arguments : 1;
1439 /* Flag set if fixup_partial_die has been called on this die. */
1440 unsigned int fixup_called : 1;
1442 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1443 unsigned int is_dwz : 1;
1445 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1446 unsigned int spec_is_dwz : 1;
1448 /* The name of this DIE. Normally the value of DW_AT_name, but
1449 sometimes a default name for unnamed DIEs. */
1452 /* The linkage name, if present. */
1453 const char *linkage_name;
1455 /* The scope to prepend to our children. This is generally
1456 allocated on the comp_unit_obstack, so will disappear
1457 when this compilation unit leaves the cache. */
1460 /* Some data associated with the partial DIE. The tag determines
1461 which field is live. */
1464 /* The location description associated with this DIE, if any. */
1465 struct dwarf_block *locdesc;
1466 /* The offset of an import, for DW_TAG_imported_unit. */
1467 sect_offset sect_off;
1470 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1474 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1475 DW_AT_sibling, if any. */
1476 /* NOTE: This member isn't strictly necessary, read_partial_die could
1477 return DW_AT_sibling values to its caller load_partial_dies. */
1478 const gdb_byte *sibling;
1480 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1481 DW_AT_specification (or DW_AT_abstract_origin or
1482 DW_AT_extension). */
1483 sect_offset spec_offset;
1485 /* Pointers to this DIE's parent, first child, and next sibling,
1487 struct partial_die_info *die_parent, *die_child, *die_sibling;
1490 /* This data structure holds the information of an abbrev. */
1493 unsigned int number; /* number identifying abbrev */
1494 enum dwarf_tag tag; /* dwarf tag */
1495 unsigned short has_children; /* boolean */
1496 unsigned short num_attrs; /* number of attributes */
1497 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1498 struct abbrev_info *next; /* next in chain */
1503 ENUM_BITFIELD(dwarf_attribute) name : 16;
1504 ENUM_BITFIELD(dwarf_form) form : 16;
1506 /* It is valid only if FORM is DW_FORM_implicit_const. */
1507 LONGEST implicit_const;
1510 /* Size of abbrev_table.abbrev_hash_table. */
1511 #define ABBREV_HASH_SIZE 121
1513 /* Top level data structure to contain an abbreviation table. */
1517 explicit abbrev_table (sect_offset off)
1521 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1522 memset (abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1525 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1527 /* Allocate space for a struct abbrev_info object in
1529 struct abbrev_info *alloc_abbrev ();
1531 /* Add an abbreviation to the table. */
1532 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1534 /* Look up an abbrev in the table.
1535 Returns NULL if the abbrev is not found. */
1537 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1540 /* Where the abbrev table came from.
1541 This is used as a sanity check when the table is used. */
1542 const sect_offset sect_off;
1544 /* Storage for the abbrev table. */
1545 auto_obstack abbrev_obstack;
1547 /* Hash table of abbrevs.
1548 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1549 It could be statically allocated, but the previous code didn't so we
1551 struct abbrev_info **abbrevs;
1554 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1556 /* Attributes have a name and a value. */
1559 ENUM_BITFIELD(dwarf_attribute) name : 16;
1560 ENUM_BITFIELD(dwarf_form) form : 15;
1562 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1563 field should be in u.str (existing only for DW_STRING) but it is kept
1564 here for better struct attribute alignment. */
1565 unsigned int string_is_canonical : 1;
1570 struct dwarf_block *blk;
1579 /* This data structure holds a complete die structure. */
1582 /* DWARF-2 tag for this DIE. */
1583 ENUM_BITFIELD(dwarf_tag) tag : 16;
1585 /* Number of attributes */
1586 unsigned char num_attrs;
1588 /* True if we're presently building the full type name for the
1589 type derived from this DIE. */
1590 unsigned char building_fullname : 1;
1592 /* True if this die is in process. PR 16581. */
1593 unsigned char in_process : 1;
1596 unsigned int abbrev;
1598 /* Offset in .debug_info or .debug_types section. */
1599 sect_offset sect_off;
1601 /* The dies in a compilation unit form an n-ary tree. PARENT
1602 points to this die's parent; CHILD points to the first child of
1603 this node; and all the children of a given node are chained
1604 together via their SIBLING fields. */
1605 struct die_info *child; /* Its first child, if any. */
1606 struct die_info *sibling; /* Its next sibling, if any. */
1607 struct die_info *parent; /* Its parent, if any. */
1609 /* An array of attributes, with NUM_ATTRS elements. There may be
1610 zero, but it's not common and zero-sized arrays are not
1611 sufficiently portable C. */
1612 struct attribute attrs[1];
1615 /* Get at parts of an attribute structure. */
1617 #define DW_STRING(attr) ((attr)->u.str)
1618 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1619 #define DW_UNSND(attr) ((attr)->u.unsnd)
1620 #define DW_BLOCK(attr) ((attr)->u.blk)
1621 #define DW_SND(attr) ((attr)->u.snd)
1622 #define DW_ADDR(attr) ((attr)->u.addr)
1623 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1625 /* Blocks are a bunch of untyped bytes. */
1630 /* Valid only if SIZE is not zero. */
1631 const gdb_byte *data;
1634 #ifndef ATTR_ALLOC_CHUNK
1635 #define ATTR_ALLOC_CHUNK 4
1638 /* Allocate fields for structs, unions and enums in this size. */
1639 #ifndef DW_FIELD_ALLOC_CHUNK
1640 #define DW_FIELD_ALLOC_CHUNK 4
1643 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1644 but this would require a corresponding change in unpack_field_as_long
1646 static int bits_per_byte = 8;
1650 struct nextfield *next;
1658 struct nextfnfield *next;
1659 struct fn_field fnfield;
1666 struct nextfnfield *head;
1669 struct decl_field_list
1671 struct decl_field field;
1672 struct decl_field_list *next;
1675 /* The routines that read and process dies for a C struct or C++ class
1676 pass lists of data member fields and lists of member function fields
1677 in an instance of a field_info structure, as defined below. */
1680 /* List of data member and baseclasses fields. */
1681 struct nextfield *fields, *baseclasses;
1683 /* Number of fields (including baseclasses). */
1686 /* Number of baseclasses. */
1689 /* Set if the accesibility of one of the fields is not public. */
1690 int non_public_fields;
1692 /* Member function fieldlist array, contains name of possibly overloaded
1693 member function, number of overloaded member functions and a pointer
1694 to the head of the member function field chain. */
1695 struct fnfieldlist *fnfieldlists;
1697 /* Number of entries in the fnfieldlists array. */
1700 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1701 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1702 struct decl_field_list *typedef_field_list;
1703 unsigned typedef_field_list_count;
1705 /* Nested types defined by this class and the number of elements in this
1707 struct decl_field_list *nested_types_list;
1708 unsigned nested_types_list_count;
1711 /* One item on the queue of compilation units to read in full symbols
1713 struct dwarf2_queue_item
1715 struct dwarf2_per_cu_data *per_cu;
1716 enum language pretend_language;
1717 struct dwarf2_queue_item *next;
1720 /* The current queue. */
1721 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1723 /* Loaded secondary compilation units are kept in memory until they
1724 have not been referenced for the processing of this many
1725 compilation units. Set this to zero to disable caching. Cache
1726 sizes of up to at least twenty will improve startup time for
1727 typical inter-CU-reference binaries, at an obvious memory cost. */
1728 static int dwarf_max_cache_age = 5;
1730 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1731 struct cmd_list_element *c, const char *value)
1733 fprintf_filtered (file, _("The upper bound on the age of cached "
1734 "DWARF compilation units is %s.\n"),
1738 /* local function prototypes */
1740 static const char *get_section_name (const struct dwarf2_section_info *);
1742 static const char *get_section_file_name (const struct dwarf2_section_info *);
1744 static void dwarf2_find_base_address (struct die_info *die,
1745 struct dwarf2_cu *cu);
1747 static struct partial_symtab *create_partial_symtab
1748 (struct dwarf2_per_cu_data *per_cu, const char *name);
1750 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1751 const gdb_byte *info_ptr,
1752 struct die_info *type_unit_die,
1753 int has_children, void *data);
1755 static void dwarf2_build_psymtabs_hard
1756 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1758 static void scan_partial_symbols (struct partial_die_info *,
1759 CORE_ADDR *, CORE_ADDR *,
1760 int, struct dwarf2_cu *);
1762 static void add_partial_symbol (struct partial_die_info *,
1763 struct dwarf2_cu *);
1765 static void add_partial_namespace (struct partial_die_info *pdi,
1766 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1767 int set_addrmap, struct dwarf2_cu *cu);
1769 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1770 CORE_ADDR *highpc, int set_addrmap,
1771 struct dwarf2_cu *cu);
1773 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1774 struct dwarf2_cu *cu);
1776 static void add_partial_subprogram (struct partial_die_info *pdi,
1777 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1778 int need_pc, struct dwarf2_cu *cu);
1780 static void dwarf2_read_symtab (struct partial_symtab *,
1783 static void psymtab_to_symtab_1 (struct partial_symtab *);
1785 static abbrev_table_up abbrev_table_read_table
1786 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1789 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1791 static struct partial_die_info *load_partial_dies
1792 (const struct die_reader_specs *, const gdb_byte *, int);
1794 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1795 struct partial_die_info *,
1796 struct abbrev_info *,
1800 static struct partial_die_info *find_partial_die (sect_offset, int,
1801 struct dwarf2_cu *);
1803 static void fixup_partial_die (struct partial_die_info *,
1804 struct dwarf2_cu *);
1806 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1807 struct attribute *, struct attr_abbrev *,
1810 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1812 static int read_1_signed_byte (bfd *, const gdb_byte *);
1814 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1816 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1818 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1820 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1823 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1825 static LONGEST read_checked_initial_length_and_offset
1826 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1827 unsigned int *, unsigned int *);
1829 static LONGEST read_offset (bfd *, const gdb_byte *,
1830 const struct comp_unit_head *,
1833 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1835 static sect_offset read_abbrev_offset
1836 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1837 struct dwarf2_section_info *, sect_offset);
1839 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1841 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1843 static const char *read_indirect_string
1844 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1845 const struct comp_unit_head *, unsigned int *);
1847 static const char *read_indirect_line_string
1848 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1849 const struct comp_unit_head *, unsigned int *);
1851 static const char *read_indirect_string_at_offset
1852 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1853 LONGEST str_offset);
1855 static const char *read_indirect_string_from_dwz
1856 (struct objfile *objfile, struct dwz_file *, LONGEST);
1858 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1860 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1864 static const char *read_str_index (const struct die_reader_specs *reader,
1865 ULONGEST str_index);
1867 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1869 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1870 struct dwarf2_cu *);
1872 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1875 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1876 struct dwarf2_cu *cu);
1878 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1879 struct dwarf2_cu *cu);
1881 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1883 static struct die_info *die_specification (struct die_info *die,
1884 struct dwarf2_cu **);
1886 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1887 struct dwarf2_cu *cu);
1889 static void dwarf_decode_lines (struct line_header *, const char *,
1890 struct dwarf2_cu *, struct partial_symtab *,
1891 CORE_ADDR, int decode_mapping);
1893 static void dwarf2_start_subfile (const char *, const char *);
1895 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1896 const char *, const char *,
1899 static struct symbol *new_symbol (struct die_info *, struct type *,
1900 struct dwarf2_cu *, struct symbol * = NULL);
1902 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1903 struct dwarf2_cu *);
1905 static void dwarf2_const_value_attr (const struct attribute *attr,
1908 struct obstack *obstack,
1909 struct dwarf2_cu *cu, LONGEST *value,
1910 const gdb_byte **bytes,
1911 struct dwarf2_locexpr_baton **baton);
1913 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1915 static int need_gnat_info (struct dwarf2_cu *);
1917 static struct type *die_descriptive_type (struct die_info *,
1918 struct dwarf2_cu *);
1920 static void set_descriptive_type (struct type *, struct die_info *,
1921 struct dwarf2_cu *);
1923 static struct type *die_containing_type (struct die_info *,
1924 struct dwarf2_cu *);
1926 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1927 struct dwarf2_cu *);
1929 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1931 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1933 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1935 static char *typename_concat (struct obstack *obs, const char *prefix,
1936 const char *suffix, int physname,
1937 struct dwarf2_cu *cu);
1939 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1941 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1943 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1945 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1947 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1949 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1951 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1952 struct dwarf2_cu *, struct partial_symtab *);
1954 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1955 values. Keep the items ordered with increasing constraints compliance. */
1958 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1959 PC_BOUNDS_NOT_PRESENT,
1961 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1962 were present but they do not form a valid range of PC addresses. */
1965 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1968 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1972 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1973 CORE_ADDR *, CORE_ADDR *,
1975 struct partial_symtab *);
1977 static void get_scope_pc_bounds (struct die_info *,
1978 CORE_ADDR *, CORE_ADDR *,
1979 struct dwarf2_cu *);
1981 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1982 CORE_ADDR, struct dwarf2_cu *);
1984 static void dwarf2_add_field (struct field_info *, struct die_info *,
1985 struct dwarf2_cu *);
1987 static void dwarf2_attach_fields_to_type (struct field_info *,
1988 struct type *, struct dwarf2_cu *);
1990 static void dwarf2_add_member_fn (struct field_info *,
1991 struct die_info *, struct type *,
1992 struct dwarf2_cu *);
1994 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1996 struct dwarf2_cu *);
1998 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
2000 static void read_common_block (struct die_info *, struct dwarf2_cu *);
2002 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
2004 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
2006 static struct using_direct **using_directives (enum language);
2008 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
2010 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
2012 static struct type *read_module_type (struct die_info *die,
2013 struct dwarf2_cu *cu);
2015 static const char *namespace_name (struct die_info *die,
2016 int *is_anonymous, struct dwarf2_cu *);
2018 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
2020 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
2022 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
2023 struct dwarf2_cu *);
2025 static struct die_info *read_die_and_siblings_1
2026 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
2029 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
2030 const gdb_byte *info_ptr,
2031 const gdb_byte **new_info_ptr,
2032 struct die_info *parent);
2034 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
2035 struct die_info **, const gdb_byte *,
2038 static const gdb_byte *read_full_die (const struct die_reader_specs *,
2039 struct die_info **, const gdb_byte *,
2042 static void process_die (struct die_info *, struct dwarf2_cu *);
2044 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
2047 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
2049 static const char *dwarf2_full_name (const char *name,
2050 struct die_info *die,
2051 struct dwarf2_cu *cu);
2053 static const char *dwarf2_physname (const char *name, struct die_info *die,
2054 struct dwarf2_cu *cu);
2056 static struct die_info *dwarf2_extension (struct die_info *die,
2057 struct dwarf2_cu **);
2059 static const char *dwarf_tag_name (unsigned int);
2061 static const char *dwarf_attr_name (unsigned int);
2063 static const char *dwarf_form_name (unsigned int);
2065 static const char *dwarf_bool_name (unsigned int);
2067 static const char *dwarf_type_encoding_name (unsigned int);
2069 static struct die_info *sibling_die (struct die_info *);
2071 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
2073 static void dump_die_for_error (struct die_info *);
2075 static void dump_die_1 (struct ui_file *, int level, int max_level,
2078 /*static*/ void dump_die (struct die_info *, int max_level);
2080 static void store_in_ref_table (struct die_info *,
2081 struct dwarf2_cu *);
2083 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
2085 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
2087 static struct die_info *follow_die_ref_or_sig (struct die_info *,
2088 const struct attribute *,
2089 struct dwarf2_cu **);
2091 static struct die_info *follow_die_ref (struct die_info *,
2092 const struct attribute *,
2093 struct dwarf2_cu **);
2095 static struct die_info *follow_die_sig (struct die_info *,
2096 const struct attribute *,
2097 struct dwarf2_cu **);
2099 static struct type *get_signatured_type (struct die_info *, ULONGEST,
2100 struct dwarf2_cu *);
2102 static struct type *get_DW_AT_signature_type (struct die_info *,
2103 const struct attribute *,
2104 struct dwarf2_cu *);
2106 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
2108 static void read_signatured_type (struct signatured_type *);
2110 static int attr_to_dynamic_prop (const struct attribute *attr,
2111 struct die_info *die, struct dwarf2_cu *cu,
2112 struct dynamic_prop *prop);
2114 /* memory allocation interface */
2116 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
2118 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
2120 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2122 static int attr_form_is_block (const struct attribute *);
2124 static int attr_form_is_section_offset (const struct attribute *);
2126 static int attr_form_is_constant (const struct attribute *);
2128 static int attr_form_is_ref (const struct attribute *);
2130 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
2131 struct dwarf2_loclist_baton *baton,
2132 const struct attribute *attr);
2134 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
2136 struct dwarf2_cu *cu,
2139 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
2140 const gdb_byte *info_ptr,
2141 struct abbrev_info *abbrev);
2143 static hashval_t partial_die_hash (const void *item);
2145 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2147 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
2148 (sect_offset sect_off, unsigned int offset_in_dwz,
2149 struct dwarf2_per_objfile *dwarf2_per_objfile);
2151 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
2152 struct die_info *comp_unit_die,
2153 enum language pretend_language);
2155 static void free_cached_comp_units (void *);
2157 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2159 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
2161 static struct type *set_die_type (struct die_info *, struct type *,
2162 struct dwarf2_cu *);
2164 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2166 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2168 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2171 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2174 static void process_full_type_unit (struct dwarf2_per_cu_data *,
2177 static void dwarf2_add_dependence (struct dwarf2_cu *,
2178 struct dwarf2_per_cu_data *);
2180 static void dwarf2_mark (struct dwarf2_cu *);
2182 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2184 static struct type *get_die_type_at_offset (sect_offset,
2185 struct dwarf2_per_cu_data *);
2187 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
2189 static void dwarf2_release_queue (void *dummy);
2191 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2192 enum language pretend_language);
2194 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
2196 /* The return type of find_file_and_directory. Note, the enclosed
2197 string pointers are only valid while this object is valid. */
2199 struct file_and_directory
2201 /* The filename. This is never NULL. */
2204 /* The compilation directory. NULL if not known. If we needed to
2205 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2206 points directly to the DW_AT_comp_dir string attribute owned by
2207 the obstack that owns the DIE. */
2208 const char *comp_dir;
2210 /* If we needed to build a new string for comp_dir, this is what
2211 owns the storage. */
2212 std::string comp_dir_storage;
2215 static file_and_directory find_file_and_directory (struct die_info *die,
2216 struct dwarf2_cu *cu);
2218 static char *file_full_name (int file, struct line_header *lh,
2219 const char *comp_dir);
2221 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
2222 enum class rcuh_kind { COMPILE, TYPE };
2224 static const gdb_byte *read_and_check_comp_unit_head
2225 (struct dwarf2_per_objfile* dwarf2_per_objfile,
2226 struct comp_unit_head *header,
2227 struct dwarf2_section_info *section,
2228 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
2229 rcuh_kind section_kind);
2231 static void init_cutu_and_read_dies
2232 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2233 int use_existing_cu, int keep,
2234 die_reader_func_ftype *die_reader_func, void *data);
2236 static void init_cutu_and_read_dies_simple
2237 (struct dwarf2_per_cu_data *this_cu,
2238 die_reader_func_ftype *die_reader_func, void *data);
2240 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2242 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2244 static struct dwo_unit *lookup_dwo_unit_in_dwp
2245 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2246 struct dwp_file *dwp_file, const char *comp_dir,
2247 ULONGEST signature, int is_debug_types);
2249 static struct dwp_file *get_dwp_file
2250 (struct dwarf2_per_objfile *dwarf2_per_objfile);
2252 static struct dwo_unit *lookup_dwo_comp_unit
2253 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2255 static struct dwo_unit *lookup_dwo_type_unit
2256 (struct signatured_type *, const char *, const char *);
2258 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2260 static void free_dwo_file_cleanup (void *);
2262 struct free_dwo_file_cleanup_data
2264 struct dwo_file *dwo_file;
2265 struct dwarf2_per_objfile *dwarf2_per_objfile;
2268 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2270 static void check_producer (struct dwarf2_cu *cu);
2272 static void free_line_header_voidp (void *arg);
2274 /* Various complaints about symbol reading that don't abort the process. */
2277 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2279 complaint (&symfile_complaints,
2280 _("statement list doesn't fit in .debug_line section"));
2284 dwarf2_debug_line_missing_file_complaint (void)
2286 complaint (&symfile_complaints,
2287 _(".debug_line section has line data without a file"));
2291 dwarf2_debug_line_missing_end_sequence_complaint (void)
2293 complaint (&symfile_complaints,
2294 _(".debug_line section has line "
2295 "program sequence without an end"));
2299 dwarf2_complex_location_expr_complaint (void)
2301 complaint (&symfile_complaints, _("location expression too complex"));
2305 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2308 complaint (&symfile_complaints,
2309 _("const value length mismatch for '%s', got %d, expected %d"),
2314 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2316 complaint (&symfile_complaints,
2317 _("debug info runs off end of %s section"
2319 get_section_name (section),
2320 get_section_file_name (section));
2324 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2326 complaint (&symfile_complaints,
2327 _("macro debug info contains a "
2328 "malformed macro definition:\n`%s'"),
2333 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2335 complaint (&symfile_complaints,
2336 _("invalid attribute class or form for '%s' in '%s'"),
2340 /* Hash function for line_header_hash. */
2343 line_header_hash (const struct line_header *ofs)
2345 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2348 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2351 line_header_hash_voidp (const void *item)
2353 const struct line_header *ofs = (const struct line_header *) item;
2355 return line_header_hash (ofs);
2358 /* Equality function for line_header_hash. */
2361 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2363 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2364 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2366 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2367 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2372 /* Read the given attribute value as an address, taking the attribute's
2373 form into account. */
2376 attr_value_as_address (struct attribute *attr)
2380 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2382 /* Aside from a few clearly defined exceptions, attributes that
2383 contain an address must always be in DW_FORM_addr form.
2384 Unfortunately, some compilers happen to be violating this
2385 requirement by encoding addresses using other forms, such
2386 as DW_FORM_data4 for example. For those broken compilers,
2387 we try to do our best, without any guarantee of success,
2388 to interpret the address correctly. It would also be nice
2389 to generate a complaint, but that would require us to maintain
2390 a list of legitimate cases where a non-address form is allowed,
2391 as well as update callers to pass in at least the CU's DWARF
2392 version. This is more overhead than what we're willing to
2393 expand for a pretty rare case. */
2394 addr = DW_UNSND (attr);
2397 addr = DW_ADDR (attr);
2402 /* The suffix for an index file. */
2403 #define INDEX4_SUFFIX ".gdb-index"
2404 #define INDEX5_SUFFIX ".debug_names"
2405 #define DEBUG_STR_SUFFIX ".debug_str"
2407 /* See declaration. */
2409 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2410 const dwarf2_debug_sections *names)
2411 : objfile (objfile_)
2414 names = &dwarf2_elf_names;
2416 bfd *obfd = objfile->obfd;
2418 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2419 locate_sections (obfd, sec, *names);
2422 dwarf2_per_objfile::~dwarf2_per_objfile ()
2424 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2425 free_cached_comp_units ();
2427 if (quick_file_names_table)
2428 htab_delete (quick_file_names_table);
2430 if (line_header_hash)
2431 htab_delete (line_header_hash);
2433 /* Everything else should be on the objfile obstack. */
2436 /* See declaration. */
2439 dwarf2_per_objfile::free_cached_comp_units ()
2441 dwarf2_per_cu_data *per_cu = read_in_chain;
2442 dwarf2_per_cu_data **last_chain = &read_in_chain;
2443 while (per_cu != NULL)
2445 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2448 *last_chain = next_cu;
2453 /* Try to locate the sections we need for DWARF 2 debugging
2454 information and return true if we have enough to do something.
2455 NAMES points to the dwarf2 section names, or is NULL if the standard
2456 ELF names are used. */
2459 dwarf2_has_info (struct objfile *objfile,
2460 const struct dwarf2_debug_sections *names)
2462 if (objfile->flags & OBJF_READNEVER)
2465 struct dwarf2_per_objfile *dwarf2_per_objfile
2466 = get_dwarf2_per_objfile (objfile);
2468 if (dwarf2_per_objfile == NULL)
2470 /* Initialize per-objfile state. */
2471 struct dwarf2_per_objfile *data
2472 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
2474 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
2475 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
2477 return (!dwarf2_per_objfile->info.is_virtual
2478 && dwarf2_per_objfile->info.s.section != NULL
2479 && !dwarf2_per_objfile->abbrev.is_virtual
2480 && dwarf2_per_objfile->abbrev.s.section != NULL);
2483 /* Return the containing section of virtual section SECTION. */
2485 static struct dwarf2_section_info *
2486 get_containing_section (const struct dwarf2_section_info *section)
2488 gdb_assert (section->is_virtual);
2489 return section->s.containing_section;
2492 /* Return the bfd owner of SECTION. */
2495 get_section_bfd_owner (const struct dwarf2_section_info *section)
2497 if (section->is_virtual)
2499 section = get_containing_section (section);
2500 gdb_assert (!section->is_virtual);
2502 return section->s.section->owner;
2505 /* Return the bfd section of SECTION.
2506 Returns NULL if the section is not present. */
2509 get_section_bfd_section (const struct dwarf2_section_info *section)
2511 if (section->is_virtual)
2513 section = get_containing_section (section);
2514 gdb_assert (!section->is_virtual);
2516 return section->s.section;
2519 /* Return the name of SECTION. */
2522 get_section_name (const struct dwarf2_section_info *section)
2524 asection *sectp = get_section_bfd_section (section);
2526 gdb_assert (sectp != NULL);
2527 return bfd_section_name (get_section_bfd_owner (section), sectp);
2530 /* Return the name of the file SECTION is in. */
2533 get_section_file_name (const struct dwarf2_section_info *section)
2535 bfd *abfd = get_section_bfd_owner (section);
2537 return bfd_get_filename (abfd);
2540 /* Return the id of SECTION.
2541 Returns 0 if SECTION doesn't exist. */
2544 get_section_id (const struct dwarf2_section_info *section)
2546 asection *sectp = get_section_bfd_section (section);
2553 /* Return the flags of SECTION.
2554 SECTION (or containing section if this is a virtual section) must exist. */
2557 get_section_flags (const struct dwarf2_section_info *section)
2559 asection *sectp = get_section_bfd_section (section);
2561 gdb_assert (sectp != NULL);
2562 return bfd_get_section_flags (sectp->owner, sectp);
2565 /* When loading sections, we look either for uncompressed section or for
2566 compressed section names. */
2569 section_is_p (const char *section_name,
2570 const struct dwarf2_section_names *names)
2572 if (names->normal != NULL
2573 && strcmp (section_name, names->normal) == 0)
2575 if (names->compressed != NULL
2576 && strcmp (section_name, names->compressed) == 0)
2581 /* See declaration. */
2584 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2585 const dwarf2_debug_sections &names)
2587 flagword aflag = bfd_get_section_flags (abfd, sectp);
2589 if ((aflag & SEC_HAS_CONTENTS) == 0)
2592 else if (section_is_p (sectp->name, &names.info))
2594 this->info.s.section = sectp;
2595 this->info.size = bfd_get_section_size (sectp);
2597 else if (section_is_p (sectp->name, &names.abbrev))
2599 this->abbrev.s.section = sectp;
2600 this->abbrev.size = bfd_get_section_size (sectp);
2602 else if (section_is_p (sectp->name, &names.line))
2604 this->line.s.section = sectp;
2605 this->line.size = bfd_get_section_size (sectp);
2607 else if (section_is_p (sectp->name, &names.loc))
2609 this->loc.s.section = sectp;
2610 this->loc.size = bfd_get_section_size (sectp);
2612 else if (section_is_p (sectp->name, &names.loclists))
2614 this->loclists.s.section = sectp;
2615 this->loclists.size = bfd_get_section_size (sectp);
2617 else if (section_is_p (sectp->name, &names.macinfo))
2619 this->macinfo.s.section = sectp;
2620 this->macinfo.size = bfd_get_section_size (sectp);
2622 else if (section_is_p (sectp->name, &names.macro))
2624 this->macro.s.section = sectp;
2625 this->macro.size = bfd_get_section_size (sectp);
2627 else if (section_is_p (sectp->name, &names.str))
2629 this->str.s.section = sectp;
2630 this->str.size = bfd_get_section_size (sectp);
2632 else if (section_is_p (sectp->name, &names.line_str))
2634 this->line_str.s.section = sectp;
2635 this->line_str.size = bfd_get_section_size (sectp);
2637 else if (section_is_p (sectp->name, &names.addr))
2639 this->addr.s.section = sectp;
2640 this->addr.size = bfd_get_section_size (sectp);
2642 else if (section_is_p (sectp->name, &names.frame))
2644 this->frame.s.section = sectp;
2645 this->frame.size = bfd_get_section_size (sectp);
2647 else if (section_is_p (sectp->name, &names.eh_frame))
2649 this->eh_frame.s.section = sectp;
2650 this->eh_frame.size = bfd_get_section_size (sectp);
2652 else if (section_is_p (sectp->name, &names.ranges))
2654 this->ranges.s.section = sectp;
2655 this->ranges.size = bfd_get_section_size (sectp);
2657 else if (section_is_p (sectp->name, &names.rnglists))
2659 this->rnglists.s.section = sectp;
2660 this->rnglists.size = bfd_get_section_size (sectp);
2662 else if (section_is_p (sectp->name, &names.types))
2664 struct dwarf2_section_info type_section;
2666 memset (&type_section, 0, sizeof (type_section));
2667 type_section.s.section = sectp;
2668 type_section.size = bfd_get_section_size (sectp);
2670 VEC_safe_push (dwarf2_section_info_def, this->types,
2673 else if (section_is_p (sectp->name, &names.gdb_index))
2675 this->gdb_index.s.section = sectp;
2676 this->gdb_index.size = bfd_get_section_size (sectp);
2678 else if (section_is_p (sectp->name, &names.debug_names))
2680 this->debug_names.s.section = sectp;
2681 this->debug_names.size = bfd_get_section_size (sectp);
2683 else if (section_is_p (sectp->name, &names.debug_aranges))
2685 this->debug_aranges.s.section = sectp;
2686 this->debug_aranges.size = bfd_get_section_size (sectp);
2689 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2690 && bfd_section_vma (abfd, sectp) == 0)
2691 this->has_section_at_zero = true;
2694 /* A helper function that decides whether a section is empty,
2698 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2700 if (section->is_virtual)
2701 return section->size == 0;
2702 return section->s.section == NULL || section->size == 0;
2705 /* Read the contents of the section INFO.
2706 OBJFILE is the main object file, but not necessarily the file where
2707 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2709 If the section is compressed, uncompress it before returning. */
2712 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2716 gdb_byte *buf, *retbuf;
2720 info->buffer = NULL;
2723 if (dwarf2_section_empty_p (info))
2726 sectp = get_section_bfd_section (info);
2728 /* If this is a virtual section we need to read in the real one first. */
2729 if (info->is_virtual)
2731 struct dwarf2_section_info *containing_section =
2732 get_containing_section (info);
2734 gdb_assert (sectp != NULL);
2735 if ((sectp->flags & SEC_RELOC) != 0)
2737 error (_("Dwarf Error: DWP format V2 with relocations is not"
2738 " supported in section %s [in module %s]"),
2739 get_section_name (info), get_section_file_name (info));
2741 dwarf2_read_section (objfile, containing_section);
2742 /* Other code should have already caught virtual sections that don't
2744 gdb_assert (info->virtual_offset + info->size
2745 <= containing_section->size);
2746 /* If the real section is empty or there was a problem reading the
2747 section we shouldn't get here. */
2748 gdb_assert (containing_section->buffer != NULL);
2749 info->buffer = containing_section->buffer + info->virtual_offset;
2753 /* If the section has relocations, we must read it ourselves.
2754 Otherwise we attach it to the BFD. */
2755 if ((sectp->flags & SEC_RELOC) == 0)
2757 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2761 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2764 /* When debugging .o files, we may need to apply relocations; see
2765 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2766 We never compress sections in .o files, so we only need to
2767 try this when the section is not compressed. */
2768 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2771 info->buffer = retbuf;
2775 abfd = get_section_bfd_owner (info);
2776 gdb_assert (abfd != NULL);
2778 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2779 || bfd_bread (buf, info->size, abfd) != info->size)
2781 error (_("Dwarf Error: Can't read DWARF data"
2782 " in section %s [in module %s]"),
2783 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2787 /* A helper function that returns the size of a section in a safe way.
2788 If you are positive that the section has been read before using the
2789 size, then it is safe to refer to the dwarf2_section_info object's
2790 "size" field directly. In other cases, you must call this
2791 function, because for compressed sections the size field is not set
2792 correctly until the section has been read. */
2794 static bfd_size_type
2795 dwarf2_section_size (struct objfile *objfile,
2796 struct dwarf2_section_info *info)
2799 dwarf2_read_section (objfile, info);
2803 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2807 dwarf2_get_section_info (struct objfile *objfile,
2808 enum dwarf2_section_enum sect,
2809 asection **sectp, const gdb_byte **bufp,
2810 bfd_size_type *sizep)
2812 struct dwarf2_per_objfile *data
2813 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2814 dwarf2_objfile_data_key);
2815 struct dwarf2_section_info *info;
2817 /* We may see an objfile without any DWARF, in which case we just
2828 case DWARF2_DEBUG_FRAME:
2829 info = &data->frame;
2831 case DWARF2_EH_FRAME:
2832 info = &data->eh_frame;
2835 gdb_assert_not_reached ("unexpected section");
2838 dwarf2_read_section (objfile, info);
2840 *sectp = get_section_bfd_section (info);
2841 *bufp = info->buffer;
2842 *sizep = info->size;
2845 /* A helper function to find the sections for a .dwz file. */
2848 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2850 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2852 /* Note that we only support the standard ELF names, because .dwz
2853 is ELF-only (at the time of writing). */
2854 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2856 dwz_file->abbrev.s.section = sectp;
2857 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2859 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2861 dwz_file->info.s.section = sectp;
2862 dwz_file->info.size = bfd_get_section_size (sectp);
2864 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2866 dwz_file->str.s.section = sectp;
2867 dwz_file->str.size = bfd_get_section_size (sectp);
2869 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2871 dwz_file->line.s.section = sectp;
2872 dwz_file->line.size = bfd_get_section_size (sectp);
2874 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2876 dwz_file->macro.s.section = sectp;
2877 dwz_file->macro.size = bfd_get_section_size (sectp);
2879 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2881 dwz_file->gdb_index.s.section = sectp;
2882 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2884 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2886 dwz_file->debug_names.s.section = sectp;
2887 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2891 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2892 there is no .gnu_debugaltlink section in the file. Error if there
2893 is such a section but the file cannot be found. */
2895 static struct dwz_file *
2896 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2898 const char *filename;
2899 struct dwz_file *result;
2900 bfd_size_type buildid_len_arg;
2904 if (dwarf2_per_objfile->dwz_file != NULL)
2905 return dwarf2_per_objfile->dwz_file;
2907 bfd_set_error (bfd_error_no_error);
2908 gdb::unique_xmalloc_ptr<char> data
2909 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2910 &buildid_len_arg, &buildid));
2913 if (bfd_get_error () == bfd_error_no_error)
2915 error (_("could not read '.gnu_debugaltlink' section: %s"),
2916 bfd_errmsg (bfd_get_error ()));
2919 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2921 buildid_len = (size_t) buildid_len_arg;
2923 filename = data.get ();
2925 std::string abs_storage;
2926 if (!IS_ABSOLUTE_PATH (filename))
2928 gdb::unique_xmalloc_ptr<char> abs
2929 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2931 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2932 filename = abs_storage.c_str ();
2935 /* First try the file name given in the section. If that doesn't
2936 work, try to use the build-id instead. */
2937 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2938 if (dwz_bfd != NULL)
2940 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2944 if (dwz_bfd == NULL)
2945 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2947 if (dwz_bfd == NULL)
2948 error (_("could not find '.gnu_debugaltlink' file for %s"),
2949 objfile_name (dwarf2_per_objfile->objfile));
2951 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2953 result->dwz_bfd = dwz_bfd.release ();
2955 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
2957 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
2958 dwarf2_per_objfile->dwz_file = result;
2962 /* DWARF quick_symbols_functions support. */
2964 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2965 unique line tables, so we maintain a separate table of all .debug_line
2966 derived entries to support the sharing.
2967 All the quick functions need is the list of file names. We discard the
2968 line_header when we're done and don't need to record it here. */
2969 struct quick_file_names
2971 /* The data used to construct the hash key. */
2972 struct stmt_list_hash hash;
2974 /* The number of entries in file_names, real_names. */
2975 unsigned int num_file_names;
2977 /* The file names from the line table, after being run through
2979 const char **file_names;
2981 /* The file names from the line table after being run through
2982 gdb_realpath. These are computed lazily. */
2983 const char **real_names;
2986 /* When using the index (and thus not using psymtabs), each CU has an
2987 object of this type. This is used to hold information needed by
2988 the various "quick" methods. */
2989 struct dwarf2_per_cu_quick_data
2991 /* The file table. This can be NULL if there was no file table
2992 or it's currently not read in.
2993 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2994 struct quick_file_names *file_names;
2996 /* The corresponding symbol table. This is NULL if symbols for this
2997 CU have not yet been read. */
2998 struct compunit_symtab *compunit_symtab;
3000 /* A temporary mark bit used when iterating over all CUs in
3001 expand_symtabs_matching. */
3002 unsigned int mark : 1;
3004 /* True if we've tried to read the file table and found there isn't one.
3005 There will be no point in trying to read it again next time. */
3006 unsigned int no_file_data : 1;
3009 /* Utility hash function for a stmt_list_hash. */
3012 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
3016 if (stmt_list_hash->dwo_unit != NULL)
3017 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
3018 v += to_underlying (stmt_list_hash->line_sect_off);
3022 /* Utility equality function for a stmt_list_hash. */
3025 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
3026 const struct stmt_list_hash *rhs)
3028 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
3030 if (lhs->dwo_unit != NULL
3031 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
3034 return lhs->line_sect_off == rhs->line_sect_off;
3037 /* Hash function for a quick_file_names. */
3040 hash_file_name_entry (const void *e)
3042 const struct quick_file_names *file_data
3043 = (const struct quick_file_names *) e;
3045 return hash_stmt_list_entry (&file_data->hash);
3048 /* Equality function for a quick_file_names. */
3051 eq_file_name_entry (const void *a, const void *b)
3053 const struct quick_file_names *ea = (const struct quick_file_names *) a;
3054 const struct quick_file_names *eb = (const struct quick_file_names *) b;
3056 return eq_stmt_list_entry (&ea->hash, &eb->hash);
3059 /* Delete function for a quick_file_names. */
3062 delete_file_name_entry (void *e)
3064 struct quick_file_names *file_data = (struct quick_file_names *) e;
3067 for (i = 0; i < file_data->num_file_names; ++i)
3069 xfree ((void*) file_data->file_names[i]);
3070 if (file_data->real_names)
3071 xfree ((void*) file_data->real_names[i]);
3074 /* The space for the struct itself lives on objfile_obstack,
3075 so we don't free it here. */
3078 /* Create a quick_file_names hash table. */
3081 create_quick_file_names_table (unsigned int nr_initial_entries)
3083 return htab_create_alloc (nr_initial_entries,
3084 hash_file_name_entry, eq_file_name_entry,
3085 delete_file_name_entry, xcalloc, xfree);
3088 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
3089 have to be created afterwards. You should call age_cached_comp_units after
3090 processing PER_CU->CU. dw2_setup must have been already called. */
3093 load_cu (struct dwarf2_per_cu_data *per_cu)
3095 if (per_cu->is_debug_types)
3096 load_full_type_unit (per_cu);
3098 load_full_comp_unit (per_cu, language_minimal);
3100 if (per_cu->cu == NULL)
3101 return; /* Dummy CU. */
3103 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
3106 /* Read in the symbols for PER_CU. */
3109 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3111 struct cleanup *back_to;
3112 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3114 /* Skip type_unit_groups, reading the type units they contain
3115 is handled elsewhere. */
3116 if (IS_TYPE_UNIT_GROUP (per_cu))
3119 back_to = make_cleanup (dwarf2_release_queue, NULL);
3121 if (dwarf2_per_objfile->using_index
3122 ? per_cu->v.quick->compunit_symtab == NULL
3123 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
3125 queue_comp_unit (per_cu, language_minimal);
3128 /* If we just loaded a CU from a DWO, and we're working with an index
3129 that may badly handle TUs, load all the TUs in that DWO as well.
3130 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
3131 if (!per_cu->is_debug_types
3132 && per_cu->cu != NULL
3133 && per_cu->cu->dwo_unit != NULL
3134 && dwarf2_per_objfile->index_table != NULL
3135 && dwarf2_per_objfile->index_table->version <= 7
3136 /* DWP files aren't supported yet. */
3137 && get_dwp_file (dwarf2_per_objfile) == NULL)
3138 queue_and_load_all_dwo_tus (per_cu);
3141 process_queue (dwarf2_per_objfile);
3143 /* Age the cache, releasing compilation units that have not
3144 been used recently. */
3145 age_cached_comp_units (dwarf2_per_objfile);
3147 do_cleanups (back_to);
3150 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
3151 the objfile from which this CU came. Returns the resulting symbol
3154 static struct compunit_symtab *
3155 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3157 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3159 gdb_assert (dwarf2_per_objfile->using_index);
3160 if (!per_cu->v.quick->compunit_symtab)
3162 struct cleanup *back_to = make_cleanup (free_cached_comp_units,
3163 dwarf2_per_objfile);
3164 scoped_restore decrementer = increment_reading_symtab ();
3165 dw2_do_instantiate_symtab (per_cu);
3166 process_cu_includes (dwarf2_per_objfile);
3167 do_cleanups (back_to);
3170 return per_cu->v.quick->compunit_symtab;
3173 /* Return the CU/TU given its index.
3175 This is intended for loops like:
3177 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3178 + dwarf2_per_objfile->n_type_units); ++i)
3180 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3186 static struct dwarf2_per_cu_data *
3187 dw2_get_cutu (struct dwarf2_per_objfile *dwarf2_per_objfile,
3190 if (index >= dwarf2_per_objfile->n_comp_units)
3192 index -= dwarf2_per_objfile->n_comp_units;
3193 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3194 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
3197 return dwarf2_per_objfile->all_comp_units[index];
3200 /* Return the CU given its index.
3201 This differs from dw2_get_cutu in that it's for when you know INDEX
3204 static struct dwarf2_per_cu_data *
3205 dw2_get_cu (struct dwarf2_per_objfile *dwarf2_per_objfile, int index)
3207 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
3209 return dwarf2_per_objfile->all_comp_units[index];
3212 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3213 objfile_obstack, and constructed with the specified field
3216 static dwarf2_per_cu_data *
3217 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3218 struct dwarf2_section_info *section,
3220 sect_offset sect_off, ULONGEST length)
3222 struct objfile *objfile = dwarf2_per_objfile->objfile;
3223 dwarf2_per_cu_data *the_cu
3224 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3225 struct dwarf2_per_cu_data);
3226 the_cu->sect_off = sect_off;
3227 the_cu->length = length;
3228 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
3229 the_cu->section = section;
3230 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3231 struct dwarf2_per_cu_quick_data);
3232 the_cu->is_dwz = is_dwz;
3236 /* A helper for create_cus_from_index that handles a given list of
3240 create_cus_from_index_list (struct objfile *objfile,
3241 const gdb_byte *cu_list, offset_type n_elements,
3242 struct dwarf2_section_info *section,
3247 struct dwarf2_per_objfile *dwarf2_per_objfile
3248 = get_dwarf2_per_objfile (objfile);
3250 for (i = 0; i < n_elements; i += 2)
3252 gdb_static_assert (sizeof (ULONGEST) >= 8);
3254 sect_offset sect_off
3255 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3256 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3259 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
3260 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3265 /* Read the CU list from the mapped index, and use it to create all
3266 the CU objects for this objfile. */
3269 create_cus_from_index (struct objfile *objfile,
3270 const gdb_byte *cu_list, offset_type cu_list_elements,
3271 const gdb_byte *dwz_list, offset_type dwz_elements)
3273 struct dwz_file *dwz;
3274 struct dwarf2_per_objfile *dwarf2_per_objfile
3275 = get_dwarf2_per_objfile (objfile);
3277 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
3278 dwarf2_per_objfile->all_comp_units =
3279 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3280 dwarf2_per_objfile->n_comp_units);
3282 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3283 &dwarf2_per_objfile->info, 0, 0);
3285 if (dwz_elements == 0)
3288 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3289 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3290 cu_list_elements / 2);
3293 /* Create the signatured type hash table from the index. */
3296 create_signatured_type_table_from_index (struct objfile *objfile,
3297 struct dwarf2_section_info *section,
3298 const gdb_byte *bytes,
3299 offset_type elements)
3302 htab_t sig_types_hash;
3303 struct dwarf2_per_objfile *dwarf2_per_objfile
3304 = get_dwarf2_per_objfile (objfile);
3306 dwarf2_per_objfile->n_type_units
3307 = dwarf2_per_objfile->n_allocated_type_units
3309 dwarf2_per_objfile->all_type_units =
3310 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3312 sig_types_hash = allocate_signatured_type_table (objfile);
3314 for (i = 0; i < elements; i += 3)
3316 struct signatured_type *sig_type;
3319 cu_offset type_offset_in_tu;
3321 gdb_static_assert (sizeof (ULONGEST) >= 8);
3322 sect_offset sect_off
3323 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3325 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3327 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3330 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3331 struct signatured_type);
3332 sig_type->signature = signature;
3333 sig_type->type_offset_in_tu = type_offset_in_tu;
3334 sig_type->per_cu.is_debug_types = 1;
3335 sig_type->per_cu.section = section;
3336 sig_type->per_cu.sect_off = sect_off;
3337 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3338 sig_type->per_cu.v.quick
3339 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3340 struct dwarf2_per_cu_quick_data);
3342 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3345 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3348 dwarf2_per_objfile->signatured_types = sig_types_hash;
3351 /* Create the signatured type hash table from .debug_names. */
3354 create_signatured_type_table_from_debug_names
3355 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3356 const mapped_debug_names &map,
3357 struct dwarf2_section_info *section,
3358 struct dwarf2_section_info *abbrev_section)
3360 struct objfile *objfile = dwarf2_per_objfile->objfile;
3362 dwarf2_read_section (objfile, section);
3363 dwarf2_read_section (objfile, abbrev_section);
3365 dwarf2_per_objfile->n_type_units
3366 = dwarf2_per_objfile->n_allocated_type_units
3368 dwarf2_per_objfile->all_type_units
3369 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3371 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3373 for (uint32_t i = 0; i < map.tu_count; ++i)
3375 struct signatured_type *sig_type;
3378 cu_offset type_offset_in_tu;
3380 sect_offset sect_off
3381 = (sect_offset) (extract_unsigned_integer
3382 (map.tu_table_reordered + i * map.offset_size,
3384 map.dwarf5_byte_order));
3386 comp_unit_head cu_header;
3387 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3389 section->buffer + to_underlying (sect_off),
3392 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3393 struct signatured_type);
3394 sig_type->signature = cu_header.signature;
3395 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3396 sig_type->per_cu.is_debug_types = 1;
3397 sig_type->per_cu.section = section;
3398 sig_type->per_cu.sect_off = sect_off;
3399 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3400 sig_type->per_cu.v.quick
3401 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3402 struct dwarf2_per_cu_quick_data);
3404 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3407 dwarf2_per_objfile->all_type_units[i] = sig_type;
3410 dwarf2_per_objfile->signatured_types = sig_types_hash;
3413 /* Read the address map data from the mapped index, and use it to
3414 populate the objfile's psymtabs_addrmap. */
3417 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3418 struct mapped_index *index)
3420 struct objfile *objfile = dwarf2_per_objfile->objfile;
3421 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3422 const gdb_byte *iter, *end;
3423 struct addrmap *mutable_map;
3426 auto_obstack temp_obstack;
3428 mutable_map = addrmap_create_mutable (&temp_obstack);
3430 iter = index->address_table.data ();
3431 end = iter + index->address_table.size ();
3433 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3437 ULONGEST hi, lo, cu_index;
3438 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3440 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3442 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3447 complaint (&symfile_complaints,
3448 _(".gdb_index address table has invalid range (%s - %s)"),
3449 hex_string (lo), hex_string (hi));
3453 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3455 complaint (&symfile_complaints,
3456 _(".gdb_index address table has invalid CU number %u"),
3457 (unsigned) cu_index);
3461 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3462 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3463 addrmap_set_empty (mutable_map, lo, hi - 1,
3464 dw2_get_cutu (dwarf2_per_objfile, cu_index));
3467 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3468 &objfile->objfile_obstack);
3471 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3472 populate the objfile's psymtabs_addrmap. */
3475 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3476 struct dwarf2_section_info *section)
3478 struct objfile *objfile = dwarf2_per_objfile->objfile;
3479 bfd *abfd = objfile->obfd;
3480 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3481 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3482 SECT_OFF_TEXT (objfile));
3484 auto_obstack temp_obstack;
3485 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3487 std::unordered_map<sect_offset,
3488 dwarf2_per_cu_data *,
3489 gdb::hash_enum<sect_offset>>
3490 debug_info_offset_to_per_cu;
3491 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3493 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, cui);
3494 const auto insertpair
3495 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3496 if (!insertpair.second)
3498 warning (_("Section .debug_aranges in %s has duplicate "
3499 "debug_info_offset %u, ignoring .debug_aranges."),
3500 objfile_name (objfile), to_underlying (per_cu->sect_off));
3505 dwarf2_read_section (objfile, section);
3507 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3509 const gdb_byte *addr = section->buffer;
3511 while (addr < section->buffer + section->size)
3513 const gdb_byte *const entry_addr = addr;
3514 unsigned int bytes_read;
3516 const LONGEST entry_length = read_initial_length (abfd, addr,
3520 const gdb_byte *const entry_end = addr + entry_length;
3521 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3522 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3523 if (addr + entry_length > section->buffer + section->size)
3525 warning (_("Section .debug_aranges in %s entry at offset %zu "
3526 "length %s exceeds section length %s, "
3527 "ignoring .debug_aranges."),
3528 objfile_name (objfile), entry_addr - section->buffer,
3529 plongest (bytes_read + entry_length),
3530 pulongest (section->size));
3534 /* The version number. */
3535 const uint16_t version = read_2_bytes (abfd, addr);
3539 warning (_("Section .debug_aranges in %s entry at offset %zu "
3540 "has unsupported version %d, ignoring .debug_aranges."),
3541 objfile_name (objfile), entry_addr - section->buffer,
3546 const uint64_t debug_info_offset
3547 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3548 addr += offset_size;
3549 const auto per_cu_it
3550 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3551 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3553 warning (_("Section .debug_aranges in %s entry at offset %zu "
3554 "debug_info_offset %s does not exists, "
3555 "ignoring .debug_aranges."),
3556 objfile_name (objfile), entry_addr - section->buffer,
3557 pulongest (debug_info_offset));
3560 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3562 const uint8_t address_size = *addr++;
3563 if (address_size < 1 || address_size > 8)
3565 warning (_("Section .debug_aranges in %s entry at offset %zu "
3566 "address_size %u is invalid, ignoring .debug_aranges."),
3567 objfile_name (objfile), entry_addr - section->buffer,
3572 const uint8_t segment_selector_size = *addr++;
3573 if (segment_selector_size != 0)
3575 warning (_("Section .debug_aranges in %s entry at offset %zu "
3576 "segment_selector_size %u is not supported, "
3577 "ignoring .debug_aranges."),
3578 objfile_name (objfile), entry_addr - section->buffer,
3579 segment_selector_size);
3583 /* Must pad to an alignment boundary that is twice the address
3584 size. It is undocumented by the DWARF standard but GCC does
3586 for (size_t padding = ((-(addr - section->buffer))
3587 & (2 * address_size - 1));
3588 padding > 0; padding--)
3591 warning (_("Section .debug_aranges in %s entry at offset %zu "
3592 "padding is not zero, ignoring .debug_aranges."),
3593 objfile_name (objfile), entry_addr - section->buffer);
3599 if (addr + 2 * address_size > entry_end)
3601 warning (_("Section .debug_aranges in %s entry at offset %zu "
3602 "address list is not properly terminated, "
3603 "ignoring .debug_aranges."),
3604 objfile_name (objfile), entry_addr - section->buffer);
3607 ULONGEST start = extract_unsigned_integer (addr, address_size,
3609 addr += address_size;
3610 ULONGEST length = extract_unsigned_integer (addr, address_size,
3612 addr += address_size;
3613 if (start == 0 && length == 0)
3615 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3617 /* Symbol was eliminated due to a COMDAT group. */
3620 ULONGEST end = start + length;
3621 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3622 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3623 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3627 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3628 &objfile->objfile_obstack);
3631 /* The hash function for strings in the mapped index. This is the same as
3632 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3633 implementation. This is necessary because the hash function is tied to the
3634 format of the mapped index file. The hash values do not have to match with
3637 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3640 mapped_index_string_hash (int index_version, const void *p)
3642 const unsigned char *str = (const unsigned char *) p;
3646 while ((c = *str++) != 0)
3648 if (index_version >= 5)
3650 r = r * 67 + c - 113;
3656 /* Find a slot in the mapped index INDEX for the object named NAME.
3657 If NAME is found, set *VEC_OUT to point to the CU vector in the
3658 constant pool and return true. If NAME cannot be found, return
3662 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3663 offset_type **vec_out)
3666 offset_type slot, step;
3667 int (*cmp) (const char *, const char *);
3669 gdb::unique_xmalloc_ptr<char> without_params;
3670 if (current_language->la_language == language_cplus
3671 || current_language->la_language == language_fortran
3672 || current_language->la_language == language_d)
3674 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3677 if (strchr (name, '(') != NULL)
3679 without_params = cp_remove_params (name);
3681 if (without_params != NULL)
3682 name = without_params.get ();
3686 /* Index version 4 did not support case insensitive searches. But the
3687 indices for case insensitive languages are built in lowercase, therefore
3688 simulate our NAME being searched is also lowercased. */
3689 hash = mapped_index_string_hash ((index->version == 4
3690 && case_sensitivity == case_sensitive_off
3691 ? 5 : index->version),
3694 slot = hash & (index->symbol_table.size () - 1);
3695 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3696 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3702 const auto &bucket = index->symbol_table[slot];
3703 if (bucket.name == 0 && bucket.vec == 0)
3706 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3707 if (!cmp (name, str))
3709 *vec_out = (offset_type *) (index->constant_pool
3710 + MAYBE_SWAP (bucket.vec));
3714 slot = (slot + step) & (index->symbol_table.size () - 1);
3718 /* A helper function that reads the .gdb_index from SECTION and fills
3719 in MAP. FILENAME is the name of the file containing the section;
3720 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3721 ok to use deprecated sections.
3723 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3724 out parameters that are filled in with information about the CU and
3725 TU lists in the section.
3727 Returns 1 if all went well, 0 otherwise. */
3730 read_index_from_section (struct objfile *objfile,
3731 const char *filename,
3733 struct dwarf2_section_info *section,
3734 struct mapped_index *map,
3735 const gdb_byte **cu_list,
3736 offset_type *cu_list_elements,
3737 const gdb_byte **types_list,
3738 offset_type *types_list_elements)
3740 const gdb_byte *addr;
3741 offset_type version;
3742 offset_type *metadata;
3745 if (dwarf2_section_empty_p (section))
3748 /* Older elfutils strip versions could keep the section in the main
3749 executable while splitting it for the separate debug info file. */
3750 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3753 dwarf2_read_section (objfile, section);
3755 addr = section->buffer;
3756 /* Version check. */
3757 version = MAYBE_SWAP (*(offset_type *) addr);
3758 /* Versions earlier than 3 emitted every copy of a psymbol. This
3759 causes the index to behave very poorly for certain requests. Version 3
3760 contained incomplete addrmap. So, it seems better to just ignore such
3764 static int warning_printed = 0;
3765 if (!warning_printed)
3767 warning (_("Skipping obsolete .gdb_index section in %s."),
3769 warning_printed = 1;
3773 /* Index version 4 uses a different hash function than index version
3776 Versions earlier than 6 did not emit psymbols for inlined
3777 functions. Using these files will cause GDB not to be able to
3778 set breakpoints on inlined functions by name, so we ignore these
3779 indices unless the user has done
3780 "set use-deprecated-index-sections on". */
3781 if (version < 6 && !deprecated_ok)
3783 static int warning_printed = 0;
3784 if (!warning_printed)
3787 Skipping deprecated .gdb_index section in %s.\n\
3788 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3789 to use the section anyway."),
3791 warning_printed = 1;
3795 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3796 of the TU (for symbols coming from TUs),
3797 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3798 Plus gold-generated indices can have duplicate entries for global symbols,
3799 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3800 These are just performance bugs, and we can't distinguish gdb-generated
3801 indices from gold-generated ones, so issue no warning here. */
3803 /* Indexes with higher version than the one supported by GDB may be no
3804 longer backward compatible. */
3808 map->version = version;
3809 map->total_size = section->size;
3811 metadata = (offset_type *) (addr + sizeof (offset_type));
3814 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3815 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3819 *types_list = addr + MAYBE_SWAP (metadata[i]);
3820 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3821 - MAYBE_SWAP (metadata[i]))
3825 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3826 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3828 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3831 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3832 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3834 = gdb::array_view<mapped_index::symbol_table_slot>
3835 ((mapped_index::symbol_table_slot *) symbol_table,
3836 (mapped_index::symbol_table_slot *) symbol_table_end);
3839 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3844 /* Read .gdb_index. If everything went ok, initialize the "quick"
3845 elements of all the CUs and return 1. Otherwise, return 0. */
3848 dwarf2_read_index (struct objfile *objfile)
3850 struct mapped_index local_map, *map;
3851 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3852 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3853 struct dwz_file *dwz;
3854 struct dwarf2_per_objfile *dwarf2_per_objfile
3855 = get_dwarf2_per_objfile (objfile);
3857 if (!read_index_from_section (objfile, objfile_name (objfile),
3858 use_deprecated_index_sections,
3859 &dwarf2_per_objfile->gdb_index, &local_map,
3860 &cu_list, &cu_list_elements,
3861 &types_list, &types_list_elements))
3864 /* Don't use the index if it's empty. */
3865 if (local_map.symbol_table.empty ())
3868 /* If there is a .dwz file, read it so we can get its CU list as
3870 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3873 struct mapped_index dwz_map;
3874 const gdb_byte *dwz_types_ignore;
3875 offset_type dwz_types_elements_ignore;
3877 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3879 &dwz->gdb_index, &dwz_map,
3880 &dwz_list, &dwz_list_elements,
3882 &dwz_types_elements_ignore))
3884 warning (_("could not read '.gdb_index' section from %s; skipping"),
3885 bfd_get_filename (dwz->dwz_bfd));
3890 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3893 if (types_list_elements)
3895 struct dwarf2_section_info *section;
3897 /* We can only handle a single .debug_types when we have an
3899 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3902 section = VEC_index (dwarf2_section_info_def,
3903 dwarf2_per_objfile->types, 0);
3905 create_signatured_type_table_from_index (objfile, section, types_list,
3906 types_list_elements);
3909 create_addrmap_from_index (dwarf2_per_objfile, &local_map);
3911 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3912 map = new (map) mapped_index ();
3915 dwarf2_per_objfile->index_table = map;
3916 dwarf2_per_objfile->using_index = 1;
3917 dwarf2_per_objfile->quick_file_names_table =
3918 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3923 /* die_reader_func for dw2_get_file_names. */
3926 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3927 const gdb_byte *info_ptr,
3928 struct die_info *comp_unit_die,
3932 struct dwarf2_cu *cu = reader->cu;
3933 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3934 struct dwarf2_per_objfile *dwarf2_per_objfile
3935 = cu->per_cu->dwarf2_per_objfile;
3936 struct objfile *objfile = dwarf2_per_objfile->objfile;
3937 struct dwarf2_per_cu_data *lh_cu;
3938 struct attribute *attr;
3941 struct quick_file_names *qfn;
3943 gdb_assert (! this_cu->is_debug_types);
3945 /* Our callers never want to match partial units -- instead they
3946 will match the enclosing full CU. */
3947 if (comp_unit_die->tag == DW_TAG_partial_unit)
3949 this_cu->v.quick->no_file_data = 1;
3957 sect_offset line_offset {};
3959 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3962 struct quick_file_names find_entry;
3964 line_offset = (sect_offset) DW_UNSND (attr);
3966 /* We may have already read in this line header (TU line header sharing).
3967 If we have we're done. */
3968 find_entry.hash.dwo_unit = cu->dwo_unit;
3969 find_entry.hash.line_sect_off = line_offset;
3970 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3971 &find_entry, INSERT);
3974 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3978 lh = dwarf_decode_line_header (line_offset, cu);
3982 lh_cu->v.quick->no_file_data = 1;
3986 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3987 qfn->hash.dwo_unit = cu->dwo_unit;
3988 qfn->hash.line_sect_off = line_offset;
3989 gdb_assert (slot != NULL);
3992 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3994 qfn->num_file_names = lh->file_names.size ();
3996 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3997 for (i = 0; i < lh->file_names.size (); ++i)
3998 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3999 qfn->real_names = NULL;
4001 lh_cu->v.quick->file_names = qfn;
4004 /* A helper for the "quick" functions which attempts to read the line
4005 table for THIS_CU. */
4007 static struct quick_file_names *
4008 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
4010 /* This should never be called for TUs. */
4011 gdb_assert (! this_cu->is_debug_types);
4012 /* Nor type unit groups. */
4013 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
4015 if (this_cu->v.quick->file_names != NULL)
4016 return this_cu->v.quick->file_names;
4017 /* If we know there is no line data, no point in looking again. */
4018 if (this_cu->v.quick->no_file_data)
4021 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
4023 if (this_cu->v.quick->no_file_data)
4025 return this_cu->v.quick->file_names;
4028 /* A helper for the "quick" functions which computes and caches the
4029 real path for a given file name from the line table. */
4032 dw2_get_real_path (struct objfile *objfile,
4033 struct quick_file_names *qfn, int index)
4035 if (qfn->real_names == NULL)
4036 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
4037 qfn->num_file_names, const char *);
4039 if (qfn->real_names[index] == NULL)
4040 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
4042 return qfn->real_names[index];
4045 static struct symtab *
4046 dw2_find_last_source_symtab (struct objfile *objfile)
4048 struct dwarf2_per_objfile *dwarf2_per_objfile
4049 = get_dwarf2_per_objfile (objfile);
4050 int index = dwarf2_per_objfile->n_comp_units - 1;
4051 dwarf2_per_cu_data *dwarf_cu = dw2_get_cutu (dwarf2_per_objfile, index);
4052 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu);
4057 return compunit_primary_filetab (cust);
4060 /* Traversal function for dw2_forget_cached_source_info. */
4063 dw2_free_cached_file_names (void **slot, void *info)
4065 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
4067 if (file_data->real_names)
4071 for (i = 0; i < file_data->num_file_names; ++i)
4073 xfree ((void*) file_data->real_names[i]);
4074 file_data->real_names[i] = NULL;
4082 dw2_forget_cached_source_info (struct objfile *objfile)
4084 struct dwarf2_per_objfile *dwarf2_per_objfile
4085 = get_dwarf2_per_objfile (objfile);
4087 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
4088 dw2_free_cached_file_names, NULL);
4091 /* Helper function for dw2_map_symtabs_matching_filename that expands
4092 the symtabs and calls the iterator. */
4095 dw2_map_expand_apply (struct objfile *objfile,
4096 struct dwarf2_per_cu_data *per_cu,
4097 const char *name, const char *real_path,
4098 gdb::function_view<bool (symtab *)> callback)
4100 struct compunit_symtab *last_made = objfile->compunit_symtabs;
4102 /* Don't visit already-expanded CUs. */
4103 if (per_cu->v.quick->compunit_symtab)
4106 /* This may expand more than one symtab, and we want to iterate over
4108 dw2_instantiate_symtab (per_cu);
4110 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
4111 last_made, callback);
4114 /* Implementation of the map_symtabs_matching_filename method. */
4117 dw2_map_symtabs_matching_filename
4118 (struct objfile *objfile, const char *name, const char *real_path,
4119 gdb::function_view<bool (symtab *)> callback)
4122 const char *name_basename = lbasename (name);
4123 struct dwarf2_per_objfile *dwarf2_per_objfile
4124 = get_dwarf2_per_objfile (objfile);
4126 /* The rule is CUs specify all the files, including those used by
4127 any TU, so there's no need to scan TUs here. */
4129 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4132 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
4133 struct quick_file_names *file_data;
4135 /* We only need to look at symtabs not already expanded. */
4136 if (per_cu->v.quick->compunit_symtab)
4139 file_data = dw2_get_file_names (per_cu);
4140 if (file_data == NULL)
4143 for (j = 0; j < file_data->num_file_names; ++j)
4145 const char *this_name = file_data->file_names[j];
4146 const char *this_real_name;
4148 if (compare_filenames_for_search (this_name, name))
4150 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4156 /* Before we invoke realpath, which can get expensive when many
4157 files are involved, do a quick comparison of the basenames. */
4158 if (! basenames_may_differ
4159 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
4162 this_real_name = dw2_get_real_path (objfile, file_data, j);
4163 if (compare_filenames_for_search (this_real_name, name))
4165 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4171 if (real_path != NULL)
4173 gdb_assert (IS_ABSOLUTE_PATH (real_path));
4174 gdb_assert (IS_ABSOLUTE_PATH (name));
4175 if (this_real_name != NULL
4176 && FILENAME_CMP (real_path, this_real_name) == 0)
4178 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4190 /* Struct used to manage iterating over all CUs looking for a symbol. */
4192 struct dw2_symtab_iterator
4194 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
4195 struct dwarf2_per_objfile *dwarf2_per_objfile;
4196 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
4197 int want_specific_block;
4198 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
4199 Unused if !WANT_SPECIFIC_BLOCK. */
4201 /* The kind of symbol we're looking for. */
4203 /* The list of CUs from the index entry of the symbol,
4204 or NULL if not found. */
4206 /* The next element in VEC to look at. */
4208 /* The number of elements in VEC, or zero if there is no match. */
4210 /* Have we seen a global version of the symbol?
4211 If so we can ignore all further global instances.
4212 This is to work around gold/15646, inefficient gold-generated
4217 /* Initialize the index symtab iterator ITER.
4218 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
4219 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
4222 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
4223 struct dwarf2_per_objfile *dwarf2_per_objfile,
4224 int want_specific_block,
4229 iter->dwarf2_per_objfile = dwarf2_per_objfile;
4230 iter->want_specific_block = want_specific_block;
4231 iter->block_index = block_index;
4232 iter->domain = domain;
4234 iter->global_seen = 0;
4236 mapped_index *index = dwarf2_per_objfile->index_table;
4238 /* index is NULL if OBJF_READNOW. */
4239 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
4240 iter->length = MAYBE_SWAP (*iter->vec);
4248 /* Return the next matching CU or NULL if there are no more. */
4250 static struct dwarf2_per_cu_data *
4251 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4253 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
4255 for ( ; iter->next < iter->length; ++iter->next)
4257 offset_type cu_index_and_attrs =
4258 MAYBE_SWAP (iter->vec[iter->next + 1]);
4259 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4260 struct dwarf2_per_cu_data *per_cu;
4261 int want_static = iter->block_index != GLOBAL_BLOCK;
4262 /* This value is only valid for index versions >= 7. */
4263 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4264 gdb_index_symbol_kind symbol_kind =
4265 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4266 /* Only check the symbol attributes if they're present.
4267 Indices prior to version 7 don't record them,
4268 and indices >= 7 may elide them for certain symbols
4269 (gold does this). */
4271 (dwarf2_per_objfile->index_table->version >= 7
4272 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4274 /* Don't crash on bad data. */
4275 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4276 + dwarf2_per_objfile->n_type_units))
4278 complaint (&symfile_complaints,
4279 _(".gdb_index entry has bad CU index"
4281 objfile_name (dwarf2_per_objfile->objfile));
4285 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
4287 /* Skip if already read in. */
4288 if (per_cu->v.quick->compunit_symtab)
4291 /* Check static vs global. */
4294 if (iter->want_specific_block
4295 && want_static != is_static)
4297 /* Work around gold/15646. */
4298 if (!is_static && iter->global_seen)
4301 iter->global_seen = 1;
4304 /* Only check the symbol's kind if it has one. */
4307 switch (iter->domain)
4310 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4311 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4312 /* Some types are also in VAR_DOMAIN. */
4313 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4317 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4321 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4336 static struct compunit_symtab *
4337 dw2_lookup_symbol (struct objfile *objfile, int block_index,
4338 const char *name, domain_enum domain)
4340 struct compunit_symtab *stab_best = NULL;
4341 struct dwarf2_per_objfile *dwarf2_per_objfile
4342 = get_dwarf2_per_objfile (objfile);
4344 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4346 struct dw2_symtab_iterator iter;
4347 struct dwarf2_per_cu_data *per_cu;
4349 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
4351 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4353 struct symbol *sym, *with_opaque = NULL;
4354 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4355 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4356 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4358 sym = block_find_symbol (block, name, domain,
4359 block_find_non_opaque_type_preferred,
4362 /* Some caution must be observed with overloaded functions
4363 and methods, since the index will not contain any overload
4364 information (but NAME might contain it). */
4367 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4369 if (with_opaque != NULL
4370 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4373 /* Keep looking through other CUs. */
4380 dw2_print_stats (struct objfile *objfile)
4382 struct dwarf2_per_objfile *dwarf2_per_objfile
4383 = get_dwarf2_per_objfile (objfile);
4384 int total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4387 for (int i = 0; i < total; ++i)
4389 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4391 if (!per_cu->v.quick->compunit_symtab)
4394 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4395 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4398 /* This dumps minimal information about the index.
4399 It is called via "mt print objfiles".
4400 One use is to verify .gdb_index has been loaded by the
4401 gdb.dwarf2/gdb-index.exp testcase. */
4404 dw2_dump (struct objfile *objfile)
4406 struct dwarf2_per_objfile *dwarf2_per_objfile
4407 = get_dwarf2_per_objfile (objfile);
4409 gdb_assert (dwarf2_per_objfile->using_index);
4410 printf_filtered (".gdb_index:");
4411 if (dwarf2_per_objfile->index_table != NULL)
4413 printf_filtered (" version %d\n",
4414 dwarf2_per_objfile->index_table->version);
4417 printf_filtered (" faked for \"readnow\"\n");
4418 printf_filtered ("\n");
4422 dw2_relocate (struct objfile *objfile,
4423 const struct section_offsets *new_offsets,
4424 const struct section_offsets *delta)
4426 /* There's nothing to relocate here. */
4430 dw2_expand_symtabs_for_function (struct objfile *objfile,
4431 const char *func_name)
4433 struct dwarf2_per_objfile *dwarf2_per_objfile
4434 = get_dwarf2_per_objfile (objfile);
4436 struct dw2_symtab_iterator iter;
4437 struct dwarf2_per_cu_data *per_cu;
4439 /* Note: It doesn't matter what we pass for block_index here. */
4440 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4443 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4444 dw2_instantiate_symtab (per_cu);
4449 dw2_expand_all_symtabs (struct objfile *objfile)
4451 struct dwarf2_per_objfile *dwarf2_per_objfile
4452 = get_dwarf2_per_objfile (objfile);
4453 int total_units = (dwarf2_per_objfile->n_comp_units
4454 + dwarf2_per_objfile->n_type_units);
4456 for (int i = 0; i < total_units; ++i)
4458 struct dwarf2_per_cu_data *per_cu
4459 = dw2_get_cutu (dwarf2_per_objfile, i);
4461 dw2_instantiate_symtab (per_cu);
4466 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4467 const char *fullname)
4469 struct dwarf2_per_objfile *dwarf2_per_objfile
4470 = get_dwarf2_per_objfile (objfile);
4472 /* We don't need to consider type units here.
4473 This is only called for examining code, e.g. expand_line_sal.
4474 There can be an order of magnitude (or more) more type units
4475 than comp units, and we avoid them if we can. */
4477 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4480 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4481 struct quick_file_names *file_data;
4483 /* We only need to look at symtabs not already expanded. */
4484 if (per_cu->v.quick->compunit_symtab)
4487 file_data = dw2_get_file_names (per_cu);
4488 if (file_data == NULL)
4491 for (j = 0; j < file_data->num_file_names; ++j)
4493 const char *this_fullname = file_data->file_names[j];
4495 if (filename_cmp (this_fullname, fullname) == 0)
4497 dw2_instantiate_symtab (per_cu);
4505 dw2_map_matching_symbols (struct objfile *objfile,
4506 const char * name, domain_enum domain,
4508 int (*callback) (struct block *,
4509 struct symbol *, void *),
4510 void *data, symbol_name_match_type match,
4511 symbol_compare_ftype *ordered_compare)
4513 /* Currently unimplemented; used for Ada. The function can be called if the
4514 current language is Ada for a non-Ada objfile using GNU index. As Ada
4515 does not look for non-Ada symbols this function should just return. */
4518 /* Symbol name matcher for .gdb_index names.
4520 Symbol names in .gdb_index have a few particularities:
4522 - There's no indication of which is the language of each symbol.
4524 Since each language has its own symbol name matching algorithm,
4525 and we don't know which language is the right one, we must match
4526 each symbol against all languages. This would be a potential
4527 performance problem if it were not mitigated by the
4528 mapped_index::name_components lookup table, which significantly
4529 reduces the number of times we need to call into this matcher,
4530 making it a non-issue.
4532 - Symbol names in the index have no overload (parameter)
4533 information. I.e., in C++, "foo(int)" and "foo(long)" both
4534 appear as "foo" in the index, for example.
4536 This means that the lookup names passed to the symbol name
4537 matcher functions must have no parameter information either
4538 because (e.g.) symbol search name "foo" does not match
4539 lookup-name "foo(int)" [while swapping search name for lookup
4542 class gdb_index_symbol_name_matcher
4545 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4546 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4548 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4549 Returns true if any matcher matches. */
4550 bool matches (const char *symbol_name);
4553 /* A reference to the lookup name we're matching against. */
4554 const lookup_name_info &m_lookup_name;
4556 /* A vector holding all the different symbol name matchers, for all
4558 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4561 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4562 (const lookup_name_info &lookup_name)
4563 : m_lookup_name (lookup_name)
4565 /* Prepare the vector of comparison functions upfront, to avoid
4566 doing the same work for each symbol. Care is taken to avoid
4567 matching with the same matcher more than once if/when multiple
4568 languages use the same matcher function. */
4569 auto &matchers = m_symbol_name_matcher_funcs;
4570 matchers.reserve (nr_languages);
4572 matchers.push_back (default_symbol_name_matcher);
4574 for (int i = 0; i < nr_languages; i++)
4576 const language_defn *lang = language_def ((enum language) i);
4577 symbol_name_matcher_ftype *name_matcher
4578 = get_symbol_name_matcher (lang, m_lookup_name);
4580 /* Don't insert the same comparison routine more than once.
4581 Note that we do this linear walk instead of a seemingly
4582 cheaper sorted insert, or use a std::set or something like
4583 that, because relative order of function addresses is not
4584 stable. This is not a problem in practice because the number
4585 of supported languages is low, and the cost here is tiny
4586 compared to the number of searches we'll do afterwards using
4588 if (name_matcher != default_symbol_name_matcher
4589 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4590 == matchers.end ()))
4591 matchers.push_back (name_matcher);
4596 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4598 for (auto matches_name : m_symbol_name_matcher_funcs)
4599 if (matches_name (symbol_name, m_lookup_name, NULL))
4605 /* Starting from a search name, return the string that finds the upper
4606 bound of all strings that start with SEARCH_NAME in a sorted name
4607 list. Returns the empty string to indicate that the upper bound is
4608 the end of the list. */
4611 make_sort_after_prefix_name (const char *search_name)
4613 /* When looking to complete "func", we find the upper bound of all
4614 symbols that start with "func" by looking for where we'd insert
4615 the closest string that would follow "func" in lexicographical
4616 order. Usually, that's "func"-with-last-character-incremented,
4617 i.e. "fund". Mind non-ASCII characters, though. Usually those
4618 will be UTF-8 multi-byte sequences, but we can't be certain.
4619 Especially mind the 0xff character, which is a valid character in
4620 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4621 rule out compilers allowing it in identifiers. Note that
4622 conveniently, strcmp/strcasecmp are specified to compare
4623 characters interpreted as unsigned char. So what we do is treat
4624 the whole string as a base 256 number composed of a sequence of
4625 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4626 to 0, and carries 1 to the following more-significant position.
4627 If the very first character in SEARCH_NAME ends up incremented
4628 and carries/overflows, then the upper bound is the end of the
4629 list. The string after the empty string is also the empty
4632 Some examples of this operation:
4634 SEARCH_NAME => "+1" RESULT
4638 "\xff" "a" "\xff" => "\xff" "b"
4643 Then, with these symbols for example:
4649 completing "func" looks for symbols between "func" and
4650 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4651 which finds "func" and "func1", but not "fund".
4655 funcÿ (Latin1 'ÿ' [0xff])
4659 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4660 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4664 ÿÿ (Latin1 'ÿ' [0xff])
4667 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4668 the end of the list.
4670 std::string after = search_name;
4671 while (!after.empty () && (unsigned char) after.back () == 0xff)
4673 if (!after.empty ())
4674 after.back () = (unsigned char) after.back () + 1;
4678 /* See declaration. */
4680 std::pair<std::vector<name_component>::const_iterator,
4681 std::vector<name_component>::const_iterator>
4682 mapped_index_base::find_name_components_bounds
4683 (const lookup_name_info &lookup_name_without_params) const
4686 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4689 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4691 /* Comparison function object for lower_bound that matches against a
4692 given symbol name. */
4693 auto lookup_compare_lower = [&] (const name_component &elem,
4696 const char *elem_qualified = this->symbol_name_at (elem.idx);
4697 const char *elem_name = elem_qualified + elem.name_offset;
4698 return name_cmp (elem_name, name) < 0;
4701 /* Comparison function object for upper_bound that matches against a
4702 given symbol name. */
4703 auto lookup_compare_upper = [&] (const char *name,
4704 const name_component &elem)
4706 const char *elem_qualified = this->symbol_name_at (elem.idx);
4707 const char *elem_name = elem_qualified + elem.name_offset;
4708 return name_cmp (name, elem_name) < 0;
4711 auto begin = this->name_components.begin ();
4712 auto end = this->name_components.end ();
4714 /* Find the lower bound. */
4717 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4720 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4723 /* Find the upper bound. */
4726 if (lookup_name_without_params.completion_mode ())
4728 /* In completion mode, we want UPPER to point past all
4729 symbols names that have the same prefix. I.e., with
4730 these symbols, and completing "func":
4732 function << lower bound
4734 other_function << upper bound
4736 We find the upper bound by looking for the insertion
4737 point of "func"-with-last-character-incremented,
4739 std::string after = make_sort_after_prefix_name (cplus);
4742 return std::lower_bound (lower, end, after.c_str (),
4743 lookup_compare_lower);
4746 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4749 return {lower, upper};
4752 /* See declaration. */
4755 mapped_index_base::build_name_components ()
4757 if (!this->name_components.empty ())
4760 this->name_components_casing = case_sensitivity;
4762 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4764 /* The code below only knows how to break apart components of C++
4765 symbol names (and other languages that use '::' as
4766 namespace/module separator). If we add support for wild matching
4767 to some language that uses some other operator (E.g., Ada, Go and
4768 D use '.'), then we'll need to try splitting the symbol name
4769 according to that language too. Note that Ada does support wild
4770 matching, but doesn't currently support .gdb_index. */
4771 auto count = this->symbol_name_count ();
4772 for (offset_type idx = 0; idx < count; idx++)
4774 if (this->symbol_name_slot_invalid (idx))
4777 const char *name = this->symbol_name_at (idx);
4779 /* Add each name component to the name component table. */
4780 unsigned int previous_len = 0;
4781 for (unsigned int current_len = cp_find_first_component (name);
4782 name[current_len] != '\0';
4783 current_len += cp_find_first_component (name + current_len))
4785 gdb_assert (name[current_len] == ':');
4786 this->name_components.push_back ({previous_len, idx});
4787 /* Skip the '::'. */
4789 previous_len = current_len;
4791 this->name_components.push_back ({previous_len, idx});
4794 /* Sort name_components elements by name. */
4795 auto name_comp_compare = [&] (const name_component &left,
4796 const name_component &right)
4798 const char *left_qualified = this->symbol_name_at (left.idx);
4799 const char *right_qualified = this->symbol_name_at (right.idx);
4801 const char *left_name = left_qualified + left.name_offset;
4802 const char *right_name = right_qualified + right.name_offset;
4804 return name_cmp (left_name, right_name) < 0;
4807 std::sort (this->name_components.begin (),
4808 this->name_components.end (),
4812 /* Helper for dw2_expand_symtabs_matching that works with a
4813 mapped_index_base instead of the containing objfile. This is split
4814 to a separate function in order to be able to unit test the
4815 name_components matching using a mock mapped_index_base. For each
4816 symbol name that matches, calls MATCH_CALLBACK, passing it the
4817 symbol's index in the mapped_index_base symbol table. */
4820 dw2_expand_symtabs_matching_symbol
4821 (mapped_index_base &index,
4822 const lookup_name_info &lookup_name_in,
4823 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4824 enum search_domain kind,
4825 gdb::function_view<void (offset_type)> match_callback)
4827 lookup_name_info lookup_name_without_params
4828 = lookup_name_in.make_ignore_params ();
4829 gdb_index_symbol_name_matcher lookup_name_matcher
4830 (lookup_name_without_params);
4832 /* Build the symbol name component sorted vector, if we haven't
4834 index.build_name_components ();
4836 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4838 /* Now for each symbol name in range, check to see if we have a name
4839 match, and if so, call the MATCH_CALLBACK callback. */
4841 /* The same symbol may appear more than once in the range though.
4842 E.g., if we're looking for symbols that complete "w", and we have
4843 a symbol named "w1::w2", we'll find the two name components for
4844 that same symbol in the range. To be sure we only call the
4845 callback once per symbol, we first collect the symbol name
4846 indexes that matched in a temporary vector and ignore
4848 std::vector<offset_type> matches;
4849 matches.reserve (std::distance (bounds.first, bounds.second));
4851 for (; bounds.first != bounds.second; ++bounds.first)
4853 const char *qualified = index.symbol_name_at (bounds.first->idx);
4855 if (!lookup_name_matcher.matches (qualified)
4856 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4859 matches.push_back (bounds.first->idx);
4862 std::sort (matches.begin (), matches.end ());
4864 /* Finally call the callback, once per match. */
4866 for (offset_type idx : matches)
4870 match_callback (idx);
4875 /* Above we use a type wider than idx's for 'prev', since 0 and
4876 (offset_type)-1 are both possible values. */
4877 static_assert (sizeof (prev) > sizeof (offset_type), "");
4882 namespace selftests { namespace dw2_expand_symtabs_matching {
4884 /* A mock .gdb_index/.debug_names-like name index table, enough to
4885 exercise dw2_expand_symtabs_matching_symbol, which works with the
4886 mapped_index_base interface. Builds an index from the symbol list
4887 passed as parameter to the constructor. */
4888 class mock_mapped_index : public mapped_index_base
4891 mock_mapped_index (gdb::array_view<const char *> symbols)
4892 : m_symbol_table (symbols)
4895 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4897 /* Return the number of names in the symbol table. */
4898 virtual size_t symbol_name_count () const
4900 return m_symbol_table.size ();
4903 /* Get the name of the symbol at IDX in the symbol table. */
4904 virtual const char *symbol_name_at (offset_type idx) const
4906 return m_symbol_table[idx];
4910 gdb::array_view<const char *> m_symbol_table;
4913 /* Convenience function that converts a NULL pointer to a "<null>"
4914 string, to pass to print routines. */
4917 string_or_null (const char *str)
4919 return str != NULL ? str : "<null>";
4922 /* Check if a lookup_name_info built from
4923 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4924 index. EXPECTED_LIST is the list of expected matches, in expected
4925 matching order. If no match expected, then an empty list is
4926 specified. Returns true on success. On failure prints a warning
4927 indicating the file:line that failed, and returns false. */
4930 check_match (const char *file, int line,
4931 mock_mapped_index &mock_index,
4932 const char *name, symbol_name_match_type match_type,
4933 bool completion_mode,
4934 std::initializer_list<const char *> expected_list)
4936 lookup_name_info lookup_name (name, match_type, completion_mode);
4938 bool matched = true;
4940 auto mismatch = [&] (const char *expected_str,
4943 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4944 "expected=\"%s\", got=\"%s\"\n"),
4946 (match_type == symbol_name_match_type::FULL
4948 name, string_or_null (expected_str), string_or_null (got));
4952 auto expected_it = expected_list.begin ();
4953 auto expected_end = expected_list.end ();
4955 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4957 [&] (offset_type idx)
4959 const char *matched_name = mock_index.symbol_name_at (idx);
4960 const char *expected_str
4961 = expected_it == expected_end ? NULL : *expected_it++;
4963 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4964 mismatch (expected_str, matched_name);
4967 const char *expected_str
4968 = expected_it == expected_end ? NULL : *expected_it++;
4969 if (expected_str != NULL)
4970 mismatch (expected_str, NULL);
4975 /* The symbols added to the mock mapped_index for testing (in
4977 static const char *test_symbols[] = {
4986 "ns2::tmpl<int>::foo2",
4987 "(anonymous namespace)::A::B::C",
4989 /* These are used to check that the increment-last-char in the
4990 matching algorithm for completion doesn't match "t1_fund" when
4991 completing "t1_func". */
4997 /* A UTF-8 name with multi-byte sequences to make sure that
4998 cp-name-parser understands this as a single identifier ("função"
4999 is "function" in PT). */
5002 /* \377 (0xff) is Latin1 'ÿ'. */
5005 /* \377 (0xff) is Latin1 'ÿ'. */
5009 /* A name with all sorts of complications. Starts with "z" to make
5010 it easier for the completion tests below. */
5011 #define Z_SYM_NAME \
5012 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
5013 "::tuple<(anonymous namespace)::ui*, " \
5014 "std::default_delete<(anonymous namespace)::ui>, void>"
5019 /* Returns true if the mapped_index_base::find_name_component_bounds
5020 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
5021 in completion mode. */
5024 check_find_bounds_finds (mapped_index_base &index,
5025 const char *search_name,
5026 gdb::array_view<const char *> expected_syms)
5028 lookup_name_info lookup_name (search_name,
5029 symbol_name_match_type::FULL, true);
5031 auto bounds = index.find_name_components_bounds (lookup_name);
5033 size_t distance = std::distance (bounds.first, bounds.second);
5034 if (distance != expected_syms.size ())
5037 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
5039 auto nc_elem = bounds.first + exp_elem;
5040 const char *qualified = index.symbol_name_at (nc_elem->idx);
5041 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
5048 /* Test the lower-level mapped_index::find_name_component_bounds
5052 test_mapped_index_find_name_component_bounds ()
5054 mock_mapped_index mock_index (test_symbols);
5056 mock_index.build_name_components ();
5058 /* Test the lower-level mapped_index::find_name_component_bounds
5059 method in completion mode. */
5061 static const char *expected_syms[] = {
5066 SELF_CHECK (check_find_bounds_finds (mock_index,
5067 "t1_func", expected_syms));
5070 /* Check that the increment-last-char in the name matching algorithm
5071 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
5073 static const char *expected_syms1[] = {
5077 SELF_CHECK (check_find_bounds_finds (mock_index,
5078 "\377", expected_syms1));
5080 static const char *expected_syms2[] = {
5083 SELF_CHECK (check_find_bounds_finds (mock_index,
5084 "\377\377", expected_syms2));
5088 /* Test dw2_expand_symtabs_matching_symbol. */
5091 test_dw2_expand_symtabs_matching_symbol ()
5093 mock_mapped_index mock_index (test_symbols);
5095 /* We let all tests run until the end even if some fails, for debug
5097 bool any_mismatch = false;
5099 /* Create the expected symbols list (an initializer_list). Needed
5100 because lists have commas, and we need to pass them to CHECK,
5101 which is a macro. */
5102 #define EXPECT(...) { __VA_ARGS__ }
5104 /* Wrapper for check_match that passes down the current
5105 __FILE__/__LINE__. */
5106 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
5107 any_mismatch |= !check_match (__FILE__, __LINE__, \
5109 NAME, MATCH_TYPE, COMPLETION_MODE, \
5112 /* Identity checks. */
5113 for (const char *sym : test_symbols)
5115 /* Should be able to match all existing symbols. */
5116 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
5119 /* Should be able to match all existing symbols with
5121 std::string with_params = std::string (sym) + "(int)";
5122 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5125 /* Should be able to match all existing symbols with
5126 parameters and qualifiers. */
5127 with_params = std::string (sym) + " ( int ) const";
5128 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5131 /* This should really find sym, but cp-name-parser.y doesn't
5132 know about lvalue/rvalue qualifiers yet. */
5133 with_params = std::string (sym) + " ( int ) &&";
5134 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5138 /* Check that the name matching algorithm for completion doesn't get
5139 confused with Latin1 'ÿ' / 0xff. */
5141 static const char str[] = "\377";
5142 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5143 EXPECT ("\377", "\377\377123"));
5146 /* Check that the increment-last-char in the matching algorithm for
5147 completion doesn't match "t1_fund" when completing "t1_func". */
5149 static const char str[] = "t1_func";
5150 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5151 EXPECT ("t1_func", "t1_func1"));
5154 /* Check that completion mode works at each prefix of the expected
5157 static const char str[] = "function(int)";
5158 size_t len = strlen (str);
5161 for (size_t i = 1; i < len; i++)
5163 lookup.assign (str, i);
5164 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5165 EXPECT ("function"));
5169 /* While "w" is a prefix of both components, the match function
5170 should still only be called once. */
5172 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
5174 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
5178 /* Same, with a "complicated" symbol. */
5180 static const char str[] = Z_SYM_NAME;
5181 size_t len = strlen (str);
5184 for (size_t i = 1; i < len; i++)
5186 lookup.assign (str, i);
5187 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5188 EXPECT (Z_SYM_NAME));
5192 /* In FULL mode, an incomplete symbol doesn't match. */
5194 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
5198 /* A complete symbol with parameters matches any overload, since the
5199 index has no overload info. */
5201 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
5202 EXPECT ("std::zfunction", "std::zfunction2"));
5203 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
5204 EXPECT ("std::zfunction", "std::zfunction2"));
5205 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
5206 EXPECT ("std::zfunction", "std::zfunction2"));
5209 /* Check that whitespace is ignored appropriately. A symbol with a
5210 template argument list. */
5212 static const char expected[] = "ns::foo<int>";
5213 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
5215 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
5219 /* Check that whitespace is ignored appropriately. A symbol with a
5220 template argument list that includes a pointer. */
5222 static const char expected[] = "ns::foo<char*>";
5223 /* Try both completion and non-completion modes. */
5224 static const bool completion_mode[2] = {false, true};
5225 for (size_t i = 0; i < 2; i++)
5227 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
5228 completion_mode[i], EXPECT (expected));
5229 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
5230 completion_mode[i], EXPECT (expected));
5232 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
5233 completion_mode[i], EXPECT (expected));
5234 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
5235 completion_mode[i], EXPECT (expected));
5240 /* Check method qualifiers are ignored. */
5241 static const char expected[] = "ns::foo<char*>";
5242 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
5243 symbol_name_match_type::FULL, true, EXPECT (expected));
5244 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
5245 symbol_name_match_type::FULL, true, EXPECT (expected));
5246 CHECK_MATCH ("foo < char * > ( int ) const",
5247 symbol_name_match_type::WILD, true, EXPECT (expected));
5248 CHECK_MATCH ("foo < char * > ( int ) &&",
5249 symbol_name_match_type::WILD, true, EXPECT (expected));
5252 /* Test lookup names that don't match anything. */
5254 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
5257 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
5261 /* Some wild matching tests, exercising "(anonymous namespace)",
5262 which should not be confused with a parameter list. */
5264 static const char *syms[] = {
5268 "A :: B :: C ( int )",
5273 for (const char *s : syms)
5275 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
5276 EXPECT ("(anonymous namespace)::A::B::C"));
5281 static const char expected[] = "ns2::tmpl<int>::foo2";
5282 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5284 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5288 SELF_CHECK (!any_mismatch);
5297 test_mapped_index_find_name_component_bounds ();
5298 test_dw2_expand_symtabs_matching_symbol ();
5301 }} // namespace selftests::dw2_expand_symtabs_matching
5303 #endif /* GDB_SELF_TEST */
5305 /* If FILE_MATCHER is NULL or if PER_CU has
5306 dwarf2_per_cu_quick_data::MARK set (see
5307 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5308 EXPANSION_NOTIFY on it. */
5311 dw2_expand_symtabs_matching_one
5312 (struct dwarf2_per_cu_data *per_cu,
5313 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5314 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5316 if (file_matcher == NULL || per_cu->v.quick->mark)
5318 bool symtab_was_null
5319 = (per_cu->v.quick->compunit_symtab == NULL);
5321 dw2_instantiate_symtab (per_cu);
5323 if (expansion_notify != NULL
5325 && per_cu->v.quick->compunit_symtab != NULL)
5326 expansion_notify (per_cu->v.quick->compunit_symtab);
5330 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5331 matched, to expand corresponding CUs that were marked. IDX is the
5332 index of the symbol name that matched. */
5335 dw2_expand_marked_cus
5336 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5337 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5338 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5341 offset_type *vec, vec_len, vec_idx;
5342 bool global_seen = false;
5343 mapped_index &index = *dwarf2_per_objfile->index_table;
5345 vec = (offset_type *) (index.constant_pool
5346 + MAYBE_SWAP (index.symbol_table[idx].vec));
5347 vec_len = MAYBE_SWAP (vec[0]);
5348 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5350 struct dwarf2_per_cu_data *per_cu;
5351 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5352 /* This value is only valid for index versions >= 7. */
5353 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5354 gdb_index_symbol_kind symbol_kind =
5355 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5356 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5357 /* Only check the symbol attributes if they're present.
5358 Indices prior to version 7 don't record them,
5359 and indices >= 7 may elide them for certain symbols
5360 (gold does this). */
5363 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5365 /* Work around gold/15646. */
5368 if (!is_static && global_seen)
5374 /* Only check the symbol's kind if it has one. */
5379 case VARIABLES_DOMAIN:
5380 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5383 case FUNCTIONS_DOMAIN:
5384 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5388 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5396 /* Don't crash on bad data. */
5397 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5398 + dwarf2_per_objfile->n_type_units))
5400 complaint (&symfile_complaints,
5401 _(".gdb_index entry has bad CU index"
5403 objfile_name (dwarf2_per_objfile->objfile));
5407 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
5408 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5413 /* If FILE_MATCHER is non-NULL, set all the
5414 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5415 that match FILE_MATCHER. */
5418 dw_expand_symtabs_matching_file_matcher
5419 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5420 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5422 if (file_matcher == NULL)
5425 objfile *const objfile = dwarf2_per_objfile->objfile;
5427 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5429 NULL, xcalloc, xfree));
5430 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5432 NULL, xcalloc, xfree));
5434 /* The rule is CUs specify all the files, including those used by
5435 any TU, so there's no need to scan TUs here. */
5437 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5440 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5441 struct quick_file_names *file_data;
5446 per_cu->v.quick->mark = 0;
5448 /* We only need to look at symtabs not already expanded. */
5449 if (per_cu->v.quick->compunit_symtab)
5452 file_data = dw2_get_file_names (per_cu);
5453 if (file_data == NULL)
5456 if (htab_find (visited_not_found.get (), file_data) != NULL)
5458 else if (htab_find (visited_found.get (), file_data) != NULL)
5460 per_cu->v.quick->mark = 1;
5464 for (j = 0; j < file_data->num_file_names; ++j)
5466 const char *this_real_name;
5468 if (file_matcher (file_data->file_names[j], false))
5470 per_cu->v.quick->mark = 1;
5474 /* Before we invoke realpath, which can get expensive when many
5475 files are involved, do a quick comparison of the basenames. */
5476 if (!basenames_may_differ
5477 && !file_matcher (lbasename (file_data->file_names[j]),
5481 this_real_name = dw2_get_real_path (objfile, file_data, j);
5482 if (file_matcher (this_real_name, false))
5484 per_cu->v.quick->mark = 1;
5489 slot = htab_find_slot (per_cu->v.quick->mark
5490 ? visited_found.get ()
5491 : visited_not_found.get (),
5498 dw2_expand_symtabs_matching
5499 (struct objfile *objfile,
5500 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5501 const lookup_name_info &lookup_name,
5502 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5503 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5504 enum search_domain kind)
5506 struct dwarf2_per_objfile *dwarf2_per_objfile
5507 = get_dwarf2_per_objfile (objfile);
5509 /* index_table is NULL if OBJF_READNOW. */
5510 if (!dwarf2_per_objfile->index_table)
5513 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5515 mapped_index &index = *dwarf2_per_objfile->index_table;
5517 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5519 kind, [&] (offset_type idx)
5521 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5522 expansion_notify, kind);
5526 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5529 static struct compunit_symtab *
5530 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5535 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5536 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5539 if (cust->includes == NULL)
5542 for (i = 0; cust->includes[i]; ++i)
5544 struct compunit_symtab *s = cust->includes[i];
5546 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5554 static struct compunit_symtab *
5555 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5556 struct bound_minimal_symbol msymbol,
5558 struct obj_section *section,
5561 struct dwarf2_per_cu_data *data;
5562 struct compunit_symtab *result;
5564 if (!objfile->psymtabs_addrmap)
5567 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5572 if (warn_if_readin && data->v.quick->compunit_symtab)
5573 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5574 paddress (get_objfile_arch (objfile), pc));
5577 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5579 gdb_assert (result != NULL);
5584 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5585 void *data, int need_fullname)
5587 struct dwarf2_per_objfile *dwarf2_per_objfile
5588 = get_dwarf2_per_objfile (objfile);
5590 if (!dwarf2_per_objfile->filenames_cache)
5592 dwarf2_per_objfile->filenames_cache.emplace ();
5594 htab_up visited (htab_create_alloc (10,
5595 htab_hash_pointer, htab_eq_pointer,
5596 NULL, xcalloc, xfree));
5598 /* The rule is CUs specify all the files, including those used
5599 by any TU, so there's no need to scan TUs here. We can
5600 ignore file names coming from already-expanded CUs. */
5602 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5604 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
5606 if (per_cu->v.quick->compunit_symtab)
5608 void **slot = htab_find_slot (visited.get (),
5609 per_cu->v.quick->file_names,
5612 *slot = per_cu->v.quick->file_names;
5616 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5618 dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5619 struct quick_file_names *file_data;
5622 /* We only need to look at symtabs not already expanded. */
5623 if (per_cu->v.quick->compunit_symtab)
5626 file_data = dw2_get_file_names (per_cu);
5627 if (file_data == NULL)
5630 slot = htab_find_slot (visited.get (), file_data, INSERT);
5633 /* Already visited. */
5638 for (int j = 0; j < file_data->num_file_names; ++j)
5640 const char *filename = file_data->file_names[j];
5641 dwarf2_per_objfile->filenames_cache->seen (filename);
5646 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5648 gdb::unique_xmalloc_ptr<char> this_real_name;
5651 this_real_name = gdb_realpath (filename);
5652 (*fun) (filename, this_real_name.get (), data);
5657 dw2_has_symbols (struct objfile *objfile)
5662 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5665 dw2_find_last_source_symtab,
5666 dw2_forget_cached_source_info,
5667 dw2_map_symtabs_matching_filename,
5672 dw2_expand_symtabs_for_function,
5673 dw2_expand_all_symtabs,
5674 dw2_expand_symtabs_with_fullname,
5675 dw2_map_matching_symbols,
5676 dw2_expand_symtabs_matching,
5677 dw2_find_pc_sect_compunit_symtab,
5679 dw2_map_symbol_filenames
5682 /* DWARF-5 debug_names reader. */
5684 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5685 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5687 /* A helper function that reads the .debug_names section in SECTION
5688 and fills in MAP. FILENAME is the name of the file containing the
5689 section; it is used for error reporting.
5691 Returns true if all went well, false otherwise. */
5694 read_debug_names_from_section (struct objfile *objfile,
5695 const char *filename,
5696 struct dwarf2_section_info *section,
5697 mapped_debug_names &map)
5699 if (dwarf2_section_empty_p (section))
5702 /* Older elfutils strip versions could keep the section in the main
5703 executable while splitting it for the separate debug info file. */
5704 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5707 dwarf2_read_section (objfile, section);
5709 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5711 const gdb_byte *addr = section->buffer;
5713 bfd *const abfd = get_section_bfd_owner (section);
5715 unsigned int bytes_read;
5716 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5719 map.dwarf5_is_dwarf64 = bytes_read != 4;
5720 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5721 if (bytes_read + length != section->size)
5723 /* There may be multiple per-CU indices. */
5724 warning (_("Section .debug_names in %s length %s does not match "
5725 "section length %s, ignoring .debug_names."),
5726 filename, plongest (bytes_read + length),
5727 pulongest (section->size));
5731 /* The version number. */
5732 uint16_t version = read_2_bytes (abfd, addr);
5736 warning (_("Section .debug_names in %s has unsupported version %d, "
5737 "ignoring .debug_names."),
5743 uint16_t padding = read_2_bytes (abfd, addr);
5747 warning (_("Section .debug_names in %s has unsupported padding %d, "
5748 "ignoring .debug_names."),
5753 /* comp_unit_count - The number of CUs in the CU list. */
5754 map.cu_count = read_4_bytes (abfd, addr);
5757 /* local_type_unit_count - The number of TUs in the local TU
5759 map.tu_count = read_4_bytes (abfd, addr);
5762 /* foreign_type_unit_count - The number of TUs in the foreign TU
5764 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5766 if (foreign_tu_count != 0)
5768 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5769 "ignoring .debug_names."),
5770 filename, static_cast<unsigned long> (foreign_tu_count));
5774 /* bucket_count - The number of hash buckets in the hash lookup
5776 map.bucket_count = read_4_bytes (abfd, addr);
5779 /* name_count - The number of unique names in the index. */
5780 map.name_count = read_4_bytes (abfd, addr);
5783 /* abbrev_table_size - The size in bytes of the abbreviations
5785 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5788 /* augmentation_string_size - The size in bytes of the augmentation
5789 string. This value is rounded up to a multiple of 4. */
5790 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5792 map.augmentation_is_gdb = ((augmentation_string_size
5793 == sizeof (dwarf5_augmentation))
5794 && memcmp (addr, dwarf5_augmentation,
5795 sizeof (dwarf5_augmentation)) == 0);
5796 augmentation_string_size += (-augmentation_string_size) & 3;
5797 addr += augmentation_string_size;
5800 map.cu_table_reordered = addr;
5801 addr += map.cu_count * map.offset_size;
5803 /* List of Local TUs */
5804 map.tu_table_reordered = addr;
5805 addr += map.tu_count * map.offset_size;
5807 /* Hash Lookup Table */
5808 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5809 addr += map.bucket_count * 4;
5810 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5811 addr += map.name_count * 4;
5814 map.name_table_string_offs_reordered = addr;
5815 addr += map.name_count * map.offset_size;
5816 map.name_table_entry_offs_reordered = addr;
5817 addr += map.name_count * map.offset_size;
5819 const gdb_byte *abbrev_table_start = addr;
5822 unsigned int bytes_read;
5823 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5828 const auto insertpair
5829 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5830 if (!insertpair.second)
5832 warning (_("Section .debug_names in %s has duplicate index %s, "
5833 "ignoring .debug_names."),
5834 filename, pulongest (index_num));
5837 mapped_debug_names::index_val &indexval = insertpair.first->second;
5838 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5843 mapped_debug_names::index_val::attr attr;
5844 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5846 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5848 if (attr.form == DW_FORM_implicit_const)
5850 attr.implicit_const = read_signed_leb128 (abfd, addr,
5854 if (attr.dw_idx == 0 && attr.form == 0)
5856 indexval.attr_vec.push_back (std::move (attr));
5859 if (addr != abbrev_table_start + abbrev_table_size)
5861 warning (_("Section .debug_names in %s has abbreviation_table "
5862 "of size %zu vs. written as %u, ignoring .debug_names."),
5863 filename, addr - abbrev_table_start, abbrev_table_size);
5866 map.entry_pool = addr;
5871 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5875 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5876 const mapped_debug_names &map,
5877 dwarf2_section_info §ion,
5878 bool is_dwz, int base_offset)
5880 sect_offset sect_off_prev;
5881 for (uint32_t i = 0; i <= map.cu_count; ++i)
5883 sect_offset sect_off_next;
5884 if (i < map.cu_count)
5887 = (sect_offset) (extract_unsigned_integer
5888 (map.cu_table_reordered + i * map.offset_size,
5890 map.dwarf5_byte_order));
5893 sect_off_next = (sect_offset) section.size;
5896 const ULONGEST length = sect_off_next - sect_off_prev;
5897 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
5898 = create_cu_from_index_list (dwarf2_per_objfile, §ion, is_dwz,
5899 sect_off_prev, length);
5901 sect_off_prev = sect_off_next;
5905 /* Read the CU list from the mapped index, and use it to create all
5906 the CU objects for this dwarf2_per_objfile. */
5909 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5910 const mapped_debug_names &map,
5911 const mapped_debug_names &dwz_map)
5913 struct objfile *objfile = dwarf2_per_objfile->objfile;
5915 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
5916 dwarf2_per_objfile->all_comp_units
5917 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
5918 dwarf2_per_objfile->n_comp_units);
5920 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5921 dwarf2_per_objfile->info,
5923 0 /* base_offset */);
5925 if (dwz_map.cu_count == 0)
5928 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5929 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5931 map.cu_count /* base_offset */);
5934 /* Read .debug_names. If everything went ok, initialize the "quick"
5935 elements of all the CUs and return true. Otherwise, return false. */
5938 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5940 mapped_debug_names local_map (dwarf2_per_objfile);
5941 mapped_debug_names dwz_map (dwarf2_per_objfile);
5942 struct objfile *objfile = dwarf2_per_objfile->objfile;
5944 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5945 &dwarf2_per_objfile->debug_names,
5949 /* Don't use the index if it's empty. */
5950 if (local_map.name_count == 0)
5953 /* If there is a .dwz file, read it so we can get its CU list as
5955 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5958 if (!read_debug_names_from_section (objfile,
5959 bfd_get_filename (dwz->dwz_bfd),
5960 &dwz->debug_names, dwz_map))
5962 warning (_("could not read '.debug_names' section from %s; skipping"),
5963 bfd_get_filename (dwz->dwz_bfd));
5968 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
5970 if (local_map.tu_count != 0)
5972 /* We can only handle a single .debug_types when we have an
5974 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5977 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5978 dwarf2_per_objfile->types, 0);
5980 create_signatured_type_table_from_debug_names
5981 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
5984 create_addrmap_from_aranges (dwarf2_per_objfile,
5985 &dwarf2_per_objfile->debug_aranges);
5987 dwarf2_per_objfile->debug_names_table.reset
5988 (new mapped_debug_names (dwarf2_per_objfile));
5989 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
5990 dwarf2_per_objfile->using_index = 1;
5991 dwarf2_per_objfile->quick_file_names_table =
5992 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
5997 /* Symbol name hashing function as specified by DWARF-5. */
6000 dwarf5_djb_hash (const char *str_)
6002 const unsigned char *str = (const unsigned char *) str_;
6004 /* Note: tolower here ignores UTF-8, which isn't fully compliant.
6005 See http://dwarfstd.org/ShowIssue.php?issue=161027.1. */
6007 uint32_t hash = 5381;
6008 while (int c = *str++)
6009 hash = hash * 33 + tolower (c);
6013 /* Type used to manage iterating over all CUs looking for a symbol for
6016 class dw2_debug_names_iterator
6019 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
6020 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
6021 dw2_debug_names_iterator (const mapped_debug_names &map,
6022 bool want_specific_block,
6023 block_enum block_index, domain_enum domain,
6025 : m_map (map), m_want_specific_block (want_specific_block),
6026 m_block_index (block_index), m_domain (domain),
6027 m_addr (find_vec_in_debug_names (map, name))
6030 dw2_debug_names_iterator (const mapped_debug_names &map,
6031 search_domain search, uint32_t namei)
6034 m_addr (find_vec_in_debug_names (map, namei))
6037 /* Return the next matching CU or NULL if there are no more. */
6038 dwarf2_per_cu_data *next ();
6041 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6043 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6046 /* The internalized form of .debug_names. */
6047 const mapped_debug_names &m_map;
6049 /* If true, only look for symbols that match BLOCK_INDEX. */
6050 const bool m_want_specific_block = false;
6052 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
6053 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
6055 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
6057 /* The kind of symbol we're looking for. */
6058 const domain_enum m_domain = UNDEF_DOMAIN;
6059 const search_domain m_search = ALL_DOMAIN;
6061 /* The list of CUs from the index entry of the symbol, or NULL if
6063 const gdb_byte *m_addr;
6067 mapped_debug_names::namei_to_name (uint32_t namei) const
6069 const ULONGEST namei_string_offs
6070 = extract_unsigned_integer ((name_table_string_offs_reordered
6071 + namei * offset_size),
6074 return read_indirect_string_at_offset
6075 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
6078 /* Find a slot in .debug_names for the object named NAME. If NAME is
6079 found, return pointer to its pool data. If NAME cannot be found,
6083 dw2_debug_names_iterator::find_vec_in_debug_names
6084 (const mapped_debug_names &map, const char *name)
6086 int (*cmp) (const char *, const char *);
6088 if (current_language->la_language == language_cplus
6089 || current_language->la_language == language_fortran
6090 || current_language->la_language == language_d)
6092 /* NAME is already canonical. Drop any qualifiers as
6093 .debug_names does not contain any. */
6095 if (strchr (name, '(') != NULL)
6097 gdb::unique_xmalloc_ptr<char> without_params
6098 = cp_remove_params (name);
6100 if (without_params != NULL)
6102 name = without_params.get();
6107 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
6109 const uint32_t full_hash = dwarf5_djb_hash (name);
6111 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6112 (map.bucket_table_reordered
6113 + (full_hash % map.bucket_count)), 4,
6114 map.dwarf5_byte_order);
6118 if (namei >= map.name_count)
6120 complaint (&symfile_complaints,
6121 _("Wrong .debug_names with name index %u but name_count=%u "
6123 namei, map.name_count,
6124 objfile_name (map.dwarf2_per_objfile->objfile));
6130 const uint32_t namei_full_hash
6131 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6132 (map.hash_table_reordered + namei), 4,
6133 map.dwarf5_byte_order);
6134 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
6137 if (full_hash == namei_full_hash)
6139 const char *const namei_string = map.namei_to_name (namei);
6141 #if 0 /* An expensive sanity check. */
6142 if (namei_full_hash != dwarf5_djb_hash (namei_string))
6144 complaint (&symfile_complaints,
6145 _("Wrong .debug_names hash for string at index %u "
6147 namei, objfile_name (dwarf2_per_objfile->objfile));
6152 if (cmp (namei_string, name) == 0)
6154 const ULONGEST namei_entry_offs
6155 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6156 + namei * map.offset_size),
6157 map.offset_size, map.dwarf5_byte_order);
6158 return map.entry_pool + namei_entry_offs;
6163 if (namei >= map.name_count)
6169 dw2_debug_names_iterator::find_vec_in_debug_names
6170 (const mapped_debug_names &map, uint32_t namei)
6172 if (namei >= map.name_count)
6174 complaint (&symfile_complaints,
6175 _("Wrong .debug_names with name index %u but name_count=%u "
6177 namei, map.name_count,
6178 objfile_name (map.dwarf2_per_objfile->objfile));
6182 const ULONGEST namei_entry_offs
6183 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6184 + namei * map.offset_size),
6185 map.offset_size, map.dwarf5_byte_order);
6186 return map.entry_pool + namei_entry_offs;
6189 /* See dw2_debug_names_iterator. */
6191 dwarf2_per_cu_data *
6192 dw2_debug_names_iterator::next ()
6197 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
6198 struct objfile *objfile = dwarf2_per_objfile->objfile;
6199 bfd *const abfd = objfile->obfd;
6203 unsigned int bytes_read;
6204 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6205 m_addr += bytes_read;
6209 const auto indexval_it = m_map.abbrev_map.find (abbrev);
6210 if (indexval_it == m_map.abbrev_map.cend ())
6212 complaint (&symfile_complaints,
6213 _("Wrong .debug_names undefined abbrev code %s "
6215 pulongest (abbrev), objfile_name (objfile));
6218 const mapped_debug_names::index_val &indexval = indexval_it->second;
6219 bool have_is_static = false;
6221 dwarf2_per_cu_data *per_cu = NULL;
6222 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
6227 case DW_FORM_implicit_const:
6228 ull = attr.implicit_const;
6230 case DW_FORM_flag_present:
6234 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6235 m_addr += bytes_read;
6238 complaint (&symfile_complaints,
6239 _("Unsupported .debug_names form %s [in module %s]"),
6240 dwarf_form_name (attr.form),
6241 objfile_name (objfile));
6244 switch (attr.dw_idx)
6246 case DW_IDX_compile_unit:
6247 /* Don't crash on bad data. */
6248 if (ull >= dwarf2_per_objfile->n_comp_units)
6250 complaint (&symfile_complaints,
6251 _(".debug_names entry has bad CU index %s"
6254 objfile_name (dwarf2_per_objfile->objfile));
6257 per_cu = dw2_get_cutu (dwarf2_per_objfile, ull);
6259 case DW_IDX_type_unit:
6260 /* Don't crash on bad data. */
6261 if (ull >= dwarf2_per_objfile->n_type_units)
6263 complaint (&symfile_complaints,
6264 _(".debug_names entry has bad TU index %s"
6267 objfile_name (dwarf2_per_objfile->objfile));
6270 per_cu = dw2_get_cutu (dwarf2_per_objfile,
6271 dwarf2_per_objfile->n_comp_units + ull);
6273 case DW_IDX_GNU_internal:
6274 if (!m_map.augmentation_is_gdb)
6276 have_is_static = true;
6279 case DW_IDX_GNU_external:
6280 if (!m_map.augmentation_is_gdb)
6282 have_is_static = true;
6288 /* Skip if already read in. */
6289 if (per_cu->v.quick->compunit_symtab)
6292 /* Check static vs global. */
6295 const bool want_static = m_block_index != GLOBAL_BLOCK;
6296 if (m_want_specific_block && want_static != is_static)
6300 /* Match dw2_symtab_iter_next, symbol_kind
6301 and debug_names::psymbol_tag. */
6305 switch (indexval.dwarf_tag)
6307 case DW_TAG_variable:
6308 case DW_TAG_subprogram:
6309 /* Some types are also in VAR_DOMAIN. */
6310 case DW_TAG_typedef:
6311 case DW_TAG_structure_type:
6318 switch (indexval.dwarf_tag)
6320 case DW_TAG_typedef:
6321 case DW_TAG_structure_type:
6328 switch (indexval.dwarf_tag)
6331 case DW_TAG_variable:
6341 /* Match dw2_expand_symtabs_matching, symbol_kind and
6342 debug_names::psymbol_tag. */
6345 case VARIABLES_DOMAIN:
6346 switch (indexval.dwarf_tag)
6348 case DW_TAG_variable:
6354 case FUNCTIONS_DOMAIN:
6355 switch (indexval.dwarf_tag)
6357 case DW_TAG_subprogram:
6364 switch (indexval.dwarf_tag)
6366 case DW_TAG_typedef:
6367 case DW_TAG_structure_type:
6380 static struct compunit_symtab *
6381 dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6382 const char *name, domain_enum domain)
6384 const block_enum block_index = static_cast<block_enum> (block_index_int);
6385 struct dwarf2_per_objfile *dwarf2_per_objfile
6386 = get_dwarf2_per_objfile (objfile);
6388 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6391 /* index is NULL if OBJF_READNOW. */
6394 const auto &map = *mapp;
6396 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6397 block_index, domain, name);
6399 struct compunit_symtab *stab_best = NULL;
6400 struct dwarf2_per_cu_data *per_cu;
6401 while ((per_cu = iter.next ()) != NULL)
6403 struct symbol *sym, *with_opaque = NULL;
6404 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6405 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6406 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6408 sym = block_find_symbol (block, name, domain,
6409 block_find_non_opaque_type_preferred,
6412 /* Some caution must be observed with overloaded functions and
6413 methods, since the index will not contain any overload
6414 information (but NAME might contain it). */
6417 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6419 if (with_opaque != NULL
6420 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6423 /* Keep looking through other CUs. */
6429 /* This dumps minimal information about .debug_names. It is called
6430 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6431 uses this to verify that .debug_names has been loaded. */
6434 dw2_debug_names_dump (struct objfile *objfile)
6436 struct dwarf2_per_objfile *dwarf2_per_objfile
6437 = get_dwarf2_per_objfile (objfile);
6439 gdb_assert (dwarf2_per_objfile->using_index);
6440 printf_filtered (".debug_names:");
6441 if (dwarf2_per_objfile->debug_names_table)
6442 printf_filtered (" exists\n");
6444 printf_filtered (" faked for \"readnow\"\n");
6445 printf_filtered ("\n");
6449 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6450 const char *func_name)
6452 struct dwarf2_per_objfile *dwarf2_per_objfile
6453 = get_dwarf2_per_objfile (objfile);
6455 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6456 if (dwarf2_per_objfile->debug_names_table)
6458 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6460 /* Note: It doesn't matter what we pass for block_index here. */
6461 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6462 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
6464 struct dwarf2_per_cu_data *per_cu;
6465 while ((per_cu = iter.next ()) != NULL)
6466 dw2_instantiate_symtab (per_cu);
6471 dw2_debug_names_expand_symtabs_matching
6472 (struct objfile *objfile,
6473 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6474 const lookup_name_info &lookup_name,
6475 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6476 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6477 enum search_domain kind)
6479 struct dwarf2_per_objfile *dwarf2_per_objfile
6480 = get_dwarf2_per_objfile (objfile);
6482 /* debug_names_table is NULL if OBJF_READNOW. */
6483 if (!dwarf2_per_objfile->debug_names_table)
6486 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6488 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6490 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6492 kind, [&] (offset_type namei)
6494 /* The name was matched, now expand corresponding CUs that were
6496 dw2_debug_names_iterator iter (map, kind, namei);
6498 struct dwarf2_per_cu_data *per_cu;
6499 while ((per_cu = iter.next ()) != NULL)
6500 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6505 const struct quick_symbol_functions dwarf2_debug_names_functions =
6508 dw2_find_last_source_symtab,
6509 dw2_forget_cached_source_info,
6510 dw2_map_symtabs_matching_filename,
6511 dw2_debug_names_lookup_symbol,
6513 dw2_debug_names_dump,
6515 dw2_debug_names_expand_symtabs_for_function,
6516 dw2_expand_all_symtabs,
6517 dw2_expand_symtabs_with_fullname,
6518 dw2_map_matching_symbols,
6519 dw2_debug_names_expand_symtabs_matching,
6520 dw2_find_pc_sect_compunit_symtab,
6522 dw2_map_symbol_filenames
6525 /* See symfile.h. */
6528 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6530 struct dwarf2_per_objfile *dwarf2_per_objfile
6531 = get_dwarf2_per_objfile (objfile);
6533 /* If we're about to read full symbols, don't bother with the
6534 indices. In this case we also don't care if some other debug
6535 format is making psymtabs, because they are all about to be
6537 if ((objfile->flags & OBJF_READNOW))
6541 dwarf2_per_objfile->using_index = 1;
6542 create_all_comp_units (dwarf2_per_objfile);
6543 create_all_type_units (dwarf2_per_objfile);
6544 dwarf2_per_objfile->quick_file_names_table =
6545 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6547 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
6548 + dwarf2_per_objfile->n_type_units); ++i)
6550 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
6552 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6553 struct dwarf2_per_cu_quick_data);
6556 /* Return 1 so that gdb sees the "quick" functions. However,
6557 these functions will be no-ops because we will have expanded
6559 *index_kind = dw_index_kind::GDB_INDEX;
6563 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6565 *index_kind = dw_index_kind::DEBUG_NAMES;
6569 if (dwarf2_read_index (objfile))
6571 *index_kind = dw_index_kind::GDB_INDEX;
6580 /* Build a partial symbol table. */
6583 dwarf2_build_psymtabs (struct objfile *objfile)
6585 struct dwarf2_per_objfile *dwarf2_per_objfile
6586 = get_dwarf2_per_objfile (objfile);
6588 if (objfile->global_psymbols.capacity () == 0
6589 && objfile->static_psymbols.capacity () == 0)
6590 init_psymbol_list (objfile, 1024);
6594 /* This isn't really ideal: all the data we allocate on the
6595 objfile's obstack is still uselessly kept around. However,
6596 freeing it seems unsafe. */
6597 psymtab_discarder psymtabs (objfile);
6598 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6601 CATCH (except, RETURN_MASK_ERROR)
6603 exception_print (gdb_stderr, except);
6608 /* Return the total length of the CU described by HEADER. */
6611 get_cu_length (const struct comp_unit_head *header)
6613 return header->initial_length_size + header->length;
6616 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6619 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6621 sect_offset bottom = cu_header->sect_off;
6622 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6624 return sect_off >= bottom && sect_off < top;
6627 /* Find the base address of the compilation unit for range lists and
6628 location lists. It will normally be specified by DW_AT_low_pc.
6629 In DWARF-3 draft 4, the base address could be overridden by
6630 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6631 compilation units with discontinuous ranges. */
6634 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6636 struct attribute *attr;
6639 cu->base_address = 0;
6641 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6644 cu->base_address = attr_value_as_address (attr);
6649 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6652 cu->base_address = attr_value_as_address (attr);
6658 /* Read in the comp unit header information from the debug_info at info_ptr.
6659 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6660 NOTE: This leaves members offset, first_die_offset to be filled in
6663 static const gdb_byte *
6664 read_comp_unit_head (struct comp_unit_head *cu_header,
6665 const gdb_byte *info_ptr,
6666 struct dwarf2_section_info *section,
6667 rcuh_kind section_kind)
6670 unsigned int bytes_read;
6671 const char *filename = get_section_file_name (section);
6672 bfd *abfd = get_section_bfd_owner (section);
6674 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6675 cu_header->initial_length_size = bytes_read;
6676 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6677 info_ptr += bytes_read;
6678 cu_header->version = read_2_bytes (abfd, info_ptr);
6680 if (cu_header->version < 5)
6681 switch (section_kind)
6683 case rcuh_kind::COMPILE:
6684 cu_header->unit_type = DW_UT_compile;
6686 case rcuh_kind::TYPE:
6687 cu_header->unit_type = DW_UT_type;
6690 internal_error (__FILE__, __LINE__,
6691 _("read_comp_unit_head: invalid section_kind"));
6695 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6696 (read_1_byte (abfd, info_ptr));
6698 switch (cu_header->unit_type)
6701 if (section_kind != rcuh_kind::COMPILE)
6702 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6703 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6707 section_kind = rcuh_kind::TYPE;
6710 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6711 "(is %d, should be %d or %d) [in module %s]"),
6712 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6715 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6718 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6721 info_ptr += bytes_read;
6722 if (cu_header->version < 5)
6724 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6727 signed_addr = bfd_get_sign_extend_vma (abfd);
6728 if (signed_addr < 0)
6729 internal_error (__FILE__, __LINE__,
6730 _("read_comp_unit_head: dwarf from non elf file"));
6731 cu_header->signed_addr_p = signed_addr;
6733 if (section_kind == rcuh_kind::TYPE)
6735 LONGEST type_offset;
6737 cu_header->signature = read_8_bytes (abfd, info_ptr);
6740 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6741 info_ptr += bytes_read;
6742 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6743 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6744 error (_("Dwarf Error: Too big type_offset in compilation unit "
6745 "header (is %s) [in module %s]"), plongest (type_offset),
6752 /* Helper function that returns the proper abbrev section for
6755 static struct dwarf2_section_info *
6756 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6758 struct dwarf2_section_info *abbrev;
6759 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6761 if (this_cu->is_dwz)
6762 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6764 abbrev = &dwarf2_per_objfile->abbrev;
6769 /* Subroutine of read_and_check_comp_unit_head and
6770 read_and_check_type_unit_head to simplify them.
6771 Perform various error checking on the header. */
6774 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6775 struct comp_unit_head *header,
6776 struct dwarf2_section_info *section,
6777 struct dwarf2_section_info *abbrev_section)
6779 const char *filename = get_section_file_name (section);
6781 if (header->version < 2 || header->version > 5)
6782 error (_("Dwarf Error: wrong version in compilation unit header "
6783 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
6786 if (to_underlying (header->abbrev_sect_off)
6787 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6788 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
6789 "(offset 0x%x + 6) [in module %s]"),
6790 to_underlying (header->abbrev_sect_off),
6791 to_underlying (header->sect_off),
6794 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6795 avoid potential 32-bit overflow. */
6796 if (((ULONGEST) header->sect_off + get_cu_length (header))
6798 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6799 "(offset 0x%x + 0) [in module %s]"),
6800 header->length, to_underlying (header->sect_off),
6804 /* Read in a CU/TU header and perform some basic error checking.
6805 The contents of the header are stored in HEADER.
6806 The result is a pointer to the start of the first DIE. */
6808 static const gdb_byte *
6809 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6810 struct comp_unit_head *header,
6811 struct dwarf2_section_info *section,
6812 struct dwarf2_section_info *abbrev_section,
6813 const gdb_byte *info_ptr,
6814 rcuh_kind section_kind)
6816 const gdb_byte *beg_of_comp_unit = info_ptr;
6818 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6820 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6822 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6824 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6830 /* Fetch the abbreviation table offset from a comp or type unit header. */
6833 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6834 struct dwarf2_section_info *section,
6835 sect_offset sect_off)
6837 bfd *abfd = get_section_bfd_owner (section);
6838 const gdb_byte *info_ptr;
6839 unsigned int initial_length_size, offset_size;
6842 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6843 info_ptr = section->buffer + to_underlying (sect_off);
6844 read_initial_length (abfd, info_ptr, &initial_length_size);
6845 offset_size = initial_length_size == 4 ? 4 : 8;
6846 info_ptr += initial_length_size;
6848 version = read_2_bytes (abfd, info_ptr);
6852 /* Skip unit type and address size. */
6856 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6859 /* Allocate a new partial symtab for file named NAME and mark this new
6860 partial symtab as being an include of PST. */
6863 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6864 struct objfile *objfile)
6866 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6868 if (!IS_ABSOLUTE_PATH (subpst->filename))
6870 /* It shares objfile->objfile_obstack. */
6871 subpst->dirname = pst->dirname;
6874 subpst->textlow = 0;
6875 subpst->texthigh = 0;
6877 subpst->dependencies
6878 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
6879 subpst->dependencies[0] = pst;
6880 subpst->number_of_dependencies = 1;
6882 subpst->globals_offset = 0;
6883 subpst->n_global_syms = 0;
6884 subpst->statics_offset = 0;
6885 subpst->n_static_syms = 0;
6886 subpst->compunit_symtab = NULL;
6887 subpst->read_symtab = pst->read_symtab;
6890 /* No private part is necessary for include psymtabs. This property
6891 can be used to differentiate between such include psymtabs and
6892 the regular ones. */
6893 subpst->read_symtab_private = NULL;
6896 /* Read the Line Number Program data and extract the list of files
6897 included by the source file represented by PST. Build an include
6898 partial symtab for each of these included files. */
6901 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6902 struct die_info *die,
6903 struct partial_symtab *pst)
6906 struct attribute *attr;
6908 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6910 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6912 return; /* No linetable, so no includes. */
6914 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
6915 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
6919 hash_signatured_type (const void *item)
6921 const struct signatured_type *sig_type
6922 = (const struct signatured_type *) item;
6924 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6925 return sig_type->signature;
6929 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6931 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6932 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6934 return lhs->signature == rhs->signature;
6937 /* Allocate a hash table for signatured types. */
6940 allocate_signatured_type_table (struct objfile *objfile)
6942 return htab_create_alloc_ex (41,
6943 hash_signatured_type,
6946 &objfile->objfile_obstack,
6947 hashtab_obstack_allocate,
6948 dummy_obstack_deallocate);
6951 /* A helper function to add a signatured type CU to a table. */
6954 add_signatured_type_cu_to_table (void **slot, void *datum)
6956 struct signatured_type *sigt = (struct signatured_type *) *slot;
6957 struct signatured_type ***datap = (struct signatured_type ***) datum;
6965 /* A helper for create_debug_types_hash_table. Read types from SECTION
6966 and fill them into TYPES_HTAB. It will process only type units,
6967 therefore DW_UT_type. */
6970 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6971 struct dwo_file *dwo_file,
6972 dwarf2_section_info *section, htab_t &types_htab,
6973 rcuh_kind section_kind)
6975 struct objfile *objfile = dwarf2_per_objfile->objfile;
6976 struct dwarf2_section_info *abbrev_section;
6978 const gdb_byte *info_ptr, *end_ptr;
6980 abbrev_section = (dwo_file != NULL
6981 ? &dwo_file->sections.abbrev
6982 : &dwarf2_per_objfile->abbrev);
6984 if (dwarf_read_debug)
6985 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6986 get_section_name (section),
6987 get_section_file_name (abbrev_section));
6989 dwarf2_read_section (objfile, section);
6990 info_ptr = section->buffer;
6992 if (info_ptr == NULL)
6995 /* We can't set abfd until now because the section may be empty or
6996 not present, in which case the bfd is unknown. */
6997 abfd = get_section_bfd_owner (section);
6999 /* We don't use init_cutu_and_read_dies_simple, or some such, here
7000 because we don't need to read any dies: the signature is in the
7003 end_ptr = info_ptr + section->size;
7004 while (info_ptr < end_ptr)
7006 struct signatured_type *sig_type;
7007 struct dwo_unit *dwo_tu;
7009 const gdb_byte *ptr = info_ptr;
7010 struct comp_unit_head header;
7011 unsigned int length;
7013 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
7015 /* Initialize it due to a false compiler warning. */
7016 header.signature = -1;
7017 header.type_cu_offset_in_tu = (cu_offset) -1;
7019 /* We need to read the type's signature in order to build the hash
7020 table, but we don't need anything else just yet. */
7022 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
7023 abbrev_section, ptr, section_kind);
7025 length = get_cu_length (&header);
7027 /* Skip dummy type units. */
7028 if (ptr >= info_ptr + length
7029 || peek_abbrev_code (abfd, ptr) == 0
7030 || header.unit_type != DW_UT_type)
7036 if (types_htab == NULL)
7039 types_htab = allocate_dwo_unit_table (objfile);
7041 types_htab = allocate_signatured_type_table (objfile);
7047 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7049 dwo_tu->dwo_file = dwo_file;
7050 dwo_tu->signature = header.signature;
7051 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
7052 dwo_tu->section = section;
7053 dwo_tu->sect_off = sect_off;
7054 dwo_tu->length = length;
7058 /* N.B.: type_offset is not usable if this type uses a DWO file.
7059 The real type_offset is in the DWO file. */
7061 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7062 struct signatured_type);
7063 sig_type->signature = header.signature;
7064 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
7065 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7066 sig_type->per_cu.is_debug_types = 1;
7067 sig_type->per_cu.section = section;
7068 sig_type->per_cu.sect_off = sect_off;
7069 sig_type->per_cu.length = length;
7072 slot = htab_find_slot (types_htab,
7073 dwo_file ? (void*) dwo_tu : (void *) sig_type,
7075 gdb_assert (slot != NULL);
7078 sect_offset dup_sect_off;
7082 const struct dwo_unit *dup_tu
7083 = (const struct dwo_unit *) *slot;
7085 dup_sect_off = dup_tu->sect_off;
7089 const struct signatured_type *dup_tu
7090 = (const struct signatured_type *) *slot;
7092 dup_sect_off = dup_tu->per_cu.sect_off;
7095 complaint (&symfile_complaints,
7096 _("debug type entry at offset 0x%x is duplicate to"
7097 " the entry at offset 0x%x, signature %s"),
7098 to_underlying (sect_off), to_underlying (dup_sect_off),
7099 hex_string (header.signature));
7101 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
7103 if (dwarf_read_debug > 1)
7104 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
7105 to_underlying (sect_off),
7106 hex_string (header.signature));
7112 /* Create the hash table of all entries in the .debug_types
7113 (or .debug_types.dwo) section(s).
7114 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
7115 otherwise it is NULL.
7117 The result is a pointer to the hash table or NULL if there are no types.
7119 Note: This function processes DWO files only, not DWP files. */
7122 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7123 struct dwo_file *dwo_file,
7124 VEC (dwarf2_section_info_def) *types,
7128 struct dwarf2_section_info *section;
7130 if (VEC_empty (dwarf2_section_info_def, types))
7134 VEC_iterate (dwarf2_section_info_def, types, ix, section);
7136 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
7137 types_htab, rcuh_kind::TYPE);
7140 /* Create the hash table of all entries in the .debug_types section,
7141 and initialize all_type_units.
7142 The result is zero if there is an error (e.g. missing .debug_types section),
7143 otherwise non-zero. */
7146 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7148 htab_t types_htab = NULL;
7149 struct signatured_type **iter;
7151 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
7152 &dwarf2_per_objfile->info, types_htab,
7153 rcuh_kind::COMPILE);
7154 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
7155 dwarf2_per_objfile->types, types_htab);
7156 if (types_htab == NULL)
7158 dwarf2_per_objfile->signatured_types = NULL;
7162 dwarf2_per_objfile->signatured_types = types_htab;
7164 dwarf2_per_objfile->n_type_units
7165 = dwarf2_per_objfile->n_allocated_type_units
7166 = htab_elements (types_htab);
7167 dwarf2_per_objfile->all_type_units =
7168 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
7169 iter = &dwarf2_per_objfile->all_type_units[0];
7170 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
7171 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
7172 == dwarf2_per_objfile->n_type_units);
7177 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
7178 If SLOT is non-NULL, it is the entry to use in the hash table.
7179 Otherwise we find one. */
7181 static struct signatured_type *
7182 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
7185 struct objfile *objfile = dwarf2_per_objfile->objfile;
7186 int n_type_units = dwarf2_per_objfile->n_type_units;
7187 struct signatured_type *sig_type;
7189 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
7191 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
7193 if (dwarf2_per_objfile->n_allocated_type_units == 0)
7194 dwarf2_per_objfile->n_allocated_type_units = 1;
7195 dwarf2_per_objfile->n_allocated_type_units *= 2;
7196 dwarf2_per_objfile->all_type_units
7197 = XRESIZEVEC (struct signatured_type *,
7198 dwarf2_per_objfile->all_type_units,
7199 dwarf2_per_objfile->n_allocated_type_units);
7200 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
7202 dwarf2_per_objfile->n_type_units = n_type_units;
7204 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7205 struct signatured_type);
7206 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
7207 sig_type->signature = sig;
7208 sig_type->per_cu.is_debug_types = 1;
7209 if (dwarf2_per_objfile->using_index)
7211 sig_type->per_cu.v.quick =
7212 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7213 struct dwarf2_per_cu_quick_data);
7218 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7221 gdb_assert (*slot == NULL);
7223 /* The rest of sig_type must be filled in by the caller. */
7227 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7228 Fill in SIG_ENTRY with DWO_ENTRY. */
7231 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
7232 struct signatured_type *sig_entry,
7233 struct dwo_unit *dwo_entry)
7235 /* Make sure we're not clobbering something we don't expect to. */
7236 gdb_assert (! sig_entry->per_cu.queued);
7237 gdb_assert (sig_entry->per_cu.cu == NULL);
7238 if (dwarf2_per_objfile->using_index)
7240 gdb_assert (sig_entry->per_cu.v.quick != NULL);
7241 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
7244 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7245 gdb_assert (sig_entry->signature == dwo_entry->signature);
7246 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7247 gdb_assert (sig_entry->type_unit_group == NULL);
7248 gdb_assert (sig_entry->dwo_unit == NULL);
7250 sig_entry->per_cu.section = dwo_entry->section;
7251 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7252 sig_entry->per_cu.length = dwo_entry->length;
7253 sig_entry->per_cu.reading_dwo_directly = 1;
7254 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7255 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7256 sig_entry->dwo_unit = dwo_entry;
7259 /* Subroutine of lookup_signatured_type.
7260 If we haven't read the TU yet, create the signatured_type data structure
7261 for a TU to be read in directly from a DWO file, bypassing the stub.
7262 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7263 using .gdb_index, then when reading a CU we want to stay in the DWO file
7264 containing that CU. Otherwise we could end up reading several other DWO
7265 files (due to comdat folding) to process the transitive closure of all the
7266 mentioned TUs, and that can be slow. The current DWO file will have every
7267 type signature that it needs.
7268 We only do this for .gdb_index because in the psymtab case we already have
7269 to read all the DWOs to build the type unit groups. */
7271 static struct signatured_type *
7272 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7274 struct dwarf2_per_objfile *dwarf2_per_objfile
7275 = cu->per_cu->dwarf2_per_objfile;
7276 struct objfile *objfile = dwarf2_per_objfile->objfile;
7277 struct dwo_file *dwo_file;
7278 struct dwo_unit find_dwo_entry, *dwo_entry;
7279 struct signatured_type find_sig_entry, *sig_entry;
7282 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7284 /* If TU skeletons have been removed then we may not have read in any
7286 if (dwarf2_per_objfile->signatured_types == NULL)
7288 dwarf2_per_objfile->signatured_types
7289 = allocate_signatured_type_table (objfile);
7292 /* We only ever need to read in one copy of a signatured type.
7293 Use the global signatured_types array to do our own comdat-folding
7294 of types. If this is the first time we're reading this TU, and
7295 the TU has an entry in .gdb_index, replace the recorded data from
7296 .gdb_index with this TU. */
7298 find_sig_entry.signature = sig;
7299 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7300 &find_sig_entry, INSERT);
7301 sig_entry = (struct signatured_type *) *slot;
7303 /* We can get here with the TU already read, *or* in the process of being
7304 read. Don't reassign the global entry to point to this DWO if that's
7305 the case. Also note that if the TU is already being read, it may not
7306 have come from a DWO, the program may be a mix of Fission-compiled
7307 code and non-Fission-compiled code. */
7309 /* Have we already tried to read this TU?
7310 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7311 needn't exist in the global table yet). */
7312 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7315 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7316 dwo_unit of the TU itself. */
7317 dwo_file = cu->dwo_unit->dwo_file;
7319 /* Ok, this is the first time we're reading this TU. */
7320 if (dwo_file->tus == NULL)
7322 find_dwo_entry.signature = sig;
7323 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7324 if (dwo_entry == NULL)
7327 /* If the global table doesn't have an entry for this TU, add one. */
7328 if (sig_entry == NULL)
7329 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7331 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7332 sig_entry->per_cu.tu_read = 1;
7336 /* Subroutine of lookup_signatured_type.
7337 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7338 then try the DWP file. If the TU stub (skeleton) has been removed then
7339 it won't be in .gdb_index. */
7341 static struct signatured_type *
7342 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7344 struct dwarf2_per_objfile *dwarf2_per_objfile
7345 = cu->per_cu->dwarf2_per_objfile;
7346 struct objfile *objfile = dwarf2_per_objfile->objfile;
7347 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7348 struct dwo_unit *dwo_entry;
7349 struct signatured_type find_sig_entry, *sig_entry;
7352 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7353 gdb_assert (dwp_file != NULL);
7355 /* If TU skeletons have been removed then we may not have read in any
7357 if (dwarf2_per_objfile->signatured_types == NULL)
7359 dwarf2_per_objfile->signatured_types
7360 = allocate_signatured_type_table (objfile);
7363 find_sig_entry.signature = sig;
7364 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7365 &find_sig_entry, INSERT);
7366 sig_entry = (struct signatured_type *) *slot;
7368 /* Have we already tried to read this TU?
7369 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7370 needn't exist in the global table yet). */
7371 if (sig_entry != NULL)
7374 if (dwp_file->tus == NULL)
7376 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7377 sig, 1 /* is_debug_types */);
7378 if (dwo_entry == NULL)
7381 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7382 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7387 /* Lookup a signature based type for DW_FORM_ref_sig8.
7388 Returns NULL if signature SIG is not present in the table.
7389 It is up to the caller to complain about this. */
7391 static struct signatured_type *
7392 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7394 struct dwarf2_per_objfile *dwarf2_per_objfile
7395 = cu->per_cu->dwarf2_per_objfile;
7398 && dwarf2_per_objfile->using_index)
7400 /* We're in a DWO/DWP file, and we're using .gdb_index.
7401 These cases require special processing. */
7402 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7403 return lookup_dwo_signatured_type (cu, sig);
7405 return lookup_dwp_signatured_type (cu, sig);
7409 struct signatured_type find_entry, *entry;
7411 if (dwarf2_per_objfile->signatured_types == NULL)
7413 find_entry.signature = sig;
7414 entry = ((struct signatured_type *)
7415 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7420 /* Low level DIE reading support. */
7422 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7425 init_cu_die_reader (struct die_reader_specs *reader,
7426 struct dwarf2_cu *cu,
7427 struct dwarf2_section_info *section,
7428 struct dwo_file *dwo_file,
7429 struct abbrev_table *abbrev_table)
7431 gdb_assert (section->readin && section->buffer != NULL);
7432 reader->abfd = get_section_bfd_owner (section);
7434 reader->dwo_file = dwo_file;
7435 reader->die_section = section;
7436 reader->buffer = section->buffer;
7437 reader->buffer_end = section->buffer + section->size;
7438 reader->comp_dir = NULL;
7439 reader->abbrev_table = abbrev_table;
7442 /* Subroutine of init_cutu_and_read_dies to simplify it.
7443 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7444 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7447 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7448 from it to the DIE in the DWO. If NULL we are skipping the stub.
7449 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7450 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7451 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7452 STUB_COMP_DIR may be non-NULL.
7453 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7454 are filled in with the info of the DIE from the DWO file.
7455 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7456 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7457 kept around for at least as long as *RESULT_READER.
7459 The result is non-zero if a valid (non-dummy) DIE was found. */
7462 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7463 struct dwo_unit *dwo_unit,
7464 struct die_info *stub_comp_unit_die,
7465 const char *stub_comp_dir,
7466 struct die_reader_specs *result_reader,
7467 const gdb_byte **result_info_ptr,
7468 struct die_info **result_comp_unit_die,
7469 int *result_has_children,
7470 abbrev_table_up *result_dwo_abbrev_table)
7472 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7473 struct objfile *objfile = dwarf2_per_objfile->objfile;
7474 struct dwarf2_cu *cu = this_cu->cu;
7476 const gdb_byte *begin_info_ptr, *info_ptr;
7477 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7478 int i,num_extra_attrs;
7479 struct dwarf2_section_info *dwo_abbrev_section;
7480 struct attribute *attr;
7481 struct die_info *comp_unit_die;
7483 /* At most one of these may be provided. */
7484 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7486 /* These attributes aren't processed until later:
7487 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7488 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7489 referenced later. However, these attributes are found in the stub
7490 which we won't have later. In order to not impose this complication
7491 on the rest of the code, we read them here and copy them to the
7500 if (stub_comp_unit_die != NULL)
7502 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7504 if (! this_cu->is_debug_types)
7505 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7506 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7507 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7508 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7509 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7511 /* There should be a DW_AT_addr_base attribute here (if needed).
7512 We need the value before we can process DW_FORM_GNU_addr_index. */
7514 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7516 cu->addr_base = DW_UNSND (attr);
7518 /* There should be a DW_AT_ranges_base attribute here (if needed).
7519 We need the value before we can process DW_AT_ranges. */
7520 cu->ranges_base = 0;
7521 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7523 cu->ranges_base = DW_UNSND (attr);
7525 else if (stub_comp_dir != NULL)
7527 /* Reconstruct the comp_dir attribute to simplify the code below. */
7528 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7529 comp_dir->name = DW_AT_comp_dir;
7530 comp_dir->form = DW_FORM_string;
7531 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7532 DW_STRING (comp_dir) = stub_comp_dir;
7535 /* Set up for reading the DWO CU/TU. */
7536 cu->dwo_unit = dwo_unit;
7537 dwarf2_section_info *section = dwo_unit->section;
7538 dwarf2_read_section (objfile, section);
7539 abfd = get_section_bfd_owner (section);
7540 begin_info_ptr = info_ptr = (section->buffer
7541 + to_underlying (dwo_unit->sect_off));
7542 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7544 if (this_cu->is_debug_types)
7546 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7548 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7549 &cu->header, section,
7551 info_ptr, rcuh_kind::TYPE);
7552 /* This is not an assert because it can be caused by bad debug info. */
7553 if (sig_type->signature != cu->header.signature)
7555 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7556 " TU at offset 0x%x [in module %s]"),
7557 hex_string (sig_type->signature),
7558 hex_string (cu->header.signature),
7559 to_underlying (dwo_unit->sect_off),
7560 bfd_get_filename (abfd));
7562 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7563 /* For DWOs coming from DWP files, we don't know the CU length
7564 nor the type's offset in the TU until now. */
7565 dwo_unit->length = get_cu_length (&cu->header);
7566 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7568 /* Establish the type offset that can be used to lookup the type.
7569 For DWO files, we don't know it until now. */
7570 sig_type->type_offset_in_section
7571 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7575 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7576 &cu->header, section,
7578 info_ptr, rcuh_kind::COMPILE);
7579 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7580 /* For DWOs coming from DWP files, we don't know the CU length
7582 dwo_unit->length = get_cu_length (&cu->header);
7585 *result_dwo_abbrev_table
7586 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7587 cu->header.abbrev_sect_off);
7588 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7589 result_dwo_abbrev_table->get ());
7591 /* Read in the die, but leave space to copy over the attributes
7592 from the stub. This has the benefit of simplifying the rest of
7593 the code - all the work to maintain the illusion of a single
7594 DW_TAG_{compile,type}_unit DIE is done here. */
7595 num_extra_attrs = ((stmt_list != NULL)
7599 + (comp_dir != NULL));
7600 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7601 result_has_children, num_extra_attrs);
7603 /* Copy over the attributes from the stub to the DIE we just read in. */
7604 comp_unit_die = *result_comp_unit_die;
7605 i = comp_unit_die->num_attrs;
7606 if (stmt_list != NULL)
7607 comp_unit_die->attrs[i++] = *stmt_list;
7609 comp_unit_die->attrs[i++] = *low_pc;
7610 if (high_pc != NULL)
7611 comp_unit_die->attrs[i++] = *high_pc;
7613 comp_unit_die->attrs[i++] = *ranges;
7614 if (comp_dir != NULL)
7615 comp_unit_die->attrs[i++] = *comp_dir;
7616 comp_unit_die->num_attrs += num_extra_attrs;
7618 if (dwarf_die_debug)
7620 fprintf_unfiltered (gdb_stdlog,
7621 "Read die from %s@0x%x of %s:\n",
7622 get_section_name (section),
7623 (unsigned) (begin_info_ptr - section->buffer),
7624 bfd_get_filename (abfd));
7625 dump_die (comp_unit_die, dwarf_die_debug);
7628 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7629 TUs by skipping the stub and going directly to the entry in the DWO file.
7630 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7631 to get it via circuitous means. Blech. */
7632 if (comp_dir != NULL)
7633 result_reader->comp_dir = DW_STRING (comp_dir);
7635 /* Skip dummy compilation units. */
7636 if (info_ptr >= begin_info_ptr + dwo_unit->length
7637 || peek_abbrev_code (abfd, info_ptr) == 0)
7640 *result_info_ptr = info_ptr;
7644 /* Subroutine of init_cutu_and_read_dies to simplify it.
7645 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7646 Returns NULL if the specified DWO unit cannot be found. */
7648 static struct dwo_unit *
7649 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7650 struct die_info *comp_unit_die)
7652 struct dwarf2_cu *cu = this_cu->cu;
7654 struct dwo_unit *dwo_unit;
7655 const char *comp_dir, *dwo_name;
7657 gdb_assert (cu != NULL);
7659 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7660 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7661 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7663 if (this_cu->is_debug_types)
7665 struct signatured_type *sig_type;
7667 /* Since this_cu is the first member of struct signatured_type,
7668 we can go from a pointer to one to a pointer to the other. */
7669 sig_type = (struct signatured_type *) this_cu;
7670 signature = sig_type->signature;
7671 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7675 struct attribute *attr;
7677 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7679 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7681 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7682 signature = DW_UNSND (attr);
7683 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7690 /* Subroutine of init_cutu_and_read_dies to simplify it.
7691 See it for a description of the parameters.
7692 Read a TU directly from a DWO file, bypassing the stub. */
7695 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7696 int use_existing_cu, int keep,
7697 die_reader_func_ftype *die_reader_func,
7700 std::unique_ptr<dwarf2_cu> new_cu;
7701 struct signatured_type *sig_type;
7702 struct die_reader_specs reader;
7703 const gdb_byte *info_ptr;
7704 struct die_info *comp_unit_die;
7706 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7708 /* Verify we can do the following downcast, and that we have the
7710 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7711 sig_type = (struct signatured_type *) this_cu;
7712 gdb_assert (sig_type->dwo_unit != NULL);
7714 if (use_existing_cu && this_cu->cu != NULL)
7716 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7717 /* There's no need to do the rereading_dwo_cu handling that
7718 init_cutu_and_read_dies does since we don't read the stub. */
7722 /* If !use_existing_cu, this_cu->cu must be NULL. */
7723 gdb_assert (this_cu->cu == NULL);
7724 new_cu.reset (new dwarf2_cu (this_cu));
7727 /* A future optimization, if needed, would be to use an existing
7728 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7729 could share abbrev tables. */
7731 /* The abbreviation table used by READER, this must live at least as long as
7733 abbrev_table_up dwo_abbrev_table;
7735 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7736 NULL /* stub_comp_unit_die */,
7737 sig_type->dwo_unit->dwo_file->comp_dir,
7739 &comp_unit_die, &has_children,
7740 &dwo_abbrev_table) == 0)
7746 /* All the "real" work is done here. */
7747 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7749 /* This duplicates the code in init_cutu_and_read_dies,
7750 but the alternative is making the latter more complex.
7751 This function is only for the special case of using DWO files directly:
7752 no point in overly complicating the general case just to handle this. */
7753 if (new_cu != NULL && keep)
7755 /* Link this CU into read_in_chain. */
7756 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7757 dwarf2_per_objfile->read_in_chain = this_cu;
7758 /* The chain owns it now. */
7763 /* Initialize a CU (or TU) and read its DIEs.
7764 If the CU defers to a DWO file, read the DWO file as well.
7766 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7767 Otherwise the table specified in the comp unit header is read in and used.
7768 This is an optimization for when we already have the abbrev table.
7770 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7771 Otherwise, a new CU is allocated with xmalloc.
7773 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7774 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7776 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7777 linker) then DIE_READER_FUNC will not get called. */
7780 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7781 struct abbrev_table *abbrev_table,
7782 int use_existing_cu, int keep,
7783 die_reader_func_ftype *die_reader_func,
7786 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7787 struct objfile *objfile = dwarf2_per_objfile->objfile;
7788 struct dwarf2_section_info *section = this_cu->section;
7789 bfd *abfd = get_section_bfd_owner (section);
7790 struct dwarf2_cu *cu;
7791 const gdb_byte *begin_info_ptr, *info_ptr;
7792 struct die_reader_specs reader;
7793 struct die_info *comp_unit_die;
7795 struct attribute *attr;
7796 struct signatured_type *sig_type = NULL;
7797 struct dwarf2_section_info *abbrev_section;
7798 /* Non-zero if CU currently points to a DWO file and we need to
7799 reread it. When this happens we need to reread the skeleton die
7800 before we can reread the DWO file (this only applies to CUs, not TUs). */
7801 int rereading_dwo_cu = 0;
7803 if (dwarf_die_debug)
7804 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
7805 this_cu->is_debug_types ? "type" : "comp",
7806 to_underlying (this_cu->sect_off));
7808 if (use_existing_cu)
7811 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7812 file (instead of going through the stub), short-circuit all of this. */
7813 if (this_cu->reading_dwo_directly)
7815 /* Narrow down the scope of possibilities to have to understand. */
7816 gdb_assert (this_cu->is_debug_types);
7817 gdb_assert (abbrev_table == NULL);
7818 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7819 die_reader_func, data);
7823 /* This is cheap if the section is already read in. */
7824 dwarf2_read_section (objfile, section);
7826 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7828 abbrev_section = get_abbrev_section_for_cu (this_cu);
7830 std::unique_ptr<dwarf2_cu> new_cu;
7831 if (use_existing_cu && this_cu->cu != NULL)
7834 /* If this CU is from a DWO file we need to start over, we need to
7835 refetch the attributes from the skeleton CU.
7836 This could be optimized by retrieving those attributes from when we
7837 were here the first time: the previous comp_unit_die was stored in
7838 comp_unit_obstack. But there's no data yet that we need this
7840 if (cu->dwo_unit != NULL)
7841 rereading_dwo_cu = 1;
7845 /* If !use_existing_cu, this_cu->cu must be NULL. */
7846 gdb_assert (this_cu->cu == NULL);
7847 new_cu.reset (new dwarf2_cu (this_cu));
7851 /* Get the header. */
7852 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7854 /* We already have the header, there's no need to read it in again. */
7855 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7859 if (this_cu->is_debug_types)
7861 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7862 &cu->header, section,
7863 abbrev_section, info_ptr,
7866 /* Since per_cu is the first member of struct signatured_type,
7867 we can go from a pointer to one to a pointer to the other. */
7868 sig_type = (struct signatured_type *) this_cu;
7869 gdb_assert (sig_type->signature == cu->header.signature);
7870 gdb_assert (sig_type->type_offset_in_tu
7871 == cu->header.type_cu_offset_in_tu);
7872 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7874 /* LENGTH has not been set yet for type units if we're
7875 using .gdb_index. */
7876 this_cu->length = get_cu_length (&cu->header);
7878 /* Establish the type offset that can be used to lookup the type. */
7879 sig_type->type_offset_in_section =
7880 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7882 this_cu->dwarf_version = cu->header.version;
7886 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7887 &cu->header, section,
7890 rcuh_kind::COMPILE);
7892 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7893 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7894 this_cu->dwarf_version = cu->header.version;
7898 /* Skip dummy compilation units. */
7899 if (info_ptr >= begin_info_ptr + this_cu->length
7900 || peek_abbrev_code (abfd, info_ptr) == 0)
7903 /* If we don't have them yet, read the abbrevs for this compilation unit.
7904 And if we need to read them now, make sure they're freed when we're
7905 done (own the table through ABBREV_TABLE_HOLDER). */
7906 abbrev_table_up abbrev_table_holder;
7907 if (abbrev_table != NULL)
7908 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7912 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7913 cu->header.abbrev_sect_off);
7914 abbrev_table = abbrev_table_holder.get ();
7917 /* Read the top level CU/TU die. */
7918 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7919 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7921 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7922 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7923 table from the DWO file and pass the ownership over to us. It will be
7924 referenced from READER, so we must make sure to free it after we're done
7927 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7928 DWO CU, that this test will fail (the attribute will not be present). */
7929 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7930 abbrev_table_up dwo_abbrev_table;
7933 struct dwo_unit *dwo_unit;
7934 struct die_info *dwo_comp_unit_die;
7938 complaint (&symfile_complaints,
7939 _("compilation unit with DW_AT_GNU_dwo_name"
7940 " has children (offset 0x%x) [in module %s]"),
7941 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
7943 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7944 if (dwo_unit != NULL)
7946 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7947 comp_unit_die, NULL,
7949 &dwo_comp_unit_die, &has_children,
7950 &dwo_abbrev_table) == 0)
7955 comp_unit_die = dwo_comp_unit_die;
7959 /* Yikes, we couldn't find the rest of the DIE, we only have
7960 the stub. A complaint has already been logged. There's
7961 not much more we can do except pass on the stub DIE to
7962 die_reader_func. We don't want to throw an error on bad
7967 /* All of the above is setup for this call. Yikes. */
7968 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7970 /* Done, clean up. */
7971 if (new_cu != NULL && keep)
7973 /* Link this CU into read_in_chain. */
7974 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7975 dwarf2_per_objfile->read_in_chain = this_cu;
7976 /* The chain owns it now. */
7981 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7982 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7983 to have already done the lookup to find the DWO file).
7985 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7986 THIS_CU->is_debug_types, but nothing else.
7988 We fill in THIS_CU->length.
7990 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7991 linker) then DIE_READER_FUNC will not get called.
7993 THIS_CU->cu is always freed when done.
7994 This is done in order to not leave THIS_CU->cu in a state where we have
7995 to care whether it refers to the "main" CU or the DWO CU. */
7998 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7999 struct dwo_file *dwo_file,
8000 die_reader_func_ftype *die_reader_func,
8003 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
8004 struct objfile *objfile = dwarf2_per_objfile->objfile;
8005 struct dwarf2_section_info *section = this_cu->section;
8006 bfd *abfd = get_section_bfd_owner (section);
8007 struct dwarf2_section_info *abbrev_section;
8008 const gdb_byte *begin_info_ptr, *info_ptr;
8009 struct die_reader_specs reader;
8010 struct die_info *comp_unit_die;
8013 if (dwarf_die_debug)
8014 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
8015 this_cu->is_debug_types ? "type" : "comp",
8016 to_underlying (this_cu->sect_off));
8018 gdb_assert (this_cu->cu == NULL);
8020 abbrev_section = (dwo_file != NULL
8021 ? &dwo_file->sections.abbrev
8022 : get_abbrev_section_for_cu (this_cu));
8024 /* This is cheap if the section is already read in. */
8025 dwarf2_read_section (objfile, section);
8027 struct dwarf2_cu cu (this_cu);
8029 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
8030 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
8031 &cu.header, section,
8032 abbrev_section, info_ptr,
8033 (this_cu->is_debug_types
8035 : rcuh_kind::COMPILE));
8037 this_cu->length = get_cu_length (&cu.header);
8039 /* Skip dummy compilation units. */
8040 if (info_ptr >= begin_info_ptr + this_cu->length
8041 || peek_abbrev_code (abfd, info_ptr) == 0)
8044 abbrev_table_up abbrev_table
8045 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
8046 cu.header.abbrev_sect_off);
8048 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
8049 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8051 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8054 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
8055 does not lookup the specified DWO file.
8056 This cannot be used to read DWO files.
8058 THIS_CU->cu is always freed when done.
8059 This is done in order to not leave THIS_CU->cu in a state where we have
8060 to care whether it refers to the "main" CU or the DWO CU.
8061 We can revisit this if the data shows there's a performance issue. */
8064 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
8065 die_reader_func_ftype *die_reader_func,
8068 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
8071 /* Type Unit Groups.
8073 Type Unit Groups are a way to collapse the set of all TUs (type units) into
8074 a more manageable set. The grouping is done by DW_AT_stmt_list entry
8075 so that all types coming from the same compilation (.o file) are grouped
8076 together. A future step could be to put the types in the same symtab as
8077 the CU the types ultimately came from. */
8080 hash_type_unit_group (const void *item)
8082 const struct type_unit_group *tu_group
8083 = (const struct type_unit_group *) item;
8085 return hash_stmt_list_entry (&tu_group->hash);
8089 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
8091 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
8092 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
8094 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
8097 /* Allocate a hash table for type unit groups. */
8100 allocate_type_unit_groups_table (struct objfile *objfile)
8102 return htab_create_alloc_ex (3,
8103 hash_type_unit_group,
8106 &objfile->objfile_obstack,
8107 hashtab_obstack_allocate,
8108 dummy_obstack_deallocate);
8111 /* Type units that don't have DW_AT_stmt_list are grouped into their own
8112 partial symtabs. We combine several TUs per psymtab to not let the size
8113 of any one psymtab grow too big. */
8114 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
8115 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
8117 /* Helper routine for get_type_unit_group.
8118 Create the type_unit_group object used to hold one or more TUs. */
8120 static struct type_unit_group *
8121 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
8123 struct dwarf2_per_objfile *dwarf2_per_objfile
8124 = cu->per_cu->dwarf2_per_objfile;
8125 struct objfile *objfile = dwarf2_per_objfile->objfile;
8126 struct dwarf2_per_cu_data *per_cu;
8127 struct type_unit_group *tu_group;
8129 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8130 struct type_unit_group);
8131 per_cu = &tu_group->per_cu;
8132 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8134 if (dwarf2_per_objfile->using_index)
8136 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8137 struct dwarf2_per_cu_quick_data);
8141 unsigned int line_offset = to_underlying (line_offset_struct);
8142 struct partial_symtab *pst;
8145 /* Give the symtab a useful name for debug purposes. */
8146 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
8147 name = xstrprintf ("<type_units_%d>",
8148 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
8150 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
8152 pst = create_partial_symtab (per_cu, name);
8158 tu_group->hash.dwo_unit = cu->dwo_unit;
8159 tu_group->hash.line_sect_off = line_offset_struct;
8164 /* Look up the type_unit_group for type unit CU, and create it if necessary.
8165 STMT_LIST is a DW_AT_stmt_list attribute. */
8167 static struct type_unit_group *
8168 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
8170 struct dwarf2_per_objfile *dwarf2_per_objfile
8171 = cu->per_cu->dwarf2_per_objfile;
8172 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8173 struct type_unit_group *tu_group;
8175 unsigned int line_offset;
8176 struct type_unit_group type_unit_group_for_lookup;
8178 if (dwarf2_per_objfile->type_unit_groups == NULL)
8180 dwarf2_per_objfile->type_unit_groups =
8181 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
8184 /* Do we need to create a new group, or can we use an existing one? */
8188 line_offset = DW_UNSND (stmt_list);
8189 ++tu_stats->nr_symtab_sharers;
8193 /* Ugh, no stmt_list. Rare, but we have to handle it.
8194 We can do various things here like create one group per TU or
8195 spread them over multiple groups to split up the expansion work.
8196 To avoid worst case scenarios (too many groups or too large groups)
8197 we, umm, group them in bunches. */
8198 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
8199 | (tu_stats->nr_stmt_less_type_units
8200 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
8201 ++tu_stats->nr_stmt_less_type_units;
8204 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
8205 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
8206 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8207 &type_unit_group_for_lookup, INSERT);
8210 tu_group = (struct type_unit_group *) *slot;
8211 gdb_assert (tu_group != NULL);
8215 sect_offset line_offset_struct = (sect_offset) line_offset;
8216 tu_group = create_type_unit_group (cu, line_offset_struct);
8218 ++tu_stats->nr_symtabs;
8224 /* Partial symbol tables. */
8226 /* Create a psymtab named NAME and assign it to PER_CU.
8228 The caller must fill in the following details:
8229 dirname, textlow, texthigh. */
8231 static struct partial_symtab *
8232 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8234 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
8235 struct partial_symtab *pst;
8237 pst = start_psymtab_common (objfile, name, 0,
8238 objfile->global_psymbols,
8239 objfile->static_psymbols);
8241 pst->psymtabs_addrmap_supported = 1;
8243 /* This is the glue that links PST into GDB's symbol API. */
8244 pst->read_symtab_private = per_cu;
8245 pst->read_symtab = dwarf2_read_symtab;
8246 per_cu->v.psymtab = pst;
8251 /* The DATA object passed to process_psymtab_comp_unit_reader has this
8254 struct process_psymtab_comp_unit_data
8256 /* True if we are reading a DW_TAG_partial_unit. */
8258 int want_partial_unit;
8260 /* The "pretend" language that is used if the CU doesn't declare a
8263 enum language pretend_language;
8266 /* die_reader_func for process_psymtab_comp_unit. */
8269 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8270 const gdb_byte *info_ptr,
8271 struct die_info *comp_unit_die,
8275 struct dwarf2_cu *cu = reader->cu;
8276 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8277 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8278 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8280 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8281 struct partial_symtab *pst;
8282 enum pc_bounds_kind cu_bounds_kind;
8283 const char *filename;
8284 struct process_psymtab_comp_unit_data *info
8285 = (struct process_psymtab_comp_unit_data *) data;
8287 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8290 gdb_assert (! per_cu->is_debug_types);
8292 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8294 cu->list_in_scope = &file_symbols;
8296 /* Allocate a new partial symbol table structure. */
8297 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8298 if (filename == NULL)
8301 pst = create_partial_symtab (per_cu, filename);
8303 /* This must be done before calling dwarf2_build_include_psymtabs. */
8304 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8306 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8308 dwarf2_find_base_address (comp_unit_die, cu);
8310 /* Possibly set the default values of LOWPC and HIGHPC from
8312 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8313 &best_highpc, cu, pst);
8314 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8315 /* Store the contiguous range if it is not empty; it can be empty for
8316 CUs with no code. */
8317 addrmap_set_empty (objfile->psymtabs_addrmap,
8318 gdbarch_adjust_dwarf2_addr (gdbarch,
8319 best_lowpc + baseaddr),
8320 gdbarch_adjust_dwarf2_addr (gdbarch,
8321 best_highpc + baseaddr) - 1,
8324 /* Check if comp unit has_children.
8325 If so, read the rest of the partial symbols from this comp unit.
8326 If not, there's no more debug_info for this comp unit. */
8329 struct partial_die_info *first_die;
8330 CORE_ADDR lowpc, highpc;
8332 lowpc = ((CORE_ADDR) -1);
8333 highpc = ((CORE_ADDR) 0);
8335 first_die = load_partial_dies (reader, info_ptr, 1);
8337 scan_partial_symbols (first_die, &lowpc, &highpc,
8338 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8340 /* If we didn't find a lowpc, set it to highpc to avoid
8341 complaints from `maint check'. */
8342 if (lowpc == ((CORE_ADDR) -1))
8345 /* If the compilation unit didn't have an explicit address range,
8346 then use the information extracted from its child dies. */
8347 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8350 best_highpc = highpc;
8353 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8354 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
8356 end_psymtab_common (objfile, pst);
8358 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8361 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8362 struct dwarf2_per_cu_data *iter;
8364 /* Fill in 'dependencies' here; we fill in 'users' in a
8366 pst->number_of_dependencies = len;
8368 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8370 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8373 pst->dependencies[i] = iter->v.psymtab;
8375 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8378 /* Get the list of files included in the current compilation unit,
8379 and build a psymtab for each of them. */
8380 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8382 if (dwarf_read_debug)
8384 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8386 fprintf_unfiltered (gdb_stdlog,
8387 "Psymtab for %s unit @0x%x: %s - %s"
8388 ", %d global, %d static syms\n",
8389 per_cu->is_debug_types ? "type" : "comp",
8390 to_underlying (per_cu->sect_off),
8391 paddress (gdbarch, pst->textlow),
8392 paddress (gdbarch, pst->texthigh),
8393 pst->n_global_syms, pst->n_static_syms);
8397 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8398 Process compilation unit THIS_CU for a psymtab. */
8401 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8402 int want_partial_unit,
8403 enum language pretend_language)
8405 /* If this compilation unit was already read in, free the
8406 cached copy in order to read it in again. This is
8407 necessary because we skipped some symbols when we first
8408 read in the compilation unit (see load_partial_dies).
8409 This problem could be avoided, but the benefit is unclear. */
8410 if (this_cu->cu != NULL)
8411 free_one_cached_comp_unit (this_cu);
8413 if (this_cu->is_debug_types)
8414 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8418 process_psymtab_comp_unit_data info;
8419 info.want_partial_unit = want_partial_unit;
8420 info.pretend_language = pretend_language;
8421 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8422 process_psymtab_comp_unit_reader, &info);
8425 /* Age out any secondary CUs. */
8426 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8429 /* Reader function for build_type_psymtabs. */
8432 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8433 const gdb_byte *info_ptr,
8434 struct die_info *type_unit_die,
8438 struct dwarf2_per_objfile *dwarf2_per_objfile
8439 = reader->cu->per_cu->dwarf2_per_objfile;
8440 struct objfile *objfile = dwarf2_per_objfile->objfile;
8441 struct dwarf2_cu *cu = reader->cu;
8442 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8443 struct signatured_type *sig_type;
8444 struct type_unit_group *tu_group;
8445 struct attribute *attr;
8446 struct partial_die_info *first_die;
8447 CORE_ADDR lowpc, highpc;
8448 struct partial_symtab *pst;
8450 gdb_assert (data == NULL);
8451 gdb_assert (per_cu->is_debug_types);
8452 sig_type = (struct signatured_type *) per_cu;
8457 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8458 tu_group = get_type_unit_group (cu, attr);
8460 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8462 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8463 cu->list_in_scope = &file_symbols;
8464 pst = create_partial_symtab (per_cu, "");
8467 first_die = load_partial_dies (reader, info_ptr, 1);
8469 lowpc = (CORE_ADDR) -1;
8470 highpc = (CORE_ADDR) 0;
8471 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8473 end_psymtab_common (objfile, pst);
8476 /* Struct used to sort TUs by their abbreviation table offset. */
8478 struct tu_abbrev_offset
8480 struct signatured_type *sig_type;
8481 sect_offset abbrev_offset;
8484 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
8487 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
8489 const struct tu_abbrev_offset * const *a
8490 = (const struct tu_abbrev_offset * const*) ap;
8491 const struct tu_abbrev_offset * const *b
8492 = (const struct tu_abbrev_offset * const*) bp;
8493 sect_offset aoff = (*a)->abbrev_offset;
8494 sect_offset boff = (*b)->abbrev_offset;
8496 return (aoff > boff) - (aoff < boff);
8499 /* Efficiently read all the type units.
8500 This does the bulk of the work for build_type_psymtabs.
8502 The efficiency is because we sort TUs by the abbrev table they use and
8503 only read each abbrev table once. In one program there are 200K TUs
8504 sharing 8K abbrev tables.
8506 The main purpose of this function is to support building the
8507 dwarf2_per_objfile->type_unit_groups table.
8508 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8509 can collapse the search space by grouping them by stmt_list.
8510 The savings can be significant, in the same program from above the 200K TUs
8511 share 8K stmt_list tables.
8513 FUNC is expected to call get_type_unit_group, which will create the
8514 struct type_unit_group if necessary and add it to
8515 dwarf2_per_objfile->type_unit_groups. */
8518 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8520 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8521 struct cleanup *cleanups;
8522 abbrev_table_up abbrev_table;
8523 sect_offset abbrev_offset;
8524 struct tu_abbrev_offset *sorted_by_abbrev;
8527 /* It's up to the caller to not call us multiple times. */
8528 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8530 if (dwarf2_per_objfile->n_type_units == 0)
8533 /* TUs typically share abbrev tables, and there can be way more TUs than
8534 abbrev tables. Sort by abbrev table to reduce the number of times we
8535 read each abbrev table in.
8536 Alternatives are to punt or to maintain a cache of abbrev tables.
8537 This is simpler and efficient enough for now.
8539 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8540 symtab to use). Typically TUs with the same abbrev offset have the same
8541 stmt_list value too so in practice this should work well.
8543 The basic algorithm here is:
8545 sort TUs by abbrev table
8546 for each TU with same abbrev table:
8547 read abbrev table if first user
8548 read TU top level DIE
8549 [IWBN if DWO skeletons had DW_AT_stmt_list]
8552 if (dwarf_read_debug)
8553 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8555 /* Sort in a separate table to maintain the order of all_type_units
8556 for .gdb_index: TU indices directly index all_type_units. */
8557 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
8558 dwarf2_per_objfile->n_type_units);
8559 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8561 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8563 sorted_by_abbrev[i].sig_type = sig_type;
8564 sorted_by_abbrev[i].abbrev_offset =
8565 read_abbrev_offset (dwarf2_per_objfile,
8566 sig_type->per_cu.section,
8567 sig_type->per_cu.sect_off);
8569 cleanups = make_cleanup (xfree, sorted_by_abbrev);
8570 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
8571 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
8573 abbrev_offset = (sect_offset) ~(unsigned) 0;
8575 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8577 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8579 /* Switch to the next abbrev table if necessary. */
8580 if (abbrev_table == NULL
8581 || tu->abbrev_offset != abbrev_offset)
8583 abbrev_offset = tu->abbrev_offset;
8585 abbrev_table_read_table (dwarf2_per_objfile,
8586 &dwarf2_per_objfile->abbrev,
8588 ++tu_stats->nr_uniq_abbrev_tables;
8591 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table.get (),
8592 0, 0, build_type_psymtabs_reader, NULL);
8595 do_cleanups (cleanups);
8598 /* Print collected type unit statistics. */
8601 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8603 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8605 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8606 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8607 dwarf2_per_objfile->n_type_units);
8608 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8609 tu_stats->nr_uniq_abbrev_tables);
8610 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8611 tu_stats->nr_symtabs);
8612 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8613 tu_stats->nr_symtab_sharers);
8614 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8615 tu_stats->nr_stmt_less_type_units);
8616 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8617 tu_stats->nr_all_type_units_reallocs);
8620 /* Traversal function for build_type_psymtabs. */
8623 build_type_psymtab_dependencies (void **slot, void *info)
8625 struct dwarf2_per_objfile *dwarf2_per_objfile
8626 = (struct dwarf2_per_objfile *) info;
8627 struct objfile *objfile = dwarf2_per_objfile->objfile;
8628 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8629 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8630 struct partial_symtab *pst = per_cu->v.psymtab;
8631 int len = VEC_length (sig_type_ptr, tu_group->tus);
8632 struct signatured_type *iter;
8635 gdb_assert (len > 0);
8636 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8638 pst->number_of_dependencies = len;
8640 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8642 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8645 gdb_assert (iter->per_cu.is_debug_types);
8646 pst->dependencies[i] = iter->per_cu.v.psymtab;
8647 iter->type_unit_group = tu_group;
8650 VEC_free (sig_type_ptr, tu_group->tus);
8655 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8656 Build partial symbol tables for the .debug_types comp-units. */
8659 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8661 if (! create_all_type_units (dwarf2_per_objfile))
8664 build_type_psymtabs_1 (dwarf2_per_objfile);
8667 /* Traversal function for process_skeletonless_type_unit.
8668 Read a TU in a DWO file and build partial symbols for it. */
8671 process_skeletonless_type_unit (void **slot, void *info)
8673 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8674 struct dwarf2_per_objfile *dwarf2_per_objfile
8675 = (struct dwarf2_per_objfile *) info;
8676 struct signatured_type find_entry, *entry;
8678 /* If this TU doesn't exist in the global table, add it and read it in. */
8680 if (dwarf2_per_objfile->signatured_types == NULL)
8682 dwarf2_per_objfile->signatured_types
8683 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8686 find_entry.signature = dwo_unit->signature;
8687 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8689 /* If we've already seen this type there's nothing to do. What's happening
8690 is we're doing our own version of comdat-folding here. */
8694 /* This does the job that create_all_type_units would have done for
8696 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8697 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8700 /* This does the job that build_type_psymtabs_1 would have done. */
8701 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8702 build_type_psymtabs_reader, NULL);
8707 /* Traversal function for process_skeletonless_type_units. */
8710 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8712 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8714 if (dwo_file->tus != NULL)
8716 htab_traverse_noresize (dwo_file->tus,
8717 process_skeletonless_type_unit, info);
8723 /* Scan all TUs of DWO files, verifying we've processed them.
8724 This is needed in case a TU was emitted without its skeleton.
8725 Note: This can't be done until we know what all the DWO files are. */
8728 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8730 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8731 if (get_dwp_file (dwarf2_per_objfile) == NULL
8732 && dwarf2_per_objfile->dwo_files != NULL)
8734 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8735 process_dwo_file_for_skeletonless_type_units,
8736 dwarf2_per_objfile);
8740 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8743 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8747 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8749 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8750 struct partial_symtab *pst = per_cu->v.psymtab;
8756 for (j = 0; j < pst->number_of_dependencies; ++j)
8758 /* Set the 'user' field only if it is not already set. */
8759 if (pst->dependencies[j]->user == NULL)
8760 pst->dependencies[j]->user = pst;
8765 /* Build the partial symbol table by doing a quick pass through the
8766 .debug_info and .debug_abbrev sections. */
8769 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8771 struct cleanup *back_to;
8773 struct objfile *objfile = dwarf2_per_objfile->objfile;
8775 if (dwarf_read_debug)
8777 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8778 objfile_name (objfile));
8781 dwarf2_per_objfile->reading_partial_symbols = 1;
8783 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8785 /* Any cached compilation units will be linked by the per-objfile
8786 read_in_chain. Make sure to free them when we're done. */
8787 back_to = make_cleanup (free_cached_comp_units, dwarf2_per_objfile);
8789 build_type_psymtabs (dwarf2_per_objfile);
8791 create_all_comp_units (dwarf2_per_objfile);
8793 /* Create a temporary address map on a temporary obstack. We later
8794 copy this to the final obstack. */
8795 auto_obstack temp_obstack;
8797 scoped_restore save_psymtabs_addrmap
8798 = make_scoped_restore (&objfile->psymtabs_addrmap,
8799 addrmap_create_mutable (&temp_obstack));
8801 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8803 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8805 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8808 /* This has to wait until we read the CUs, we need the list of DWOs. */
8809 process_skeletonless_type_units (dwarf2_per_objfile);
8811 /* Now that all TUs have been processed we can fill in the dependencies. */
8812 if (dwarf2_per_objfile->type_unit_groups != NULL)
8814 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8815 build_type_psymtab_dependencies, dwarf2_per_objfile);
8818 if (dwarf_read_debug)
8819 print_tu_stats (dwarf2_per_objfile);
8821 set_partial_user (dwarf2_per_objfile);
8823 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8824 &objfile->objfile_obstack);
8825 /* At this point we want to keep the address map. */
8826 save_psymtabs_addrmap.release ();
8828 do_cleanups (back_to);
8830 if (dwarf_read_debug)
8831 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8832 objfile_name (objfile));
8835 /* die_reader_func for load_partial_comp_unit. */
8838 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8839 const gdb_byte *info_ptr,
8840 struct die_info *comp_unit_die,
8844 struct dwarf2_cu *cu = reader->cu;
8846 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8848 /* Check if comp unit has_children.
8849 If so, read the rest of the partial symbols from this comp unit.
8850 If not, there's no more debug_info for this comp unit. */
8852 load_partial_dies (reader, info_ptr, 0);
8855 /* Load the partial DIEs for a secondary CU into memory.
8856 This is also used when rereading a primary CU with load_all_dies. */
8859 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8861 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8862 load_partial_comp_unit_reader, NULL);
8866 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8867 struct dwarf2_section_info *section,
8868 struct dwarf2_section_info *abbrev_section,
8869 unsigned int is_dwz,
8872 struct dwarf2_per_cu_data ***all_comp_units)
8874 const gdb_byte *info_ptr;
8875 struct objfile *objfile = dwarf2_per_objfile->objfile;
8877 if (dwarf_read_debug)
8878 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8879 get_section_name (section),
8880 get_section_file_name (section));
8882 dwarf2_read_section (objfile, section);
8884 info_ptr = section->buffer;
8886 while (info_ptr < section->buffer + section->size)
8888 struct dwarf2_per_cu_data *this_cu;
8890 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8892 comp_unit_head cu_header;
8893 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8894 abbrev_section, info_ptr,
8895 rcuh_kind::COMPILE);
8897 /* Save the compilation unit for later lookup. */
8898 if (cu_header.unit_type != DW_UT_type)
8900 this_cu = XOBNEW (&objfile->objfile_obstack,
8901 struct dwarf2_per_cu_data);
8902 memset (this_cu, 0, sizeof (*this_cu));
8906 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8907 struct signatured_type);
8908 memset (sig_type, 0, sizeof (*sig_type));
8909 sig_type->signature = cu_header.signature;
8910 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8911 this_cu = &sig_type->per_cu;
8913 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8914 this_cu->sect_off = sect_off;
8915 this_cu->length = cu_header.length + cu_header.initial_length_size;
8916 this_cu->is_dwz = is_dwz;
8917 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8918 this_cu->section = section;
8920 if (*n_comp_units == *n_allocated)
8923 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
8924 *all_comp_units, *n_allocated);
8926 (*all_comp_units)[*n_comp_units] = this_cu;
8929 info_ptr = info_ptr + this_cu->length;
8933 /* Create a list of all compilation units in OBJFILE.
8934 This is only done for -readnow and building partial symtabs. */
8937 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8941 struct dwarf2_per_cu_data **all_comp_units;
8942 struct dwz_file *dwz;
8943 struct objfile *objfile = dwarf2_per_objfile->objfile;
8947 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
8949 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8950 &dwarf2_per_objfile->abbrev, 0,
8951 &n_allocated, &n_comp_units, &all_comp_units);
8953 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8955 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8956 1, &n_allocated, &n_comp_units,
8959 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
8960 struct dwarf2_per_cu_data *,
8962 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
8963 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
8964 xfree (all_comp_units);
8965 dwarf2_per_objfile->n_comp_units = n_comp_units;
8968 /* Process all loaded DIEs for compilation unit CU, starting at
8969 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8970 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8971 DW_AT_ranges). See the comments of add_partial_subprogram on how
8972 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8975 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8976 CORE_ADDR *highpc, int set_addrmap,
8977 struct dwarf2_cu *cu)
8979 struct partial_die_info *pdi;
8981 /* Now, march along the PDI's, descending into ones which have
8982 interesting children but skipping the children of the other ones,
8983 until we reach the end of the compilation unit. */
8989 fixup_partial_die (pdi, cu);
8991 /* Anonymous namespaces or modules have no name but have interesting
8992 children, so we need to look at them. Ditto for anonymous
8995 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8996 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8997 || pdi->tag == DW_TAG_imported_unit
8998 || pdi->tag == DW_TAG_inlined_subroutine)
9002 case DW_TAG_subprogram:
9003 case DW_TAG_inlined_subroutine:
9004 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9006 case DW_TAG_constant:
9007 case DW_TAG_variable:
9008 case DW_TAG_typedef:
9009 case DW_TAG_union_type:
9010 if (!pdi->is_declaration)
9012 add_partial_symbol (pdi, cu);
9015 case DW_TAG_class_type:
9016 case DW_TAG_interface_type:
9017 case DW_TAG_structure_type:
9018 if (!pdi->is_declaration)
9020 add_partial_symbol (pdi, cu);
9022 if (cu->language == language_rust && pdi->has_children)
9023 scan_partial_symbols (pdi->die_child, lowpc, highpc,
9026 case DW_TAG_enumeration_type:
9027 if (!pdi->is_declaration)
9028 add_partial_enumeration (pdi, cu);
9030 case DW_TAG_base_type:
9031 case DW_TAG_subrange_type:
9032 /* File scope base type definitions are added to the partial
9034 add_partial_symbol (pdi, cu);
9036 case DW_TAG_namespace:
9037 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
9040 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
9042 case DW_TAG_imported_unit:
9044 struct dwarf2_per_cu_data *per_cu;
9046 /* For now we don't handle imported units in type units. */
9047 if (cu->per_cu->is_debug_types)
9049 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9050 " supported in type units [in module %s]"),
9051 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9054 per_cu = dwarf2_find_containing_comp_unit
9055 (pdi->d.sect_off, pdi->is_dwz,
9056 cu->per_cu->dwarf2_per_objfile);
9058 /* Go read the partial unit, if needed. */
9059 if (per_cu->v.psymtab == NULL)
9060 process_psymtab_comp_unit (per_cu, 1, cu->language);
9062 VEC_safe_push (dwarf2_per_cu_ptr,
9063 cu->per_cu->imported_symtabs, per_cu);
9066 case DW_TAG_imported_declaration:
9067 add_partial_symbol (pdi, cu);
9074 /* If the die has a sibling, skip to the sibling. */
9076 pdi = pdi->die_sibling;
9080 /* Functions used to compute the fully scoped name of a partial DIE.
9082 Normally, this is simple. For C++, the parent DIE's fully scoped
9083 name is concatenated with "::" and the partial DIE's name.
9084 Enumerators are an exception; they use the scope of their parent
9085 enumeration type, i.e. the name of the enumeration type is not
9086 prepended to the enumerator.
9088 There are two complexities. One is DW_AT_specification; in this
9089 case "parent" means the parent of the target of the specification,
9090 instead of the direct parent of the DIE. The other is compilers
9091 which do not emit DW_TAG_namespace; in this case we try to guess
9092 the fully qualified name of structure types from their members'
9093 linkage names. This must be done using the DIE's children rather
9094 than the children of any DW_AT_specification target. We only need
9095 to do this for structures at the top level, i.e. if the target of
9096 any DW_AT_specification (if any; otherwise the DIE itself) does not
9099 /* Compute the scope prefix associated with PDI's parent, in
9100 compilation unit CU. The result will be allocated on CU's
9101 comp_unit_obstack, or a copy of the already allocated PDI->NAME
9102 field. NULL is returned if no prefix is necessary. */
9104 partial_die_parent_scope (struct partial_die_info *pdi,
9105 struct dwarf2_cu *cu)
9107 const char *grandparent_scope;
9108 struct partial_die_info *parent, *real_pdi;
9110 /* We need to look at our parent DIE; if we have a DW_AT_specification,
9111 then this means the parent of the specification DIE. */
9114 while (real_pdi->has_specification)
9115 real_pdi = find_partial_die (real_pdi->spec_offset,
9116 real_pdi->spec_is_dwz, cu);
9118 parent = real_pdi->die_parent;
9122 if (parent->scope_set)
9123 return parent->scope;
9125 fixup_partial_die (parent, cu);
9127 grandparent_scope = partial_die_parent_scope (parent, cu);
9129 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9130 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9131 Work around this problem here. */
9132 if (cu->language == language_cplus
9133 && parent->tag == DW_TAG_namespace
9134 && strcmp (parent->name, "::") == 0
9135 && grandparent_scope == NULL)
9137 parent->scope = NULL;
9138 parent->scope_set = 1;
9142 if (pdi->tag == DW_TAG_enumerator)
9143 /* Enumerators should not get the name of the enumeration as a prefix. */
9144 parent->scope = grandparent_scope;
9145 else if (parent->tag == DW_TAG_namespace
9146 || parent->tag == DW_TAG_module
9147 || parent->tag == DW_TAG_structure_type
9148 || parent->tag == DW_TAG_class_type
9149 || parent->tag == DW_TAG_interface_type
9150 || parent->tag == DW_TAG_union_type
9151 || parent->tag == DW_TAG_enumeration_type)
9153 if (grandparent_scope == NULL)
9154 parent->scope = parent->name;
9156 parent->scope = typename_concat (&cu->comp_unit_obstack,
9158 parent->name, 0, cu);
9162 /* FIXME drow/2004-04-01: What should we be doing with
9163 function-local names? For partial symbols, we should probably be
9165 complaint (&symfile_complaints,
9166 _("unhandled containing DIE tag %d for DIE at %d"),
9167 parent->tag, to_underlying (pdi->sect_off));
9168 parent->scope = grandparent_scope;
9171 parent->scope_set = 1;
9172 return parent->scope;
9175 /* Return the fully scoped name associated with PDI, from compilation unit
9176 CU. The result will be allocated with malloc. */
9179 partial_die_full_name (struct partial_die_info *pdi,
9180 struct dwarf2_cu *cu)
9182 const char *parent_scope;
9184 /* If this is a template instantiation, we can not work out the
9185 template arguments from partial DIEs. So, unfortunately, we have
9186 to go through the full DIEs. At least any work we do building
9187 types here will be reused if full symbols are loaded later. */
9188 if (pdi->has_template_arguments)
9190 fixup_partial_die (pdi, cu);
9192 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
9194 struct die_info *die;
9195 struct attribute attr;
9196 struct dwarf2_cu *ref_cu = cu;
9198 /* DW_FORM_ref_addr is using section offset. */
9199 attr.name = (enum dwarf_attribute) 0;
9200 attr.form = DW_FORM_ref_addr;
9201 attr.u.unsnd = to_underlying (pdi->sect_off);
9202 die = follow_die_ref (NULL, &attr, &ref_cu);
9204 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
9208 parent_scope = partial_die_parent_scope (pdi, cu);
9209 if (parent_scope == NULL)
9212 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
9216 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
9218 struct dwarf2_per_objfile *dwarf2_per_objfile
9219 = cu->per_cu->dwarf2_per_objfile;
9220 struct objfile *objfile = dwarf2_per_objfile->objfile;
9221 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9223 const char *actual_name = NULL;
9225 char *built_actual_name;
9227 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9229 built_actual_name = partial_die_full_name (pdi, cu);
9230 if (built_actual_name != NULL)
9231 actual_name = built_actual_name;
9233 if (actual_name == NULL)
9234 actual_name = pdi->name;
9238 case DW_TAG_inlined_subroutine:
9239 case DW_TAG_subprogram:
9240 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
9241 if (pdi->is_external || cu->language == language_ada)
9243 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
9244 of the global scope. But in Ada, we want to be able to access
9245 nested procedures globally. So all Ada subprograms are stored
9246 in the global scope. */
9247 add_psymbol_to_list (actual_name, strlen (actual_name),
9248 built_actual_name != NULL,
9249 VAR_DOMAIN, LOC_BLOCK,
9250 &objfile->global_psymbols,
9251 addr, cu->language, objfile);
9255 add_psymbol_to_list (actual_name, strlen (actual_name),
9256 built_actual_name != NULL,
9257 VAR_DOMAIN, LOC_BLOCK,
9258 &objfile->static_psymbols,
9259 addr, cu->language, objfile);
9262 if (pdi->main_subprogram && actual_name != NULL)
9263 set_objfile_main_name (objfile, actual_name, cu->language);
9265 case DW_TAG_constant:
9267 std::vector<partial_symbol *> *list;
9269 if (pdi->is_external)
9270 list = &objfile->global_psymbols;
9272 list = &objfile->static_psymbols;
9273 add_psymbol_to_list (actual_name, strlen (actual_name),
9274 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9275 list, 0, cu->language, objfile);
9278 case DW_TAG_variable:
9280 addr = decode_locdesc (pdi->d.locdesc, cu);
9284 && !dwarf2_per_objfile->has_section_at_zero)
9286 /* A global or static variable may also have been stripped
9287 out by the linker if unused, in which case its address
9288 will be nullified; do not add such variables into partial
9289 symbol table then. */
9291 else if (pdi->is_external)
9294 Don't enter into the minimal symbol tables as there is
9295 a minimal symbol table entry from the ELF symbols already.
9296 Enter into partial symbol table if it has a location
9297 descriptor or a type.
9298 If the location descriptor is missing, new_symbol will create
9299 a LOC_UNRESOLVED symbol, the address of the variable will then
9300 be determined from the minimal symbol table whenever the variable
9302 The address for the partial symbol table entry is not
9303 used by GDB, but it comes in handy for debugging partial symbol
9306 if (pdi->d.locdesc || pdi->has_type)
9307 add_psymbol_to_list (actual_name, strlen (actual_name),
9308 built_actual_name != NULL,
9309 VAR_DOMAIN, LOC_STATIC,
9310 &objfile->global_psymbols,
9312 cu->language, objfile);
9316 int has_loc = pdi->d.locdesc != NULL;
9318 /* Static Variable. Skip symbols whose value we cannot know (those
9319 without location descriptors or constant values). */
9320 if (!has_loc && !pdi->has_const_value)
9322 xfree (built_actual_name);
9326 add_psymbol_to_list (actual_name, strlen (actual_name),
9327 built_actual_name != NULL,
9328 VAR_DOMAIN, LOC_STATIC,
9329 &objfile->static_psymbols,
9330 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
9331 cu->language, objfile);
9334 case DW_TAG_typedef:
9335 case DW_TAG_base_type:
9336 case DW_TAG_subrange_type:
9337 add_psymbol_to_list (actual_name, strlen (actual_name),
9338 built_actual_name != NULL,
9339 VAR_DOMAIN, LOC_TYPEDEF,
9340 &objfile->static_psymbols,
9341 0, cu->language, objfile);
9343 case DW_TAG_imported_declaration:
9344 case DW_TAG_namespace:
9345 add_psymbol_to_list (actual_name, strlen (actual_name),
9346 built_actual_name != NULL,
9347 VAR_DOMAIN, LOC_TYPEDEF,
9348 &objfile->global_psymbols,
9349 0, cu->language, objfile);
9352 add_psymbol_to_list (actual_name, strlen (actual_name),
9353 built_actual_name != NULL,
9354 MODULE_DOMAIN, LOC_TYPEDEF,
9355 &objfile->global_psymbols,
9356 0, cu->language, objfile);
9358 case DW_TAG_class_type:
9359 case DW_TAG_interface_type:
9360 case DW_TAG_structure_type:
9361 case DW_TAG_union_type:
9362 case DW_TAG_enumeration_type:
9363 /* Skip external references. The DWARF standard says in the section
9364 about "Structure, Union, and Class Type Entries": "An incomplete
9365 structure, union or class type is represented by a structure,
9366 union or class entry that does not have a byte size attribute
9367 and that has a DW_AT_declaration attribute." */
9368 if (!pdi->has_byte_size && pdi->is_declaration)
9370 xfree (built_actual_name);
9374 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9375 static vs. global. */
9376 add_psymbol_to_list (actual_name, strlen (actual_name),
9377 built_actual_name != NULL,
9378 STRUCT_DOMAIN, LOC_TYPEDEF,
9379 cu->language == language_cplus
9380 ? &objfile->global_psymbols
9381 : &objfile->static_psymbols,
9382 0, cu->language, objfile);
9385 case DW_TAG_enumerator:
9386 add_psymbol_to_list (actual_name, strlen (actual_name),
9387 built_actual_name != NULL,
9388 VAR_DOMAIN, LOC_CONST,
9389 cu->language == language_cplus
9390 ? &objfile->global_psymbols
9391 : &objfile->static_psymbols,
9392 0, cu->language, objfile);
9398 xfree (built_actual_name);
9401 /* Read a partial die corresponding to a namespace; also, add a symbol
9402 corresponding to that namespace to the symbol table. NAMESPACE is
9403 the name of the enclosing namespace. */
9406 add_partial_namespace (struct partial_die_info *pdi,
9407 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9408 int set_addrmap, struct dwarf2_cu *cu)
9410 /* Add a symbol for the namespace. */
9412 add_partial_symbol (pdi, cu);
9414 /* Now scan partial symbols in that namespace. */
9416 if (pdi->has_children)
9417 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9420 /* Read a partial die corresponding to a Fortran module. */
9423 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9424 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9426 /* Add a symbol for the namespace. */
9428 add_partial_symbol (pdi, cu);
9430 /* Now scan partial symbols in that module. */
9432 if (pdi->has_children)
9433 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9436 /* Read a partial die corresponding to a subprogram or an inlined
9437 subprogram and create a partial symbol for that subprogram.
9438 When the CU language allows it, this routine also defines a partial
9439 symbol for each nested subprogram that this subprogram contains.
9440 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9441 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9443 PDI may also be a lexical block, in which case we simply search
9444 recursively for subprograms defined inside that lexical block.
9445 Again, this is only performed when the CU language allows this
9446 type of definitions. */
9449 add_partial_subprogram (struct partial_die_info *pdi,
9450 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9451 int set_addrmap, struct dwarf2_cu *cu)
9453 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9455 if (pdi->has_pc_info)
9457 if (pdi->lowpc < *lowpc)
9458 *lowpc = pdi->lowpc;
9459 if (pdi->highpc > *highpc)
9460 *highpc = pdi->highpc;
9463 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9464 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9469 baseaddr = ANOFFSET (objfile->section_offsets,
9470 SECT_OFF_TEXT (objfile));
9471 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9472 pdi->lowpc + baseaddr);
9473 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9474 pdi->highpc + baseaddr);
9475 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9476 cu->per_cu->v.psymtab);
9480 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9482 if (!pdi->is_declaration)
9483 /* Ignore subprogram DIEs that do not have a name, they are
9484 illegal. Do not emit a complaint at this point, we will
9485 do so when we convert this psymtab into a symtab. */
9487 add_partial_symbol (pdi, cu);
9491 if (! pdi->has_children)
9494 if (cu->language == language_ada)
9496 pdi = pdi->die_child;
9499 fixup_partial_die (pdi, cu);
9500 if (pdi->tag == DW_TAG_subprogram
9501 || pdi->tag == DW_TAG_inlined_subroutine
9502 || pdi->tag == DW_TAG_lexical_block)
9503 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9504 pdi = pdi->die_sibling;
9509 /* Read a partial die corresponding to an enumeration type. */
9512 add_partial_enumeration (struct partial_die_info *enum_pdi,
9513 struct dwarf2_cu *cu)
9515 struct partial_die_info *pdi;
9517 if (enum_pdi->name != NULL)
9518 add_partial_symbol (enum_pdi, cu);
9520 pdi = enum_pdi->die_child;
9523 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9524 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
9526 add_partial_symbol (pdi, cu);
9527 pdi = pdi->die_sibling;
9531 /* Return the initial uleb128 in the die at INFO_PTR. */
9534 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9536 unsigned int bytes_read;
9538 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9541 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9542 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9544 Return the corresponding abbrev, or NULL if the number is zero (indicating
9545 an empty DIE). In either case *BYTES_READ will be set to the length of
9546 the initial number. */
9548 static struct abbrev_info *
9549 peek_die_abbrev (const die_reader_specs &reader,
9550 const gdb_byte *info_ptr, unsigned int *bytes_read)
9552 dwarf2_cu *cu = reader.cu;
9553 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9554 unsigned int abbrev_number
9555 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9557 if (abbrev_number == 0)
9560 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9563 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9564 " at offset 0x%x [in module %s]"),
9565 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9566 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
9572 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9573 Returns a pointer to the end of a series of DIEs, terminated by an empty
9574 DIE. Any children of the skipped DIEs will also be skipped. */
9576 static const gdb_byte *
9577 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9581 unsigned int bytes_read;
9582 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9585 return info_ptr + bytes_read;
9587 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9591 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9592 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9593 abbrev corresponding to that skipped uleb128 should be passed in
9594 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9597 static const gdb_byte *
9598 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9599 struct abbrev_info *abbrev)
9601 unsigned int bytes_read;
9602 struct attribute attr;
9603 bfd *abfd = reader->abfd;
9604 struct dwarf2_cu *cu = reader->cu;
9605 const gdb_byte *buffer = reader->buffer;
9606 const gdb_byte *buffer_end = reader->buffer_end;
9607 unsigned int form, i;
9609 for (i = 0; i < abbrev->num_attrs; i++)
9611 /* The only abbrev we care about is DW_AT_sibling. */
9612 if (abbrev->attrs[i].name == DW_AT_sibling)
9614 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9615 if (attr.form == DW_FORM_ref_addr)
9616 complaint (&symfile_complaints,
9617 _("ignoring absolute DW_AT_sibling"));
9620 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9621 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9623 if (sibling_ptr < info_ptr)
9624 complaint (&symfile_complaints,
9625 _("DW_AT_sibling points backwards"));
9626 else if (sibling_ptr > reader->buffer_end)
9627 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9633 /* If it isn't DW_AT_sibling, skip this attribute. */
9634 form = abbrev->attrs[i].form;
9638 case DW_FORM_ref_addr:
9639 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9640 and later it is offset sized. */
9641 if (cu->header.version == 2)
9642 info_ptr += cu->header.addr_size;
9644 info_ptr += cu->header.offset_size;
9646 case DW_FORM_GNU_ref_alt:
9647 info_ptr += cu->header.offset_size;
9650 info_ptr += cu->header.addr_size;
9657 case DW_FORM_flag_present:
9658 case DW_FORM_implicit_const:
9670 case DW_FORM_ref_sig8:
9673 case DW_FORM_data16:
9676 case DW_FORM_string:
9677 read_direct_string (abfd, info_ptr, &bytes_read);
9678 info_ptr += bytes_read;
9680 case DW_FORM_sec_offset:
9682 case DW_FORM_GNU_strp_alt:
9683 info_ptr += cu->header.offset_size;
9685 case DW_FORM_exprloc:
9687 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9688 info_ptr += bytes_read;
9690 case DW_FORM_block1:
9691 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9693 case DW_FORM_block2:
9694 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9696 case DW_FORM_block4:
9697 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9701 case DW_FORM_ref_udata:
9702 case DW_FORM_GNU_addr_index:
9703 case DW_FORM_GNU_str_index:
9704 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9706 case DW_FORM_indirect:
9707 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9708 info_ptr += bytes_read;
9709 /* We need to continue parsing from here, so just go back to
9711 goto skip_attribute;
9714 error (_("Dwarf Error: Cannot handle %s "
9715 "in DWARF reader [in module %s]"),
9716 dwarf_form_name (form),
9717 bfd_get_filename (abfd));
9721 if (abbrev->has_children)
9722 return skip_children (reader, info_ptr);
9727 /* Locate ORIG_PDI's sibling.
9728 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9730 static const gdb_byte *
9731 locate_pdi_sibling (const struct die_reader_specs *reader,
9732 struct partial_die_info *orig_pdi,
9733 const gdb_byte *info_ptr)
9735 /* Do we know the sibling already? */
9737 if (orig_pdi->sibling)
9738 return orig_pdi->sibling;
9740 /* Are there any children to deal with? */
9742 if (!orig_pdi->has_children)
9745 /* Skip the children the long way. */
9747 return skip_children (reader, info_ptr);
9750 /* Expand this partial symbol table into a full symbol table. SELF is
9754 dwarf2_read_symtab (struct partial_symtab *self,
9755 struct objfile *objfile)
9757 struct dwarf2_per_objfile *dwarf2_per_objfile
9758 = get_dwarf2_per_objfile (objfile);
9762 warning (_("bug: psymtab for %s is already read in."),
9769 printf_filtered (_("Reading in symbols for %s..."),
9771 gdb_flush (gdb_stdout);
9774 /* If this psymtab is constructed from a debug-only objfile, the
9775 has_section_at_zero flag will not necessarily be correct. We
9776 can get the correct value for this flag by looking at the data
9777 associated with the (presumably stripped) associated objfile. */
9778 if (objfile->separate_debug_objfile_backlink)
9780 struct dwarf2_per_objfile *dpo_backlink
9781 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9783 dwarf2_per_objfile->has_section_at_zero
9784 = dpo_backlink->has_section_at_zero;
9787 dwarf2_per_objfile->reading_partial_symbols = 0;
9789 psymtab_to_symtab_1 (self);
9791 /* Finish up the debug error message. */
9793 printf_filtered (_("done.\n"));
9796 process_cu_includes (dwarf2_per_objfile);
9799 /* Reading in full CUs. */
9801 /* Add PER_CU to the queue. */
9804 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9805 enum language pretend_language)
9807 struct dwarf2_queue_item *item;
9810 item = XNEW (struct dwarf2_queue_item);
9811 item->per_cu = per_cu;
9812 item->pretend_language = pretend_language;
9815 if (dwarf2_queue == NULL)
9816 dwarf2_queue = item;
9818 dwarf2_queue_tail->next = item;
9820 dwarf2_queue_tail = item;
9823 /* If PER_CU is not yet queued, add it to the queue.
9824 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9826 The result is non-zero if PER_CU was queued, otherwise the result is zero
9827 meaning either PER_CU is already queued or it is already loaded.
9829 N.B. There is an invariant here that if a CU is queued then it is loaded.
9830 The caller is required to load PER_CU if we return non-zero. */
9833 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9834 struct dwarf2_per_cu_data *per_cu,
9835 enum language pretend_language)
9837 /* We may arrive here during partial symbol reading, if we need full
9838 DIEs to process an unusual case (e.g. template arguments). Do
9839 not queue PER_CU, just tell our caller to load its DIEs. */
9840 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9842 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9847 /* Mark the dependence relation so that we don't flush PER_CU
9849 if (dependent_cu != NULL)
9850 dwarf2_add_dependence (dependent_cu, per_cu);
9852 /* If it's already on the queue, we have nothing to do. */
9856 /* If the compilation unit is already loaded, just mark it as
9858 if (per_cu->cu != NULL)
9860 per_cu->cu->last_used = 0;
9864 /* Add it to the queue. */
9865 queue_comp_unit (per_cu, pretend_language);
9870 /* Process the queue. */
9873 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9875 struct dwarf2_queue_item *item, *next_item;
9877 if (dwarf_read_debug)
9879 fprintf_unfiltered (gdb_stdlog,
9880 "Expanding one or more symtabs of objfile %s ...\n",
9881 objfile_name (dwarf2_per_objfile->objfile));
9884 /* The queue starts out with one item, but following a DIE reference
9885 may load a new CU, adding it to the end of the queue. */
9886 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9888 if ((dwarf2_per_objfile->using_index
9889 ? !item->per_cu->v.quick->compunit_symtab
9890 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9891 /* Skip dummy CUs. */
9892 && item->per_cu->cu != NULL)
9894 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9895 unsigned int debug_print_threshold;
9898 if (per_cu->is_debug_types)
9900 struct signatured_type *sig_type =
9901 (struct signatured_type *) per_cu;
9903 sprintf (buf, "TU %s at offset 0x%x",
9904 hex_string (sig_type->signature),
9905 to_underlying (per_cu->sect_off));
9906 /* There can be 100s of TUs.
9907 Only print them in verbose mode. */
9908 debug_print_threshold = 2;
9912 sprintf (buf, "CU at offset 0x%x",
9913 to_underlying (per_cu->sect_off));
9914 debug_print_threshold = 1;
9917 if (dwarf_read_debug >= debug_print_threshold)
9918 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9920 if (per_cu->is_debug_types)
9921 process_full_type_unit (per_cu, item->pretend_language);
9923 process_full_comp_unit (per_cu, item->pretend_language);
9925 if (dwarf_read_debug >= debug_print_threshold)
9926 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9929 item->per_cu->queued = 0;
9930 next_item = item->next;
9934 dwarf2_queue_tail = NULL;
9936 if (dwarf_read_debug)
9938 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9939 objfile_name (dwarf2_per_objfile->objfile));
9943 /* Free all allocated queue entries. This function only releases anything if
9944 an error was thrown; if the queue was processed then it would have been
9945 freed as we went along. */
9948 dwarf2_release_queue (void *dummy)
9950 struct dwarf2_queue_item *item, *last;
9952 item = dwarf2_queue;
9955 /* Anything still marked queued is likely to be in an
9956 inconsistent state, so discard it. */
9957 if (item->per_cu->queued)
9959 if (item->per_cu->cu != NULL)
9960 free_one_cached_comp_unit (item->per_cu);
9961 item->per_cu->queued = 0;
9969 dwarf2_queue = dwarf2_queue_tail = NULL;
9972 /* Read in full symbols for PST, and anything it depends on. */
9975 psymtab_to_symtab_1 (struct partial_symtab *pst)
9977 struct dwarf2_per_cu_data *per_cu;
9983 for (i = 0; i < pst->number_of_dependencies; i++)
9984 if (!pst->dependencies[i]->readin
9985 && pst->dependencies[i]->user == NULL)
9987 /* Inform about additional files that need to be read in. */
9990 /* FIXME: i18n: Need to make this a single string. */
9991 fputs_filtered (" ", gdb_stdout);
9993 fputs_filtered ("and ", gdb_stdout);
9995 printf_filtered ("%s...", pst->dependencies[i]->filename);
9996 wrap_here (""); /* Flush output. */
9997 gdb_flush (gdb_stdout);
9999 psymtab_to_symtab_1 (pst->dependencies[i]);
10002 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10004 if (per_cu == NULL)
10006 /* It's an include file, no symbols to read for it.
10007 Everything is in the parent symtab. */
10012 dw2_do_instantiate_symtab (per_cu);
10015 /* Trivial hash function for die_info: the hash value of a DIE
10016 is its offset in .debug_info for this objfile. */
10019 die_hash (const void *item)
10021 const struct die_info *die = (const struct die_info *) item;
10023 return to_underlying (die->sect_off);
10026 /* Trivial comparison function for die_info structures: two DIEs
10027 are equal if they have the same offset. */
10030 die_eq (const void *item_lhs, const void *item_rhs)
10032 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
10033 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
10035 return die_lhs->sect_off == die_rhs->sect_off;
10038 /* die_reader_func for load_full_comp_unit.
10039 This is identical to read_signatured_type_reader,
10040 but is kept separate for now. */
10043 load_full_comp_unit_reader (const struct die_reader_specs *reader,
10044 const gdb_byte *info_ptr,
10045 struct die_info *comp_unit_die,
10049 struct dwarf2_cu *cu = reader->cu;
10050 enum language *language_ptr = (enum language *) data;
10052 gdb_assert (cu->die_hash == NULL);
10054 htab_create_alloc_ex (cu->header.length / 12,
10058 &cu->comp_unit_obstack,
10059 hashtab_obstack_allocate,
10060 dummy_obstack_deallocate);
10063 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
10064 &info_ptr, comp_unit_die);
10065 cu->dies = comp_unit_die;
10066 /* comp_unit_die is not stored in die_hash, no need. */
10068 /* We try not to read any attributes in this function, because not
10069 all CUs needed for references have been loaded yet, and symbol
10070 table processing isn't initialized. But we have to set the CU language,
10071 or we won't be able to build types correctly.
10072 Similarly, if we do not read the producer, we can not apply
10073 producer-specific interpretation. */
10074 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
10077 /* Load the DIEs associated with PER_CU into memory. */
10080 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
10081 enum language pretend_language)
10083 gdb_assert (! this_cu->is_debug_types);
10085 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
10086 load_full_comp_unit_reader, &pretend_language);
10089 /* Add a DIE to the delayed physname list. */
10092 add_to_method_list (struct type *type, int fnfield_index, int index,
10093 const char *name, struct die_info *die,
10094 struct dwarf2_cu *cu)
10096 struct delayed_method_info mi;
10098 mi.fnfield_index = fnfield_index;
10102 cu->method_list.push_back (mi);
10105 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
10106 "const" / "volatile". If so, decrements LEN by the length of the
10107 modifier and return true. Otherwise return false. */
10111 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
10113 size_t mod_len = sizeof (mod) - 1;
10114 if (len > mod_len && startswith (physname + (len - mod_len), mod))
10122 /* Compute the physnames of any methods on the CU's method list.
10124 The computation of method physnames is delayed in order to avoid the
10125 (bad) condition that one of the method's formal parameters is of an as yet
10126 incomplete type. */
10129 compute_delayed_physnames (struct dwarf2_cu *cu)
10131 /* Only C++ delays computing physnames. */
10132 if (cu->method_list.empty ())
10134 gdb_assert (cu->language == language_cplus);
10136 for (struct delayed_method_info &mi : cu->method_list)
10138 const char *physname;
10139 struct fn_fieldlist *fn_flp
10140 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
10141 physname = dwarf2_physname (mi.name, mi.die, cu);
10142 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
10143 = physname ? physname : "";
10145 /* Since there's no tag to indicate whether a method is a
10146 const/volatile overload, extract that information out of the
10148 if (physname != NULL)
10150 size_t len = strlen (physname);
10154 if (physname[len] == ')') /* shortcut */
10156 else if (check_modifier (physname, len, " const"))
10157 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
10158 else if (check_modifier (physname, len, " volatile"))
10159 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
10166 /* The list is no longer needed. */
10167 cu->method_list.clear ();
10170 /* Go objects should be embedded in a DW_TAG_module DIE,
10171 and it's not clear if/how imported objects will appear.
10172 To keep Go support simple until that's worked out,
10173 go back through what we've read and create something usable.
10174 We could do this while processing each DIE, and feels kinda cleaner,
10175 but that way is more invasive.
10176 This is to, for example, allow the user to type "p var" or "b main"
10177 without having to specify the package name, and allow lookups
10178 of module.object to work in contexts that use the expression
10182 fixup_go_packaging (struct dwarf2_cu *cu)
10184 char *package_name = NULL;
10185 struct pending *list;
10188 for (list = global_symbols; list != NULL; list = list->next)
10190 for (i = 0; i < list->nsyms; ++i)
10192 struct symbol *sym = list->symbol[i];
10194 if (SYMBOL_LANGUAGE (sym) == language_go
10195 && SYMBOL_CLASS (sym) == LOC_BLOCK)
10197 char *this_package_name = go_symbol_package_name (sym);
10199 if (this_package_name == NULL)
10201 if (package_name == NULL)
10202 package_name = this_package_name;
10205 struct objfile *objfile
10206 = cu->per_cu->dwarf2_per_objfile->objfile;
10207 if (strcmp (package_name, this_package_name) != 0)
10208 complaint (&symfile_complaints,
10209 _("Symtab %s has objects from two different Go packages: %s and %s"),
10210 (symbol_symtab (sym) != NULL
10211 ? symtab_to_filename_for_display
10212 (symbol_symtab (sym))
10213 : objfile_name (objfile)),
10214 this_package_name, package_name);
10215 xfree (this_package_name);
10221 if (package_name != NULL)
10223 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10224 const char *saved_package_name
10225 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
10227 strlen (package_name));
10228 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
10229 saved_package_name);
10230 struct symbol *sym;
10232 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10234 sym = allocate_symbol (objfile);
10235 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
10236 SYMBOL_SET_NAMES (sym, saved_package_name,
10237 strlen (saved_package_name), 0, objfile);
10238 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
10239 e.g., "main" finds the "main" module and not C's main(). */
10240 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10241 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
10242 SYMBOL_TYPE (sym) = type;
10244 add_symbol_to_list (sym, &global_symbols);
10246 xfree (package_name);
10250 /* Return the symtab for PER_CU. This works properly regardless of
10251 whether we're using the index or psymtabs. */
10253 static struct compunit_symtab *
10254 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10256 return (per_cu->dwarf2_per_objfile->using_index
10257 ? per_cu->v.quick->compunit_symtab
10258 : per_cu->v.psymtab->compunit_symtab);
10261 /* A helper function for computing the list of all symbol tables
10262 included by PER_CU. */
10265 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
10266 htab_t all_children, htab_t all_type_symtabs,
10267 struct dwarf2_per_cu_data *per_cu,
10268 struct compunit_symtab *immediate_parent)
10272 struct compunit_symtab *cust;
10273 struct dwarf2_per_cu_data *iter;
10275 slot = htab_find_slot (all_children, per_cu, INSERT);
10278 /* This inclusion and its children have been processed. */
10283 /* Only add a CU if it has a symbol table. */
10284 cust = get_compunit_symtab (per_cu);
10287 /* If this is a type unit only add its symbol table if we haven't
10288 seen it yet (type unit per_cu's can share symtabs). */
10289 if (per_cu->is_debug_types)
10291 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10295 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10296 if (cust->user == NULL)
10297 cust->user = immediate_parent;
10302 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10303 if (cust->user == NULL)
10304 cust->user = immediate_parent;
10309 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10312 recursively_compute_inclusions (result, all_children,
10313 all_type_symtabs, iter, cust);
10317 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10321 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10323 gdb_assert (! per_cu->is_debug_types);
10325 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10328 struct dwarf2_per_cu_data *per_cu_iter;
10329 struct compunit_symtab *compunit_symtab_iter;
10330 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
10331 htab_t all_children, all_type_symtabs;
10332 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10334 /* If we don't have a symtab, we can just skip this case. */
10338 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10339 NULL, xcalloc, xfree);
10340 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10341 NULL, xcalloc, xfree);
10344 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10348 recursively_compute_inclusions (&result_symtabs, all_children,
10349 all_type_symtabs, per_cu_iter,
10353 /* Now we have a transitive closure of all the included symtabs. */
10354 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10356 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10357 struct compunit_symtab *, len + 1);
10359 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10360 compunit_symtab_iter);
10362 cust->includes[ix] = compunit_symtab_iter;
10363 cust->includes[len] = NULL;
10365 VEC_free (compunit_symtab_ptr, result_symtabs);
10366 htab_delete (all_children);
10367 htab_delete (all_type_symtabs);
10371 /* Compute the 'includes' field for the symtabs of all the CUs we just
10375 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10378 struct dwarf2_per_cu_data *iter;
10381 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10385 if (! iter->is_debug_types)
10386 compute_compunit_symtab_includes (iter);
10389 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10392 /* Generate full symbol information for PER_CU, whose DIEs have
10393 already been loaded into memory. */
10396 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10397 enum language pretend_language)
10399 struct dwarf2_cu *cu = per_cu->cu;
10400 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10401 struct objfile *objfile = dwarf2_per_objfile->objfile;
10402 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10403 CORE_ADDR lowpc, highpc;
10404 struct compunit_symtab *cust;
10405 CORE_ADDR baseaddr;
10406 struct block *static_block;
10409 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10412 scoped_free_pendings free_pending;
10414 /* Clear the list here in case something was left over. */
10415 cu->method_list.clear ();
10417 cu->list_in_scope = &file_symbols;
10419 cu->language = pretend_language;
10420 cu->language_defn = language_def (cu->language);
10422 /* Do line number decoding in read_file_scope () */
10423 process_die (cu->dies, cu);
10425 /* For now fudge the Go package. */
10426 if (cu->language == language_go)
10427 fixup_go_packaging (cu);
10429 /* Now that we have processed all the DIEs in the CU, all the types
10430 should be complete, and it should now be safe to compute all of the
10432 compute_delayed_physnames (cu);
10434 /* Some compilers don't define a DW_AT_high_pc attribute for the
10435 compilation unit. If the DW_AT_high_pc is missing, synthesize
10436 it, by scanning the DIE's below the compilation unit. */
10437 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10439 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10440 static_block = end_symtab_get_static_block (addr, 0, 1);
10442 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10443 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10444 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10445 addrmap to help ensure it has an accurate map of pc values belonging to
10447 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10449 cust = end_symtab_from_static_block (static_block,
10450 SECT_OFF_TEXT (objfile), 0);
10454 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10456 /* Set symtab language to language from DW_AT_language. If the
10457 compilation is from a C file generated by language preprocessors, do
10458 not set the language if it was already deduced by start_subfile. */
10459 if (!(cu->language == language_c
10460 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10461 COMPUNIT_FILETABS (cust)->language = cu->language;
10463 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10464 produce DW_AT_location with location lists but it can be possibly
10465 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10466 there were bugs in prologue debug info, fixed later in GCC-4.5
10467 by "unwind info for epilogues" patch (which is not directly related).
10469 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10470 needed, it would be wrong due to missing DW_AT_producer there.
10472 Still one can confuse GDB by using non-standard GCC compilation
10473 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10475 if (cu->has_loclist && gcc_4_minor >= 5)
10476 cust->locations_valid = 1;
10478 if (gcc_4_minor >= 5)
10479 cust->epilogue_unwind_valid = 1;
10481 cust->call_site_htab = cu->call_site_htab;
10484 if (dwarf2_per_objfile->using_index)
10485 per_cu->v.quick->compunit_symtab = cust;
10488 struct partial_symtab *pst = per_cu->v.psymtab;
10489 pst->compunit_symtab = cust;
10493 /* Push it for inclusion processing later. */
10494 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
10497 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10498 already been loaded into memory. */
10501 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10502 enum language pretend_language)
10504 struct dwarf2_cu *cu = per_cu->cu;
10505 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10506 struct objfile *objfile = dwarf2_per_objfile->objfile;
10507 struct compunit_symtab *cust;
10508 struct signatured_type *sig_type;
10510 gdb_assert (per_cu->is_debug_types);
10511 sig_type = (struct signatured_type *) per_cu;
10514 scoped_free_pendings free_pending;
10516 /* Clear the list here in case something was left over. */
10517 cu->method_list.clear ();
10519 cu->list_in_scope = &file_symbols;
10521 cu->language = pretend_language;
10522 cu->language_defn = language_def (cu->language);
10524 /* The symbol tables are set up in read_type_unit_scope. */
10525 process_die (cu->dies, cu);
10527 /* For now fudge the Go package. */
10528 if (cu->language == language_go)
10529 fixup_go_packaging (cu);
10531 /* Now that we have processed all the DIEs in the CU, all the types
10532 should be complete, and it should now be safe to compute all of the
10534 compute_delayed_physnames (cu);
10536 /* TUs share symbol tables.
10537 If this is the first TU to use this symtab, complete the construction
10538 of it with end_expandable_symtab. Otherwise, complete the addition of
10539 this TU's symbols to the existing symtab. */
10540 if (sig_type->type_unit_group->compunit_symtab == NULL)
10542 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10543 sig_type->type_unit_group->compunit_symtab = cust;
10547 /* Set symtab language to language from DW_AT_language. If the
10548 compilation is from a C file generated by language preprocessors,
10549 do not set the language if it was already deduced by
10551 if (!(cu->language == language_c
10552 && COMPUNIT_FILETABS (cust)->language != language_c))
10553 COMPUNIT_FILETABS (cust)->language = cu->language;
10558 augment_type_symtab ();
10559 cust = sig_type->type_unit_group->compunit_symtab;
10562 if (dwarf2_per_objfile->using_index)
10563 per_cu->v.quick->compunit_symtab = cust;
10566 struct partial_symtab *pst = per_cu->v.psymtab;
10567 pst->compunit_symtab = cust;
10572 /* Process an imported unit DIE. */
10575 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10577 struct attribute *attr;
10579 /* For now we don't handle imported units in type units. */
10580 if (cu->per_cu->is_debug_types)
10582 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10583 " supported in type units [in module %s]"),
10584 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10587 attr = dwarf2_attr (die, DW_AT_import, cu);
10590 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10591 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10592 dwarf2_per_cu_data *per_cu
10593 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10594 cu->per_cu->dwarf2_per_objfile);
10596 /* If necessary, add it to the queue and load its DIEs. */
10597 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10598 load_full_comp_unit (per_cu, cu->language);
10600 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10605 /* RAII object that represents a process_die scope: i.e.,
10606 starts/finishes processing a DIE. */
10607 class process_die_scope
10610 process_die_scope (die_info *die, dwarf2_cu *cu)
10611 : m_die (die), m_cu (cu)
10613 /* We should only be processing DIEs not already in process. */
10614 gdb_assert (!m_die->in_process);
10615 m_die->in_process = true;
10618 ~process_die_scope ()
10620 m_die->in_process = false;
10622 /* If we're done processing the DIE for the CU that owns the line
10623 header, we don't need the line header anymore. */
10624 if (m_cu->line_header_die_owner == m_die)
10626 delete m_cu->line_header;
10627 m_cu->line_header = NULL;
10628 m_cu->line_header_die_owner = NULL;
10637 /* Process a die and its children. */
10640 process_die (struct die_info *die, struct dwarf2_cu *cu)
10642 process_die_scope scope (die, cu);
10646 case DW_TAG_padding:
10648 case DW_TAG_compile_unit:
10649 case DW_TAG_partial_unit:
10650 read_file_scope (die, cu);
10652 case DW_TAG_type_unit:
10653 read_type_unit_scope (die, cu);
10655 case DW_TAG_subprogram:
10656 case DW_TAG_inlined_subroutine:
10657 read_func_scope (die, cu);
10659 case DW_TAG_lexical_block:
10660 case DW_TAG_try_block:
10661 case DW_TAG_catch_block:
10662 read_lexical_block_scope (die, cu);
10664 case DW_TAG_call_site:
10665 case DW_TAG_GNU_call_site:
10666 read_call_site_scope (die, cu);
10668 case DW_TAG_class_type:
10669 case DW_TAG_interface_type:
10670 case DW_TAG_structure_type:
10671 case DW_TAG_union_type:
10672 process_structure_scope (die, cu);
10674 case DW_TAG_enumeration_type:
10675 process_enumeration_scope (die, cu);
10678 /* These dies have a type, but processing them does not create
10679 a symbol or recurse to process the children. Therefore we can
10680 read them on-demand through read_type_die. */
10681 case DW_TAG_subroutine_type:
10682 case DW_TAG_set_type:
10683 case DW_TAG_array_type:
10684 case DW_TAG_pointer_type:
10685 case DW_TAG_ptr_to_member_type:
10686 case DW_TAG_reference_type:
10687 case DW_TAG_rvalue_reference_type:
10688 case DW_TAG_string_type:
10691 case DW_TAG_base_type:
10692 case DW_TAG_subrange_type:
10693 case DW_TAG_typedef:
10694 /* Add a typedef symbol for the type definition, if it has a
10696 new_symbol (die, read_type_die (die, cu), cu);
10698 case DW_TAG_common_block:
10699 read_common_block (die, cu);
10701 case DW_TAG_common_inclusion:
10703 case DW_TAG_namespace:
10704 cu->processing_has_namespace_info = 1;
10705 read_namespace (die, cu);
10707 case DW_TAG_module:
10708 cu->processing_has_namespace_info = 1;
10709 read_module (die, cu);
10711 case DW_TAG_imported_declaration:
10712 cu->processing_has_namespace_info = 1;
10713 if (read_namespace_alias (die, cu))
10715 /* The declaration is not a global namespace alias: fall through. */
10716 case DW_TAG_imported_module:
10717 cu->processing_has_namespace_info = 1;
10718 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10719 || cu->language != language_fortran))
10720 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10721 dwarf_tag_name (die->tag));
10722 read_import_statement (die, cu);
10725 case DW_TAG_imported_unit:
10726 process_imported_unit_die (die, cu);
10729 case DW_TAG_variable:
10730 read_variable (die, cu);
10734 new_symbol (die, NULL, cu);
10739 /* DWARF name computation. */
10741 /* A helper function for dwarf2_compute_name which determines whether DIE
10742 needs to have the name of the scope prepended to the name listed in the
10746 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10748 struct attribute *attr;
10752 case DW_TAG_namespace:
10753 case DW_TAG_typedef:
10754 case DW_TAG_class_type:
10755 case DW_TAG_interface_type:
10756 case DW_TAG_structure_type:
10757 case DW_TAG_union_type:
10758 case DW_TAG_enumeration_type:
10759 case DW_TAG_enumerator:
10760 case DW_TAG_subprogram:
10761 case DW_TAG_inlined_subroutine:
10762 case DW_TAG_member:
10763 case DW_TAG_imported_declaration:
10766 case DW_TAG_variable:
10767 case DW_TAG_constant:
10768 /* We only need to prefix "globally" visible variables. These include
10769 any variable marked with DW_AT_external or any variable that
10770 lives in a namespace. [Variables in anonymous namespaces
10771 require prefixing, but they are not DW_AT_external.] */
10773 if (dwarf2_attr (die, DW_AT_specification, cu))
10775 struct dwarf2_cu *spec_cu = cu;
10777 return die_needs_namespace (die_specification (die, &spec_cu),
10781 attr = dwarf2_attr (die, DW_AT_external, cu);
10782 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10783 && die->parent->tag != DW_TAG_module)
10785 /* A variable in a lexical block of some kind does not need a
10786 namespace, even though in C++ such variables may be external
10787 and have a mangled name. */
10788 if (die->parent->tag == DW_TAG_lexical_block
10789 || die->parent->tag == DW_TAG_try_block
10790 || die->parent->tag == DW_TAG_catch_block
10791 || die->parent->tag == DW_TAG_subprogram)
10800 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10801 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10802 defined for the given DIE. */
10804 static struct attribute *
10805 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10807 struct attribute *attr;
10809 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10811 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10816 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10817 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10818 defined for the given DIE. */
10820 static const char *
10821 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10823 const char *linkage_name;
10825 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10826 if (linkage_name == NULL)
10827 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10829 return linkage_name;
10832 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10833 compute the physname for the object, which include a method's:
10834 - formal parameters (C++),
10835 - receiver type (Go),
10837 The term "physname" is a bit confusing.
10838 For C++, for example, it is the demangled name.
10839 For Go, for example, it's the mangled name.
10841 For Ada, return the DIE's linkage name rather than the fully qualified
10842 name. PHYSNAME is ignored..
10844 The result is allocated on the objfile_obstack and canonicalized. */
10846 static const char *
10847 dwarf2_compute_name (const char *name,
10848 struct die_info *die, struct dwarf2_cu *cu,
10851 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10854 name = dwarf2_name (die, cu);
10856 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10857 but otherwise compute it by typename_concat inside GDB.
10858 FIXME: Actually this is not really true, or at least not always true.
10859 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10860 Fortran names because there is no mangling standard. So new_symbol
10861 will set the demangled name to the result of dwarf2_full_name, and it is
10862 the demangled name that GDB uses if it exists. */
10863 if (cu->language == language_ada
10864 || (cu->language == language_fortran && physname))
10866 /* For Ada unit, we prefer the linkage name over the name, as
10867 the former contains the exported name, which the user expects
10868 to be able to reference. Ideally, we want the user to be able
10869 to reference this entity using either natural or linkage name,
10870 but we haven't started looking at this enhancement yet. */
10871 const char *linkage_name = dw2_linkage_name (die, cu);
10873 if (linkage_name != NULL)
10874 return linkage_name;
10877 /* These are the only languages we know how to qualify names in. */
10879 && (cu->language == language_cplus
10880 || cu->language == language_fortran || cu->language == language_d
10881 || cu->language == language_rust))
10883 if (die_needs_namespace (die, cu))
10885 const char *prefix;
10886 const char *canonical_name = NULL;
10890 prefix = determine_prefix (die, cu);
10891 if (*prefix != '\0')
10893 char *prefixed_name = typename_concat (NULL, prefix, name,
10896 buf.puts (prefixed_name);
10897 xfree (prefixed_name);
10902 /* Template parameters may be specified in the DIE's DW_AT_name, or
10903 as children with DW_TAG_template_type_param or
10904 DW_TAG_value_type_param. If the latter, add them to the name
10905 here. If the name already has template parameters, then
10906 skip this step; some versions of GCC emit both, and
10907 it is more efficient to use the pre-computed name.
10909 Something to keep in mind about this process: it is very
10910 unlikely, or in some cases downright impossible, to produce
10911 something that will match the mangled name of a function.
10912 If the definition of the function has the same debug info,
10913 we should be able to match up with it anyway. But fallbacks
10914 using the minimal symbol, for instance to find a method
10915 implemented in a stripped copy of libstdc++, will not work.
10916 If we do not have debug info for the definition, we will have to
10917 match them up some other way.
10919 When we do name matching there is a related problem with function
10920 templates; two instantiated function templates are allowed to
10921 differ only by their return types, which we do not add here. */
10923 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10925 struct attribute *attr;
10926 struct die_info *child;
10929 die->building_fullname = 1;
10931 for (child = die->child; child != NULL; child = child->sibling)
10935 const gdb_byte *bytes;
10936 struct dwarf2_locexpr_baton *baton;
10939 if (child->tag != DW_TAG_template_type_param
10940 && child->tag != DW_TAG_template_value_param)
10951 attr = dwarf2_attr (child, DW_AT_type, cu);
10954 complaint (&symfile_complaints,
10955 _("template parameter missing DW_AT_type"));
10956 buf.puts ("UNKNOWN_TYPE");
10959 type = die_type (child, cu);
10961 if (child->tag == DW_TAG_template_type_param)
10963 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
10967 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10970 complaint (&symfile_complaints,
10971 _("template parameter missing "
10972 "DW_AT_const_value"));
10973 buf.puts ("UNKNOWN_VALUE");
10977 dwarf2_const_value_attr (attr, type, name,
10978 &cu->comp_unit_obstack, cu,
10979 &value, &bytes, &baton);
10981 if (TYPE_NOSIGN (type))
10982 /* GDB prints characters as NUMBER 'CHAR'. If that's
10983 changed, this can use value_print instead. */
10984 c_printchar (value, type, &buf);
10987 struct value_print_options opts;
10990 v = dwarf2_evaluate_loc_desc (type, NULL,
10994 else if (bytes != NULL)
10996 v = allocate_value (type);
10997 memcpy (value_contents_writeable (v), bytes,
10998 TYPE_LENGTH (type));
11001 v = value_from_longest (type, value);
11003 /* Specify decimal so that we do not depend on
11005 get_formatted_print_options (&opts, 'd');
11007 value_print (v, &buf, &opts);
11013 die->building_fullname = 0;
11017 /* Close the argument list, with a space if necessary
11018 (nested templates). */
11019 if (!buf.empty () && buf.string ().back () == '>')
11026 /* For C++ methods, append formal parameter type
11027 information, if PHYSNAME. */
11029 if (physname && die->tag == DW_TAG_subprogram
11030 && cu->language == language_cplus)
11032 struct type *type = read_type_die (die, cu);
11034 c_type_print_args (type, &buf, 1, cu->language,
11035 &type_print_raw_options);
11037 if (cu->language == language_cplus)
11039 /* Assume that an artificial first parameter is
11040 "this", but do not crash if it is not. RealView
11041 marks unnamed (and thus unused) parameters as
11042 artificial; there is no way to differentiate
11044 if (TYPE_NFIELDS (type) > 0
11045 && TYPE_FIELD_ARTIFICIAL (type, 0)
11046 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11047 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11049 buf.puts (" const");
11053 const std::string &intermediate_name = buf.string ();
11055 if (cu->language == language_cplus)
11057 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11058 &objfile->per_bfd->storage_obstack);
11060 /* If we only computed INTERMEDIATE_NAME, or if
11061 INTERMEDIATE_NAME is already canonical, then we need to
11062 copy it to the appropriate obstack. */
11063 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11064 name = ((const char *)
11065 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11066 intermediate_name.c_str (),
11067 intermediate_name.length ()));
11069 name = canonical_name;
11076 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11077 If scope qualifiers are appropriate they will be added. The result
11078 will be allocated on the storage_obstack, or NULL if the DIE does
11079 not have a name. NAME may either be from a previous call to
11080 dwarf2_name or NULL.
11082 The output string will be canonicalized (if C++). */
11084 static const char *
11085 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11087 return dwarf2_compute_name (name, die, cu, 0);
11090 /* Construct a physname for the given DIE in CU. NAME may either be
11091 from a previous call to dwarf2_name or NULL. The result will be
11092 allocated on the objfile_objstack or NULL if the DIE does not have a
11095 The output string will be canonicalized (if C++). */
11097 static const char *
11098 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11100 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11101 const char *retval, *mangled = NULL, *canon = NULL;
11104 /* In this case dwarf2_compute_name is just a shortcut not building anything
11106 if (!die_needs_namespace (die, cu))
11107 return dwarf2_compute_name (name, die, cu, 1);
11109 mangled = dw2_linkage_name (die, cu);
11111 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11112 See https://github.com/rust-lang/rust/issues/32925. */
11113 if (cu->language == language_rust && mangled != NULL
11114 && strchr (mangled, '{') != NULL)
11117 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11119 gdb::unique_xmalloc_ptr<char> demangled;
11120 if (mangled != NULL)
11122 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
11123 type. It is easier for GDB users to search for such functions as
11124 `name(params)' than `long name(params)'. In such case the minimal
11125 symbol names do not match the full symbol names but for template
11126 functions there is never a need to look up their definition from their
11127 declaration so the only disadvantage remains the minimal symbol
11128 variant `long name(params)' does not have the proper inferior type.
11131 if (cu->language == language_go)
11133 /* This is a lie, but we already lie to the caller new_symbol.
11134 new_symbol assumes we return the mangled name.
11135 This just undoes that lie until things are cleaned up. */
11139 demangled.reset (gdb_demangle (mangled,
11140 (DMGL_PARAMS | DMGL_ANSI
11141 | DMGL_RET_DROP)));
11144 canon = demangled.get ();
11152 if (canon == NULL || check_physname)
11154 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11156 if (canon != NULL && strcmp (physname, canon) != 0)
11158 /* It may not mean a bug in GDB. The compiler could also
11159 compute DW_AT_linkage_name incorrectly. But in such case
11160 GDB would need to be bug-to-bug compatible. */
11162 complaint (&symfile_complaints,
11163 _("Computed physname <%s> does not match demangled <%s> "
11164 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
11165 physname, canon, mangled, to_underlying (die->sect_off),
11166 objfile_name (objfile));
11168 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11169 is available here - over computed PHYSNAME. It is safer
11170 against both buggy GDB and buggy compilers. */
11184 retval = ((const char *)
11185 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11186 retval, strlen (retval)));
11191 /* Inspect DIE in CU for a namespace alias. If one exists, record
11192 a new symbol for it.
11194 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11197 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11199 struct attribute *attr;
11201 /* If the die does not have a name, this is not a namespace
11203 attr = dwarf2_attr (die, DW_AT_name, cu);
11207 struct die_info *d = die;
11208 struct dwarf2_cu *imported_cu = cu;
11210 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11211 keep inspecting DIEs until we hit the underlying import. */
11212 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11213 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11215 attr = dwarf2_attr (d, DW_AT_import, cu);
11219 d = follow_die_ref (d, attr, &imported_cu);
11220 if (d->tag != DW_TAG_imported_declaration)
11224 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11226 complaint (&symfile_complaints,
11227 _("DIE at 0x%x has too many recursively imported "
11228 "declarations"), to_underlying (d->sect_off));
11235 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11237 type = get_die_type_at_offset (sect_off, cu->per_cu);
11238 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11240 /* This declaration is a global namespace alias. Add
11241 a symbol for it whose type is the aliased namespace. */
11242 new_symbol (die, type, cu);
11251 /* Return the using directives repository (global or local?) to use in the
11252 current context for LANGUAGE.
11254 For Ada, imported declarations can materialize renamings, which *may* be
11255 global. However it is impossible (for now?) in DWARF to distinguish
11256 "external" imported declarations and "static" ones. As all imported
11257 declarations seem to be static in all other languages, make them all CU-wide
11258 global only in Ada. */
11260 static struct using_direct **
11261 using_directives (enum language language)
11263 if (language == language_ada && context_stack_depth == 0)
11264 return &global_using_directives;
11266 return &local_using_directives;
11269 /* Read the import statement specified by the given die and record it. */
11272 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11274 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11275 struct attribute *import_attr;
11276 struct die_info *imported_die, *child_die;
11277 struct dwarf2_cu *imported_cu;
11278 const char *imported_name;
11279 const char *imported_name_prefix;
11280 const char *canonical_name;
11281 const char *import_alias;
11282 const char *imported_declaration = NULL;
11283 const char *import_prefix;
11284 std::vector<const char *> excludes;
11286 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11287 if (import_attr == NULL)
11289 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11290 dwarf_tag_name (die->tag));
11295 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11296 imported_name = dwarf2_name (imported_die, imported_cu);
11297 if (imported_name == NULL)
11299 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11301 The import in the following code:
11315 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11316 <52> DW_AT_decl_file : 1
11317 <53> DW_AT_decl_line : 6
11318 <54> DW_AT_import : <0x75>
11319 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11320 <59> DW_AT_name : B
11321 <5b> DW_AT_decl_file : 1
11322 <5c> DW_AT_decl_line : 2
11323 <5d> DW_AT_type : <0x6e>
11325 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11326 <76> DW_AT_byte_size : 4
11327 <77> DW_AT_encoding : 5 (signed)
11329 imports the wrong die ( 0x75 instead of 0x58 ).
11330 This case will be ignored until the gcc bug is fixed. */
11334 /* Figure out the local name after import. */
11335 import_alias = dwarf2_name (die, cu);
11337 /* Figure out where the statement is being imported to. */
11338 import_prefix = determine_prefix (die, cu);
11340 /* Figure out what the scope of the imported die is and prepend it
11341 to the name of the imported die. */
11342 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11344 if (imported_die->tag != DW_TAG_namespace
11345 && imported_die->tag != DW_TAG_module)
11347 imported_declaration = imported_name;
11348 canonical_name = imported_name_prefix;
11350 else if (strlen (imported_name_prefix) > 0)
11351 canonical_name = obconcat (&objfile->objfile_obstack,
11352 imported_name_prefix,
11353 (cu->language == language_d ? "." : "::"),
11354 imported_name, (char *) NULL);
11356 canonical_name = imported_name;
11358 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11359 for (child_die = die->child; child_die && child_die->tag;
11360 child_die = sibling_die (child_die))
11362 /* DWARF-4: A Fortran use statement with a “rename list” may be
11363 represented by an imported module entry with an import attribute
11364 referring to the module and owned entries corresponding to those
11365 entities that are renamed as part of being imported. */
11367 if (child_die->tag != DW_TAG_imported_declaration)
11369 complaint (&symfile_complaints,
11370 _("child DW_TAG_imported_declaration expected "
11371 "- DIE at 0x%x [in module %s]"),
11372 to_underlying (child_die->sect_off), objfile_name (objfile));
11376 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11377 if (import_attr == NULL)
11379 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11380 dwarf_tag_name (child_die->tag));
11385 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11387 imported_name = dwarf2_name (imported_die, imported_cu);
11388 if (imported_name == NULL)
11390 complaint (&symfile_complaints,
11391 _("child DW_TAG_imported_declaration has unknown "
11392 "imported name - DIE at 0x%x [in module %s]"),
11393 to_underlying (child_die->sect_off), objfile_name (objfile));
11397 excludes.push_back (imported_name);
11399 process_die (child_die, cu);
11402 add_using_directive (using_directives (cu->language),
11406 imported_declaration,
11409 &objfile->objfile_obstack);
11412 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11413 types, but gives them a size of zero. Starting with version 14,
11414 ICC is compatible with GCC. */
11417 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11419 if (!cu->checked_producer)
11420 check_producer (cu);
11422 return cu->producer_is_icc_lt_14;
11425 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11426 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11427 this, it was first present in GCC release 4.3.0. */
11430 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11432 if (!cu->checked_producer)
11433 check_producer (cu);
11435 return cu->producer_is_gcc_lt_4_3;
11438 static file_and_directory
11439 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11441 file_and_directory res;
11443 /* Find the filename. Do not use dwarf2_name here, since the filename
11444 is not a source language identifier. */
11445 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11446 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11448 if (res.comp_dir == NULL
11449 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11450 && IS_ABSOLUTE_PATH (res.name))
11452 res.comp_dir_storage = ldirname (res.name);
11453 if (!res.comp_dir_storage.empty ())
11454 res.comp_dir = res.comp_dir_storage.c_str ();
11456 if (res.comp_dir != NULL)
11458 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11459 directory, get rid of it. */
11460 const char *cp = strchr (res.comp_dir, ':');
11462 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11463 res.comp_dir = cp + 1;
11466 if (res.name == NULL)
11467 res.name = "<unknown>";
11472 /* Handle DW_AT_stmt_list for a compilation unit.
11473 DIE is the DW_TAG_compile_unit die for CU.
11474 COMP_DIR is the compilation directory. LOWPC is passed to
11475 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11478 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11479 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11481 struct dwarf2_per_objfile *dwarf2_per_objfile
11482 = cu->per_cu->dwarf2_per_objfile;
11483 struct objfile *objfile = dwarf2_per_objfile->objfile;
11484 struct attribute *attr;
11485 struct line_header line_header_local;
11486 hashval_t line_header_local_hash;
11488 int decode_mapping;
11490 gdb_assert (! cu->per_cu->is_debug_types);
11492 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11496 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11498 /* The line header hash table is only created if needed (it exists to
11499 prevent redundant reading of the line table for partial_units).
11500 If we're given a partial_unit, we'll need it. If we're given a
11501 compile_unit, then use the line header hash table if it's already
11502 created, but don't create one just yet. */
11504 if (dwarf2_per_objfile->line_header_hash == NULL
11505 && die->tag == DW_TAG_partial_unit)
11507 dwarf2_per_objfile->line_header_hash
11508 = htab_create_alloc_ex (127, line_header_hash_voidp,
11509 line_header_eq_voidp,
11510 free_line_header_voidp,
11511 &objfile->objfile_obstack,
11512 hashtab_obstack_allocate,
11513 dummy_obstack_deallocate);
11516 line_header_local.sect_off = line_offset;
11517 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11518 line_header_local_hash = line_header_hash (&line_header_local);
11519 if (dwarf2_per_objfile->line_header_hash != NULL)
11521 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11522 &line_header_local,
11523 line_header_local_hash, NO_INSERT);
11525 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11526 is not present in *SLOT (since if there is something in *SLOT then
11527 it will be for a partial_unit). */
11528 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11530 gdb_assert (*slot != NULL);
11531 cu->line_header = (struct line_header *) *slot;
11536 /* dwarf_decode_line_header does not yet provide sufficient information.
11537 We always have to call also dwarf_decode_lines for it. */
11538 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11542 cu->line_header = lh.release ();
11543 cu->line_header_die_owner = die;
11545 if (dwarf2_per_objfile->line_header_hash == NULL)
11549 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11550 &line_header_local,
11551 line_header_local_hash, INSERT);
11552 gdb_assert (slot != NULL);
11554 if (slot != NULL && *slot == NULL)
11556 /* This newly decoded line number information unit will be owned
11557 by line_header_hash hash table. */
11558 *slot = cu->line_header;
11559 cu->line_header_die_owner = NULL;
11563 /* We cannot free any current entry in (*slot) as that struct line_header
11564 may be already used by multiple CUs. Create only temporary decoded
11565 line_header for this CU - it may happen at most once for each line
11566 number information unit. And if we're not using line_header_hash
11567 then this is what we want as well. */
11568 gdb_assert (die->tag != DW_TAG_partial_unit);
11570 decode_mapping = (die->tag != DW_TAG_partial_unit);
11571 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11576 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11579 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11581 struct dwarf2_per_objfile *dwarf2_per_objfile
11582 = cu->per_cu->dwarf2_per_objfile;
11583 struct objfile *objfile = dwarf2_per_objfile->objfile;
11584 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11585 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11586 CORE_ADDR highpc = ((CORE_ADDR) 0);
11587 struct attribute *attr;
11588 struct die_info *child_die;
11589 CORE_ADDR baseaddr;
11591 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11593 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11595 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11596 from finish_block. */
11597 if (lowpc == ((CORE_ADDR) -1))
11599 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11601 file_and_directory fnd = find_file_and_directory (die, cu);
11603 prepare_one_comp_unit (cu, die, cu->language);
11605 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11606 standardised yet. As a workaround for the language detection we fall
11607 back to the DW_AT_producer string. */
11608 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11609 cu->language = language_opencl;
11611 /* Similar hack for Go. */
11612 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11613 set_cu_language (DW_LANG_Go, cu);
11615 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
11617 /* Decode line number information if present. We do this before
11618 processing child DIEs, so that the line header table is available
11619 for DW_AT_decl_file. */
11620 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11622 /* Process all dies in compilation unit. */
11623 if (die->child != NULL)
11625 child_die = die->child;
11626 while (child_die && child_die->tag)
11628 process_die (child_die, cu);
11629 child_die = sibling_die (child_die);
11633 /* Decode macro information, if present. Dwarf 2 macro information
11634 refers to information in the line number info statement program
11635 header, so we can only read it if we've read the header
11637 attr = dwarf2_attr (die, DW_AT_macros, cu);
11639 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11640 if (attr && cu->line_header)
11642 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11643 complaint (&symfile_complaints,
11644 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11646 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11650 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11651 if (attr && cu->line_header)
11653 unsigned int macro_offset = DW_UNSND (attr);
11655 dwarf_decode_macros (cu, macro_offset, 0);
11660 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11661 Create the set of symtabs used by this TU, or if this TU is sharing
11662 symtabs with another TU and the symtabs have already been created
11663 then restore those symtabs in the line header.
11664 We don't need the pc/line-number mapping for type units. */
11667 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
11669 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11670 struct type_unit_group *tu_group;
11672 struct attribute *attr;
11674 struct signatured_type *sig_type;
11676 gdb_assert (per_cu->is_debug_types);
11677 sig_type = (struct signatured_type *) per_cu;
11679 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11681 /* If we're using .gdb_index (includes -readnow) then
11682 per_cu->type_unit_group may not have been set up yet. */
11683 if (sig_type->type_unit_group == NULL)
11684 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11685 tu_group = sig_type->type_unit_group;
11687 /* If we've already processed this stmt_list there's no real need to
11688 do it again, we could fake it and just recreate the part we need
11689 (file name,index -> symtab mapping). If data shows this optimization
11690 is useful we can do it then. */
11691 first_time = tu_group->compunit_symtab == NULL;
11693 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11698 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11699 lh = dwarf_decode_line_header (line_offset, cu);
11704 dwarf2_start_symtab (cu, "", NULL, 0);
11707 gdb_assert (tu_group->symtabs == NULL);
11708 restart_symtab (tu_group->compunit_symtab, "", 0);
11713 cu->line_header = lh.release ();
11714 cu->line_header_die_owner = die;
11718 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
11720 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11721 still initializing it, and our caller (a few levels up)
11722 process_full_type_unit still needs to know if this is the first
11725 tu_group->num_symtabs = cu->line_header->file_names.size ();
11726 tu_group->symtabs = XNEWVEC (struct symtab *,
11727 cu->line_header->file_names.size ());
11729 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11731 file_entry &fe = cu->line_header->file_names[i];
11733 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
11735 if (current_subfile->symtab == NULL)
11737 /* NOTE: start_subfile will recognize when it's been
11738 passed a file it has already seen. So we can't
11739 assume there's a simple mapping from
11740 cu->line_header->file_names to subfiles, plus
11741 cu->line_header->file_names may contain dups. */
11742 current_subfile->symtab
11743 = allocate_symtab (cust, current_subfile->name);
11746 fe.symtab = current_subfile->symtab;
11747 tu_group->symtabs[i] = fe.symtab;
11752 restart_symtab (tu_group->compunit_symtab, "", 0);
11754 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11756 file_entry &fe = cu->line_header->file_names[i];
11758 fe.symtab = tu_group->symtabs[i];
11762 /* The main symtab is allocated last. Type units don't have DW_AT_name
11763 so they don't have a "real" (so to speak) symtab anyway.
11764 There is later code that will assign the main symtab to all symbols
11765 that don't have one. We need to handle the case of a symbol with a
11766 missing symtab (DW_AT_decl_file) anyway. */
11769 /* Process DW_TAG_type_unit.
11770 For TUs we want to skip the first top level sibling if it's not the
11771 actual type being defined by this TU. In this case the first top
11772 level sibling is there to provide context only. */
11775 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11777 struct die_info *child_die;
11779 prepare_one_comp_unit (cu, die, language_minimal);
11781 /* Initialize (or reinitialize) the machinery for building symtabs.
11782 We do this before processing child DIEs, so that the line header table
11783 is available for DW_AT_decl_file. */
11784 setup_type_unit_groups (die, cu);
11786 if (die->child != NULL)
11788 child_die = die->child;
11789 while (child_die && child_die->tag)
11791 process_die (child_die, cu);
11792 child_die = sibling_die (child_die);
11799 http://gcc.gnu.org/wiki/DebugFission
11800 http://gcc.gnu.org/wiki/DebugFissionDWP
11802 To simplify handling of both DWO files ("object" files with the DWARF info)
11803 and DWP files (a file with the DWOs packaged up into one file), we treat
11804 DWP files as having a collection of virtual DWO files. */
11807 hash_dwo_file (const void *item)
11809 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11812 hash = htab_hash_string (dwo_file->dwo_name);
11813 if (dwo_file->comp_dir != NULL)
11814 hash += htab_hash_string (dwo_file->comp_dir);
11819 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11821 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11822 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11824 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11826 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11827 return lhs->comp_dir == rhs->comp_dir;
11828 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11831 /* Allocate a hash table for DWO files. */
11834 allocate_dwo_file_hash_table (struct objfile *objfile)
11836 return htab_create_alloc_ex (41,
11840 &objfile->objfile_obstack,
11841 hashtab_obstack_allocate,
11842 dummy_obstack_deallocate);
11845 /* Lookup DWO file DWO_NAME. */
11848 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11849 const char *dwo_name,
11850 const char *comp_dir)
11852 struct dwo_file find_entry;
11855 if (dwarf2_per_objfile->dwo_files == NULL)
11856 dwarf2_per_objfile->dwo_files
11857 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11859 memset (&find_entry, 0, sizeof (find_entry));
11860 find_entry.dwo_name = dwo_name;
11861 find_entry.comp_dir = comp_dir;
11862 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11868 hash_dwo_unit (const void *item)
11870 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11872 /* This drops the top 32 bits of the id, but is ok for a hash. */
11873 return dwo_unit->signature;
11877 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11879 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11880 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11882 /* The signature is assumed to be unique within the DWO file.
11883 So while object file CU dwo_id's always have the value zero,
11884 that's OK, assuming each object file DWO file has only one CU,
11885 and that's the rule for now. */
11886 return lhs->signature == rhs->signature;
11889 /* Allocate a hash table for DWO CUs,TUs.
11890 There is one of these tables for each of CUs,TUs for each DWO file. */
11893 allocate_dwo_unit_table (struct objfile *objfile)
11895 /* Start out with a pretty small number.
11896 Generally DWO files contain only one CU and maybe some TUs. */
11897 return htab_create_alloc_ex (3,
11901 &objfile->objfile_obstack,
11902 hashtab_obstack_allocate,
11903 dummy_obstack_deallocate);
11906 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11908 struct create_dwo_cu_data
11910 struct dwo_file *dwo_file;
11911 struct dwo_unit dwo_unit;
11914 /* die_reader_func for create_dwo_cu. */
11917 create_dwo_cu_reader (const struct die_reader_specs *reader,
11918 const gdb_byte *info_ptr,
11919 struct die_info *comp_unit_die,
11923 struct dwarf2_cu *cu = reader->cu;
11924 sect_offset sect_off = cu->per_cu->sect_off;
11925 struct dwarf2_section_info *section = cu->per_cu->section;
11926 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11927 struct dwo_file *dwo_file = data->dwo_file;
11928 struct dwo_unit *dwo_unit = &data->dwo_unit;
11929 struct attribute *attr;
11931 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11934 complaint (&symfile_complaints,
11935 _("Dwarf Error: debug entry at offset 0x%x is missing"
11936 " its dwo_id [in module %s]"),
11937 to_underlying (sect_off), dwo_file->dwo_name);
11941 dwo_unit->dwo_file = dwo_file;
11942 dwo_unit->signature = DW_UNSND (attr);
11943 dwo_unit->section = section;
11944 dwo_unit->sect_off = sect_off;
11945 dwo_unit->length = cu->per_cu->length;
11947 if (dwarf_read_debug)
11948 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
11949 to_underlying (sect_off),
11950 hex_string (dwo_unit->signature));
11953 /* Create the dwo_units for the CUs in a DWO_FILE.
11954 Note: This function processes DWO files only, not DWP files. */
11957 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11958 struct dwo_file &dwo_file, dwarf2_section_info §ion,
11961 struct objfile *objfile = dwarf2_per_objfile->objfile;
11962 const gdb_byte *info_ptr, *end_ptr;
11964 dwarf2_read_section (objfile, §ion);
11965 info_ptr = section.buffer;
11967 if (info_ptr == NULL)
11970 if (dwarf_read_debug)
11972 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11973 get_section_name (§ion),
11974 get_section_file_name (§ion));
11977 end_ptr = info_ptr + section.size;
11978 while (info_ptr < end_ptr)
11980 struct dwarf2_per_cu_data per_cu;
11981 struct create_dwo_cu_data create_dwo_cu_data;
11982 struct dwo_unit *dwo_unit;
11984 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11986 memset (&create_dwo_cu_data.dwo_unit, 0,
11987 sizeof (create_dwo_cu_data.dwo_unit));
11988 memset (&per_cu, 0, sizeof (per_cu));
11989 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11990 per_cu.is_debug_types = 0;
11991 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11992 per_cu.section = §ion;
11993 create_dwo_cu_data.dwo_file = &dwo_file;
11995 init_cutu_and_read_dies_no_follow (
11996 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11997 info_ptr += per_cu.length;
11999 // If the unit could not be parsed, skip it.
12000 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12003 if (cus_htab == NULL)
12004 cus_htab = allocate_dwo_unit_table (objfile);
12006 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12007 *dwo_unit = create_dwo_cu_data.dwo_unit;
12008 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12009 gdb_assert (slot != NULL);
12012 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12013 sect_offset dup_sect_off = dup_cu->sect_off;
12015 complaint (&symfile_complaints,
12016 _("debug cu entry at offset 0x%x is duplicate to"
12017 " the entry at offset 0x%x, signature %s"),
12018 to_underlying (sect_off), to_underlying (dup_sect_off),
12019 hex_string (dwo_unit->signature));
12021 *slot = (void *)dwo_unit;
12025 /* DWP file .debug_{cu,tu}_index section format:
12026 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12030 Both index sections have the same format, and serve to map a 64-bit
12031 signature to a set of section numbers. Each section begins with a header,
12032 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12033 indexes, and a pool of 32-bit section numbers. The index sections will be
12034 aligned at 8-byte boundaries in the file.
12036 The index section header consists of:
12038 V, 32 bit version number
12040 N, 32 bit number of compilation units or type units in the index
12041 M, 32 bit number of slots in the hash table
12043 Numbers are recorded using the byte order of the application binary.
12045 The hash table begins at offset 16 in the section, and consists of an array
12046 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12047 order of the application binary). Unused slots in the hash table are 0.
12048 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12050 The parallel table begins immediately after the hash table
12051 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12052 array of 32-bit indexes (using the byte order of the application binary),
12053 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12054 table contains a 32-bit index into the pool of section numbers. For unused
12055 hash table slots, the corresponding entry in the parallel table will be 0.
12057 The pool of section numbers begins immediately following the hash table
12058 (at offset 16 + 12 * M from the beginning of the section). The pool of
12059 section numbers consists of an array of 32-bit words (using the byte order
12060 of the application binary). Each item in the array is indexed starting
12061 from 0. The hash table entry provides the index of the first section
12062 number in the set. Additional section numbers in the set follow, and the
12063 set is terminated by a 0 entry (section number 0 is not used in ELF).
12065 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12066 section must be the first entry in the set, and the .debug_abbrev.dwo must
12067 be the second entry. Other members of the set may follow in any order.
12073 DWP Version 2 combines all the .debug_info, etc. sections into one,
12074 and the entries in the index tables are now offsets into these sections.
12075 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12078 Index Section Contents:
12080 Hash Table of Signatures dwp_hash_table.hash_table
12081 Parallel Table of Indices dwp_hash_table.unit_table
12082 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12083 Table of Section Sizes dwp_hash_table.v2.sizes
12085 The index section header consists of:
12087 V, 32 bit version number
12088 L, 32 bit number of columns in the table of section offsets
12089 N, 32 bit number of compilation units or type units in the index
12090 M, 32 bit number of slots in the hash table
12092 Numbers are recorded using the byte order of the application binary.
12094 The hash table has the same format as version 1.
12095 The parallel table of indices has the same format as version 1,
12096 except that the entries are origin-1 indices into the table of sections
12097 offsets and the table of section sizes.
12099 The table of offsets begins immediately following the parallel table
12100 (at offset 16 + 12 * M from the beginning of the section). The table is
12101 a two-dimensional array of 32-bit words (using the byte order of the
12102 application binary), with L columns and N+1 rows, in row-major order.
12103 Each row in the array is indexed starting from 0. The first row provides
12104 a key to the remaining rows: each column in this row provides an identifier
12105 for a debug section, and the offsets in the same column of subsequent rows
12106 refer to that section. The section identifiers are:
12108 DW_SECT_INFO 1 .debug_info.dwo
12109 DW_SECT_TYPES 2 .debug_types.dwo
12110 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12111 DW_SECT_LINE 4 .debug_line.dwo
12112 DW_SECT_LOC 5 .debug_loc.dwo
12113 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12114 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12115 DW_SECT_MACRO 8 .debug_macro.dwo
12117 The offsets provided by the CU and TU index sections are the base offsets
12118 for the contributions made by each CU or TU to the corresponding section
12119 in the package file. Each CU and TU header contains an abbrev_offset
12120 field, used to find the abbreviations table for that CU or TU within the
12121 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12122 be interpreted as relative to the base offset given in the index section.
12123 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12124 should be interpreted as relative to the base offset for .debug_line.dwo,
12125 and offsets into other debug sections obtained from DWARF attributes should
12126 also be interpreted as relative to the corresponding base offset.
12128 The table of sizes begins immediately following the table of offsets.
12129 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12130 with L columns and N rows, in row-major order. Each row in the array is
12131 indexed starting from 1 (row 0 is shared by the two tables).
12135 Hash table lookup is handled the same in version 1 and 2:
12137 We assume that N and M will not exceed 2^32 - 1.
12138 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12140 Given a 64-bit compilation unit signature or a type signature S, an entry
12141 in the hash table is located as follows:
12143 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12144 the low-order k bits all set to 1.
12146 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12148 3) If the hash table entry at index H matches the signature, use that
12149 entry. If the hash table entry at index H is unused (all zeroes),
12150 terminate the search: the signature is not present in the table.
12152 4) Let H = (H + H') modulo M. Repeat at Step 3.
12154 Because M > N and H' and M are relatively prime, the search is guaranteed
12155 to stop at an unused slot or find the match. */
12157 /* Create a hash table to map DWO IDs to their CU/TU entry in
12158 .debug_{info,types}.dwo in DWP_FILE.
12159 Returns NULL if there isn't one.
12160 Note: This function processes DWP files only, not DWO files. */
12162 static struct dwp_hash_table *
12163 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12164 struct dwp_file *dwp_file, int is_debug_types)
12166 struct objfile *objfile = dwarf2_per_objfile->objfile;
12167 bfd *dbfd = dwp_file->dbfd;
12168 const gdb_byte *index_ptr, *index_end;
12169 struct dwarf2_section_info *index;
12170 uint32_t version, nr_columns, nr_units, nr_slots;
12171 struct dwp_hash_table *htab;
12173 if (is_debug_types)
12174 index = &dwp_file->sections.tu_index;
12176 index = &dwp_file->sections.cu_index;
12178 if (dwarf2_section_empty_p (index))
12180 dwarf2_read_section (objfile, index);
12182 index_ptr = index->buffer;
12183 index_end = index_ptr + index->size;
12185 version = read_4_bytes (dbfd, index_ptr);
12188 nr_columns = read_4_bytes (dbfd, index_ptr);
12192 nr_units = read_4_bytes (dbfd, index_ptr);
12194 nr_slots = read_4_bytes (dbfd, index_ptr);
12197 if (version != 1 && version != 2)
12199 error (_("Dwarf Error: unsupported DWP file version (%s)"
12200 " [in module %s]"),
12201 pulongest (version), dwp_file->name);
12203 if (nr_slots != (nr_slots & -nr_slots))
12205 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12206 " is not power of 2 [in module %s]"),
12207 pulongest (nr_slots), dwp_file->name);
12210 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12211 htab->version = version;
12212 htab->nr_columns = nr_columns;
12213 htab->nr_units = nr_units;
12214 htab->nr_slots = nr_slots;
12215 htab->hash_table = index_ptr;
12216 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12218 /* Exit early if the table is empty. */
12219 if (nr_slots == 0 || nr_units == 0
12220 || (version == 2 && nr_columns == 0))
12222 /* All must be zero. */
12223 if (nr_slots != 0 || nr_units != 0
12224 || (version == 2 && nr_columns != 0))
12226 complaint (&symfile_complaints,
12227 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12228 " all zero [in modules %s]"),
12236 htab->section_pool.v1.indices =
12237 htab->unit_table + sizeof (uint32_t) * nr_slots;
12238 /* It's harder to decide whether the section is too small in v1.
12239 V1 is deprecated anyway so we punt. */
12243 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12244 int *ids = htab->section_pool.v2.section_ids;
12245 /* Reverse map for error checking. */
12246 int ids_seen[DW_SECT_MAX + 1];
12249 if (nr_columns < 2)
12251 error (_("Dwarf Error: bad DWP hash table, too few columns"
12252 " in section table [in module %s]"),
12255 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12257 error (_("Dwarf Error: bad DWP hash table, too many columns"
12258 " in section table [in module %s]"),
12261 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12262 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12263 for (i = 0; i < nr_columns; ++i)
12265 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12267 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12269 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12270 " in section table [in module %s]"),
12271 id, dwp_file->name);
12273 if (ids_seen[id] != -1)
12275 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12276 " id %d in section table [in module %s]"),
12277 id, dwp_file->name);
12282 /* Must have exactly one info or types section. */
12283 if (((ids_seen[DW_SECT_INFO] != -1)
12284 + (ids_seen[DW_SECT_TYPES] != -1))
12287 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12288 " DWO info/types section [in module %s]"),
12291 /* Must have an abbrev section. */
12292 if (ids_seen[DW_SECT_ABBREV] == -1)
12294 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12295 " section [in module %s]"),
12298 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12299 htab->section_pool.v2.sizes =
12300 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12301 * nr_units * nr_columns);
12302 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12303 * nr_units * nr_columns))
12306 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12307 " [in module %s]"),
12315 /* Update SECTIONS with the data from SECTP.
12317 This function is like the other "locate" section routines that are
12318 passed to bfd_map_over_sections, but in this context the sections to
12319 read comes from the DWP V1 hash table, not the full ELF section table.
12321 The result is non-zero for success, or zero if an error was found. */
12324 locate_v1_virtual_dwo_sections (asection *sectp,
12325 struct virtual_v1_dwo_sections *sections)
12327 const struct dwop_section_names *names = &dwop_section_names;
12329 if (section_is_p (sectp->name, &names->abbrev_dwo))
12331 /* There can be only one. */
12332 if (sections->abbrev.s.section != NULL)
12334 sections->abbrev.s.section = sectp;
12335 sections->abbrev.size = bfd_get_section_size (sectp);
12337 else if (section_is_p (sectp->name, &names->info_dwo)
12338 || section_is_p (sectp->name, &names->types_dwo))
12340 /* There can be only one. */
12341 if (sections->info_or_types.s.section != NULL)
12343 sections->info_or_types.s.section = sectp;
12344 sections->info_or_types.size = bfd_get_section_size (sectp);
12346 else if (section_is_p (sectp->name, &names->line_dwo))
12348 /* There can be only one. */
12349 if (sections->line.s.section != NULL)
12351 sections->line.s.section = sectp;
12352 sections->line.size = bfd_get_section_size (sectp);
12354 else if (section_is_p (sectp->name, &names->loc_dwo))
12356 /* There can be only one. */
12357 if (sections->loc.s.section != NULL)
12359 sections->loc.s.section = sectp;
12360 sections->loc.size = bfd_get_section_size (sectp);
12362 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12364 /* There can be only one. */
12365 if (sections->macinfo.s.section != NULL)
12367 sections->macinfo.s.section = sectp;
12368 sections->macinfo.size = bfd_get_section_size (sectp);
12370 else if (section_is_p (sectp->name, &names->macro_dwo))
12372 /* There can be only one. */
12373 if (sections->macro.s.section != NULL)
12375 sections->macro.s.section = sectp;
12376 sections->macro.size = bfd_get_section_size (sectp);
12378 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12380 /* There can be only one. */
12381 if (sections->str_offsets.s.section != NULL)
12383 sections->str_offsets.s.section = sectp;
12384 sections->str_offsets.size = bfd_get_section_size (sectp);
12388 /* No other kind of section is valid. */
12395 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12396 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12397 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12398 This is for DWP version 1 files. */
12400 static struct dwo_unit *
12401 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12402 struct dwp_file *dwp_file,
12403 uint32_t unit_index,
12404 const char *comp_dir,
12405 ULONGEST signature, int is_debug_types)
12407 struct objfile *objfile = dwarf2_per_objfile->objfile;
12408 const struct dwp_hash_table *dwp_htab =
12409 is_debug_types ? dwp_file->tus : dwp_file->cus;
12410 bfd *dbfd = dwp_file->dbfd;
12411 const char *kind = is_debug_types ? "TU" : "CU";
12412 struct dwo_file *dwo_file;
12413 struct dwo_unit *dwo_unit;
12414 struct virtual_v1_dwo_sections sections;
12415 void **dwo_file_slot;
12418 gdb_assert (dwp_file->version == 1);
12420 if (dwarf_read_debug)
12422 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12424 pulongest (unit_index), hex_string (signature),
12428 /* Fetch the sections of this DWO unit.
12429 Put a limit on the number of sections we look for so that bad data
12430 doesn't cause us to loop forever. */
12432 #define MAX_NR_V1_DWO_SECTIONS \
12433 (1 /* .debug_info or .debug_types */ \
12434 + 1 /* .debug_abbrev */ \
12435 + 1 /* .debug_line */ \
12436 + 1 /* .debug_loc */ \
12437 + 1 /* .debug_str_offsets */ \
12438 + 1 /* .debug_macro or .debug_macinfo */ \
12439 + 1 /* trailing zero */)
12441 memset (§ions, 0, sizeof (sections));
12443 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12446 uint32_t section_nr =
12447 read_4_bytes (dbfd,
12448 dwp_htab->section_pool.v1.indices
12449 + (unit_index + i) * sizeof (uint32_t));
12451 if (section_nr == 0)
12453 if (section_nr >= dwp_file->num_sections)
12455 error (_("Dwarf Error: bad DWP hash table, section number too large"
12456 " [in module %s]"),
12460 sectp = dwp_file->elf_sections[section_nr];
12461 if (! locate_v1_virtual_dwo_sections (sectp, §ions))
12463 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12464 " [in module %s]"),
12470 || dwarf2_section_empty_p (§ions.info_or_types)
12471 || dwarf2_section_empty_p (§ions.abbrev))
12473 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12474 " [in module %s]"),
12477 if (i == MAX_NR_V1_DWO_SECTIONS)
12479 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12480 " [in module %s]"),
12484 /* It's easier for the rest of the code if we fake a struct dwo_file and
12485 have dwo_unit "live" in that. At least for now.
12487 The DWP file can be made up of a random collection of CUs and TUs.
12488 However, for each CU + set of TUs that came from the same original DWO
12489 file, we can combine them back into a virtual DWO file to save space
12490 (fewer struct dwo_file objects to allocate). Remember that for really
12491 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12493 std::string virtual_dwo_name =
12494 string_printf ("virtual-dwo/%d-%d-%d-%d",
12495 get_section_id (§ions.abbrev),
12496 get_section_id (§ions.line),
12497 get_section_id (§ions.loc),
12498 get_section_id (§ions.str_offsets));
12499 /* Can we use an existing virtual DWO file? */
12500 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12501 virtual_dwo_name.c_str (),
12503 /* Create one if necessary. */
12504 if (*dwo_file_slot == NULL)
12506 if (dwarf_read_debug)
12508 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12509 virtual_dwo_name.c_str ());
12511 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12513 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12514 virtual_dwo_name.c_str (),
12515 virtual_dwo_name.size ());
12516 dwo_file->comp_dir = comp_dir;
12517 dwo_file->sections.abbrev = sections.abbrev;
12518 dwo_file->sections.line = sections.line;
12519 dwo_file->sections.loc = sections.loc;
12520 dwo_file->sections.macinfo = sections.macinfo;
12521 dwo_file->sections.macro = sections.macro;
12522 dwo_file->sections.str_offsets = sections.str_offsets;
12523 /* The "str" section is global to the entire DWP file. */
12524 dwo_file->sections.str = dwp_file->sections.str;
12525 /* The info or types section is assigned below to dwo_unit,
12526 there's no need to record it in dwo_file.
12527 Also, we can't simply record type sections in dwo_file because
12528 we record a pointer into the vector in dwo_unit. As we collect more
12529 types we'll grow the vector and eventually have to reallocate space
12530 for it, invalidating all copies of pointers into the previous
12532 *dwo_file_slot = dwo_file;
12536 if (dwarf_read_debug)
12538 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12539 virtual_dwo_name.c_str ());
12541 dwo_file = (struct dwo_file *) *dwo_file_slot;
12544 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12545 dwo_unit->dwo_file = dwo_file;
12546 dwo_unit->signature = signature;
12547 dwo_unit->section =
12548 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12549 *dwo_unit->section = sections.info_or_types;
12550 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12555 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12556 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12557 piece within that section used by a TU/CU, return a virtual section
12558 of just that piece. */
12560 static struct dwarf2_section_info
12561 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12562 struct dwarf2_section_info *section,
12563 bfd_size_type offset, bfd_size_type size)
12565 struct dwarf2_section_info result;
12568 gdb_assert (section != NULL);
12569 gdb_assert (!section->is_virtual);
12571 memset (&result, 0, sizeof (result));
12572 result.s.containing_section = section;
12573 result.is_virtual = 1;
12578 sectp = get_section_bfd_section (section);
12580 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12581 bounds of the real section. This is a pretty-rare event, so just
12582 flag an error (easier) instead of a warning and trying to cope. */
12584 || offset + size > bfd_get_section_size (sectp))
12586 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12587 " in section %s [in module %s]"),
12588 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12589 objfile_name (dwarf2_per_objfile->objfile));
12592 result.virtual_offset = offset;
12593 result.size = size;
12597 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12598 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12599 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12600 This is for DWP version 2 files. */
12602 static struct dwo_unit *
12603 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12604 struct dwp_file *dwp_file,
12605 uint32_t unit_index,
12606 const char *comp_dir,
12607 ULONGEST signature, int is_debug_types)
12609 struct objfile *objfile = dwarf2_per_objfile->objfile;
12610 const struct dwp_hash_table *dwp_htab =
12611 is_debug_types ? dwp_file->tus : dwp_file->cus;
12612 bfd *dbfd = dwp_file->dbfd;
12613 const char *kind = is_debug_types ? "TU" : "CU";
12614 struct dwo_file *dwo_file;
12615 struct dwo_unit *dwo_unit;
12616 struct virtual_v2_dwo_sections sections;
12617 void **dwo_file_slot;
12620 gdb_assert (dwp_file->version == 2);
12622 if (dwarf_read_debug)
12624 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12626 pulongest (unit_index), hex_string (signature),
12630 /* Fetch the section offsets of this DWO unit. */
12632 memset (§ions, 0, sizeof (sections));
12634 for (i = 0; i < dwp_htab->nr_columns; ++i)
12636 uint32_t offset = read_4_bytes (dbfd,
12637 dwp_htab->section_pool.v2.offsets
12638 + (((unit_index - 1) * dwp_htab->nr_columns
12640 * sizeof (uint32_t)));
12641 uint32_t size = read_4_bytes (dbfd,
12642 dwp_htab->section_pool.v2.sizes
12643 + (((unit_index - 1) * dwp_htab->nr_columns
12645 * sizeof (uint32_t)));
12647 switch (dwp_htab->section_pool.v2.section_ids[i])
12650 case DW_SECT_TYPES:
12651 sections.info_or_types_offset = offset;
12652 sections.info_or_types_size = size;
12654 case DW_SECT_ABBREV:
12655 sections.abbrev_offset = offset;
12656 sections.abbrev_size = size;
12659 sections.line_offset = offset;
12660 sections.line_size = size;
12663 sections.loc_offset = offset;
12664 sections.loc_size = size;
12666 case DW_SECT_STR_OFFSETS:
12667 sections.str_offsets_offset = offset;
12668 sections.str_offsets_size = size;
12670 case DW_SECT_MACINFO:
12671 sections.macinfo_offset = offset;
12672 sections.macinfo_size = size;
12674 case DW_SECT_MACRO:
12675 sections.macro_offset = offset;
12676 sections.macro_size = size;
12681 /* It's easier for the rest of the code if we fake a struct dwo_file and
12682 have dwo_unit "live" in that. At least for now.
12684 The DWP file can be made up of a random collection of CUs and TUs.
12685 However, for each CU + set of TUs that came from the same original DWO
12686 file, we can combine them back into a virtual DWO file to save space
12687 (fewer struct dwo_file objects to allocate). Remember that for really
12688 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12690 std::string virtual_dwo_name =
12691 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12692 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12693 (long) (sections.line_size ? sections.line_offset : 0),
12694 (long) (sections.loc_size ? sections.loc_offset : 0),
12695 (long) (sections.str_offsets_size
12696 ? sections.str_offsets_offset : 0));
12697 /* Can we use an existing virtual DWO file? */
12698 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12699 virtual_dwo_name.c_str (),
12701 /* Create one if necessary. */
12702 if (*dwo_file_slot == NULL)
12704 if (dwarf_read_debug)
12706 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12707 virtual_dwo_name.c_str ());
12709 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12711 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12712 virtual_dwo_name.c_str (),
12713 virtual_dwo_name.size ());
12714 dwo_file->comp_dir = comp_dir;
12715 dwo_file->sections.abbrev =
12716 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12717 sections.abbrev_offset, sections.abbrev_size);
12718 dwo_file->sections.line =
12719 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12720 sections.line_offset, sections.line_size);
12721 dwo_file->sections.loc =
12722 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12723 sections.loc_offset, sections.loc_size);
12724 dwo_file->sections.macinfo =
12725 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12726 sections.macinfo_offset, sections.macinfo_size);
12727 dwo_file->sections.macro =
12728 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12729 sections.macro_offset, sections.macro_size);
12730 dwo_file->sections.str_offsets =
12731 create_dwp_v2_section (dwarf2_per_objfile,
12732 &dwp_file->sections.str_offsets,
12733 sections.str_offsets_offset,
12734 sections.str_offsets_size);
12735 /* The "str" section is global to the entire DWP file. */
12736 dwo_file->sections.str = dwp_file->sections.str;
12737 /* The info or types section is assigned below to dwo_unit,
12738 there's no need to record it in dwo_file.
12739 Also, we can't simply record type sections in dwo_file because
12740 we record a pointer into the vector in dwo_unit. As we collect more
12741 types we'll grow the vector and eventually have to reallocate space
12742 for it, invalidating all copies of pointers into the previous
12744 *dwo_file_slot = dwo_file;
12748 if (dwarf_read_debug)
12750 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12751 virtual_dwo_name.c_str ());
12753 dwo_file = (struct dwo_file *) *dwo_file_slot;
12756 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12757 dwo_unit->dwo_file = dwo_file;
12758 dwo_unit->signature = signature;
12759 dwo_unit->section =
12760 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12761 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12763 ? &dwp_file->sections.types
12764 : &dwp_file->sections.info,
12765 sections.info_or_types_offset,
12766 sections.info_or_types_size);
12767 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12772 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12773 Returns NULL if the signature isn't found. */
12775 static struct dwo_unit *
12776 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12777 struct dwp_file *dwp_file, const char *comp_dir,
12778 ULONGEST signature, int is_debug_types)
12780 const struct dwp_hash_table *dwp_htab =
12781 is_debug_types ? dwp_file->tus : dwp_file->cus;
12782 bfd *dbfd = dwp_file->dbfd;
12783 uint32_t mask = dwp_htab->nr_slots - 1;
12784 uint32_t hash = signature & mask;
12785 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12788 struct dwo_unit find_dwo_cu;
12790 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12791 find_dwo_cu.signature = signature;
12792 slot = htab_find_slot (is_debug_types
12793 ? dwp_file->loaded_tus
12794 : dwp_file->loaded_cus,
12795 &find_dwo_cu, INSERT);
12798 return (struct dwo_unit *) *slot;
12800 /* Use a for loop so that we don't loop forever on bad debug info. */
12801 for (i = 0; i < dwp_htab->nr_slots; ++i)
12803 ULONGEST signature_in_table;
12805 signature_in_table =
12806 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12807 if (signature_in_table == signature)
12809 uint32_t unit_index =
12810 read_4_bytes (dbfd,
12811 dwp_htab->unit_table + hash * sizeof (uint32_t));
12813 if (dwp_file->version == 1)
12815 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12816 dwp_file, unit_index,
12817 comp_dir, signature,
12822 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12823 dwp_file, unit_index,
12824 comp_dir, signature,
12827 return (struct dwo_unit *) *slot;
12829 if (signature_in_table == 0)
12831 hash = (hash + hash2) & mask;
12834 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12835 " [in module %s]"),
12839 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12840 Open the file specified by FILE_NAME and hand it off to BFD for
12841 preliminary analysis. Return a newly initialized bfd *, which
12842 includes a canonicalized copy of FILE_NAME.
12843 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12844 SEARCH_CWD is true if the current directory is to be searched.
12845 It will be searched before debug-file-directory.
12846 If successful, the file is added to the bfd include table of the
12847 objfile's bfd (see gdb_bfd_record_inclusion).
12848 If unable to find/open the file, return NULL.
12849 NOTE: This function is derived from symfile_bfd_open. */
12851 static gdb_bfd_ref_ptr
12852 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12853 const char *file_name, int is_dwp, int search_cwd)
12856 char *absolute_name;
12857 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12858 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12859 to debug_file_directory. */
12861 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12865 if (*debug_file_directory != '\0')
12866 search_path = concat (".", dirname_separator_string,
12867 debug_file_directory, (char *) NULL);
12869 search_path = xstrdup (".");
12872 search_path = xstrdup (debug_file_directory);
12874 flags = OPF_RETURN_REALPATH;
12876 flags |= OPF_SEARCH_IN_PATH;
12877 desc = openp (search_path, flags, file_name,
12878 O_RDONLY | O_BINARY, &absolute_name);
12879 xfree (search_path);
12883 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
12884 xfree (absolute_name);
12885 if (sym_bfd == NULL)
12887 bfd_set_cacheable (sym_bfd.get (), 1);
12889 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12892 /* Success. Record the bfd as having been included by the objfile's bfd.
12893 This is important because things like demangled_names_hash lives in the
12894 objfile's per_bfd space and may have references to things like symbol
12895 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12896 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12901 /* Try to open DWO file FILE_NAME.
12902 COMP_DIR is the DW_AT_comp_dir attribute.
12903 The result is the bfd handle of the file.
12904 If there is a problem finding or opening the file, return NULL.
12905 Upon success, the canonicalized path of the file is stored in the bfd,
12906 same as symfile_bfd_open. */
12908 static gdb_bfd_ref_ptr
12909 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12910 const char *file_name, const char *comp_dir)
12912 if (IS_ABSOLUTE_PATH (file_name))
12913 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12914 0 /*is_dwp*/, 0 /*search_cwd*/);
12916 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12918 if (comp_dir != NULL)
12920 char *path_to_try = concat (comp_dir, SLASH_STRING,
12921 file_name, (char *) NULL);
12923 /* NOTE: If comp_dir is a relative path, this will also try the
12924 search path, which seems useful. */
12925 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12928 1 /*search_cwd*/));
12929 xfree (path_to_try);
12934 /* That didn't work, try debug-file-directory, which, despite its name,
12935 is a list of paths. */
12937 if (*debug_file_directory == '\0')
12940 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12941 0 /*is_dwp*/, 1 /*search_cwd*/);
12944 /* This function is mapped across the sections and remembers the offset and
12945 size of each of the DWO debugging sections we are interested in. */
12948 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12950 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12951 const struct dwop_section_names *names = &dwop_section_names;
12953 if (section_is_p (sectp->name, &names->abbrev_dwo))
12955 dwo_sections->abbrev.s.section = sectp;
12956 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12958 else if (section_is_p (sectp->name, &names->info_dwo))
12960 dwo_sections->info.s.section = sectp;
12961 dwo_sections->info.size = bfd_get_section_size (sectp);
12963 else if (section_is_p (sectp->name, &names->line_dwo))
12965 dwo_sections->line.s.section = sectp;
12966 dwo_sections->line.size = bfd_get_section_size (sectp);
12968 else if (section_is_p (sectp->name, &names->loc_dwo))
12970 dwo_sections->loc.s.section = sectp;
12971 dwo_sections->loc.size = bfd_get_section_size (sectp);
12973 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12975 dwo_sections->macinfo.s.section = sectp;
12976 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12978 else if (section_is_p (sectp->name, &names->macro_dwo))
12980 dwo_sections->macro.s.section = sectp;
12981 dwo_sections->macro.size = bfd_get_section_size (sectp);
12983 else if (section_is_p (sectp->name, &names->str_dwo))
12985 dwo_sections->str.s.section = sectp;
12986 dwo_sections->str.size = bfd_get_section_size (sectp);
12988 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12990 dwo_sections->str_offsets.s.section = sectp;
12991 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12993 else if (section_is_p (sectp->name, &names->types_dwo))
12995 struct dwarf2_section_info type_section;
12997 memset (&type_section, 0, sizeof (type_section));
12998 type_section.s.section = sectp;
12999 type_section.size = bfd_get_section_size (sectp);
13000 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
13005 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
13006 by PER_CU. This is for the non-DWP case.
13007 The result is NULL if DWO_NAME can't be found. */
13009 static struct dwo_file *
13010 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13011 const char *dwo_name, const char *comp_dir)
13013 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
13014 struct objfile *objfile = dwarf2_per_objfile->objfile;
13015 struct dwo_file *dwo_file;
13016 struct cleanup *cleanups;
13018 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
13021 if (dwarf_read_debug)
13022 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13025 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
13026 dwo_file->dwo_name = dwo_name;
13027 dwo_file->comp_dir = comp_dir;
13028 dwo_file->dbfd = dbfd.release ();
13030 free_dwo_file_cleanup_data *cleanup_data = XNEW (free_dwo_file_cleanup_data);
13031 cleanup_data->dwo_file = dwo_file;
13032 cleanup_data->dwarf2_per_objfile = dwarf2_per_objfile;
13034 cleanups = make_cleanup (free_dwo_file_cleanup, cleanup_data);
13036 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
13037 &dwo_file->sections);
13039 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13042 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file,
13043 dwo_file->sections.types, dwo_file->tus);
13045 discard_cleanups (cleanups);
13047 if (dwarf_read_debug)
13048 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13053 /* This function is mapped across the sections and remembers the offset and
13054 size of each of the DWP debugging sections common to version 1 and 2 that
13055 we are interested in. */
13058 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13059 void *dwp_file_ptr)
13061 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13062 const struct dwop_section_names *names = &dwop_section_names;
13063 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13065 /* Record the ELF section number for later lookup: this is what the
13066 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13067 gdb_assert (elf_section_nr < dwp_file->num_sections);
13068 dwp_file->elf_sections[elf_section_nr] = sectp;
13070 /* Look for specific sections that we need. */
13071 if (section_is_p (sectp->name, &names->str_dwo))
13073 dwp_file->sections.str.s.section = sectp;
13074 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13076 else if (section_is_p (sectp->name, &names->cu_index))
13078 dwp_file->sections.cu_index.s.section = sectp;
13079 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13081 else if (section_is_p (sectp->name, &names->tu_index))
13083 dwp_file->sections.tu_index.s.section = sectp;
13084 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13088 /* This function is mapped across the sections and remembers the offset and
13089 size of each of the DWP version 2 debugging sections that we are interested
13090 in. This is split into a separate function because we don't know if we
13091 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13094 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13096 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13097 const struct dwop_section_names *names = &dwop_section_names;
13098 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13100 /* Record the ELF section number for later lookup: this is what the
13101 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13102 gdb_assert (elf_section_nr < dwp_file->num_sections);
13103 dwp_file->elf_sections[elf_section_nr] = sectp;
13105 /* Look for specific sections that we need. */
13106 if (section_is_p (sectp->name, &names->abbrev_dwo))
13108 dwp_file->sections.abbrev.s.section = sectp;
13109 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13111 else if (section_is_p (sectp->name, &names->info_dwo))
13113 dwp_file->sections.info.s.section = sectp;
13114 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13116 else if (section_is_p (sectp->name, &names->line_dwo))
13118 dwp_file->sections.line.s.section = sectp;
13119 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13121 else if (section_is_p (sectp->name, &names->loc_dwo))
13123 dwp_file->sections.loc.s.section = sectp;
13124 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13126 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13128 dwp_file->sections.macinfo.s.section = sectp;
13129 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13131 else if (section_is_p (sectp->name, &names->macro_dwo))
13133 dwp_file->sections.macro.s.section = sectp;
13134 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13136 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13138 dwp_file->sections.str_offsets.s.section = sectp;
13139 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13141 else if (section_is_p (sectp->name, &names->types_dwo))
13143 dwp_file->sections.types.s.section = sectp;
13144 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13148 /* Hash function for dwp_file loaded CUs/TUs. */
13151 hash_dwp_loaded_cutus (const void *item)
13153 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13155 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13156 return dwo_unit->signature;
13159 /* Equality function for dwp_file loaded CUs/TUs. */
13162 eq_dwp_loaded_cutus (const void *a, const void *b)
13164 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13165 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13167 return dua->signature == dub->signature;
13170 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13173 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13175 return htab_create_alloc_ex (3,
13176 hash_dwp_loaded_cutus,
13177 eq_dwp_loaded_cutus,
13179 &objfile->objfile_obstack,
13180 hashtab_obstack_allocate,
13181 dummy_obstack_deallocate);
13184 /* Try to open DWP file FILE_NAME.
13185 The result is the bfd handle of the file.
13186 If there is a problem finding or opening the file, return NULL.
13187 Upon success, the canonicalized path of the file is stored in the bfd,
13188 same as symfile_bfd_open. */
13190 static gdb_bfd_ref_ptr
13191 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13192 const char *file_name)
13194 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13196 1 /*search_cwd*/));
13200 /* Work around upstream bug 15652.
13201 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13202 [Whether that's a "bug" is debatable, but it is getting in our way.]
13203 We have no real idea where the dwp file is, because gdb's realpath-ing
13204 of the executable's path may have discarded the needed info.
13205 [IWBN if the dwp file name was recorded in the executable, akin to
13206 .gnu_debuglink, but that doesn't exist yet.]
13207 Strip the directory from FILE_NAME and search again. */
13208 if (*debug_file_directory != '\0')
13210 /* Don't implicitly search the current directory here.
13211 If the user wants to search "." to handle this case,
13212 it must be added to debug-file-directory. */
13213 return try_open_dwop_file (dwarf2_per_objfile,
13214 lbasename (file_name), 1 /*is_dwp*/,
13221 /* Initialize the use of the DWP file for the current objfile.
13222 By convention the name of the DWP file is ${objfile}.dwp.
13223 The result is NULL if it can't be found. */
13225 static struct dwp_file *
13226 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13228 struct objfile *objfile = dwarf2_per_objfile->objfile;
13229 struct dwp_file *dwp_file;
13231 /* Try to find first .dwp for the binary file before any symbolic links
13234 /* If the objfile is a debug file, find the name of the real binary
13235 file and get the name of dwp file from there. */
13236 std::string dwp_name;
13237 if (objfile->separate_debug_objfile_backlink != NULL)
13239 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13240 const char *backlink_basename = lbasename (backlink->original_name);
13242 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13245 dwp_name = objfile->original_name;
13247 dwp_name += ".dwp";
13249 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13251 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13253 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13254 dwp_name = objfile_name (objfile);
13255 dwp_name += ".dwp";
13256 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13261 if (dwarf_read_debug)
13262 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13265 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
13266 dwp_file->name = bfd_get_filename (dbfd.get ());
13267 dwp_file->dbfd = dbfd.release ();
13269 /* +1: section 0 is unused */
13270 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
13271 dwp_file->elf_sections =
13272 OBSTACK_CALLOC (&objfile->objfile_obstack,
13273 dwp_file->num_sections, asection *);
13275 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13278 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
13280 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 1);
13282 /* The DWP file version is stored in the hash table. Oh well. */
13283 if (dwp_file->cus && dwp_file->tus
13284 && dwp_file->cus->version != dwp_file->tus->version)
13286 /* Technically speaking, we should try to limp along, but this is
13287 pretty bizarre. We use pulongest here because that's the established
13288 portability solution (e.g, we cannot use %u for uint32_t). */
13289 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13290 " TU version %s [in DWP file %s]"),
13291 pulongest (dwp_file->cus->version),
13292 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13296 dwp_file->version = dwp_file->cus->version;
13297 else if (dwp_file->tus)
13298 dwp_file->version = dwp_file->tus->version;
13300 dwp_file->version = 2;
13302 if (dwp_file->version == 2)
13303 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13306 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13307 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13309 if (dwarf_read_debug)
13311 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13312 fprintf_unfiltered (gdb_stdlog,
13313 " %s CUs, %s TUs\n",
13314 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13315 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13321 /* Wrapper around open_and_init_dwp_file, only open it once. */
13323 static struct dwp_file *
13324 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13326 if (! dwarf2_per_objfile->dwp_checked)
13328 dwarf2_per_objfile->dwp_file
13329 = open_and_init_dwp_file (dwarf2_per_objfile);
13330 dwarf2_per_objfile->dwp_checked = 1;
13332 return dwarf2_per_objfile->dwp_file;
13335 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13336 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13337 or in the DWP file for the objfile, referenced by THIS_UNIT.
13338 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13339 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13341 This is called, for example, when wanting to read a variable with a
13342 complex location. Therefore we don't want to do file i/o for every call.
13343 Therefore we don't want to look for a DWO file on every call.
13344 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13345 then we check if we've already seen DWO_NAME, and only THEN do we check
13348 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13349 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13351 static struct dwo_unit *
13352 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13353 const char *dwo_name, const char *comp_dir,
13354 ULONGEST signature, int is_debug_types)
13356 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13357 struct objfile *objfile = dwarf2_per_objfile->objfile;
13358 const char *kind = is_debug_types ? "TU" : "CU";
13359 void **dwo_file_slot;
13360 struct dwo_file *dwo_file;
13361 struct dwp_file *dwp_file;
13363 /* First see if there's a DWP file.
13364 If we have a DWP file but didn't find the DWO inside it, don't
13365 look for the original DWO file. It makes gdb behave differently
13366 depending on whether one is debugging in the build tree. */
13368 dwp_file = get_dwp_file (dwarf2_per_objfile);
13369 if (dwp_file != NULL)
13371 const struct dwp_hash_table *dwp_htab =
13372 is_debug_types ? dwp_file->tus : dwp_file->cus;
13374 if (dwp_htab != NULL)
13376 struct dwo_unit *dwo_cutu =
13377 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13378 signature, is_debug_types);
13380 if (dwo_cutu != NULL)
13382 if (dwarf_read_debug)
13384 fprintf_unfiltered (gdb_stdlog,
13385 "Virtual DWO %s %s found: @%s\n",
13386 kind, hex_string (signature),
13387 host_address_to_string (dwo_cutu));
13395 /* No DWP file, look for the DWO file. */
13397 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13398 dwo_name, comp_dir);
13399 if (*dwo_file_slot == NULL)
13401 /* Read in the file and build a table of the CUs/TUs it contains. */
13402 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13404 /* NOTE: This will be NULL if unable to open the file. */
13405 dwo_file = (struct dwo_file *) *dwo_file_slot;
13407 if (dwo_file != NULL)
13409 struct dwo_unit *dwo_cutu = NULL;
13411 if (is_debug_types && dwo_file->tus)
13413 struct dwo_unit find_dwo_cutu;
13415 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13416 find_dwo_cutu.signature = signature;
13418 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13420 else if (!is_debug_types && dwo_file->cus)
13422 struct dwo_unit find_dwo_cutu;
13424 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13425 find_dwo_cutu.signature = signature;
13426 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13430 if (dwo_cutu != NULL)
13432 if (dwarf_read_debug)
13434 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13435 kind, dwo_name, hex_string (signature),
13436 host_address_to_string (dwo_cutu));
13443 /* We didn't find it. This could mean a dwo_id mismatch, or
13444 someone deleted the DWO/DWP file, or the search path isn't set up
13445 correctly to find the file. */
13447 if (dwarf_read_debug)
13449 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13450 kind, dwo_name, hex_string (signature));
13453 /* This is a warning and not a complaint because it can be caused by
13454 pilot error (e.g., user accidentally deleting the DWO). */
13456 /* Print the name of the DWP file if we looked there, helps the user
13457 better diagnose the problem. */
13458 std::string dwp_text;
13460 if (dwp_file != NULL)
13461 dwp_text = string_printf (" [in DWP file %s]",
13462 lbasename (dwp_file->name));
13464 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
13465 " [in module %s]"),
13466 kind, dwo_name, hex_string (signature),
13468 this_unit->is_debug_types ? "TU" : "CU",
13469 to_underlying (this_unit->sect_off), objfile_name (objfile));
13474 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13475 See lookup_dwo_cutu_unit for details. */
13477 static struct dwo_unit *
13478 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13479 const char *dwo_name, const char *comp_dir,
13480 ULONGEST signature)
13482 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13485 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13486 See lookup_dwo_cutu_unit for details. */
13488 static struct dwo_unit *
13489 lookup_dwo_type_unit (struct signatured_type *this_tu,
13490 const char *dwo_name, const char *comp_dir)
13492 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13495 /* Traversal function for queue_and_load_all_dwo_tus. */
13498 queue_and_load_dwo_tu (void **slot, void *info)
13500 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13501 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13502 ULONGEST signature = dwo_unit->signature;
13503 struct signatured_type *sig_type =
13504 lookup_dwo_signatured_type (per_cu->cu, signature);
13506 if (sig_type != NULL)
13508 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13510 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13511 a real dependency of PER_CU on SIG_TYPE. That is detected later
13512 while processing PER_CU. */
13513 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13514 load_full_type_unit (sig_cu);
13515 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13521 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13522 The DWO may have the only definition of the type, though it may not be
13523 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13524 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13527 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13529 struct dwo_unit *dwo_unit;
13530 struct dwo_file *dwo_file;
13532 gdb_assert (!per_cu->is_debug_types);
13533 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13534 gdb_assert (per_cu->cu != NULL);
13536 dwo_unit = per_cu->cu->dwo_unit;
13537 gdb_assert (dwo_unit != NULL);
13539 dwo_file = dwo_unit->dwo_file;
13540 if (dwo_file->tus != NULL)
13541 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13544 /* Free all resources associated with DWO_FILE.
13545 Close the DWO file and munmap the sections.
13546 All memory should be on the objfile obstack. */
13549 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
13552 /* Note: dbfd is NULL for virtual DWO files. */
13553 gdb_bfd_unref (dwo_file->dbfd);
13555 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13558 /* Wrapper for free_dwo_file for use in cleanups. */
13561 free_dwo_file_cleanup (void *arg)
13563 struct free_dwo_file_cleanup_data *data
13564 = (struct free_dwo_file_cleanup_data *) arg;
13565 struct objfile *objfile = data->dwarf2_per_objfile->objfile;
13567 free_dwo_file (data->dwo_file, objfile);
13572 /* Traversal function for free_dwo_files. */
13575 free_dwo_file_from_slot (void **slot, void *info)
13577 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13578 struct objfile *objfile = (struct objfile *) info;
13580 free_dwo_file (dwo_file, objfile);
13585 /* Free all resources associated with DWO_FILES. */
13588 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13590 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
13593 /* Read in various DIEs. */
13595 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13596 Inherit only the children of the DW_AT_abstract_origin DIE not being
13597 already referenced by DW_AT_abstract_origin from the children of the
13601 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13603 struct die_info *child_die;
13604 sect_offset *offsetp;
13605 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13606 struct die_info *origin_die;
13607 /* Iterator of the ORIGIN_DIE children. */
13608 struct die_info *origin_child_die;
13609 struct attribute *attr;
13610 struct dwarf2_cu *origin_cu;
13611 struct pending **origin_previous_list_in_scope;
13613 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13617 /* Note that following die references may follow to a die in a
13621 origin_die = follow_die_ref (die, attr, &origin_cu);
13623 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13625 origin_previous_list_in_scope = origin_cu->list_in_scope;
13626 origin_cu->list_in_scope = cu->list_in_scope;
13628 if (die->tag != origin_die->tag
13629 && !(die->tag == DW_TAG_inlined_subroutine
13630 && origin_die->tag == DW_TAG_subprogram))
13631 complaint (&symfile_complaints,
13632 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
13633 to_underlying (die->sect_off),
13634 to_underlying (origin_die->sect_off));
13636 std::vector<sect_offset> offsets;
13638 for (child_die = die->child;
13639 child_die && child_die->tag;
13640 child_die = sibling_die (child_die))
13642 struct die_info *child_origin_die;
13643 struct dwarf2_cu *child_origin_cu;
13645 /* We are trying to process concrete instance entries:
13646 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13647 it's not relevant to our analysis here. i.e. detecting DIEs that are
13648 present in the abstract instance but not referenced in the concrete
13650 if (child_die->tag == DW_TAG_call_site
13651 || child_die->tag == DW_TAG_GNU_call_site)
13654 /* For each CHILD_DIE, find the corresponding child of
13655 ORIGIN_DIE. If there is more than one layer of
13656 DW_AT_abstract_origin, follow them all; there shouldn't be,
13657 but GCC versions at least through 4.4 generate this (GCC PR
13659 child_origin_die = child_die;
13660 child_origin_cu = cu;
13663 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13667 child_origin_die = follow_die_ref (child_origin_die, attr,
13671 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13672 counterpart may exist. */
13673 if (child_origin_die != child_die)
13675 if (child_die->tag != child_origin_die->tag
13676 && !(child_die->tag == DW_TAG_inlined_subroutine
13677 && child_origin_die->tag == DW_TAG_subprogram))
13678 complaint (&symfile_complaints,
13679 _("Child DIE 0x%x and its abstract origin 0x%x have "
13681 to_underlying (child_die->sect_off),
13682 to_underlying (child_origin_die->sect_off));
13683 if (child_origin_die->parent != origin_die)
13684 complaint (&symfile_complaints,
13685 _("Child DIE 0x%x and its abstract origin 0x%x have "
13686 "different parents"),
13687 to_underlying (child_die->sect_off),
13688 to_underlying (child_origin_die->sect_off));
13690 offsets.push_back (child_origin_die->sect_off);
13693 std::sort (offsets.begin (), offsets.end ());
13694 sect_offset *offsets_end = offsets.data () + offsets.size ();
13695 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13696 if (offsetp[-1] == *offsetp)
13697 complaint (&symfile_complaints,
13698 _("Multiple children of DIE 0x%x refer "
13699 "to DIE 0x%x as their abstract origin"),
13700 to_underlying (die->sect_off), to_underlying (*offsetp));
13702 offsetp = offsets.data ();
13703 origin_child_die = origin_die->child;
13704 while (origin_child_die && origin_child_die->tag)
13706 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13707 while (offsetp < offsets_end
13708 && *offsetp < origin_child_die->sect_off)
13710 if (offsetp >= offsets_end
13711 || *offsetp > origin_child_die->sect_off)
13713 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13714 Check whether we're already processing ORIGIN_CHILD_DIE.
13715 This can happen with mutually referenced abstract_origins.
13717 if (!origin_child_die->in_process)
13718 process_die (origin_child_die, origin_cu);
13720 origin_child_die = sibling_die (origin_child_die);
13722 origin_cu->list_in_scope = origin_previous_list_in_scope;
13726 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13728 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13729 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13730 struct context_stack *newobj;
13733 struct die_info *child_die;
13734 struct attribute *attr, *call_line, *call_file;
13736 CORE_ADDR baseaddr;
13737 struct block *block;
13738 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13739 std::vector<struct symbol *> template_args;
13740 struct template_symbol *templ_func = NULL;
13744 /* If we do not have call site information, we can't show the
13745 caller of this inlined function. That's too confusing, so
13746 only use the scope for local variables. */
13747 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13748 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13749 if (call_line == NULL || call_file == NULL)
13751 read_lexical_block_scope (die, cu);
13756 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13758 name = dwarf2_name (die, cu);
13760 /* Ignore functions with missing or empty names. These are actually
13761 illegal according to the DWARF standard. */
13764 complaint (&symfile_complaints,
13765 _("missing name for subprogram DIE at %d"),
13766 to_underlying (die->sect_off));
13770 /* Ignore functions with missing or invalid low and high pc attributes. */
13771 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13772 <= PC_BOUNDS_INVALID)
13774 attr = dwarf2_attr (die, DW_AT_external, cu);
13775 if (!attr || !DW_UNSND (attr))
13776 complaint (&symfile_complaints,
13777 _("cannot get low and high bounds "
13778 "for subprogram DIE at %d"),
13779 to_underlying (die->sect_off));
13783 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13784 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13786 /* If we have any template arguments, then we must allocate a
13787 different sort of symbol. */
13788 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13790 if (child_die->tag == DW_TAG_template_type_param
13791 || child_die->tag == DW_TAG_template_value_param)
13793 templ_func = allocate_template_symbol (objfile);
13794 templ_func->subclass = SYMBOL_TEMPLATE;
13799 newobj = push_context (0, lowpc);
13800 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13801 (struct symbol *) templ_func);
13803 /* If there is a location expression for DW_AT_frame_base, record
13805 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13807 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13809 /* If there is a location for the static link, record it. */
13810 newobj->static_link = NULL;
13811 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13814 newobj->static_link
13815 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13816 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13819 cu->list_in_scope = &local_symbols;
13821 if (die->child != NULL)
13823 child_die = die->child;
13824 while (child_die && child_die->tag)
13826 if (child_die->tag == DW_TAG_template_type_param
13827 || child_die->tag == DW_TAG_template_value_param)
13829 struct symbol *arg = new_symbol (child_die, NULL, cu);
13832 template_args.push_back (arg);
13835 process_die (child_die, cu);
13836 child_die = sibling_die (child_die);
13840 inherit_abstract_dies (die, cu);
13842 /* If we have a DW_AT_specification, we might need to import using
13843 directives from the context of the specification DIE. See the
13844 comment in determine_prefix. */
13845 if (cu->language == language_cplus
13846 && dwarf2_attr (die, DW_AT_specification, cu))
13848 struct dwarf2_cu *spec_cu = cu;
13849 struct die_info *spec_die = die_specification (die, &spec_cu);
13853 child_die = spec_die->child;
13854 while (child_die && child_die->tag)
13856 if (child_die->tag == DW_TAG_imported_module)
13857 process_die (child_die, spec_cu);
13858 child_die = sibling_die (child_die);
13861 /* In some cases, GCC generates specification DIEs that
13862 themselves contain DW_AT_specification attributes. */
13863 spec_die = die_specification (spec_die, &spec_cu);
13867 newobj = pop_context ();
13868 /* Make a block for the local symbols within. */
13869 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
13870 newobj->static_link, lowpc, highpc);
13872 /* For C++, set the block's scope. */
13873 if ((cu->language == language_cplus
13874 || cu->language == language_fortran
13875 || cu->language == language_d
13876 || cu->language == language_rust)
13877 && cu->processing_has_namespace_info)
13878 block_set_scope (block, determine_prefix (die, cu),
13879 &objfile->objfile_obstack);
13881 /* If we have address ranges, record them. */
13882 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13884 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
13886 /* Attach template arguments to function. */
13887 if (!template_args.empty ())
13889 gdb_assert (templ_func != NULL);
13891 templ_func->n_template_arguments = template_args.size ();
13892 templ_func->template_arguments
13893 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13894 templ_func->n_template_arguments);
13895 memcpy (templ_func->template_arguments,
13896 template_args.data (),
13897 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13900 /* In C++, we can have functions nested inside functions (e.g., when
13901 a function declares a class that has methods). This means that
13902 when we finish processing a function scope, we may need to go
13903 back to building a containing block's symbol lists. */
13904 local_symbols = newobj->locals;
13905 local_using_directives = newobj->local_using_directives;
13907 /* If we've finished processing a top-level function, subsequent
13908 symbols go in the file symbol list. */
13909 if (outermost_context_p ())
13910 cu->list_in_scope = &file_symbols;
13913 /* Process all the DIES contained within a lexical block scope. Start
13914 a new scope, process the dies, and then close the scope. */
13917 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13919 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13920 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13921 struct context_stack *newobj;
13922 CORE_ADDR lowpc, highpc;
13923 struct die_info *child_die;
13924 CORE_ADDR baseaddr;
13926 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13928 /* Ignore blocks with missing or invalid low and high pc attributes. */
13929 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13930 as multiple lexical blocks? Handling children in a sane way would
13931 be nasty. Might be easier to properly extend generic blocks to
13932 describe ranges. */
13933 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13935 case PC_BOUNDS_NOT_PRESENT:
13936 /* DW_TAG_lexical_block has no attributes, process its children as if
13937 there was no wrapping by that DW_TAG_lexical_block.
13938 GCC does no longer produces such DWARF since GCC r224161. */
13939 for (child_die = die->child;
13940 child_die != NULL && child_die->tag;
13941 child_die = sibling_die (child_die))
13942 process_die (child_die, cu);
13944 case PC_BOUNDS_INVALID:
13947 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13948 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13950 push_context (0, lowpc);
13951 if (die->child != NULL)
13953 child_die = die->child;
13954 while (child_die && child_die->tag)
13956 process_die (child_die, cu);
13957 child_die = sibling_die (child_die);
13960 inherit_abstract_dies (die, cu);
13961 newobj = pop_context ();
13963 if (local_symbols != NULL || local_using_directives != NULL)
13965 struct block *block
13966 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
13967 newobj->start_addr, highpc);
13969 /* Note that recording ranges after traversing children, as we
13970 do here, means that recording a parent's ranges entails
13971 walking across all its children's ranges as they appear in
13972 the address map, which is quadratic behavior.
13974 It would be nicer to record the parent's ranges before
13975 traversing its children, simply overriding whatever you find
13976 there. But since we don't even decide whether to create a
13977 block until after we've traversed its children, that's hard
13979 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13981 local_symbols = newobj->locals;
13982 local_using_directives = newobj->local_using_directives;
13985 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13988 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13990 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13991 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13992 CORE_ADDR pc, baseaddr;
13993 struct attribute *attr;
13994 struct call_site *call_site, call_site_local;
13997 struct die_info *child_die;
13999 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14001 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
14004 /* This was a pre-DWARF-5 GNU extension alias
14005 for DW_AT_call_return_pc. */
14006 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14010 complaint (&symfile_complaints,
14011 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
14012 "DIE 0x%x [in module %s]"),
14013 to_underlying (die->sect_off), objfile_name (objfile));
14016 pc = attr_value_as_address (attr) + baseaddr;
14017 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
14019 if (cu->call_site_htab == NULL)
14020 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14021 NULL, &objfile->objfile_obstack,
14022 hashtab_obstack_allocate, NULL);
14023 call_site_local.pc = pc;
14024 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14027 complaint (&symfile_complaints,
14028 _("Duplicate PC %s for DW_TAG_call_site "
14029 "DIE 0x%x [in module %s]"),
14030 paddress (gdbarch, pc), to_underlying (die->sect_off),
14031 objfile_name (objfile));
14035 /* Count parameters at the caller. */
14038 for (child_die = die->child; child_die && child_die->tag;
14039 child_die = sibling_die (child_die))
14041 if (child_die->tag != DW_TAG_call_site_parameter
14042 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14044 complaint (&symfile_complaints,
14045 _("Tag %d is not DW_TAG_call_site_parameter in "
14046 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14047 child_die->tag, to_underlying (child_die->sect_off),
14048 objfile_name (objfile));
14056 = ((struct call_site *)
14057 obstack_alloc (&objfile->objfile_obstack,
14058 sizeof (*call_site)
14059 + (sizeof (*call_site->parameter) * (nparams - 1))));
14061 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14062 call_site->pc = pc;
14064 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14065 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14067 struct die_info *func_die;
14069 /* Skip also over DW_TAG_inlined_subroutine. */
14070 for (func_die = die->parent;
14071 func_die && func_die->tag != DW_TAG_subprogram
14072 && func_die->tag != DW_TAG_subroutine_type;
14073 func_die = func_die->parent);
14075 /* DW_AT_call_all_calls is a superset
14076 of DW_AT_call_all_tail_calls. */
14078 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14079 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14080 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14081 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14083 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14084 not complete. But keep CALL_SITE for look ups via call_site_htab,
14085 both the initial caller containing the real return address PC and
14086 the final callee containing the current PC of a chain of tail
14087 calls do not need to have the tail call list complete. But any
14088 function candidate for a virtual tail call frame searched via
14089 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14090 determined unambiguously. */
14094 struct type *func_type = NULL;
14097 func_type = get_die_type (func_die, cu);
14098 if (func_type != NULL)
14100 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14102 /* Enlist this call site to the function. */
14103 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14104 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14107 complaint (&symfile_complaints,
14108 _("Cannot find function owning DW_TAG_call_site "
14109 "DIE 0x%x [in module %s]"),
14110 to_underlying (die->sect_off), objfile_name (objfile));
14114 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14116 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14118 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14121 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14122 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14124 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14125 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14126 /* Keep NULL DWARF_BLOCK. */;
14127 else if (attr_form_is_block (attr))
14129 struct dwarf2_locexpr_baton *dlbaton;
14131 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14132 dlbaton->data = DW_BLOCK (attr)->data;
14133 dlbaton->size = DW_BLOCK (attr)->size;
14134 dlbaton->per_cu = cu->per_cu;
14136 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14138 else if (attr_form_is_ref (attr))
14140 struct dwarf2_cu *target_cu = cu;
14141 struct die_info *target_die;
14143 target_die = follow_die_ref (die, attr, &target_cu);
14144 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14145 if (die_is_declaration (target_die, target_cu))
14147 const char *target_physname;
14149 /* Prefer the mangled name; otherwise compute the demangled one. */
14150 target_physname = dw2_linkage_name (target_die, target_cu);
14151 if (target_physname == NULL)
14152 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14153 if (target_physname == NULL)
14154 complaint (&symfile_complaints,
14155 _("DW_AT_call_target target DIE has invalid "
14156 "physname, for referencing DIE 0x%x [in module %s]"),
14157 to_underlying (die->sect_off), objfile_name (objfile));
14159 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14165 /* DW_AT_entry_pc should be preferred. */
14166 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14167 <= PC_BOUNDS_INVALID)
14168 complaint (&symfile_complaints,
14169 _("DW_AT_call_target target DIE has invalid "
14170 "low pc, for referencing DIE 0x%x [in module %s]"),
14171 to_underlying (die->sect_off), objfile_name (objfile));
14174 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14175 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14180 complaint (&symfile_complaints,
14181 _("DW_TAG_call_site DW_AT_call_target is neither "
14182 "block nor reference, for DIE 0x%x [in module %s]"),
14183 to_underlying (die->sect_off), objfile_name (objfile));
14185 call_site->per_cu = cu->per_cu;
14187 for (child_die = die->child;
14188 child_die && child_die->tag;
14189 child_die = sibling_die (child_die))
14191 struct call_site_parameter *parameter;
14192 struct attribute *loc, *origin;
14194 if (child_die->tag != DW_TAG_call_site_parameter
14195 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14197 /* Already printed the complaint above. */
14201 gdb_assert (call_site->parameter_count < nparams);
14202 parameter = &call_site->parameter[call_site->parameter_count];
14204 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14205 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14206 register is contained in DW_AT_call_value. */
14208 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14209 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14210 if (origin == NULL)
14212 /* This was a pre-DWARF-5 GNU extension alias
14213 for DW_AT_call_parameter. */
14214 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14216 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14218 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14220 sect_offset sect_off
14221 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14222 if (!offset_in_cu_p (&cu->header, sect_off))
14224 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14225 binding can be done only inside one CU. Such referenced DIE
14226 therefore cannot be even moved to DW_TAG_partial_unit. */
14227 complaint (&symfile_complaints,
14228 _("DW_AT_call_parameter offset is not in CU for "
14229 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14230 to_underlying (child_die->sect_off),
14231 objfile_name (objfile));
14234 parameter->u.param_cu_off
14235 = (cu_offset) (sect_off - cu->header.sect_off);
14237 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14239 complaint (&symfile_complaints,
14240 _("No DW_FORM_block* DW_AT_location for "
14241 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14242 to_underlying (child_die->sect_off), objfile_name (objfile));
14247 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14248 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14249 if (parameter->u.dwarf_reg != -1)
14250 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14251 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14252 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14253 ¶meter->u.fb_offset))
14254 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14257 complaint (&symfile_complaints,
14258 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14259 "for DW_FORM_block* DW_AT_location is supported for "
14260 "DW_TAG_call_site child DIE 0x%x "
14262 to_underlying (child_die->sect_off),
14263 objfile_name (objfile));
14268 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14270 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14271 if (!attr_form_is_block (attr))
14273 complaint (&symfile_complaints,
14274 _("No DW_FORM_block* DW_AT_call_value for "
14275 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14276 to_underlying (child_die->sect_off),
14277 objfile_name (objfile));
14280 parameter->value = DW_BLOCK (attr)->data;
14281 parameter->value_size = DW_BLOCK (attr)->size;
14283 /* Parameters are not pre-cleared by memset above. */
14284 parameter->data_value = NULL;
14285 parameter->data_value_size = 0;
14286 call_site->parameter_count++;
14288 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14290 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14293 if (!attr_form_is_block (attr))
14294 complaint (&symfile_complaints,
14295 _("No DW_FORM_block* DW_AT_call_data_value for "
14296 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14297 to_underlying (child_die->sect_off),
14298 objfile_name (objfile));
14301 parameter->data_value = DW_BLOCK (attr)->data;
14302 parameter->data_value_size = DW_BLOCK (attr)->size;
14308 /* Helper function for read_variable. If DIE represents a virtual
14309 table, then return the type of the concrete object that is
14310 associated with the virtual table. Otherwise, return NULL. */
14312 static struct type *
14313 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14315 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14319 /* Find the type DIE. */
14320 struct die_info *type_die = NULL;
14321 struct dwarf2_cu *type_cu = cu;
14323 if (attr_form_is_ref (attr))
14324 type_die = follow_die_ref (die, attr, &type_cu);
14325 if (type_die == NULL)
14328 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14330 return die_containing_type (type_die, type_cu);
14333 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14336 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14338 struct rust_vtable_symbol *storage = NULL;
14340 if (cu->language == language_rust)
14342 struct type *containing_type = rust_containing_type (die, cu);
14344 if (containing_type != NULL)
14346 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14348 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14349 struct rust_vtable_symbol);
14350 initialize_objfile_symbol (storage);
14351 storage->concrete_type = containing_type;
14352 storage->subclass = SYMBOL_RUST_VTABLE;
14356 new_symbol (die, NULL, cu, storage);
14359 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14360 reading .debug_rnglists.
14361 Callback's type should be:
14362 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14363 Return true if the attributes are present and valid, otherwise,
14366 template <typename Callback>
14368 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14369 Callback &&callback)
14371 struct dwarf2_per_objfile *dwarf2_per_objfile
14372 = cu->per_cu->dwarf2_per_objfile;
14373 struct objfile *objfile = dwarf2_per_objfile->objfile;
14374 bfd *obfd = objfile->obfd;
14375 /* Base address selection entry. */
14378 const gdb_byte *buffer;
14379 CORE_ADDR baseaddr;
14380 bool overflow = false;
14382 found_base = cu->base_known;
14383 base = cu->base_address;
14385 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14386 if (offset >= dwarf2_per_objfile->rnglists.size)
14388 complaint (&symfile_complaints,
14389 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14393 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14395 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14399 /* Initialize it due to a false compiler warning. */
14400 CORE_ADDR range_beginning = 0, range_end = 0;
14401 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14402 + dwarf2_per_objfile->rnglists.size);
14403 unsigned int bytes_read;
14405 if (buffer == buf_end)
14410 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14413 case DW_RLE_end_of_list:
14415 case DW_RLE_base_address:
14416 if (buffer + cu->header.addr_size > buf_end)
14421 base = read_address (obfd, buffer, cu, &bytes_read);
14423 buffer += bytes_read;
14425 case DW_RLE_start_length:
14426 if (buffer + cu->header.addr_size > buf_end)
14431 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14432 buffer += bytes_read;
14433 range_end = (range_beginning
14434 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14435 buffer += bytes_read;
14436 if (buffer > buf_end)
14442 case DW_RLE_offset_pair:
14443 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14444 buffer += bytes_read;
14445 if (buffer > buf_end)
14450 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14451 buffer += bytes_read;
14452 if (buffer > buf_end)
14458 case DW_RLE_start_end:
14459 if (buffer + 2 * cu->header.addr_size > buf_end)
14464 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14465 buffer += bytes_read;
14466 range_end = read_address (obfd, buffer, cu, &bytes_read);
14467 buffer += bytes_read;
14470 complaint (&symfile_complaints,
14471 _("Invalid .debug_rnglists data (no base address)"));
14474 if (rlet == DW_RLE_end_of_list || overflow)
14476 if (rlet == DW_RLE_base_address)
14481 /* We have no valid base address for the ranges
14483 complaint (&symfile_complaints,
14484 _("Invalid .debug_rnglists data (no base address)"));
14488 if (range_beginning > range_end)
14490 /* Inverted range entries are invalid. */
14491 complaint (&symfile_complaints,
14492 _("Invalid .debug_rnglists data (inverted range)"));
14496 /* Empty range entries have no effect. */
14497 if (range_beginning == range_end)
14500 range_beginning += base;
14503 /* A not-uncommon case of bad debug info.
14504 Don't pollute the addrmap with bad data. */
14505 if (range_beginning + baseaddr == 0
14506 && !dwarf2_per_objfile->has_section_at_zero)
14508 complaint (&symfile_complaints,
14509 _(".debug_rnglists entry has start address of zero"
14510 " [in module %s]"), objfile_name (objfile));
14514 callback (range_beginning, range_end);
14519 complaint (&symfile_complaints,
14520 _("Offset %d is not terminated "
14521 "for DW_AT_ranges attribute"),
14529 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14530 Callback's type should be:
14531 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14532 Return 1 if the attributes are present and valid, otherwise, return 0. */
14534 template <typename Callback>
14536 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14537 Callback &&callback)
14539 struct dwarf2_per_objfile *dwarf2_per_objfile
14540 = cu->per_cu->dwarf2_per_objfile;
14541 struct objfile *objfile = dwarf2_per_objfile->objfile;
14542 struct comp_unit_head *cu_header = &cu->header;
14543 bfd *obfd = objfile->obfd;
14544 unsigned int addr_size = cu_header->addr_size;
14545 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14546 /* Base address selection entry. */
14549 unsigned int dummy;
14550 const gdb_byte *buffer;
14551 CORE_ADDR baseaddr;
14553 if (cu_header->version >= 5)
14554 return dwarf2_rnglists_process (offset, cu, callback);
14556 found_base = cu->base_known;
14557 base = cu->base_address;
14559 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14560 if (offset >= dwarf2_per_objfile->ranges.size)
14562 complaint (&symfile_complaints,
14563 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14567 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14569 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14573 CORE_ADDR range_beginning, range_end;
14575 range_beginning = read_address (obfd, buffer, cu, &dummy);
14576 buffer += addr_size;
14577 range_end = read_address (obfd, buffer, cu, &dummy);
14578 buffer += addr_size;
14579 offset += 2 * addr_size;
14581 /* An end of list marker is a pair of zero addresses. */
14582 if (range_beginning == 0 && range_end == 0)
14583 /* Found the end of list entry. */
14586 /* Each base address selection entry is a pair of 2 values.
14587 The first is the largest possible address, the second is
14588 the base address. Check for a base address here. */
14589 if ((range_beginning & mask) == mask)
14591 /* If we found the largest possible address, then we already
14592 have the base address in range_end. */
14600 /* We have no valid base address for the ranges
14602 complaint (&symfile_complaints,
14603 _("Invalid .debug_ranges data (no base address)"));
14607 if (range_beginning > range_end)
14609 /* Inverted range entries are invalid. */
14610 complaint (&symfile_complaints,
14611 _("Invalid .debug_ranges data (inverted range)"));
14615 /* Empty range entries have no effect. */
14616 if (range_beginning == range_end)
14619 range_beginning += base;
14622 /* A not-uncommon case of bad debug info.
14623 Don't pollute the addrmap with bad data. */
14624 if (range_beginning + baseaddr == 0
14625 && !dwarf2_per_objfile->has_section_at_zero)
14627 complaint (&symfile_complaints,
14628 _(".debug_ranges entry has start address of zero"
14629 " [in module %s]"), objfile_name (objfile));
14633 callback (range_beginning, range_end);
14639 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14640 Return 1 if the attributes are present and valid, otherwise, return 0.
14641 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14644 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14645 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14646 struct partial_symtab *ranges_pst)
14648 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14649 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14650 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14651 SECT_OFF_TEXT (objfile));
14654 CORE_ADDR high = 0;
14657 retval = dwarf2_ranges_process (offset, cu,
14658 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14660 if (ranges_pst != NULL)
14665 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14666 range_beginning + baseaddr);
14667 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14668 range_end + baseaddr);
14669 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14673 /* FIXME: This is recording everything as a low-high
14674 segment of consecutive addresses. We should have a
14675 data structure for discontiguous block ranges
14679 low = range_beginning;
14685 if (range_beginning < low)
14686 low = range_beginning;
14687 if (range_end > high)
14695 /* If the first entry is an end-of-list marker, the range
14696 describes an empty scope, i.e. no instructions. */
14702 *high_return = high;
14706 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14707 definition for the return value. *LOWPC and *HIGHPC are set iff
14708 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14710 static enum pc_bounds_kind
14711 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14712 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14713 struct partial_symtab *pst)
14715 struct dwarf2_per_objfile *dwarf2_per_objfile
14716 = cu->per_cu->dwarf2_per_objfile;
14717 struct attribute *attr;
14718 struct attribute *attr_high;
14720 CORE_ADDR high = 0;
14721 enum pc_bounds_kind ret;
14723 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14726 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14729 low = attr_value_as_address (attr);
14730 high = attr_value_as_address (attr_high);
14731 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14735 /* Found high w/o low attribute. */
14736 return PC_BOUNDS_INVALID;
14738 /* Found consecutive range of addresses. */
14739 ret = PC_BOUNDS_HIGH_LOW;
14743 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14746 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14747 We take advantage of the fact that DW_AT_ranges does not appear
14748 in DW_TAG_compile_unit of DWO files. */
14749 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14750 unsigned int ranges_offset = (DW_UNSND (attr)
14751 + (need_ranges_base
14755 /* Value of the DW_AT_ranges attribute is the offset in the
14756 .debug_ranges section. */
14757 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14758 return PC_BOUNDS_INVALID;
14759 /* Found discontinuous range of addresses. */
14760 ret = PC_BOUNDS_RANGES;
14763 return PC_BOUNDS_NOT_PRESENT;
14766 /* read_partial_die has also the strict LOW < HIGH requirement. */
14768 return PC_BOUNDS_INVALID;
14770 /* When using the GNU linker, .gnu.linkonce. sections are used to
14771 eliminate duplicate copies of functions and vtables and such.
14772 The linker will arbitrarily choose one and discard the others.
14773 The AT_*_pc values for such functions refer to local labels in
14774 these sections. If the section from that file was discarded, the
14775 labels are not in the output, so the relocs get a value of 0.
14776 If this is a discarded function, mark the pc bounds as invalid,
14777 so that GDB will ignore it. */
14778 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14779 return PC_BOUNDS_INVALID;
14787 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14788 its low and high PC addresses. Do nothing if these addresses could not
14789 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14790 and HIGHPC to the high address if greater than HIGHPC. */
14793 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14794 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14795 struct dwarf2_cu *cu)
14797 CORE_ADDR low, high;
14798 struct die_info *child = die->child;
14800 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14802 *lowpc = std::min (*lowpc, low);
14803 *highpc = std::max (*highpc, high);
14806 /* If the language does not allow nested subprograms (either inside
14807 subprograms or lexical blocks), we're done. */
14808 if (cu->language != language_ada)
14811 /* Check all the children of the given DIE. If it contains nested
14812 subprograms, then check their pc bounds. Likewise, we need to
14813 check lexical blocks as well, as they may also contain subprogram
14815 while (child && child->tag)
14817 if (child->tag == DW_TAG_subprogram
14818 || child->tag == DW_TAG_lexical_block)
14819 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14820 child = sibling_die (child);
14824 /* Get the low and high pc's represented by the scope DIE, and store
14825 them in *LOWPC and *HIGHPC. If the correct values can't be
14826 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14829 get_scope_pc_bounds (struct die_info *die,
14830 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14831 struct dwarf2_cu *cu)
14833 CORE_ADDR best_low = (CORE_ADDR) -1;
14834 CORE_ADDR best_high = (CORE_ADDR) 0;
14835 CORE_ADDR current_low, current_high;
14837 if (dwarf2_get_pc_bounds (die, ¤t_low, ¤t_high, cu, NULL)
14838 >= PC_BOUNDS_RANGES)
14840 best_low = current_low;
14841 best_high = current_high;
14845 struct die_info *child = die->child;
14847 while (child && child->tag)
14849 switch (child->tag) {
14850 case DW_TAG_subprogram:
14851 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14853 case DW_TAG_namespace:
14854 case DW_TAG_module:
14855 /* FIXME: carlton/2004-01-16: Should we do this for
14856 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14857 that current GCC's always emit the DIEs corresponding
14858 to definitions of methods of classes as children of a
14859 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14860 the DIEs giving the declarations, which could be
14861 anywhere). But I don't see any reason why the
14862 standards says that they have to be there. */
14863 get_scope_pc_bounds (child, ¤t_low, ¤t_high, cu);
14865 if (current_low != ((CORE_ADDR) -1))
14867 best_low = std::min (best_low, current_low);
14868 best_high = std::max (best_high, current_high);
14876 child = sibling_die (child);
14881 *highpc = best_high;
14884 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14888 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14889 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14891 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14892 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14893 struct attribute *attr;
14894 struct attribute *attr_high;
14896 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14899 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14902 CORE_ADDR low = attr_value_as_address (attr);
14903 CORE_ADDR high = attr_value_as_address (attr_high);
14905 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14908 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14909 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14910 record_block_range (block, low, high - 1);
14914 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14917 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14918 We take advantage of the fact that DW_AT_ranges does not appear
14919 in DW_TAG_compile_unit of DWO files. */
14920 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14922 /* The value of the DW_AT_ranges attribute is the offset of the
14923 address range list in the .debug_ranges section. */
14924 unsigned long offset = (DW_UNSND (attr)
14925 + (need_ranges_base ? cu->ranges_base : 0));
14926 const gdb_byte *buffer;
14928 /* For some target architectures, but not others, the
14929 read_address function sign-extends the addresses it returns.
14930 To recognize base address selection entries, we need a
14932 unsigned int addr_size = cu->header.addr_size;
14933 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14935 /* The base address, to which the next pair is relative. Note
14936 that this 'base' is a DWARF concept: most entries in a range
14937 list are relative, to reduce the number of relocs against the
14938 debugging information. This is separate from this function's
14939 'baseaddr' argument, which GDB uses to relocate debugging
14940 information from a shared library based on the address at
14941 which the library was loaded. */
14942 CORE_ADDR base = cu->base_address;
14943 int base_known = cu->base_known;
14945 dwarf2_ranges_process (offset, cu,
14946 [&] (CORE_ADDR start, CORE_ADDR end)
14950 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14951 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14952 record_block_range (block, start, end - 1);
14957 /* Check whether the producer field indicates either of GCC < 4.6, or the
14958 Intel C/C++ compiler, and cache the result in CU. */
14961 check_producer (struct dwarf2_cu *cu)
14965 if (cu->producer == NULL)
14967 /* For unknown compilers expect their behavior is DWARF version
14970 GCC started to support .debug_types sections by -gdwarf-4 since
14971 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14972 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14973 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14974 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14976 else if (producer_is_gcc (cu->producer, &major, &minor))
14978 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14979 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14981 else if (producer_is_icc (cu->producer, &major, &minor))
14982 cu->producer_is_icc_lt_14 = major < 14;
14985 /* For other non-GCC compilers, expect their behavior is DWARF version
14989 cu->checked_producer = 1;
14992 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14993 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14994 during 4.6.0 experimental. */
14997 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14999 if (!cu->checked_producer)
15000 check_producer (cu);
15002 return cu->producer_is_gxx_lt_4_6;
15005 /* Return the default accessibility type if it is not overriden by
15006 DW_AT_accessibility. */
15008 static enum dwarf_access_attribute
15009 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15011 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15013 /* The default DWARF 2 accessibility for members is public, the default
15014 accessibility for inheritance is private. */
15016 if (die->tag != DW_TAG_inheritance)
15017 return DW_ACCESS_public;
15019 return DW_ACCESS_private;
15023 /* DWARF 3+ defines the default accessibility a different way. The same
15024 rules apply now for DW_TAG_inheritance as for the members and it only
15025 depends on the container kind. */
15027 if (die->parent->tag == DW_TAG_class_type)
15028 return DW_ACCESS_private;
15030 return DW_ACCESS_public;
15034 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15035 offset. If the attribute was not found return 0, otherwise return
15036 1. If it was found but could not properly be handled, set *OFFSET
15040 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15043 struct attribute *attr;
15045 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15050 /* Note that we do not check for a section offset first here.
15051 This is because DW_AT_data_member_location is new in DWARF 4,
15052 so if we see it, we can assume that a constant form is really
15053 a constant and not a section offset. */
15054 if (attr_form_is_constant (attr))
15055 *offset = dwarf2_get_attr_constant_value (attr, 0);
15056 else if (attr_form_is_section_offset (attr))
15057 dwarf2_complex_location_expr_complaint ();
15058 else if (attr_form_is_block (attr))
15059 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15061 dwarf2_complex_location_expr_complaint ();
15069 /* Add an aggregate field to the field list. */
15072 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15073 struct dwarf2_cu *cu)
15075 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15076 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15077 struct nextfield *new_field;
15078 struct attribute *attr;
15080 const char *fieldname = "";
15082 /* Allocate a new field list entry and link it in. */
15083 new_field = XNEW (struct nextfield);
15084 make_cleanup (xfree, new_field);
15085 memset (new_field, 0, sizeof (struct nextfield));
15087 if (die->tag == DW_TAG_inheritance)
15089 new_field->next = fip->baseclasses;
15090 fip->baseclasses = new_field;
15094 new_field->next = fip->fields;
15095 fip->fields = new_field;
15099 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15101 new_field->accessibility = DW_UNSND (attr);
15103 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15104 if (new_field->accessibility != DW_ACCESS_public)
15105 fip->non_public_fields = 1;
15107 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15109 new_field->virtuality = DW_UNSND (attr);
15111 new_field->virtuality = DW_VIRTUALITY_none;
15113 fp = &new_field->field;
15115 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15119 /* Data member other than a C++ static data member. */
15121 /* Get type of field. */
15122 fp->type = die_type (die, cu);
15124 SET_FIELD_BITPOS (*fp, 0);
15126 /* Get bit size of field (zero if none). */
15127 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15130 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15134 FIELD_BITSIZE (*fp) = 0;
15137 /* Get bit offset of field. */
15138 if (handle_data_member_location (die, cu, &offset))
15139 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15140 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15143 if (gdbarch_bits_big_endian (gdbarch))
15145 /* For big endian bits, the DW_AT_bit_offset gives the
15146 additional bit offset from the MSB of the containing
15147 anonymous object to the MSB of the field. We don't
15148 have to do anything special since we don't need to
15149 know the size of the anonymous object. */
15150 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15154 /* For little endian bits, compute the bit offset to the
15155 MSB of the anonymous object, subtract off the number of
15156 bits from the MSB of the field to the MSB of the
15157 object, and then subtract off the number of bits of
15158 the field itself. The result is the bit offset of
15159 the LSB of the field. */
15160 int anonymous_size;
15161 int bit_offset = DW_UNSND (attr);
15163 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15166 /* The size of the anonymous object containing
15167 the bit field is explicit, so use the
15168 indicated size (in bytes). */
15169 anonymous_size = DW_UNSND (attr);
15173 /* The size of the anonymous object containing
15174 the bit field must be inferred from the type
15175 attribute of the data member containing the
15177 anonymous_size = TYPE_LENGTH (fp->type);
15179 SET_FIELD_BITPOS (*fp,
15180 (FIELD_BITPOS (*fp)
15181 + anonymous_size * bits_per_byte
15182 - bit_offset - FIELD_BITSIZE (*fp)));
15185 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15187 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15188 + dwarf2_get_attr_constant_value (attr, 0)));
15190 /* Get name of field. */
15191 fieldname = dwarf2_name (die, cu);
15192 if (fieldname == NULL)
15195 /* The name is already allocated along with this objfile, so we don't
15196 need to duplicate it for the type. */
15197 fp->name = fieldname;
15199 /* Change accessibility for artificial fields (e.g. virtual table
15200 pointer or virtual base class pointer) to private. */
15201 if (dwarf2_attr (die, DW_AT_artificial, cu))
15203 FIELD_ARTIFICIAL (*fp) = 1;
15204 new_field->accessibility = DW_ACCESS_private;
15205 fip->non_public_fields = 1;
15208 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15210 /* C++ static member. */
15212 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15213 is a declaration, but all versions of G++ as of this writing
15214 (so through at least 3.2.1) incorrectly generate
15215 DW_TAG_variable tags. */
15217 const char *physname;
15219 /* Get name of field. */
15220 fieldname = dwarf2_name (die, cu);
15221 if (fieldname == NULL)
15224 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15226 /* Only create a symbol if this is an external value.
15227 new_symbol checks this and puts the value in the global symbol
15228 table, which we want. If it is not external, new_symbol
15229 will try to put the value in cu->list_in_scope which is wrong. */
15230 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15232 /* A static const member, not much different than an enum as far as
15233 we're concerned, except that we can support more types. */
15234 new_symbol (die, NULL, cu);
15237 /* Get physical name. */
15238 physname = dwarf2_physname (fieldname, die, cu);
15240 /* The name is already allocated along with this objfile, so we don't
15241 need to duplicate it for the type. */
15242 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15243 FIELD_TYPE (*fp) = die_type (die, cu);
15244 FIELD_NAME (*fp) = fieldname;
15246 else if (die->tag == DW_TAG_inheritance)
15250 /* C++ base class field. */
15251 if (handle_data_member_location (die, cu, &offset))
15252 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15253 FIELD_BITSIZE (*fp) = 0;
15254 FIELD_TYPE (*fp) = die_type (die, cu);
15255 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15256 fip->nbaseclasses++;
15260 /* Can the type given by DIE define another type? */
15263 type_can_define_types (const struct die_info *die)
15267 case DW_TAG_typedef:
15268 case DW_TAG_class_type:
15269 case DW_TAG_structure_type:
15270 case DW_TAG_union_type:
15271 case DW_TAG_enumeration_type:
15279 /* Add a type definition defined in the scope of the FIP's class. */
15282 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15283 struct dwarf2_cu *cu)
15285 struct decl_field_list *new_field;
15286 struct decl_field *fp;
15288 /* Allocate a new field list entry and link it in. */
15289 new_field = XCNEW (struct decl_field_list);
15290 make_cleanup (xfree, new_field);
15292 gdb_assert (type_can_define_types (die));
15294 fp = &new_field->field;
15296 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15297 fp->name = dwarf2_name (die, cu);
15298 fp->type = read_type_die (die, cu);
15300 /* Save accessibility. */
15301 enum dwarf_access_attribute accessibility;
15302 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15304 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15306 accessibility = dwarf2_default_access_attribute (die, cu);
15307 switch (accessibility)
15309 case DW_ACCESS_public:
15310 /* The assumed value if neither private nor protected. */
15312 case DW_ACCESS_private:
15313 fp->is_private = 1;
15315 case DW_ACCESS_protected:
15316 fp->is_protected = 1;
15319 complaint (&symfile_complaints,
15320 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15323 if (die->tag == DW_TAG_typedef)
15325 new_field->next = fip->typedef_field_list;
15326 fip->typedef_field_list = new_field;
15327 fip->typedef_field_list_count++;
15331 new_field->next = fip->nested_types_list;
15332 fip->nested_types_list = new_field;
15333 fip->nested_types_list_count++;
15337 /* Create the vector of fields, and attach it to the type. */
15340 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15341 struct dwarf2_cu *cu)
15343 int nfields = fip->nfields;
15345 /* Record the field count, allocate space for the array of fields,
15346 and create blank accessibility bitfields if necessary. */
15347 TYPE_NFIELDS (type) = nfields;
15348 TYPE_FIELDS (type) = (struct field *)
15349 TYPE_ALLOC (type, sizeof (struct field) * nfields);
15350 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
15352 if (fip->non_public_fields && cu->language != language_ada)
15354 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15356 TYPE_FIELD_PRIVATE_BITS (type) =
15357 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15358 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15360 TYPE_FIELD_PROTECTED_BITS (type) =
15361 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15362 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15364 TYPE_FIELD_IGNORE_BITS (type) =
15365 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15366 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15369 /* If the type has baseclasses, allocate and clear a bit vector for
15370 TYPE_FIELD_VIRTUAL_BITS. */
15371 if (fip->nbaseclasses && cu->language != language_ada)
15373 int num_bytes = B_BYTES (fip->nbaseclasses);
15374 unsigned char *pointer;
15376 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15377 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15378 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15379 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
15380 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
15383 /* Copy the saved-up fields into the field vector. Start from the head of
15384 the list, adding to the tail of the field array, so that they end up in
15385 the same order in the array in which they were added to the list. */
15386 while (nfields-- > 0)
15388 struct nextfield *fieldp;
15392 fieldp = fip->fields;
15393 fip->fields = fieldp->next;
15397 fieldp = fip->baseclasses;
15398 fip->baseclasses = fieldp->next;
15401 TYPE_FIELD (type, nfields) = fieldp->field;
15402 switch (fieldp->accessibility)
15404 case DW_ACCESS_private:
15405 if (cu->language != language_ada)
15406 SET_TYPE_FIELD_PRIVATE (type, nfields);
15409 case DW_ACCESS_protected:
15410 if (cu->language != language_ada)
15411 SET_TYPE_FIELD_PROTECTED (type, nfields);
15414 case DW_ACCESS_public:
15418 /* Unknown accessibility. Complain and treat it as public. */
15420 complaint (&symfile_complaints, _("unsupported accessibility %d"),
15421 fieldp->accessibility);
15425 if (nfields < fip->nbaseclasses)
15427 switch (fieldp->virtuality)
15429 case DW_VIRTUALITY_virtual:
15430 case DW_VIRTUALITY_pure_virtual:
15431 if (cu->language == language_ada)
15432 error (_("unexpected virtuality in component of Ada type"));
15433 SET_TYPE_FIELD_VIRTUAL (type, nfields);
15440 /* Return true if this member function is a constructor, false
15444 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15446 const char *fieldname;
15447 const char *type_name;
15450 if (die->parent == NULL)
15453 if (die->parent->tag != DW_TAG_structure_type
15454 && die->parent->tag != DW_TAG_union_type
15455 && die->parent->tag != DW_TAG_class_type)
15458 fieldname = dwarf2_name (die, cu);
15459 type_name = dwarf2_name (die->parent, cu);
15460 if (fieldname == NULL || type_name == NULL)
15463 len = strlen (fieldname);
15464 return (strncmp (fieldname, type_name, len) == 0
15465 && (type_name[len] == '\0' || type_name[len] == '<'));
15468 /* Add a member function to the proper fieldlist. */
15471 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15472 struct type *type, struct dwarf2_cu *cu)
15474 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15475 struct attribute *attr;
15476 struct fnfieldlist *flp;
15478 struct fn_field *fnp;
15479 const char *fieldname;
15480 struct nextfnfield *new_fnfield;
15481 struct type *this_type;
15482 enum dwarf_access_attribute accessibility;
15484 if (cu->language == language_ada)
15485 error (_("unexpected member function in Ada type"));
15487 /* Get name of member function. */
15488 fieldname = dwarf2_name (die, cu);
15489 if (fieldname == NULL)
15492 /* Look up member function name in fieldlist. */
15493 for (i = 0; i < fip->nfnfields; i++)
15495 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15499 /* Create new list element if necessary. */
15500 if (i < fip->nfnfields)
15501 flp = &fip->fnfieldlists[i];
15504 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
15506 fip->fnfieldlists = (struct fnfieldlist *)
15507 xrealloc (fip->fnfieldlists,
15508 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
15509 * sizeof (struct fnfieldlist));
15510 if (fip->nfnfields == 0)
15511 make_cleanup (free_current_contents, &fip->fnfieldlists);
15513 flp = &fip->fnfieldlists[fip->nfnfields];
15514 flp->name = fieldname;
15517 i = fip->nfnfields++;
15520 /* Create a new member function field and chain it to the field list
15522 new_fnfield = XNEW (struct nextfnfield);
15523 make_cleanup (xfree, new_fnfield);
15524 memset (new_fnfield, 0, sizeof (struct nextfnfield));
15525 new_fnfield->next = flp->head;
15526 flp->head = new_fnfield;
15529 /* Fill in the member function field info. */
15530 fnp = &new_fnfield->fnfield;
15532 /* Delay processing of the physname until later. */
15533 if (cu->language == language_cplus)
15535 add_to_method_list (type, i, flp->length - 1, fieldname,
15540 const char *physname = dwarf2_physname (fieldname, die, cu);
15541 fnp->physname = physname ? physname : "";
15544 fnp->type = alloc_type (objfile);
15545 this_type = read_type_die (die, cu);
15546 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15548 int nparams = TYPE_NFIELDS (this_type);
15550 /* TYPE is the domain of this method, and THIS_TYPE is the type
15551 of the method itself (TYPE_CODE_METHOD). */
15552 smash_to_method_type (fnp->type, type,
15553 TYPE_TARGET_TYPE (this_type),
15554 TYPE_FIELDS (this_type),
15555 TYPE_NFIELDS (this_type),
15556 TYPE_VARARGS (this_type));
15558 /* Handle static member functions.
15559 Dwarf2 has no clean way to discern C++ static and non-static
15560 member functions. G++ helps GDB by marking the first
15561 parameter for non-static member functions (which is the this
15562 pointer) as artificial. We obtain this information from
15563 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15564 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15565 fnp->voffset = VOFFSET_STATIC;
15568 complaint (&symfile_complaints, _("member function type missing for '%s'"),
15569 dwarf2_full_name (fieldname, die, cu));
15571 /* Get fcontext from DW_AT_containing_type if present. */
15572 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15573 fnp->fcontext = die_containing_type (die, cu);
15575 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15576 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15578 /* Get accessibility. */
15579 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15581 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15583 accessibility = dwarf2_default_access_attribute (die, cu);
15584 switch (accessibility)
15586 case DW_ACCESS_private:
15587 fnp->is_private = 1;
15589 case DW_ACCESS_protected:
15590 fnp->is_protected = 1;
15594 /* Check for artificial methods. */
15595 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15596 if (attr && DW_UNSND (attr) != 0)
15597 fnp->is_artificial = 1;
15599 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15601 /* Get index in virtual function table if it is a virtual member
15602 function. For older versions of GCC, this is an offset in the
15603 appropriate virtual table, as specified by DW_AT_containing_type.
15604 For everyone else, it is an expression to be evaluated relative
15605 to the object address. */
15607 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15610 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15612 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15614 /* Old-style GCC. */
15615 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15617 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15618 || (DW_BLOCK (attr)->size > 1
15619 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15620 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15622 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15623 if ((fnp->voffset % cu->header.addr_size) != 0)
15624 dwarf2_complex_location_expr_complaint ();
15626 fnp->voffset /= cu->header.addr_size;
15630 dwarf2_complex_location_expr_complaint ();
15632 if (!fnp->fcontext)
15634 /* If there is no `this' field and no DW_AT_containing_type,
15635 we cannot actually find a base class context for the
15637 if (TYPE_NFIELDS (this_type) == 0
15638 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15640 complaint (&symfile_complaints,
15641 _("cannot determine context for virtual member "
15642 "function \"%s\" (offset %d)"),
15643 fieldname, to_underlying (die->sect_off));
15648 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15652 else if (attr_form_is_section_offset (attr))
15654 dwarf2_complex_location_expr_complaint ();
15658 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15664 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15665 if (attr && DW_UNSND (attr))
15667 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15668 complaint (&symfile_complaints,
15669 _("Member function \"%s\" (offset %d) is virtual "
15670 "but the vtable offset is not specified"),
15671 fieldname, to_underlying (die->sect_off));
15672 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15673 TYPE_CPLUS_DYNAMIC (type) = 1;
15678 /* Create the vector of member function fields, and attach it to the type. */
15681 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15682 struct dwarf2_cu *cu)
15684 struct fnfieldlist *flp;
15687 if (cu->language == language_ada)
15688 error (_("unexpected member functions in Ada type"));
15690 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15691 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15692 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
15694 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
15696 struct nextfnfield *nfp = flp->head;
15697 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15700 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
15701 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
15702 fn_flp->fn_fields = (struct fn_field *)
15703 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
15704 for (k = flp->length; (k--, nfp); nfp = nfp->next)
15705 fn_flp->fn_fields[k] = nfp->fnfield;
15708 TYPE_NFN_FIELDS (type) = fip->nfnfields;
15711 /* Returns non-zero if NAME is the name of a vtable member in CU's
15712 language, zero otherwise. */
15714 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15716 static const char vptr[] = "_vptr";
15718 /* Look for the C++ form of the vtable. */
15719 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15725 /* GCC outputs unnamed structures that are really pointers to member
15726 functions, with the ABI-specified layout. If TYPE describes
15727 such a structure, smash it into a member function type.
15729 GCC shouldn't do this; it should just output pointer to member DIEs.
15730 This is GCC PR debug/28767. */
15733 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15735 struct type *pfn_type, *self_type, *new_type;
15737 /* Check for a structure with no name and two children. */
15738 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15741 /* Check for __pfn and __delta members. */
15742 if (TYPE_FIELD_NAME (type, 0) == NULL
15743 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15744 || TYPE_FIELD_NAME (type, 1) == NULL
15745 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15748 /* Find the type of the method. */
15749 pfn_type = TYPE_FIELD_TYPE (type, 0);
15750 if (pfn_type == NULL
15751 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15752 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15755 /* Look for the "this" argument. */
15756 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15757 if (TYPE_NFIELDS (pfn_type) == 0
15758 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15759 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15762 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15763 new_type = alloc_type (objfile);
15764 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15765 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15766 TYPE_VARARGS (pfn_type));
15767 smash_to_methodptr_type (type, new_type);
15771 /* Called when we find the DIE that starts a structure or union scope
15772 (definition) to create a type for the structure or union. Fill in
15773 the type's name and general properties; the members will not be
15774 processed until process_structure_scope. A symbol table entry for
15775 the type will also not be done until process_structure_scope (assuming
15776 the type has a name).
15778 NOTE: we need to call these functions regardless of whether or not the
15779 DIE has a DW_AT_name attribute, since it might be an anonymous
15780 structure or union. This gets the type entered into our set of
15781 user defined types. */
15783 static struct type *
15784 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15786 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15788 struct attribute *attr;
15791 /* If the definition of this type lives in .debug_types, read that type.
15792 Don't follow DW_AT_specification though, that will take us back up
15793 the chain and we want to go down. */
15794 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15797 type = get_DW_AT_signature_type (die, attr, cu);
15799 /* The type's CU may not be the same as CU.
15800 Ensure TYPE is recorded with CU in die_type_hash. */
15801 return set_die_type (die, type, cu);
15804 type = alloc_type (objfile);
15805 INIT_CPLUS_SPECIFIC (type);
15807 name = dwarf2_name (die, cu);
15810 if (cu->language == language_cplus
15811 || cu->language == language_d
15812 || cu->language == language_rust)
15814 const char *full_name = dwarf2_full_name (name, die, cu);
15816 /* dwarf2_full_name might have already finished building the DIE's
15817 type. If so, there is no need to continue. */
15818 if (get_die_type (die, cu) != NULL)
15819 return get_die_type (die, cu);
15821 TYPE_TAG_NAME (type) = full_name;
15822 if (die->tag == DW_TAG_structure_type
15823 || die->tag == DW_TAG_class_type)
15824 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15828 /* The name is already allocated along with this objfile, so
15829 we don't need to duplicate it for the type. */
15830 TYPE_TAG_NAME (type) = name;
15831 if (die->tag == DW_TAG_class_type)
15832 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15836 if (die->tag == DW_TAG_structure_type)
15838 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15840 else if (die->tag == DW_TAG_union_type)
15842 TYPE_CODE (type) = TYPE_CODE_UNION;
15846 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15849 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15850 TYPE_DECLARED_CLASS (type) = 1;
15852 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15855 if (attr_form_is_constant (attr))
15856 TYPE_LENGTH (type) = DW_UNSND (attr);
15859 /* For the moment, dynamic type sizes are not supported
15860 by GDB's struct type. The actual size is determined
15861 on-demand when resolving the type of a given object,
15862 so set the type's length to zero for now. Otherwise,
15863 we record an expression as the length, and that expression
15864 could lead to a very large value, which could eventually
15865 lead to us trying to allocate that much memory when creating
15866 a value of that type. */
15867 TYPE_LENGTH (type) = 0;
15872 TYPE_LENGTH (type) = 0;
15875 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15877 /* ICC<14 does not output the required DW_AT_declaration on
15878 incomplete types, but gives them a size of zero. */
15879 TYPE_STUB (type) = 1;
15882 TYPE_STUB_SUPPORTED (type) = 1;
15884 if (die_is_declaration (die, cu))
15885 TYPE_STUB (type) = 1;
15886 else if (attr == NULL && die->child == NULL
15887 && producer_is_realview (cu->producer))
15888 /* RealView does not output the required DW_AT_declaration
15889 on incomplete types. */
15890 TYPE_STUB (type) = 1;
15892 /* We need to add the type field to the die immediately so we don't
15893 infinitely recurse when dealing with pointers to the structure
15894 type within the structure itself. */
15895 set_die_type (die, type, cu);
15897 /* set_die_type should be already done. */
15898 set_descriptive_type (type, die, cu);
15903 /* Finish creating a structure or union type, including filling in
15904 its members and creating a symbol for it. */
15907 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15909 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15910 struct die_info *child_die;
15913 type = get_die_type (die, cu);
15915 type = read_structure_type (die, cu);
15917 if (die->child != NULL && ! die_is_declaration (die, cu))
15919 struct field_info fi;
15920 std::vector<struct symbol *> template_args;
15921 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
15923 memset (&fi, 0, sizeof (struct field_info));
15925 child_die = die->child;
15927 while (child_die && child_die->tag)
15929 if (child_die->tag == DW_TAG_member
15930 || child_die->tag == DW_TAG_variable)
15932 /* NOTE: carlton/2002-11-05: A C++ static data member
15933 should be a DW_TAG_member that is a declaration, but
15934 all versions of G++ as of this writing (so through at
15935 least 3.2.1) incorrectly generate DW_TAG_variable
15936 tags for them instead. */
15937 dwarf2_add_field (&fi, child_die, cu);
15939 else if (child_die->tag == DW_TAG_subprogram)
15941 /* Rust doesn't have member functions in the C++ sense.
15942 However, it does emit ordinary functions as children
15943 of a struct DIE. */
15944 if (cu->language == language_rust)
15945 read_func_scope (child_die, cu);
15948 /* C++ member function. */
15949 dwarf2_add_member_fn (&fi, child_die, type, cu);
15952 else if (child_die->tag == DW_TAG_inheritance)
15954 /* C++ base class field. */
15955 dwarf2_add_field (&fi, child_die, cu);
15957 else if (type_can_define_types (child_die))
15958 dwarf2_add_type_defn (&fi, child_die, cu);
15959 else if (child_die->tag == DW_TAG_template_type_param
15960 || child_die->tag == DW_TAG_template_value_param)
15962 struct symbol *arg = new_symbol (child_die, NULL, cu);
15965 template_args.push_back (arg);
15968 child_die = sibling_die (child_die);
15971 /* Attach template arguments to type. */
15972 if (!template_args.empty ())
15974 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15975 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15976 TYPE_TEMPLATE_ARGUMENTS (type)
15977 = XOBNEWVEC (&objfile->objfile_obstack,
15979 TYPE_N_TEMPLATE_ARGUMENTS (type));
15980 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15981 template_args.data (),
15982 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15983 * sizeof (struct symbol *)));
15986 /* Attach fields and member functions to the type. */
15988 dwarf2_attach_fields_to_type (&fi, type, cu);
15991 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15993 /* Get the type which refers to the base class (possibly this
15994 class itself) which contains the vtable pointer for the current
15995 class from the DW_AT_containing_type attribute. This use of
15996 DW_AT_containing_type is a GNU extension. */
15998 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16000 struct type *t = die_containing_type (die, cu);
16002 set_type_vptr_basetype (type, t);
16007 /* Our own class provides vtbl ptr. */
16008 for (i = TYPE_NFIELDS (t) - 1;
16009 i >= TYPE_N_BASECLASSES (t);
16012 const char *fieldname = TYPE_FIELD_NAME (t, i);
16014 if (is_vtable_name (fieldname, cu))
16016 set_type_vptr_fieldno (type, i);
16021 /* Complain if virtual function table field not found. */
16022 if (i < TYPE_N_BASECLASSES (t))
16023 complaint (&symfile_complaints,
16024 _("virtual function table pointer "
16025 "not found when defining class '%s'"),
16026 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16031 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16034 else if (cu->producer
16035 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16037 /* The IBM XLC compiler does not provide direct indication
16038 of the containing type, but the vtable pointer is
16039 always named __vfp. */
16043 for (i = TYPE_NFIELDS (type) - 1;
16044 i >= TYPE_N_BASECLASSES (type);
16047 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16049 set_type_vptr_fieldno (type, i);
16050 set_type_vptr_basetype (type, type);
16057 /* Copy fi.typedef_field_list linked list elements content into the
16058 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16059 if (fi.typedef_field_list)
16061 int i = fi.typedef_field_list_count;
16063 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16064 TYPE_TYPEDEF_FIELD_ARRAY (type)
16065 = ((struct decl_field *)
16066 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
16067 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
16069 /* Reverse the list order to keep the debug info elements order. */
16072 struct decl_field *dest, *src;
16074 dest = &TYPE_TYPEDEF_FIELD (type, i);
16075 src = &fi.typedef_field_list->field;
16076 fi.typedef_field_list = fi.typedef_field_list->next;
16081 /* Copy fi.nested_types_list linked list elements content into the
16082 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16083 if (fi.nested_types_list != NULL && cu->language != language_ada)
16085 int i = fi.nested_types_list_count;
16087 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16088 TYPE_NESTED_TYPES_ARRAY (type)
16089 = ((struct decl_field *)
16090 TYPE_ALLOC (type, sizeof (struct decl_field) * i));
16091 TYPE_NESTED_TYPES_COUNT (type) = i;
16093 /* Reverse the list order to keep the debug info elements order. */
16096 struct decl_field *dest, *src;
16098 dest = &TYPE_NESTED_TYPES_FIELD (type, i);
16099 src = &fi.nested_types_list->field;
16100 fi.nested_types_list = fi.nested_types_list->next;
16105 do_cleanups (back_to);
16108 quirk_gcc_member_function_pointer (type, objfile);
16110 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16111 snapshots) has been known to create a die giving a declaration
16112 for a class that has, as a child, a die giving a definition for a
16113 nested class. So we have to process our children even if the
16114 current die is a declaration. Normally, of course, a declaration
16115 won't have any children at all. */
16117 child_die = die->child;
16119 while (child_die != NULL && child_die->tag)
16121 if (child_die->tag == DW_TAG_member
16122 || child_die->tag == DW_TAG_variable
16123 || child_die->tag == DW_TAG_inheritance
16124 || child_die->tag == DW_TAG_template_value_param
16125 || child_die->tag == DW_TAG_template_type_param)
16130 process_die (child_die, cu);
16132 child_die = sibling_die (child_die);
16135 /* Do not consider external references. According to the DWARF standard,
16136 these DIEs are identified by the fact that they have no byte_size
16137 attribute, and a declaration attribute. */
16138 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16139 || !die_is_declaration (die, cu))
16140 new_symbol (die, type, cu);
16143 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16144 update TYPE using some information only available in DIE's children. */
16147 update_enumeration_type_from_children (struct die_info *die,
16149 struct dwarf2_cu *cu)
16151 struct die_info *child_die;
16152 int unsigned_enum = 1;
16156 auto_obstack obstack;
16158 for (child_die = die->child;
16159 child_die != NULL && child_die->tag;
16160 child_die = sibling_die (child_die))
16162 struct attribute *attr;
16164 const gdb_byte *bytes;
16165 struct dwarf2_locexpr_baton *baton;
16168 if (child_die->tag != DW_TAG_enumerator)
16171 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16175 name = dwarf2_name (child_die, cu);
16177 name = "<anonymous enumerator>";
16179 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16180 &value, &bytes, &baton);
16186 else if ((mask & value) != 0)
16191 /* If we already know that the enum type is neither unsigned, nor
16192 a flag type, no need to look at the rest of the enumerates. */
16193 if (!unsigned_enum && !flag_enum)
16198 TYPE_UNSIGNED (type) = 1;
16200 TYPE_FLAG_ENUM (type) = 1;
16203 /* Given a DW_AT_enumeration_type die, set its type. We do not
16204 complete the type's fields yet, or create any symbols. */
16206 static struct type *
16207 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16209 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16211 struct attribute *attr;
16214 /* If the definition of this type lives in .debug_types, read that type.
16215 Don't follow DW_AT_specification though, that will take us back up
16216 the chain and we want to go down. */
16217 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16220 type = get_DW_AT_signature_type (die, attr, cu);
16222 /* The type's CU may not be the same as CU.
16223 Ensure TYPE is recorded with CU in die_type_hash. */
16224 return set_die_type (die, type, cu);
16227 type = alloc_type (objfile);
16229 TYPE_CODE (type) = TYPE_CODE_ENUM;
16230 name = dwarf2_full_name (NULL, die, cu);
16232 TYPE_TAG_NAME (type) = name;
16234 attr = dwarf2_attr (die, DW_AT_type, cu);
16237 struct type *underlying_type = die_type (die, cu);
16239 TYPE_TARGET_TYPE (type) = underlying_type;
16242 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16245 TYPE_LENGTH (type) = DW_UNSND (attr);
16249 TYPE_LENGTH (type) = 0;
16252 /* The enumeration DIE can be incomplete. In Ada, any type can be
16253 declared as private in the package spec, and then defined only
16254 inside the package body. Such types are known as Taft Amendment
16255 Types. When another package uses such a type, an incomplete DIE
16256 may be generated by the compiler. */
16257 if (die_is_declaration (die, cu))
16258 TYPE_STUB (type) = 1;
16260 /* Finish the creation of this type by using the enum's children.
16261 We must call this even when the underlying type has been provided
16262 so that we can determine if we're looking at a "flag" enum. */
16263 update_enumeration_type_from_children (die, type, cu);
16265 /* If this type has an underlying type that is not a stub, then we
16266 may use its attributes. We always use the "unsigned" attribute
16267 in this situation, because ordinarily we guess whether the type
16268 is unsigned -- but the guess can be wrong and the underlying type
16269 can tell us the reality. However, we defer to a local size
16270 attribute if one exists, because this lets the compiler override
16271 the underlying type if needed. */
16272 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16274 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16275 if (TYPE_LENGTH (type) == 0)
16276 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16279 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16281 return set_die_type (die, type, cu);
16284 /* Given a pointer to a die which begins an enumeration, process all
16285 the dies that define the members of the enumeration, and create the
16286 symbol for the enumeration type.
16288 NOTE: We reverse the order of the element list. */
16291 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16293 struct type *this_type;
16295 this_type = get_die_type (die, cu);
16296 if (this_type == NULL)
16297 this_type = read_enumeration_type (die, cu);
16299 if (die->child != NULL)
16301 struct die_info *child_die;
16302 struct symbol *sym;
16303 struct field *fields = NULL;
16304 int num_fields = 0;
16307 child_die = die->child;
16308 while (child_die && child_die->tag)
16310 if (child_die->tag != DW_TAG_enumerator)
16312 process_die (child_die, cu);
16316 name = dwarf2_name (child_die, cu);
16319 sym = new_symbol (child_die, this_type, cu);
16321 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16323 fields = (struct field *)
16325 (num_fields + DW_FIELD_ALLOC_CHUNK)
16326 * sizeof (struct field));
16329 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16330 FIELD_TYPE (fields[num_fields]) = NULL;
16331 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16332 FIELD_BITSIZE (fields[num_fields]) = 0;
16338 child_die = sibling_die (child_die);
16343 TYPE_NFIELDS (this_type) = num_fields;
16344 TYPE_FIELDS (this_type) = (struct field *)
16345 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16346 memcpy (TYPE_FIELDS (this_type), fields,
16347 sizeof (struct field) * num_fields);
16352 /* If we are reading an enum from a .debug_types unit, and the enum
16353 is a declaration, and the enum is not the signatured type in the
16354 unit, then we do not want to add a symbol for it. Adding a
16355 symbol would in some cases obscure the true definition of the
16356 enum, giving users an incomplete type when the definition is
16357 actually available. Note that we do not want to do this for all
16358 enums which are just declarations, because C++0x allows forward
16359 enum declarations. */
16360 if (cu->per_cu->is_debug_types
16361 && die_is_declaration (die, cu))
16363 struct signatured_type *sig_type;
16365 sig_type = (struct signatured_type *) cu->per_cu;
16366 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16367 if (sig_type->type_offset_in_section != die->sect_off)
16371 new_symbol (die, this_type, cu);
16374 /* Extract all information from a DW_TAG_array_type DIE and put it in
16375 the DIE's type field. For now, this only handles one dimensional
16378 static struct type *
16379 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16381 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16382 struct die_info *child_die;
16384 struct type *element_type, *range_type, *index_type;
16385 struct attribute *attr;
16387 struct dynamic_prop *byte_stride_prop = NULL;
16388 unsigned int bit_stride = 0;
16390 element_type = die_type (die, cu);
16392 /* The die_type call above may have already set the type for this DIE. */
16393 type = get_die_type (die, cu);
16397 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16403 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16404 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16407 complaint (&symfile_complaints,
16408 _("unable to read array DW_AT_byte_stride "
16409 " - DIE at 0x%x [in module %s]"),
16410 to_underlying (die->sect_off),
16411 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16412 /* Ignore this attribute. We will likely not be able to print
16413 arrays of this type correctly, but there is little we can do
16414 to help if we cannot read the attribute's value. */
16415 byte_stride_prop = NULL;
16419 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16421 bit_stride = DW_UNSND (attr);
16423 /* Irix 6.2 native cc creates array types without children for
16424 arrays with unspecified length. */
16425 if (die->child == NULL)
16427 index_type = objfile_type (objfile)->builtin_int;
16428 range_type = create_static_range_type (NULL, index_type, 0, -1);
16429 type = create_array_type_with_stride (NULL, element_type, range_type,
16430 byte_stride_prop, bit_stride);
16431 return set_die_type (die, type, cu);
16434 std::vector<struct type *> range_types;
16435 child_die = die->child;
16436 while (child_die && child_die->tag)
16438 if (child_die->tag == DW_TAG_subrange_type)
16440 struct type *child_type = read_type_die (child_die, cu);
16442 if (child_type != NULL)
16444 /* The range type was succesfully read. Save it for the
16445 array type creation. */
16446 range_types.push_back (child_type);
16449 child_die = sibling_die (child_die);
16452 /* Dwarf2 dimensions are output from left to right, create the
16453 necessary array types in backwards order. */
16455 type = element_type;
16457 if (read_array_order (die, cu) == DW_ORD_col_major)
16461 while (i < range_types.size ())
16462 type = create_array_type_with_stride (NULL, type, range_types[i++],
16463 byte_stride_prop, bit_stride);
16467 size_t ndim = range_types.size ();
16469 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16470 byte_stride_prop, bit_stride);
16473 /* Understand Dwarf2 support for vector types (like they occur on
16474 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16475 array type. This is not part of the Dwarf2/3 standard yet, but a
16476 custom vendor extension. The main difference between a regular
16477 array and the vector variant is that vectors are passed by value
16479 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16481 make_vector_type (type);
16483 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16484 implementation may choose to implement triple vectors using this
16486 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16489 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16490 TYPE_LENGTH (type) = DW_UNSND (attr);
16492 complaint (&symfile_complaints,
16493 _("DW_AT_byte_size for array type smaller "
16494 "than the total size of elements"));
16497 name = dwarf2_name (die, cu);
16499 TYPE_NAME (type) = name;
16501 /* Install the type in the die. */
16502 set_die_type (die, type, cu);
16504 /* set_die_type should be already done. */
16505 set_descriptive_type (type, die, cu);
16510 static enum dwarf_array_dim_ordering
16511 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16513 struct attribute *attr;
16515 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16518 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16520 /* GNU F77 is a special case, as at 08/2004 array type info is the
16521 opposite order to the dwarf2 specification, but data is still
16522 laid out as per normal fortran.
16524 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16525 version checking. */
16527 if (cu->language == language_fortran
16528 && cu->producer && strstr (cu->producer, "GNU F77"))
16530 return DW_ORD_row_major;
16533 switch (cu->language_defn->la_array_ordering)
16535 case array_column_major:
16536 return DW_ORD_col_major;
16537 case array_row_major:
16539 return DW_ORD_row_major;
16543 /* Extract all information from a DW_TAG_set_type DIE and put it in
16544 the DIE's type field. */
16546 static struct type *
16547 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16549 struct type *domain_type, *set_type;
16550 struct attribute *attr;
16552 domain_type = die_type (die, cu);
16554 /* The die_type call above may have already set the type for this DIE. */
16555 set_type = get_die_type (die, cu);
16559 set_type = create_set_type (NULL, domain_type);
16561 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16563 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16565 return set_die_type (die, set_type, cu);
16568 /* A helper for read_common_block that creates a locexpr baton.
16569 SYM is the symbol which we are marking as computed.
16570 COMMON_DIE is the DIE for the common block.
16571 COMMON_LOC is the location expression attribute for the common
16573 MEMBER_LOC is the location expression attribute for the particular
16574 member of the common block that we are processing.
16575 CU is the CU from which the above come. */
16578 mark_common_block_symbol_computed (struct symbol *sym,
16579 struct die_info *common_die,
16580 struct attribute *common_loc,
16581 struct attribute *member_loc,
16582 struct dwarf2_cu *cu)
16584 struct dwarf2_per_objfile *dwarf2_per_objfile
16585 = cu->per_cu->dwarf2_per_objfile;
16586 struct objfile *objfile = dwarf2_per_objfile->objfile;
16587 struct dwarf2_locexpr_baton *baton;
16589 unsigned int cu_off;
16590 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16591 LONGEST offset = 0;
16593 gdb_assert (common_loc && member_loc);
16594 gdb_assert (attr_form_is_block (common_loc));
16595 gdb_assert (attr_form_is_block (member_loc)
16596 || attr_form_is_constant (member_loc));
16598 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16599 baton->per_cu = cu->per_cu;
16600 gdb_assert (baton->per_cu);
16602 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16604 if (attr_form_is_constant (member_loc))
16606 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16607 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16610 baton->size += DW_BLOCK (member_loc)->size;
16612 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16615 *ptr++ = DW_OP_call4;
16616 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16617 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16620 if (attr_form_is_constant (member_loc))
16622 *ptr++ = DW_OP_addr;
16623 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16624 ptr += cu->header.addr_size;
16628 /* We have to copy the data here, because DW_OP_call4 will only
16629 use a DW_AT_location attribute. */
16630 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16631 ptr += DW_BLOCK (member_loc)->size;
16634 *ptr++ = DW_OP_plus;
16635 gdb_assert (ptr - baton->data == baton->size);
16637 SYMBOL_LOCATION_BATON (sym) = baton;
16638 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16641 /* Create appropriate locally-scoped variables for all the
16642 DW_TAG_common_block entries. Also create a struct common_block
16643 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16644 is used to sepate the common blocks name namespace from regular
16648 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16650 struct attribute *attr;
16652 attr = dwarf2_attr (die, DW_AT_location, cu);
16655 /* Support the .debug_loc offsets. */
16656 if (attr_form_is_block (attr))
16660 else if (attr_form_is_section_offset (attr))
16662 dwarf2_complex_location_expr_complaint ();
16667 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16668 "common block member");
16673 if (die->child != NULL)
16675 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16676 struct die_info *child_die;
16677 size_t n_entries = 0, size;
16678 struct common_block *common_block;
16679 struct symbol *sym;
16681 for (child_die = die->child;
16682 child_die && child_die->tag;
16683 child_die = sibling_die (child_die))
16686 size = (sizeof (struct common_block)
16687 + (n_entries - 1) * sizeof (struct symbol *));
16689 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16691 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16692 common_block->n_entries = 0;
16694 for (child_die = die->child;
16695 child_die && child_die->tag;
16696 child_die = sibling_die (child_die))
16698 /* Create the symbol in the DW_TAG_common_block block in the current
16700 sym = new_symbol (child_die, NULL, cu);
16703 struct attribute *member_loc;
16705 common_block->contents[common_block->n_entries++] = sym;
16707 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16711 /* GDB has handled this for a long time, but it is
16712 not specified by DWARF. It seems to have been
16713 emitted by gfortran at least as recently as:
16714 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16715 complaint (&symfile_complaints,
16716 _("Variable in common block has "
16717 "DW_AT_data_member_location "
16718 "- DIE at 0x%x [in module %s]"),
16719 to_underlying (child_die->sect_off),
16720 objfile_name (objfile));
16722 if (attr_form_is_section_offset (member_loc))
16723 dwarf2_complex_location_expr_complaint ();
16724 else if (attr_form_is_constant (member_loc)
16725 || attr_form_is_block (member_loc))
16728 mark_common_block_symbol_computed (sym, die, attr,
16732 dwarf2_complex_location_expr_complaint ();
16737 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16738 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16742 /* Create a type for a C++ namespace. */
16744 static struct type *
16745 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16747 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16748 const char *previous_prefix, *name;
16752 /* For extensions, reuse the type of the original namespace. */
16753 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16755 struct die_info *ext_die;
16756 struct dwarf2_cu *ext_cu = cu;
16758 ext_die = dwarf2_extension (die, &ext_cu);
16759 type = read_type_die (ext_die, ext_cu);
16761 /* EXT_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 name = namespace_name (die, &is_anonymous, cu);
16768 /* Now build the name of the current namespace. */
16770 previous_prefix = determine_prefix (die, cu);
16771 if (previous_prefix[0] != '\0')
16772 name = typename_concat (&objfile->objfile_obstack,
16773 previous_prefix, name, 0, cu);
16775 /* Create the type. */
16776 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16777 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16779 return set_die_type (die, type, cu);
16782 /* Read a namespace scope. */
16785 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16787 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16790 /* Add a symbol associated to this if we haven't seen the namespace
16791 before. Also, add a using directive if it's an anonymous
16794 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16798 type = read_type_die (die, cu);
16799 new_symbol (die, type, cu);
16801 namespace_name (die, &is_anonymous, cu);
16804 const char *previous_prefix = determine_prefix (die, cu);
16806 std::vector<const char *> excludes;
16807 add_using_directive (using_directives (cu->language),
16808 previous_prefix, TYPE_NAME (type), NULL,
16809 NULL, excludes, 0, &objfile->objfile_obstack);
16813 if (die->child != NULL)
16815 struct die_info *child_die = die->child;
16817 while (child_die && child_die->tag)
16819 process_die (child_die, cu);
16820 child_die = sibling_die (child_die);
16825 /* Read a Fortran module as type. This DIE can be only a declaration used for
16826 imported module. Still we need that type as local Fortran "use ... only"
16827 declaration imports depend on the created type in determine_prefix. */
16829 static struct type *
16830 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16832 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16833 const char *module_name;
16836 module_name = dwarf2_name (die, cu);
16838 complaint (&symfile_complaints,
16839 _("DW_TAG_module has no name, offset 0x%x"),
16840 to_underlying (die->sect_off));
16841 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16843 /* determine_prefix uses TYPE_TAG_NAME. */
16844 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16846 return set_die_type (die, type, cu);
16849 /* Read a Fortran module. */
16852 read_module (struct die_info *die, struct dwarf2_cu *cu)
16854 struct die_info *child_die = die->child;
16857 type = read_type_die (die, cu);
16858 new_symbol (die, type, cu);
16860 while (child_die && child_die->tag)
16862 process_die (child_die, cu);
16863 child_die = sibling_die (child_die);
16867 /* Return the name of the namespace represented by DIE. Set
16868 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16871 static const char *
16872 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16874 struct die_info *current_die;
16875 const char *name = NULL;
16877 /* Loop through the extensions until we find a name. */
16879 for (current_die = die;
16880 current_die != NULL;
16881 current_die = dwarf2_extension (die, &cu))
16883 /* We don't use dwarf2_name here so that we can detect the absence
16884 of a name -> anonymous namespace. */
16885 name = dwarf2_string_attr (die, DW_AT_name, cu);
16891 /* Is it an anonymous namespace? */
16893 *is_anonymous = (name == NULL);
16895 name = CP_ANONYMOUS_NAMESPACE_STR;
16900 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16901 the user defined type vector. */
16903 static struct type *
16904 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16906 struct gdbarch *gdbarch
16907 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16908 struct comp_unit_head *cu_header = &cu->header;
16910 struct attribute *attr_byte_size;
16911 struct attribute *attr_address_class;
16912 int byte_size, addr_class;
16913 struct type *target_type;
16915 target_type = die_type (die, cu);
16917 /* The die_type call above may have already set the type for this DIE. */
16918 type = get_die_type (die, cu);
16922 type = lookup_pointer_type (target_type);
16924 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16925 if (attr_byte_size)
16926 byte_size = DW_UNSND (attr_byte_size);
16928 byte_size = cu_header->addr_size;
16930 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16931 if (attr_address_class)
16932 addr_class = DW_UNSND (attr_address_class);
16934 addr_class = DW_ADDR_none;
16936 /* If the pointer size or address class is different than the
16937 default, create a type variant marked as such and set the
16938 length accordingly. */
16939 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
16941 if (gdbarch_address_class_type_flags_p (gdbarch))
16945 type_flags = gdbarch_address_class_type_flags
16946 (gdbarch, byte_size, addr_class);
16947 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16949 type = make_type_with_address_space (type, type_flags);
16951 else if (TYPE_LENGTH (type) != byte_size)
16953 complaint (&symfile_complaints,
16954 _("invalid pointer size %d"), byte_size);
16958 /* Should we also complain about unhandled address classes? */
16962 TYPE_LENGTH (type) = byte_size;
16963 return set_die_type (die, type, cu);
16966 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16967 the user defined type vector. */
16969 static struct type *
16970 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16973 struct type *to_type;
16974 struct type *domain;
16976 to_type = die_type (die, cu);
16977 domain = die_containing_type (die, cu);
16979 /* The calls above may have already set the type for this DIE. */
16980 type = get_die_type (die, cu);
16984 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16985 type = lookup_methodptr_type (to_type);
16986 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16988 struct type *new_type
16989 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16991 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16992 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16993 TYPE_VARARGS (to_type));
16994 type = lookup_methodptr_type (new_type);
16997 type = lookup_memberptr_type (to_type, domain);
16999 return set_die_type (die, type, cu);
17002 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17003 the user defined type vector. */
17005 static struct type *
17006 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17007 enum type_code refcode)
17009 struct comp_unit_head *cu_header = &cu->header;
17010 struct type *type, *target_type;
17011 struct attribute *attr;
17013 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17015 target_type = die_type (die, cu);
17017 /* The die_type call above may have already set the type for this DIE. */
17018 type = get_die_type (die, cu);
17022 type = lookup_reference_type (target_type, refcode);
17023 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17026 TYPE_LENGTH (type) = DW_UNSND (attr);
17030 TYPE_LENGTH (type) = cu_header->addr_size;
17032 return set_die_type (die, type, cu);
17035 /* Add the given cv-qualifiers to the element type of the array. GCC
17036 outputs DWARF type qualifiers that apply to an array, not the
17037 element type. But GDB relies on the array element type to carry
17038 the cv-qualifiers. This mimics section 6.7.3 of the C99
17041 static struct type *
17042 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17043 struct type *base_type, int cnst, int voltl)
17045 struct type *el_type, *inner_array;
17047 base_type = copy_type (base_type);
17048 inner_array = base_type;
17050 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17052 TYPE_TARGET_TYPE (inner_array) =
17053 copy_type (TYPE_TARGET_TYPE (inner_array));
17054 inner_array = TYPE_TARGET_TYPE (inner_array);
17057 el_type = TYPE_TARGET_TYPE (inner_array);
17058 cnst |= TYPE_CONST (el_type);
17059 voltl |= TYPE_VOLATILE (el_type);
17060 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17062 return set_die_type (die, base_type, cu);
17065 static struct type *
17066 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17068 struct type *base_type, *cv_type;
17070 base_type = die_type (die, cu);
17072 /* The die_type call above may have already set the type for this DIE. */
17073 cv_type = get_die_type (die, cu);
17077 /* In case the const qualifier is applied to an array type, the element type
17078 is so qualified, not the array type (section 6.7.3 of C99). */
17079 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17080 return add_array_cv_type (die, cu, base_type, 1, 0);
17082 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17083 return set_die_type (die, cv_type, cu);
17086 static struct type *
17087 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17089 struct type *base_type, *cv_type;
17091 base_type = die_type (die, cu);
17093 /* The die_type call above may have already set the type for this DIE. */
17094 cv_type = get_die_type (die, cu);
17098 /* In case the volatile qualifier is applied to an array type, the
17099 element type is so qualified, not the array type (section 6.7.3
17101 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17102 return add_array_cv_type (die, cu, base_type, 0, 1);
17104 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17105 return set_die_type (die, cv_type, cu);
17108 /* Handle DW_TAG_restrict_type. */
17110 static struct type *
17111 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17113 struct type *base_type, *cv_type;
17115 base_type = die_type (die, cu);
17117 /* The die_type call above may have already set the type for this DIE. */
17118 cv_type = get_die_type (die, cu);
17122 cv_type = make_restrict_type (base_type);
17123 return set_die_type (die, cv_type, cu);
17126 /* Handle DW_TAG_atomic_type. */
17128 static struct type *
17129 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17131 struct type *base_type, *cv_type;
17133 base_type = die_type (die, cu);
17135 /* The die_type call above may have already set the type for this DIE. */
17136 cv_type = get_die_type (die, cu);
17140 cv_type = make_atomic_type (base_type);
17141 return set_die_type (die, cv_type, cu);
17144 /* Extract all information from a DW_TAG_string_type DIE and add to
17145 the user defined type vector. It isn't really a user defined type,
17146 but it behaves like one, with other DIE's using an AT_user_def_type
17147 attribute to reference it. */
17149 static struct type *
17150 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17152 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17153 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17154 struct type *type, *range_type, *index_type, *char_type;
17155 struct attribute *attr;
17156 unsigned int length;
17158 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17161 length = DW_UNSND (attr);
17165 /* Check for the DW_AT_byte_size attribute. */
17166 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17169 length = DW_UNSND (attr);
17177 index_type = objfile_type (objfile)->builtin_int;
17178 range_type = create_static_range_type (NULL, index_type, 1, length);
17179 char_type = language_string_char_type (cu->language_defn, gdbarch);
17180 type = create_string_type (NULL, char_type, range_type);
17182 return set_die_type (die, type, cu);
17185 /* Assuming that DIE corresponds to a function, returns nonzero
17186 if the function is prototyped. */
17189 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17191 struct attribute *attr;
17193 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17194 if (attr && (DW_UNSND (attr) != 0))
17197 /* The DWARF standard implies that the DW_AT_prototyped attribute
17198 is only meaninful for C, but the concept also extends to other
17199 languages that allow unprototyped functions (Eg: Objective C).
17200 For all other languages, assume that functions are always
17202 if (cu->language != language_c
17203 && cu->language != language_objc
17204 && cu->language != language_opencl)
17207 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17208 prototyped and unprototyped functions; default to prototyped,
17209 since that is more common in modern code (and RealView warns
17210 about unprototyped functions). */
17211 if (producer_is_realview (cu->producer))
17217 /* Handle DIES due to C code like:
17221 int (*funcp)(int a, long l);
17225 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17227 static struct type *
17228 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17230 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17231 struct type *type; /* Type that this function returns. */
17232 struct type *ftype; /* Function that returns above type. */
17233 struct attribute *attr;
17235 type = die_type (die, cu);
17237 /* The die_type call above may have already set the type for this DIE. */
17238 ftype = get_die_type (die, cu);
17242 ftype = lookup_function_type (type);
17244 if (prototyped_function_p (die, cu))
17245 TYPE_PROTOTYPED (ftype) = 1;
17247 /* Store the calling convention in the type if it's available in
17248 the subroutine die. Otherwise set the calling convention to
17249 the default value DW_CC_normal. */
17250 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17252 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17253 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17254 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17256 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17258 /* Record whether the function returns normally to its caller or not
17259 if the DWARF producer set that information. */
17260 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17261 if (attr && (DW_UNSND (attr) != 0))
17262 TYPE_NO_RETURN (ftype) = 1;
17264 /* We need to add the subroutine type to the die immediately so
17265 we don't infinitely recurse when dealing with parameters
17266 declared as the same subroutine type. */
17267 set_die_type (die, ftype, cu);
17269 if (die->child != NULL)
17271 struct type *void_type = objfile_type (objfile)->builtin_void;
17272 struct die_info *child_die;
17273 int nparams, iparams;
17275 /* Count the number of parameters.
17276 FIXME: GDB currently ignores vararg functions, but knows about
17277 vararg member functions. */
17279 child_die = die->child;
17280 while (child_die && child_die->tag)
17282 if (child_die->tag == DW_TAG_formal_parameter)
17284 else if (child_die->tag == DW_TAG_unspecified_parameters)
17285 TYPE_VARARGS (ftype) = 1;
17286 child_die = sibling_die (child_die);
17289 /* Allocate storage for parameters and fill them in. */
17290 TYPE_NFIELDS (ftype) = nparams;
17291 TYPE_FIELDS (ftype) = (struct field *)
17292 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17294 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17295 even if we error out during the parameters reading below. */
17296 for (iparams = 0; iparams < nparams; iparams++)
17297 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17300 child_die = die->child;
17301 while (child_die && child_die->tag)
17303 if (child_die->tag == DW_TAG_formal_parameter)
17305 struct type *arg_type;
17307 /* DWARF version 2 has no clean way to discern C++
17308 static and non-static member functions. G++ helps
17309 GDB by marking the first parameter for non-static
17310 member functions (which is the this pointer) as
17311 artificial. We pass this information to
17312 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17314 DWARF version 3 added DW_AT_object_pointer, which GCC
17315 4.5 does not yet generate. */
17316 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17318 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17320 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17321 arg_type = die_type (child_die, cu);
17323 /* RealView does not mark THIS as const, which the testsuite
17324 expects. GCC marks THIS as const in method definitions,
17325 but not in the class specifications (GCC PR 43053). */
17326 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17327 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17330 struct dwarf2_cu *arg_cu = cu;
17331 const char *name = dwarf2_name (child_die, cu);
17333 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17336 /* If the compiler emits this, use it. */
17337 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17340 else if (name && strcmp (name, "this") == 0)
17341 /* Function definitions will have the argument names. */
17343 else if (name == NULL && iparams == 0)
17344 /* Declarations may not have the names, so like
17345 elsewhere in GDB, assume an artificial first
17346 argument is "this". */
17350 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17354 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17357 child_die = sibling_die (child_die);
17364 static struct type *
17365 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17367 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17368 const char *name = NULL;
17369 struct type *this_type, *target_type;
17371 name = dwarf2_full_name (NULL, die, cu);
17372 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17373 TYPE_TARGET_STUB (this_type) = 1;
17374 set_die_type (die, this_type, cu);
17375 target_type = die_type (die, cu);
17376 if (target_type != this_type)
17377 TYPE_TARGET_TYPE (this_type) = target_type;
17380 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17381 spec and cause infinite loops in GDB. */
17382 complaint (&symfile_complaints,
17383 _("Self-referential DW_TAG_typedef "
17384 "- DIE at 0x%x [in module %s]"),
17385 to_underlying (die->sect_off), objfile_name (objfile));
17386 TYPE_TARGET_TYPE (this_type) = NULL;
17391 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17392 (which may be different from NAME) to the architecture back-end to allow
17393 it to guess the correct format if necessary. */
17395 static struct type *
17396 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17397 const char *name_hint)
17399 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17400 const struct floatformat **format;
17403 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17405 type = init_float_type (objfile, bits, name, format);
17407 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17412 /* Find a representation of a given base type and install
17413 it in the TYPE field of the die. */
17415 static struct type *
17416 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17418 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17420 struct attribute *attr;
17421 int encoding = 0, bits = 0;
17424 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17427 encoding = DW_UNSND (attr);
17429 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17432 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17434 name = dwarf2_name (die, cu);
17437 complaint (&symfile_complaints,
17438 _("DW_AT_name missing from DW_TAG_base_type"));
17443 case DW_ATE_address:
17444 /* Turn DW_ATE_address into a void * pointer. */
17445 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17446 type = init_pointer_type (objfile, bits, name, type);
17448 case DW_ATE_boolean:
17449 type = init_boolean_type (objfile, bits, 1, name);
17451 case DW_ATE_complex_float:
17452 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
17453 type = init_complex_type (objfile, name, type);
17455 case DW_ATE_decimal_float:
17456 type = init_decfloat_type (objfile, bits, name);
17459 type = dwarf2_init_float_type (objfile, bits, name, name);
17461 case DW_ATE_signed:
17462 type = init_integer_type (objfile, bits, 0, name);
17464 case DW_ATE_unsigned:
17465 if (cu->language == language_fortran
17467 && startswith (name, "character("))
17468 type = init_character_type (objfile, bits, 1, name);
17470 type = init_integer_type (objfile, bits, 1, name);
17472 case DW_ATE_signed_char:
17473 if (cu->language == language_ada || cu->language == language_m2
17474 || cu->language == language_pascal
17475 || cu->language == language_fortran)
17476 type = init_character_type (objfile, bits, 0, name);
17478 type = init_integer_type (objfile, bits, 0, name);
17480 case DW_ATE_unsigned_char:
17481 if (cu->language == language_ada || cu->language == language_m2
17482 || cu->language == language_pascal
17483 || cu->language == language_fortran
17484 || cu->language == language_rust)
17485 type = init_character_type (objfile, bits, 1, name);
17487 type = init_integer_type (objfile, bits, 1, name);
17491 gdbarch *arch = get_objfile_arch (objfile);
17494 type = builtin_type (arch)->builtin_char16;
17495 else if (bits == 32)
17496 type = builtin_type (arch)->builtin_char32;
17499 complaint (&symfile_complaints,
17500 _("unsupported DW_ATE_UTF bit size: '%d'"),
17502 type = init_integer_type (objfile, bits, 1, name);
17504 return set_die_type (die, type, cu);
17509 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17510 dwarf_type_encoding_name (encoding));
17511 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17515 if (name && strcmp (name, "char") == 0)
17516 TYPE_NOSIGN (type) = 1;
17518 return set_die_type (die, type, cu);
17521 /* Parse dwarf attribute if it's a block, reference or constant and put the
17522 resulting value of the attribute into struct bound_prop.
17523 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17526 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17527 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17529 struct dwarf2_property_baton *baton;
17530 struct obstack *obstack
17531 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17533 if (attr == NULL || prop == NULL)
17536 if (attr_form_is_block (attr))
17538 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17539 baton->referenced_type = NULL;
17540 baton->locexpr.per_cu = cu->per_cu;
17541 baton->locexpr.size = DW_BLOCK (attr)->size;
17542 baton->locexpr.data = DW_BLOCK (attr)->data;
17543 prop->data.baton = baton;
17544 prop->kind = PROP_LOCEXPR;
17545 gdb_assert (prop->data.baton != NULL);
17547 else if (attr_form_is_ref (attr))
17549 struct dwarf2_cu *target_cu = cu;
17550 struct die_info *target_die;
17551 struct attribute *target_attr;
17553 target_die = follow_die_ref (die, attr, &target_cu);
17554 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17555 if (target_attr == NULL)
17556 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17558 if (target_attr == NULL)
17561 switch (target_attr->name)
17563 case DW_AT_location:
17564 if (attr_form_is_section_offset (target_attr))
17566 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17567 baton->referenced_type = die_type (target_die, target_cu);
17568 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17569 prop->data.baton = baton;
17570 prop->kind = PROP_LOCLIST;
17571 gdb_assert (prop->data.baton != NULL);
17573 else if (attr_form_is_block (target_attr))
17575 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17576 baton->referenced_type = die_type (target_die, target_cu);
17577 baton->locexpr.per_cu = cu->per_cu;
17578 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17579 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17580 prop->data.baton = baton;
17581 prop->kind = PROP_LOCEXPR;
17582 gdb_assert (prop->data.baton != NULL);
17586 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17587 "dynamic property");
17591 case DW_AT_data_member_location:
17595 if (!handle_data_member_location (target_die, target_cu,
17599 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17600 baton->referenced_type = read_type_die (target_die->parent,
17602 baton->offset_info.offset = offset;
17603 baton->offset_info.type = die_type (target_die, target_cu);
17604 prop->data.baton = baton;
17605 prop->kind = PROP_ADDR_OFFSET;
17610 else if (attr_form_is_constant (attr))
17612 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17613 prop->kind = PROP_CONST;
17617 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17618 dwarf2_name (die, cu));
17625 /* Read the given DW_AT_subrange DIE. */
17627 static struct type *
17628 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17630 struct type *base_type, *orig_base_type;
17631 struct type *range_type;
17632 struct attribute *attr;
17633 struct dynamic_prop low, high;
17634 int low_default_is_valid;
17635 int high_bound_is_count = 0;
17637 LONGEST negative_mask;
17639 orig_base_type = die_type (die, cu);
17640 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17641 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17642 creating the range type, but we use the result of check_typedef
17643 when examining properties of the type. */
17644 base_type = check_typedef (orig_base_type);
17646 /* The die_type call above may have already set the type for this DIE. */
17647 range_type = get_die_type (die, cu);
17651 low.kind = PROP_CONST;
17652 high.kind = PROP_CONST;
17653 high.data.const_val = 0;
17655 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17656 omitting DW_AT_lower_bound. */
17657 switch (cu->language)
17660 case language_cplus:
17661 low.data.const_val = 0;
17662 low_default_is_valid = 1;
17664 case language_fortran:
17665 low.data.const_val = 1;
17666 low_default_is_valid = 1;
17669 case language_objc:
17670 case language_rust:
17671 low.data.const_val = 0;
17672 low_default_is_valid = (cu->header.version >= 4);
17676 case language_pascal:
17677 low.data.const_val = 1;
17678 low_default_is_valid = (cu->header.version >= 4);
17681 low.data.const_val = 0;
17682 low_default_is_valid = 0;
17686 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17688 attr_to_dynamic_prop (attr, die, cu, &low);
17689 else if (!low_default_is_valid)
17690 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
17691 "- DIE at 0x%x [in module %s]"),
17692 to_underlying (die->sect_off),
17693 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17695 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
17696 if (!attr_to_dynamic_prop (attr, die, cu, &high))
17698 attr = dwarf2_attr (die, DW_AT_count, cu);
17699 if (attr_to_dynamic_prop (attr, die, cu, &high))
17701 /* If bounds are constant do the final calculation here. */
17702 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17703 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17705 high_bound_is_count = 1;
17709 /* Dwarf-2 specifications explicitly allows to create subrange types
17710 without specifying a base type.
17711 In that case, the base type must be set to the type of
17712 the lower bound, upper bound or count, in that order, if any of these
17713 three attributes references an object that has a type.
17714 If no base type is found, the Dwarf-2 specifications say that
17715 a signed integer type of size equal to the size of an address should
17717 For the following C code: `extern char gdb_int [];'
17718 GCC produces an empty range DIE.
17719 FIXME: muller/2010-05-28: Possible references to object for low bound,
17720 high bound or count are not yet handled by this code. */
17721 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17723 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17724 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17725 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17726 struct type *int_type = objfile_type (objfile)->builtin_int;
17728 /* Test "int", "long int", and "long long int" objfile types,
17729 and select the first one having a size above or equal to the
17730 architecture address size. */
17731 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17732 base_type = int_type;
17735 int_type = objfile_type (objfile)->builtin_long;
17736 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17737 base_type = int_type;
17740 int_type = objfile_type (objfile)->builtin_long_long;
17741 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17742 base_type = int_type;
17747 /* Normally, the DWARF producers are expected to use a signed
17748 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17749 But this is unfortunately not always the case, as witnessed
17750 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17751 is used instead. To work around that ambiguity, we treat
17752 the bounds as signed, and thus sign-extend their values, when
17753 the base type is signed. */
17755 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17756 if (low.kind == PROP_CONST
17757 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17758 low.data.const_val |= negative_mask;
17759 if (high.kind == PROP_CONST
17760 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17761 high.data.const_val |= negative_mask;
17763 range_type = create_range_type (NULL, orig_base_type, &low, &high);
17765 if (high_bound_is_count)
17766 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17768 /* Ada expects an empty array on no boundary attributes. */
17769 if (attr == NULL && cu->language != language_ada)
17770 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17772 name = dwarf2_name (die, cu);
17774 TYPE_NAME (range_type) = name;
17776 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17778 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17780 set_die_type (die, range_type, cu);
17782 /* set_die_type should be already done. */
17783 set_descriptive_type (range_type, die, cu);
17788 static struct type *
17789 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17793 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17795 TYPE_NAME (type) = dwarf2_name (die, cu);
17797 /* In Ada, an unspecified type is typically used when the description
17798 of the type is defered to a different unit. When encountering
17799 such a type, we treat it as a stub, and try to resolve it later on,
17801 if (cu->language == language_ada)
17802 TYPE_STUB (type) = 1;
17804 return set_die_type (die, type, cu);
17807 /* Read a single die and all its descendents. Set the die's sibling
17808 field to NULL; set other fields in the die correctly, and set all
17809 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17810 location of the info_ptr after reading all of those dies. PARENT
17811 is the parent of the die in question. */
17813 static struct die_info *
17814 read_die_and_children (const struct die_reader_specs *reader,
17815 const gdb_byte *info_ptr,
17816 const gdb_byte **new_info_ptr,
17817 struct die_info *parent)
17819 struct die_info *die;
17820 const gdb_byte *cur_ptr;
17823 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
17826 *new_info_ptr = cur_ptr;
17829 store_in_ref_table (die, reader->cu);
17832 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17836 *new_info_ptr = cur_ptr;
17839 die->sibling = NULL;
17840 die->parent = parent;
17844 /* Read a die, all of its descendents, and all of its siblings; set
17845 all of the fields of all of the dies correctly. Arguments are as
17846 in read_die_and_children. */
17848 static struct die_info *
17849 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17850 const gdb_byte *info_ptr,
17851 const gdb_byte **new_info_ptr,
17852 struct die_info *parent)
17854 struct die_info *first_die, *last_sibling;
17855 const gdb_byte *cur_ptr;
17857 cur_ptr = info_ptr;
17858 first_die = last_sibling = NULL;
17862 struct die_info *die
17863 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17867 *new_info_ptr = cur_ptr;
17874 last_sibling->sibling = die;
17876 last_sibling = die;
17880 /* Read a die, all of its descendents, and all of its siblings; set
17881 all of the fields of all of the dies correctly. Arguments are as
17882 in read_die_and_children.
17883 This the main entry point for reading a DIE and all its children. */
17885 static struct die_info *
17886 read_die_and_siblings (const struct die_reader_specs *reader,
17887 const gdb_byte *info_ptr,
17888 const gdb_byte **new_info_ptr,
17889 struct die_info *parent)
17891 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17892 new_info_ptr, parent);
17894 if (dwarf_die_debug)
17896 fprintf_unfiltered (gdb_stdlog,
17897 "Read die from %s@0x%x of %s:\n",
17898 get_section_name (reader->die_section),
17899 (unsigned) (info_ptr - reader->die_section->buffer),
17900 bfd_get_filename (reader->abfd));
17901 dump_die (die, dwarf_die_debug);
17907 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17909 The caller is responsible for filling in the extra attributes
17910 and updating (*DIEP)->num_attrs.
17911 Set DIEP to point to a newly allocated die with its information,
17912 except for its child, sibling, and parent fields.
17913 Set HAS_CHILDREN to tell whether the die has children or not. */
17915 static const gdb_byte *
17916 read_full_die_1 (const struct die_reader_specs *reader,
17917 struct die_info **diep, const gdb_byte *info_ptr,
17918 int *has_children, int num_extra_attrs)
17920 unsigned int abbrev_number, bytes_read, i;
17921 struct abbrev_info *abbrev;
17922 struct die_info *die;
17923 struct dwarf2_cu *cu = reader->cu;
17924 bfd *abfd = reader->abfd;
17926 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17927 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17928 info_ptr += bytes_read;
17929 if (!abbrev_number)
17936 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17938 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17940 bfd_get_filename (abfd));
17942 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17943 die->sect_off = sect_off;
17944 die->tag = abbrev->tag;
17945 die->abbrev = abbrev_number;
17947 /* Make the result usable.
17948 The caller needs to update num_attrs after adding the extra
17950 die->num_attrs = abbrev->num_attrs;
17952 for (i = 0; i < abbrev->num_attrs; ++i)
17953 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17957 *has_children = abbrev->has_children;
17961 /* Read a die and all its attributes.
17962 Set DIEP to point to a newly allocated die with its information,
17963 except for its child, sibling, and parent fields.
17964 Set HAS_CHILDREN to tell whether the die has children or not. */
17966 static const gdb_byte *
17967 read_full_die (const struct die_reader_specs *reader,
17968 struct die_info **diep, const gdb_byte *info_ptr,
17971 const gdb_byte *result;
17973 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
17975 if (dwarf_die_debug)
17977 fprintf_unfiltered (gdb_stdlog,
17978 "Read die from %s@0x%x of %s:\n",
17979 get_section_name (reader->die_section),
17980 (unsigned) (info_ptr - reader->die_section->buffer),
17981 bfd_get_filename (reader->abfd));
17982 dump_die (*diep, dwarf_die_debug);
17988 /* Abbreviation tables.
17990 In DWARF version 2, the description of the debugging information is
17991 stored in a separate .debug_abbrev section. Before we read any
17992 dies from a section we read in all abbreviations and install them
17993 in a hash table. */
17995 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
17997 struct abbrev_info *
17998 abbrev_table::alloc_abbrev ()
18000 struct abbrev_info *abbrev;
18002 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18003 memset (abbrev, 0, sizeof (struct abbrev_info));
18008 /* Add an abbreviation to the table. */
18011 abbrev_table::add_abbrev (unsigned int abbrev_number,
18012 struct abbrev_info *abbrev)
18014 unsigned int hash_number;
18016 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18017 abbrev->next = abbrevs[hash_number];
18018 abbrevs[hash_number] = abbrev;
18021 /* Look up an abbrev in the table.
18022 Returns NULL if the abbrev is not found. */
18024 struct abbrev_info *
18025 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18027 unsigned int hash_number;
18028 struct abbrev_info *abbrev;
18030 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18031 abbrev = abbrevs[hash_number];
18035 if (abbrev->number == abbrev_number)
18037 abbrev = abbrev->next;
18042 /* Read in an abbrev table. */
18044 static abbrev_table_up
18045 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18046 struct dwarf2_section_info *section,
18047 sect_offset sect_off)
18049 struct objfile *objfile = dwarf2_per_objfile->objfile;
18050 bfd *abfd = get_section_bfd_owner (section);
18051 const gdb_byte *abbrev_ptr;
18052 struct abbrev_info *cur_abbrev;
18053 unsigned int abbrev_number, bytes_read, abbrev_name;
18054 unsigned int abbrev_form;
18055 struct attr_abbrev *cur_attrs;
18056 unsigned int allocated_attrs;
18058 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18060 dwarf2_read_section (objfile, section);
18061 abbrev_ptr = section->buffer + to_underlying (sect_off);
18062 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18063 abbrev_ptr += bytes_read;
18065 allocated_attrs = ATTR_ALLOC_CHUNK;
18066 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18068 /* Loop until we reach an abbrev number of 0. */
18069 while (abbrev_number)
18071 cur_abbrev = abbrev_table->alloc_abbrev ();
18073 /* read in abbrev header */
18074 cur_abbrev->number = abbrev_number;
18076 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18077 abbrev_ptr += bytes_read;
18078 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18081 /* now read in declarations */
18084 LONGEST implicit_const;
18086 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18087 abbrev_ptr += bytes_read;
18088 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18089 abbrev_ptr += bytes_read;
18090 if (abbrev_form == DW_FORM_implicit_const)
18092 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18094 abbrev_ptr += bytes_read;
18098 /* Initialize it due to a false compiler warning. */
18099 implicit_const = -1;
18102 if (abbrev_name == 0)
18105 if (cur_abbrev->num_attrs == allocated_attrs)
18107 allocated_attrs += ATTR_ALLOC_CHUNK;
18109 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18112 cur_attrs[cur_abbrev->num_attrs].name
18113 = (enum dwarf_attribute) abbrev_name;
18114 cur_attrs[cur_abbrev->num_attrs].form
18115 = (enum dwarf_form) abbrev_form;
18116 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18117 ++cur_abbrev->num_attrs;
18120 cur_abbrev->attrs =
18121 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18122 cur_abbrev->num_attrs);
18123 memcpy (cur_abbrev->attrs, cur_attrs,
18124 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18126 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18128 /* Get next abbreviation.
18129 Under Irix6 the abbreviations for a compilation unit are not
18130 always properly terminated with an abbrev number of 0.
18131 Exit loop if we encounter an abbreviation which we have
18132 already read (which means we are about to read the abbreviations
18133 for the next compile unit) or if the end of the abbreviation
18134 table is reached. */
18135 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18137 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18138 abbrev_ptr += bytes_read;
18139 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18144 return abbrev_table;
18147 /* Returns nonzero if TAG represents a type that we might generate a partial
18151 is_type_tag_for_partial (int tag)
18156 /* Some types that would be reasonable to generate partial symbols for,
18157 that we don't at present. */
18158 case DW_TAG_array_type:
18159 case DW_TAG_file_type:
18160 case DW_TAG_ptr_to_member_type:
18161 case DW_TAG_set_type:
18162 case DW_TAG_string_type:
18163 case DW_TAG_subroutine_type:
18165 case DW_TAG_base_type:
18166 case DW_TAG_class_type:
18167 case DW_TAG_interface_type:
18168 case DW_TAG_enumeration_type:
18169 case DW_TAG_structure_type:
18170 case DW_TAG_subrange_type:
18171 case DW_TAG_typedef:
18172 case DW_TAG_union_type:
18179 /* Load all DIEs that are interesting for partial symbols into memory. */
18181 static struct partial_die_info *
18182 load_partial_dies (const struct die_reader_specs *reader,
18183 const gdb_byte *info_ptr, int building_psymtab)
18185 struct dwarf2_cu *cu = reader->cu;
18186 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18187 struct partial_die_info *part_die;
18188 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18189 unsigned int bytes_read;
18190 unsigned int load_all = 0;
18191 int nesting_level = 1;
18196 gdb_assert (cu->per_cu != NULL);
18197 if (cu->per_cu->load_all_dies)
18201 = htab_create_alloc_ex (cu->header.length / 12,
18205 &cu->comp_unit_obstack,
18206 hashtab_obstack_allocate,
18207 dummy_obstack_deallocate);
18209 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
18213 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18215 /* A NULL abbrev means the end of a series of children. */
18216 if (abbrev == NULL)
18218 if (--nesting_level == 0)
18220 /* PART_DIE was probably the last thing allocated on the
18221 comp_unit_obstack, so we could call obstack_free
18222 here. We don't do that because the waste is small,
18223 and will be cleaned up when we're done with this
18224 compilation unit. This way, we're also more robust
18225 against other users of the comp_unit_obstack. */
18228 info_ptr += bytes_read;
18229 last_die = parent_die;
18230 parent_die = parent_die->die_parent;
18234 /* Check for template arguments. We never save these; if
18235 they're seen, we just mark the parent, and go on our way. */
18236 if (parent_die != NULL
18237 && cu->language == language_cplus
18238 && (abbrev->tag == DW_TAG_template_type_param
18239 || abbrev->tag == DW_TAG_template_value_param))
18241 parent_die->has_template_arguments = 1;
18245 /* We don't need a partial DIE for the template argument. */
18246 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18251 /* We only recurse into c++ subprograms looking for template arguments.
18252 Skip their other children. */
18254 && cu->language == language_cplus
18255 && parent_die != NULL
18256 && parent_die->tag == DW_TAG_subprogram)
18258 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18262 /* Check whether this DIE is interesting enough to save. Normally
18263 we would not be interested in members here, but there may be
18264 later variables referencing them via DW_AT_specification (for
18265 static members). */
18267 && !is_type_tag_for_partial (abbrev->tag)
18268 && abbrev->tag != DW_TAG_constant
18269 && abbrev->tag != DW_TAG_enumerator
18270 && abbrev->tag != DW_TAG_subprogram
18271 && abbrev->tag != DW_TAG_inlined_subroutine
18272 && abbrev->tag != DW_TAG_lexical_block
18273 && abbrev->tag != DW_TAG_variable
18274 && abbrev->tag != DW_TAG_namespace
18275 && abbrev->tag != DW_TAG_module
18276 && abbrev->tag != DW_TAG_member
18277 && abbrev->tag != DW_TAG_imported_unit
18278 && abbrev->tag != DW_TAG_imported_declaration)
18280 /* Otherwise we skip to the next sibling, if any. */
18281 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18285 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
18288 /* This two-pass algorithm for processing partial symbols has a
18289 high cost in cache pressure. Thus, handle some simple cases
18290 here which cover the majority of C partial symbols. DIEs
18291 which neither have specification tags in them, nor could have
18292 specification tags elsewhere pointing at them, can simply be
18293 processed and discarded.
18295 This segment is also optional; scan_partial_symbols and
18296 add_partial_symbol will handle these DIEs if we chain
18297 them in normally. When compilers which do not emit large
18298 quantities of duplicate debug information are more common,
18299 this code can probably be removed. */
18301 /* Any complete simple types at the top level (pretty much all
18302 of them, for a language without namespaces), can be processed
18304 if (parent_die == NULL
18305 && part_die->has_specification == 0
18306 && part_die->is_declaration == 0
18307 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
18308 || part_die->tag == DW_TAG_base_type
18309 || part_die->tag == DW_TAG_subrange_type))
18311 if (building_psymtab && part_die->name != NULL)
18312 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
18313 VAR_DOMAIN, LOC_TYPEDEF,
18314 &objfile->static_psymbols,
18315 0, cu->language, objfile);
18316 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
18320 /* The exception for DW_TAG_typedef with has_children above is
18321 a workaround of GCC PR debug/47510. In the case of this complaint
18322 type_name_no_tag_or_error will error on such types later.
18324 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18325 it could not find the child DIEs referenced later, this is checked
18326 above. In correct DWARF DW_TAG_typedef should have no children. */
18328 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
18329 complaint (&symfile_complaints,
18330 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18331 "- DIE at 0x%x [in module %s]"),
18332 to_underlying (part_die->sect_off), objfile_name (objfile));
18334 /* If we're at the second level, and we're an enumerator, and
18335 our parent has no specification (meaning possibly lives in a
18336 namespace elsewhere), then we can add the partial symbol now
18337 instead of queueing it. */
18338 if (part_die->tag == DW_TAG_enumerator
18339 && parent_die != NULL
18340 && parent_die->die_parent == NULL
18341 && parent_die->tag == DW_TAG_enumeration_type
18342 && parent_die->has_specification == 0)
18344 if (part_die->name == NULL)
18345 complaint (&symfile_complaints,
18346 _("malformed enumerator DIE ignored"));
18347 else if (building_psymtab)
18348 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
18349 VAR_DOMAIN, LOC_CONST,
18350 cu->language == language_cplus
18351 ? &objfile->global_psymbols
18352 : &objfile->static_psymbols,
18353 0, cu->language, objfile);
18355 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
18359 /* We'll save this DIE so link it in. */
18360 part_die->die_parent = parent_die;
18361 part_die->die_sibling = NULL;
18362 part_die->die_child = NULL;
18364 if (last_die && last_die == parent_die)
18365 last_die->die_child = part_die;
18367 last_die->die_sibling = part_die;
18369 last_die = part_die;
18371 if (first_die == NULL)
18372 first_die = part_die;
18374 /* Maybe add the DIE to the hash table. Not all DIEs that we
18375 find interesting need to be in the hash table, because we
18376 also have the parent/sibling/child chains; only those that we
18377 might refer to by offset later during partial symbol reading.
18379 For now this means things that might have be the target of a
18380 DW_AT_specification, DW_AT_abstract_origin, or
18381 DW_AT_extension. DW_AT_extension will refer only to
18382 namespaces; DW_AT_abstract_origin refers to functions (and
18383 many things under the function DIE, but we do not recurse
18384 into function DIEs during partial symbol reading) and
18385 possibly variables as well; DW_AT_specification refers to
18386 declarations. Declarations ought to have the DW_AT_declaration
18387 flag. It happens that GCC forgets to put it in sometimes, but
18388 only for functions, not for types.
18390 Adding more things than necessary to the hash table is harmless
18391 except for the performance cost. Adding too few will result in
18392 wasted time in find_partial_die, when we reread the compilation
18393 unit with load_all_dies set. */
18396 || abbrev->tag == DW_TAG_constant
18397 || abbrev->tag == DW_TAG_subprogram
18398 || abbrev->tag == DW_TAG_variable
18399 || abbrev->tag == DW_TAG_namespace
18400 || part_die->is_declaration)
18404 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18405 to_underlying (part_die->sect_off),
18410 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
18412 /* For some DIEs we want to follow their children (if any). For C
18413 we have no reason to follow the children of structures; for other
18414 languages we have to, so that we can get at method physnames
18415 to infer fully qualified class names, for DW_AT_specification,
18416 and for C++ template arguments. For C++, we also look one level
18417 inside functions to find template arguments (if the name of the
18418 function does not already contain the template arguments).
18420 For Ada, we need to scan the children of subprograms and lexical
18421 blocks as well because Ada allows the definition of nested
18422 entities that could be interesting for the debugger, such as
18423 nested subprograms for instance. */
18424 if (last_die->has_children
18426 || last_die->tag == DW_TAG_namespace
18427 || last_die->tag == DW_TAG_module
18428 || last_die->tag == DW_TAG_enumeration_type
18429 || (cu->language == language_cplus
18430 && last_die->tag == DW_TAG_subprogram
18431 && (last_die->name == NULL
18432 || strchr (last_die->name, '<') == NULL))
18433 || (cu->language != language_c
18434 && (last_die->tag == DW_TAG_class_type
18435 || last_die->tag == DW_TAG_interface_type
18436 || last_die->tag == DW_TAG_structure_type
18437 || last_die->tag == DW_TAG_union_type))
18438 || (cu->language == language_ada
18439 && (last_die->tag == DW_TAG_subprogram
18440 || last_die->tag == DW_TAG_lexical_block))))
18443 parent_die = last_die;
18447 /* Otherwise we skip to the next sibling, if any. */
18448 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18450 /* Back to the top, do it again. */
18454 /* Read a minimal amount of information into the minimal die structure. */
18456 static const gdb_byte *
18457 read_partial_die (const struct die_reader_specs *reader,
18458 struct partial_die_info *part_die,
18459 struct abbrev_info *abbrev, unsigned int abbrev_len,
18460 const gdb_byte *info_ptr)
18462 struct dwarf2_cu *cu = reader->cu;
18463 struct dwarf2_per_objfile *dwarf2_per_objfile
18464 = cu->per_cu->dwarf2_per_objfile;
18465 struct objfile *objfile = dwarf2_per_objfile->objfile;
18466 const gdb_byte *buffer = reader->buffer;
18468 struct attribute attr;
18469 int has_low_pc_attr = 0;
18470 int has_high_pc_attr = 0;
18471 int high_pc_relative = 0;
18473 memset (part_die, 0, sizeof (struct partial_die_info));
18475 part_die->sect_off = (sect_offset) (info_ptr - buffer);
18477 info_ptr += abbrev_len;
18479 if (abbrev == NULL)
18482 part_die->tag = abbrev->tag;
18483 part_die->has_children = abbrev->has_children;
18485 for (i = 0; i < abbrev->num_attrs; ++i)
18487 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
18489 /* Store the data if it is of an attribute we want to keep in a
18490 partial symbol table. */
18494 switch (part_die->tag)
18496 case DW_TAG_compile_unit:
18497 case DW_TAG_partial_unit:
18498 case DW_TAG_type_unit:
18499 /* Compilation units have a DW_AT_name that is a filename, not
18500 a source language identifier. */
18501 case DW_TAG_enumeration_type:
18502 case DW_TAG_enumerator:
18503 /* These tags always have simple identifiers already; no need
18504 to canonicalize them. */
18505 part_die->name = DW_STRING (&attr);
18509 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18510 &objfile->per_bfd->storage_obstack);
18514 case DW_AT_linkage_name:
18515 case DW_AT_MIPS_linkage_name:
18516 /* Note that both forms of linkage name might appear. We
18517 assume they will be the same, and we only store the last
18519 if (cu->language == language_ada)
18520 part_die->name = DW_STRING (&attr);
18521 part_die->linkage_name = DW_STRING (&attr);
18524 has_low_pc_attr = 1;
18525 part_die->lowpc = attr_value_as_address (&attr);
18527 case DW_AT_high_pc:
18528 has_high_pc_attr = 1;
18529 part_die->highpc = attr_value_as_address (&attr);
18530 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18531 high_pc_relative = 1;
18533 case DW_AT_location:
18534 /* Support the .debug_loc offsets. */
18535 if (attr_form_is_block (&attr))
18537 part_die->d.locdesc = DW_BLOCK (&attr);
18539 else if (attr_form_is_section_offset (&attr))
18541 dwarf2_complex_location_expr_complaint ();
18545 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18546 "partial symbol information");
18549 case DW_AT_external:
18550 part_die->is_external = DW_UNSND (&attr);
18552 case DW_AT_declaration:
18553 part_die->is_declaration = DW_UNSND (&attr);
18556 part_die->has_type = 1;
18558 case DW_AT_abstract_origin:
18559 case DW_AT_specification:
18560 case DW_AT_extension:
18561 part_die->has_specification = 1;
18562 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
18563 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18564 || cu->per_cu->is_dwz);
18566 case DW_AT_sibling:
18567 /* Ignore absolute siblings, they might point outside of
18568 the current compile unit. */
18569 if (attr.form == DW_FORM_ref_addr)
18570 complaint (&symfile_complaints,
18571 _("ignoring absolute DW_AT_sibling"));
18574 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18575 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18577 if (sibling_ptr < info_ptr)
18578 complaint (&symfile_complaints,
18579 _("DW_AT_sibling points backwards"));
18580 else if (sibling_ptr > reader->buffer_end)
18581 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18583 part_die->sibling = sibling_ptr;
18586 case DW_AT_byte_size:
18587 part_die->has_byte_size = 1;
18589 case DW_AT_const_value:
18590 part_die->has_const_value = 1;
18592 case DW_AT_calling_convention:
18593 /* DWARF doesn't provide a way to identify a program's source-level
18594 entry point. DW_AT_calling_convention attributes are only meant
18595 to describe functions' calling conventions.
18597 However, because it's a necessary piece of information in
18598 Fortran, and before DWARF 4 DW_CC_program was the only
18599 piece of debugging information whose definition refers to
18600 a 'main program' at all, several compilers marked Fortran
18601 main programs with DW_CC_program --- even when those
18602 functions use the standard calling conventions.
18604 Although DWARF now specifies a way to provide this
18605 information, we support this practice for backward
18607 if (DW_UNSND (&attr) == DW_CC_program
18608 && cu->language == language_fortran)
18609 part_die->main_subprogram = 1;
18612 if (DW_UNSND (&attr) == DW_INL_inlined
18613 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18614 part_die->may_be_inlined = 1;
18618 if (part_die->tag == DW_TAG_imported_unit)
18620 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
18621 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18622 || cu->per_cu->is_dwz);
18626 case DW_AT_main_subprogram:
18627 part_die->main_subprogram = DW_UNSND (&attr);
18635 if (high_pc_relative)
18636 part_die->highpc += part_die->lowpc;
18638 if (has_low_pc_attr && has_high_pc_attr)
18640 /* When using the GNU linker, .gnu.linkonce. sections are used to
18641 eliminate duplicate copies of functions and vtables and such.
18642 The linker will arbitrarily choose one and discard the others.
18643 The AT_*_pc values for such functions refer to local labels in
18644 these sections. If the section from that file was discarded, the
18645 labels are not in the output, so the relocs get a value of 0.
18646 If this is a discarded function, mark the pc bounds as invalid,
18647 so that GDB will ignore it. */
18648 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18650 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18652 complaint (&symfile_complaints,
18653 _("DW_AT_low_pc %s is zero "
18654 "for DIE at 0x%x [in module %s]"),
18655 paddress (gdbarch, part_die->lowpc),
18656 to_underlying (part_die->sect_off), objfile_name (objfile));
18658 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18659 else if (part_die->lowpc >= part_die->highpc)
18661 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18663 complaint (&symfile_complaints,
18664 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18665 "for DIE at 0x%x [in module %s]"),
18666 paddress (gdbarch, part_die->lowpc),
18667 paddress (gdbarch, part_die->highpc),
18668 to_underlying (part_die->sect_off),
18669 objfile_name (objfile));
18672 part_die->has_pc_info = 1;
18678 /* Find a cached partial DIE at OFFSET in CU. */
18680 static struct partial_die_info *
18681 find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
18683 struct partial_die_info *lookup_die = NULL;
18684 struct partial_die_info part_die;
18686 part_die.sect_off = sect_off;
18687 lookup_die = ((struct partial_die_info *)
18688 htab_find_with_hash (cu->partial_dies, &part_die,
18689 to_underlying (sect_off)));
18694 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18695 except in the case of .debug_types DIEs which do not reference
18696 outside their CU (they do however referencing other types via
18697 DW_FORM_ref_sig8). */
18699 static struct partial_die_info *
18700 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18702 struct dwarf2_per_objfile *dwarf2_per_objfile
18703 = cu->per_cu->dwarf2_per_objfile;
18704 struct objfile *objfile = dwarf2_per_objfile->objfile;
18705 struct dwarf2_per_cu_data *per_cu = NULL;
18706 struct partial_die_info *pd = NULL;
18708 if (offset_in_dwz == cu->per_cu->is_dwz
18709 && offset_in_cu_p (&cu->header, sect_off))
18711 pd = find_partial_die_in_comp_unit (sect_off, cu);
18714 /* We missed recording what we needed.
18715 Load all dies and try again. */
18716 per_cu = cu->per_cu;
18720 /* TUs don't reference other CUs/TUs (except via type signatures). */
18721 if (cu->per_cu->is_debug_types)
18723 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
18724 " external reference to offset 0x%x [in module %s].\n"),
18725 to_underlying (cu->header.sect_off), to_underlying (sect_off),
18726 bfd_get_filename (objfile->obfd));
18728 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18729 dwarf2_per_objfile);
18731 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18732 load_partial_comp_unit (per_cu);
18734 per_cu->cu->last_used = 0;
18735 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
18738 /* If we didn't find it, and not all dies have been loaded,
18739 load them all and try again. */
18741 if (pd == NULL && per_cu->load_all_dies == 0)
18743 per_cu->load_all_dies = 1;
18745 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18746 THIS_CU->cu may already be in use. So we can't just free it and
18747 replace its DIEs with the ones we read in. Instead, we leave those
18748 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18749 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18751 load_partial_comp_unit (per_cu);
18753 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
18757 internal_error (__FILE__, __LINE__,
18758 _("could not find partial DIE 0x%x "
18759 "in cache [from module %s]\n"),
18760 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
18764 /* See if we can figure out if the class lives in a namespace. We do
18765 this by looking for a member function; its demangled name will
18766 contain namespace info, if there is any. */
18769 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18770 struct dwarf2_cu *cu)
18772 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18773 what template types look like, because the demangler
18774 frequently doesn't give the same name as the debug info. We
18775 could fix this by only using the demangled name to get the
18776 prefix (but see comment in read_structure_type). */
18778 struct partial_die_info *real_pdi;
18779 struct partial_die_info *child_pdi;
18781 /* If this DIE (this DIE's specification, if any) has a parent, then
18782 we should not do this. We'll prepend the parent's fully qualified
18783 name when we create the partial symbol. */
18785 real_pdi = struct_pdi;
18786 while (real_pdi->has_specification)
18787 real_pdi = find_partial_die (real_pdi->spec_offset,
18788 real_pdi->spec_is_dwz, cu);
18790 if (real_pdi->die_parent != NULL)
18793 for (child_pdi = struct_pdi->die_child;
18795 child_pdi = child_pdi->die_sibling)
18797 if (child_pdi->tag == DW_TAG_subprogram
18798 && child_pdi->linkage_name != NULL)
18800 char *actual_class_name
18801 = language_class_name_from_physname (cu->language_defn,
18802 child_pdi->linkage_name);
18803 if (actual_class_name != NULL)
18805 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18808 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18810 strlen (actual_class_name)));
18811 xfree (actual_class_name);
18818 /* Adjust PART_DIE before generating a symbol for it. This function
18819 may set the is_external flag or change the DIE's name. */
18822 fixup_partial_die (struct partial_die_info *part_die,
18823 struct dwarf2_cu *cu)
18825 /* Once we've fixed up a die, there's no point in doing so again.
18826 This also avoids a memory leak if we were to call
18827 guess_partial_die_structure_name multiple times. */
18828 if (part_die->fixup_called)
18831 /* If we found a reference attribute and the DIE has no name, try
18832 to find a name in the referred to DIE. */
18834 if (part_die->name == NULL && part_die->has_specification)
18836 struct partial_die_info *spec_die;
18838 spec_die = find_partial_die (part_die->spec_offset,
18839 part_die->spec_is_dwz, cu);
18841 fixup_partial_die (spec_die, cu);
18843 if (spec_die->name)
18845 part_die->name = spec_die->name;
18847 /* Copy DW_AT_external attribute if it is set. */
18848 if (spec_die->is_external)
18849 part_die->is_external = spec_die->is_external;
18853 /* Set default names for some unnamed DIEs. */
18855 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
18856 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
18858 /* If there is no parent die to provide a namespace, and there are
18859 children, see if we can determine the namespace from their linkage
18861 if (cu->language == language_cplus
18862 && !VEC_empty (dwarf2_section_info_def,
18863 cu->per_cu->dwarf2_per_objfile->types)
18864 && part_die->die_parent == NULL
18865 && part_die->has_children
18866 && (part_die->tag == DW_TAG_class_type
18867 || part_die->tag == DW_TAG_structure_type
18868 || part_die->tag == DW_TAG_union_type))
18869 guess_partial_die_structure_name (part_die, cu);
18871 /* GCC might emit a nameless struct or union that has a linkage
18872 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18873 if (part_die->name == NULL
18874 && (part_die->tag == DW_TAG_class_type
18875 || part_die->tag == DW_TAG_interface_type
18876 || part_die->tag == DW_TAG_structure_type
18877 || part_die->tag == DW_TAG_union_type)
18878 && part_die->linkage_name != NULL)
18882 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
18887 /* Strip any leading namespaces/classes, keep only the base name.
18888 DW_AT_name for named DIEs does not contain the prefixes. */
18889 base = strrchr (demangled, ':');
18890 if (base && base > demangled && base[-1] == ':')
18895 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18898 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18899 base, strlen (base)));
18904 part_die->fixup_called = 1;
18907 /* Read an attribute value described by an attribute form. */
18909 static const gdb_byte *
18910 read_attribute_value (const struct die_reader_specs *reader,
18911 struct attribute *attr, unsigned form,
18912 LONGEST implicit_const, const gdb_byte *info_ptr)
18914 struct dwarf2_cu *cu = reader->cu;
18915 struct dwarf2_per_objfile *dwarf2_per_objfile
18916 = cu->per_cu->dwarf2_per_objfile;
18917 struct objfile *objfile = dwarf2_per_objfile->objfile;
18918 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18919 bfd *abfd = reader->abfd;
18920 struct comp_unit_head *cu_header = &cu->header;
18921 unsigned int bytes_read;
18922 struct dwarf_block *blk;
18924 attr->form = (enum dwarf_form) form;
18927 case DW_FORM_ref_addr:
18928 if (cu->header.version == 2)
18929 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18931 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18932 &cu->header, &bytes_read);
18933 info_ptr += bytes_read;
18935 case DW_FORM_GNU_ref_alt:
18936 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18937 info_ptr += bytes_read;
18940 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18941 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18942 info_ptr += bytes_read;
18944 case DW_FORM_block2:
18945 blk = dwarf_alloc_block (cu);
18946 blk->size = read_2_bytes (abfd, info_ptr);
18948 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18949 info_ptr += blk->size;
18950 DW_BLOCK (attr) = blk;
18952 case DW_FORM_block4:
18953 blk = dwarf_alloc_block (cu);
18954 blk->size = read_4_bytes (abfd, info_ptr);
18956 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18957 info_ptr += blk->size;
18958 DW_BLOCK (attr) = blk;
18960 case DW_FORM_data2:
18961 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18964 case DW_FORM_data4:
18965 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18968 case DW_FORM_data8:
18969 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18972 case DW_FORM_data16:
18973 blk = dwarf_alloc_block (cu);
18975 blk->data = read_n_bytes (abfd, info_ptr, 16);
18977 DW_BLOCK (attr) = blk;
18979 case DW_FORM_sec_offset:
18980 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18981 info_ptr += bytes_read;
18983 case DW_FORM_string:
18984 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18985 DW_STRING_IS_CANONICAL (attr) = 0;
18986 info_ptr += bytes_read;
18989 if (!cu->per_cu->is_dwz)
18991 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18992 abfd, info_ptr, cu_header,
18994 DW_STRING_IS_CANONICAL (attr) = 0;
18995 info_ptr += bytes_read;
18999 case DW_FORM_line_strp:
19000 if (!cu->per_cu->is_dwz)
19002 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19004 cu_header, &bytes_read);
19005 DW_STRING_IS_CANONICAL (attr) = 0;
19006 info_ptr += bytes_read;
19010 case DW_FORM_GNU_strp_alt:
19012 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19013 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19016 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19018 DW_STRING_IS_CANONICAL (attr) = 0;
19019 info_ptr += bytes_read;
19022 case DW_FORM_exprloc:
19023 case DW_FORM_block:
19024 blk = dwarf_alloc_block (cu);
19025 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19026 info_ptr += bytes_read;
19027 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19028 info_ptr += blk->size;
19029 DW_BLOCK (attr) = blk;
19031 case DW_FORM_block1:
19032 blk = dwarf_alloc_block (cu);
19033 blk->size = read_1_byte (abfd, info_ptr);
19035 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19036 info_ptr += blk->size;
19037 DW_BLOCK (attr) = blk;
19039 case DW_FORM_data1:
19040 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19044 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19047 case DW_FORM_flag_present:
19048 DW_UNSND (attr) = 1;
19050 case DW_FORM_sdata:
19051 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19052 info_ptr += bytes_read;
19054 case DW_FORM_udata:
19055 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19056 info_ptr += bytes_read;
19059 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19060 + read_1_byte (abfd, info_ptr));
19064 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19065 + read_2_bytes (abfd, info_ptr));
19069 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19070 + read_4_bytes (abfd, info_ptr));
19074 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19075 + read_8_bytes (abfd, info_ptr));
19078 case DW_FORM_ref_sig8:
19079 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19082 case DW_FORM_ref_udata:
19083 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19084 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19085 info_ptr += bytes_read;
19087 case DW_FORM_indirect:
19088 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19089 info_ptr += bytes_read;
19090 if (form == DW_FORM_implicit_const)
19092 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19093 info_ptr += bytes_read;
19095 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19098 case DW_FORM_implicit_const:
19099 DW_SND (attr) = implicit_const;
19101 case DW_FORM_GNU_addr_index:
19102 if (reader->dwo_file == NULL)
19104 /* For now flag a hard error.
19105 Later we can turn this into a complaint. */
19106 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19107 dwarf_form_name (form),
19108 bfd_get_filename (abfd));
19110 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19111 info_ptr += bytes_read;
19113 case DW_FORM_GNU_str_index:
19114 if (reader->dwo_file == NULL)
19116 /* For now flag a hard error.
19117 Later we can turn this into a complaint if warranted. */
19118 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19119 dwarf_form_name (form),
19120 bfd_get_filename (abfd));
19123 ULONGEST str_index =
19124 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19126 DW_STRING (attr) = read_str_index (reader, str_index);
19127 DW_STRING_IS_CANONICAL (attr) = 0;
19128 info_ptr += bytes_read;
19132 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19133 dwarf_form_name (form),
19134 bfd_get_filename (abfd));
19138 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19139 attr->form = DW_FORM_GNU_ref_alt;
19141 /* We have seen instances where the compiler tried to emit a byte
19142 size attribute of -1 which ended up being encoded as an unsigned
19143 0xffffffff. Although 0xffffffff is technically a valid size value,
19144 an object of this size seems pretty unlikely so we can relatively
19145 safely treat these cases as if the size attribute was invalid and
19146 treat them as zero by default. */
19147 if (attr->name == DW_AT_byte_size
19148 && form == DW_FORM_data4
19149 && DW_UNSND (attr) >= 0xffffffff)
19152 (&symfile_complaints,
19153 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19154 hex_string (DW_UNSND (attr)));
19155 DW_UNSND (attr) = 0;
19161 /* Read an attribute described by an abbreviated attribute. */
19163 static const gdb_byte *
19164 read_attribute (const struct die_reader_specs *reader,
19165 struct attribute *attr, struct attr_abbrev *abbrev,
19166 const gdb_byte *info_ptr)
19168 attr->name = abbrev->name;
19169 return read_attribute_value (reader, attr, abbrev->form,
19170 abbrev->implicit_const, info_ptr);
19173 /* Read dwarf information from a buffer. */
19175 static unsigned int
19176 read_1_byte (bfd *abfd, const gdb_byte *buf)
19178 return bfd_get_8 (abfd, buf);
19182 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19184 return bfd_get_signed_8 (abfd, buf);
19187 static unsigned int
19188 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19190 return bfd_get_16 (abfd, buf);
19194 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19196 return bfd_get_signed_16 (abfd, buf);
19199 static unsigned int
19200 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19202 return bfd_get_32 (abfd, buf);
19206 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19208 return bfd_get_signed_32 (abfd, buf);
19212 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19214 return bfd_get_64 (abfd, buf);
19218 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19219 unsigned int *bytes_read)
19221 struct comp_unit_head *cu_header = &cu->header;
19222 CORE_ADDR retval = 0;
19224 if (cu_header->signed_addr_p)
19226 switch (cu_header->addr_size)
19229 retval = bfd_get_signed_16 (abfd, buf);
19232 retval = bfd_get_signed_32 (abfd, buf);
19235 retval = bfd_get_signed_64 (abfd, buf);
19238 internal_error (__FILE__, __LINE__,
19239 _("read_address: bad switch, signed [in module %s]"),
19240 bfd_get_filename (abfd));
19245 switch (cu_header->addr_size)
19248 retval = bfd_get_16 (abfd, buf);
19251 retval = bfd_get_32 (abfd, buf);
19254 retval = bfd_get_64 (abfd, buf);
19257 internal_error (__FILE__, __LINE__,
19258 _("read_address: bad switch, "
19259 "unsigned [in module %s]"),
19260 bfd_get_filename (abfd));
19264 *bytes_read = cu_header->addr_size;
19268 /* Read the initial length from a section. The (draft) DWARF 3
19269 specification allows the initial length to take up either 4 bytes
19270 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19271 bytes describe the length and all offsets will be 8 bytes in length
19274 An older, non-standard 64-bit format is also handled by this
19275 function. The older format in question stores the initial length
19276 as an 8-byte quantity without an escape value. Lengths greater
19277 than 2^32 aren't very common which means that the initial 4 bytes
19278 is almost always zero. Since a length value of zero doesn't make
19279 sense for the 32-bit format, this initial zero can be considered to
19280 be an escape value which indicates the presence of the older 64-bit
19281 format. As written, the code can't detect (old format) lengths
19282 greater than 4GB. If it becomes necessary to handle lengths
19283 somewhat larger than 4GB, we could allow other small values (such
19284 as the non-sensical values of 1, 2, and 3) to also be used as
19285 escape values indicating the presence of the old format.
19287 The value returned via bytes_read should be used to increment the
19288 relevant pointer after calling read_initial_length().
19290 [ Note: read_initial_length() and read_offset() are based on the
19291 document entitled "DWARF Debugging Information Format", revision
19292 3, draft 8, dated November 19, 2001. This document was obtained
19295 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19297 This document is only a draft and is subject to change. (So beware.)
19299 Details regarding the older, non-standard 64-bit format were
19300 determined empirically by examining 64-bit ELF files produced by
19301 the SGI toolchain on an IRIX 6.5 machine.
19303 - Kevin, July 16, 2002
19307 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19309 LONGEST length = bfd_get_32 (abfd, buf);
19311 if (length == 0xffffffff)
19313 length = bfd_get_64 (abfd, buf + 4);
19316 else if (length == 0)
19318 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19319 length = bfd_get_64 (abfd, buf);
19330 /* Cover function for read_initial_length.
19331 Returns the length of the object at BUF, and stores the size of the
19332 initial length in *BYTES_READ and stores the size that offsets will be in
19334 If the initial length size is not equivalent to that specified in
19335 CU_HEADER then issue a complaint.
19336 This is useful when reading non-comp-unit headers. */
19339 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19340 const struct comp_unit_head *cu_header,
19341 unsigned int *bytes_read,
19342 unsigned int *offset_size)
19344 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19346 gdb_assert (cu_header->initial_length_size == 4
19347 || cu_header->initial_length_size == 8
19348 || cu_header->initial_length_size == 12);
19350 if (cu_header->initial_length_size != *bytes_read)
19351 complaint (&symfile_complaints,
19352 _("intermixed 32-bit and 64-bit DWARF sections"));
19354 *offset_size = (*bytes_read == 4) ? 4 : 8;
19358 /* Read an offset from the data stream. The size of the offset is
19359 given by cu_header->offset_size. */
19362 read_offset (bfd *abfd, const gdb_byte *buf,
19363 const struct comp_unit_head *cu_header,
19364 unsigned int *bytes_read)
19366 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19368 *bytes_read = cu_header->offset_size;
19372 /* Read an offset from the data stream. */
19375 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19377 LONGEST retval = 0;
19379 switch (offset_size)
19382 retval = bfd_get_32 (abfd, buf);
19385 retval = bfd_get_64 (abfd, buf);
19388 internal_error (__FILE__, __LINE__,
19389 _("read_offset_1: bad switch [in module %s]"),
19390 bfd_get_filename (abfd));
19396 static const gdb_byte *
19397 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19399 /* If the size of a host char is 8 bits, we can return a pointer
19400 to the buffer, otherwise we have to copy the data to a buffer
19401 allocated on the temporary obstack. */
19402 gdb_assert (HOST_CHAR_BIT == 8);
19406 static const char *
19407 read_direct_string (bfd *abfd, const gdb_byte *buf,
19408 unsigned int *bytes_read_ptr)
19410 /* If the size of a host char is 8 bits, we can return a pointer
19411 to the string, otherwise we have to copy the string to a buffer
19412 allocated on the temporary obstack. */
19413 gdb_assert (HOST_CHAR_BIT == 8);
19416 *bytes_read_ptr = 1;
19419 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19420 return (const char *) buf;
19423 /* Return pointer to string at section SECT offset STR_OFFSET with error
19424 reporting strings FORM_NAME and SECT_NAME. */
19426 static const char *
19427 read_indirect_string_at_offset_from (struct objfile *objfile,
19428 bfd *abfd, LONGEST str_offset,
19429 struct dwarf2_section_info *sect,
19430 const char *form_name,
19431 const char *sect_name)
19433 dwarf2_read_section (objfile, sect);
19434 if (sect->buffer == NULL)
19435 error (_("%s used without %s section [in module %s]"),
19436 form_name, sect_name, bfd_get_filename (abfd));
19437 if (str_offset >= sect->size)
19438 error (_("%s pointing outside of %s section [in module %s]"),
19439 form_name, sect_name, bfd_get_filename (abfd));
19440 gdb_assert (HOST_CHAR_BIT == 8);
19441 if (sect->buffer[str_offset] == '\0')
19443 return (const char *) (sect->buffer + str_offset);
19446 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19448 static const char *
19449 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19450 bfd *abfd, LONGEST str_offset)
19452 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19454 &dwarf2_per_objfile->str,
19455 "DW_FORM_strp", ".debug_str");
19458 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19460 static const char *
19461 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19462 bfd *abfd, LONGEST str_offset)
19464 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19466 &dwarf2_per_objfile->line_str,
19467 "DW_FORM_line_strp",
19468 ".debug_line_str");
19471 /* Read a string at offset STR_OFFSET in the .debug_str section from
19472 the .dwz file DWZ. Throw an error if the offset is too large. If
19473 the string consists of a single NUL byte, return NULL; otherwise
19474 return a pointer to the string. */
19476 static const char *
19477 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19478 LONGEST str_offset)
19480 dwarf2_read_section (objfile, &dwz->str);
19482 if (dwz->str.buffer == NULL)
19483 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19484 "section [in module %s]"),
19485 bfd_get_filename (dwz->dwz_bfd));
19486 if (str_offset >= dwz->str.size)
19487 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19488 ".debug_str section [in module %s]"),
19489 bfd_get_filename (dwz->dwz_bfd));
19490 gdb_assert (HOST_CHAR_BIT == 8);
19491 if (dwz->str.buffer[str_offset] == '\0')
19493 return (const char *) (dwz->str.buffer + str_offset);
19496 /* Return pointer to string at .debug_str offset as read from BUF.
19497 BUF is assumed to be in a compilation unit described by CU_HEADER.
19498 Return *BYTES_READ_PTR count of bytes read from BUF. */
19500 static const char *
19501 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19502 const gdb_byte *buf,
19503 const struct comp_unit_head *cu_header,
19504 unsigned int *bytes_read_ptr)
19506 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19508 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19511 /* Return pointer to string at .debug_line_str offset as read from BUF.
19512 BUF is assumed to be in a compilation unit described by CU_HEADER.
19513 Return *BYTES_READ_PTR count of bytes read from BUF. */
19515 static const char *
19516 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19517 bfd *abfd, const gdb_byte *buf,
19518 const struct comp_unit_head *cu_header,
19519 unsigned int *bytes_read_ptr)
19521 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19523 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19528 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19529 unsigned int *bytes_read_ptr)
19532 unsigned int num_read;
19534 unsigned char byte;
19541 byte = bfd_get_8 (abfd, buf);
19544 result |= ((ULONGEST) (byte & 127) << shift);
19545 if ((byte & 128) == 0)
19551 *bytes_read_ptr = num_read;
19556 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19557 unsigned int *bytes_read_ptr)
19560 int shift, num_read;
19561 unsigned char byte;
19568 byte = bfd_get_8 (abfd, buf);
19571 result |= ((LONGEST) (byte & 127) << shift);
19573 if ((byte & 128) == 0)
19578 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19579 result |= -(((LONGEST) 1) << shift);
19580 *bytes_read_ptr = num_read;
19584 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19585 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19586 ADDR_SIZE is the size of addresses from the CU header. */
19589 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19590 unsigned int addr_index, ULONGEST addr_base, int addr_size)
19592 struct objfile *objfile = dwarf2_per_objfile->objfile;
19593 bfd *abfd = objfile->obfd;
19594 const gdb_byte *info_ptr;
19596 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19597 if (dwarf2_per_objfile->addr.buffer == NULL)
19598 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19599 objfile_name (objfile));
19600 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19601 error (_("DW_FORM_addr_index pointing outside of "
19602 ".debug_addr section [in module %s]"),
19603 objfile_name (objfile));
19604 info_ptr = (dwarf2_per_objfile->addr.buffer
19605 + addr_base + addr_index * addr_size);
19606 if (addr_size == 4)
19607 return bfd_get_32 (abfd, info_ptr);
19609 return bfd_get_64 (abfd, info_ptr);
19612 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19615 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19617 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19618 cu->addr_base, cu->header.addr_size);
19621 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19624 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19625 unsigned int *bytes_read)
19627 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19628 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19630 return read_addr_index (cu, addr_index);
19633 /* Data structure to pass results from dwarf2_read_addr_index_reader
19634 back to dwarf2_read_addr_index. */
19636 struct dwarf2_read_addr_index_data
19638 ULONGEST addr_base;
19642 /* die_reader_func for dwarf2_read_addr_index. */
19645 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19646 const gdb_byte *info_ptr,
19647 struct die_info *comp_unit_die,
19651 struct dwarf2_cu *cu = reader->cu;
19652 struct dwarf2_read_addr_index_data *aidata =
19653 (struct dwarf2_read_addr_index_data *) data;
19655 aidata->addr_base = cu->addr_base;
19656 aidata->addr_size = cu->header.addr_size;
19659 /* Given an index in .debug_addr, fetch the value.
19660 NOTE: This can be called during dwarf expression evaluation,
19661 long after the debug information has been read, and thus per_cu->cu
19662 may no longer exist. */
19665 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19666 unsigned int addr_index)
19668 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19669 struct objfile *objfile = dwarf2_per_objfile->objfile;
19670 struct dwarf2_cu *cu = per_cu->cu;
19671 ULONGEST addr_base;
19674 /* We need addr_base and addr_size.
19675 If we don't have PER_CU->cu, we have to get it.
19676 Nasty, but the alternative is storing the needed info in PER_CU,
19677 which at this point doesn't seem justified: it's not clear how frequently
19678 it would get used and it would increase the size of every PER_CU.
19679 Entry points like dwarf2_per_cu_addr_size do a similar thing
19680 so we're not in uncharted territory here.
19681 Alas we need to be a bit more complicated as addr_base is contained
19684 We don't need to read the entire CU(/TU).
19685 We just need the header and top level die.
19687 IWBN to use the aging mechanism to let us lazily later discard the CU.
19688 For now we skip this optimization. */
19692 addr_base = cu->addr_base;
19693 addr_size = cu->header.addr_size;
19697 struct dwarf2_read_addr_index_data aidata;
19699 /* Note: We can't use init_cutu_and_read_dies_simple here,
19700 we need addr_base. */
19701 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
19702 dwarf2_read_addr_index_reader, &aidata);
19703 addr_base = aidata.addr_base;
19704 addr_size = aidata.addr_size;
19707 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19711 /* Given a DW_FORM_GNU_str_index, fetch the string.
19712 This is only used by the Fission support. */
19714 static const char *
19715 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19717 struct dwarf2_cu *cu = reader->cu;
19718 struct dwarf2_per_objfile *dwarf2_per_objfile
19719 = cu->per_cu->dwarf2_per_objfile;
19720 struct objfile *objfile = dwarf2_per_objfile->objfile;
19721 const char *objf_name = objfile_name (objfile);
19722 bfd *abfd = objfile->obfd;
19723 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19724 struct dwarf2_section_info *str_offsets_section =
19725 &reader->dwo_file->sections.str_offsets;
19726 const gdb_byte *info_ptr;
19727 ULONGEST str_offset;
19728 static const char form_name[] = "DW_FORM_GNU_str_index";
19730 dwarf2_read_section (objfile, str_section);
19731 dwarf2_read_section (objfile, str_offsets_section);
19732 if (str_section->buffer == NULL)
19733 error (_("%s used without .debug_str.dwo section"
19734 " in CU at offset 0x%x [in module %s]"),
19735 form_name, to_underlying (cu->header.sect_off), objf_name);
19736 if (str_offsets_section->buffer == NULL)
19737 error (_("%s used without .debug_str_offsets.dwo section"
19738 " in CU at offset 0x%x [in module %s]"),
19739 form_name, to_underlying (cu->header.sect_off), objf_name);
19740 if (str_index * cu->header.offset_size >= str_offsets_section->size)
19741 error (_("%s pointing outside of .debug_str_offsets.dwo"
19742 " section in CU at offset 0x%x [in module %s]"),
19743 form_name, to_underlying (cu->header.sect_off), objf_name);
19744 info_ptr = (str_offsets_section->buffer
19745 + str_index * cu->header.offset_size);
19746 if (cu->header.offset_size == 4)
19747 str_offset = bfd_get_32 (abfd, info_ptr);
19749 str_offset = bfd_get_64 (abfd, info_ptr);
19750 if (str_offset >= str_section->size)
19751 error (_("Offset from %s pointing outside of"
19752 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
19753 form_name, to_underlying (cu->header.sect_off), objf_name);
19754 return (const char *) (str_section->buffer + str_offset);
19757 /* Return the length of an LEB128 number in BUF. */
19760 leb128_size (const gdb_byte *buf)
19762 const gdb_byte *begin = buf;
19768 if ((byte & 128) == 0)
19769 return buf - begin;
19774 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19783 cu->language = language_c;
19786 case DW_LANG_C_plus_plus:
19787 case DW_LANG_C_plus_plus_11:
19788 case DW_LANG_C_plus_plus_14:
19789 cu->language = language_cplus;
19792 cu->language = language_d;
19794 case DW_LANG_Fortran77:
19795 case DW_LANG_Fortran90:
19796 case DW_LANG_Fortran95:
19797 case DW_LANG_Fortran03:
19798 case DW_LANG_Fortran08:
19799 cu->language = language_fortran;
19802 cu->language = language_go;
19804 case DW_LANG_Mips_Assembler:
19805 cu->language = language_asm;
19807 case DW_LANG_Ada83:
19808 case DW_LANG_Ada95:
19809 cu->language = language_ada;
19811 case DW_LANG_Modula2:
19812 cu->language = language_m2;
19814 case DW_LANG_Pascal83:
19815 cu->language = language_pascal;
19818 cu->language = language_objc;
19821 case DW_LANG_Rust_old:
19822 cu->language = language_rust;
19824 case DW_LANG_Cobol74:
19825 case DW_LANG_Cobol85:
19827 cu->language = language_minimal;
19830 cu->language_defn = language_def (cu->language);
19833 /* Return the named attribute or NULL if not there. */
19835 static struct attribute *
19836 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19841 struct attribute *spec = NULL;
19843 for (i = 0; i < die->num_attrs; ++i)
19845 if (die->attrs[i].name == name)
19846 return &die->attrs[i];
19847 if (die->attrs[i].name == DW_AT_specification
19848 || die->attrs[i].name == DW_AT_abstract_origin)
19849 spec = &die->attrs[i];
19855 die = follow_die_ref (die, spec, &cu);
19861 /* Return the named attribute or NULL if not there,
19862 but do not follow DW_AT_specification, etc.
19863 This is for use in contexts where we're reading .debug_types dies.
19864 Following DW_AT_specification, DW_AT_abstract_origin will take us
19865 back up the chain, and we want to go down. */
19867 static struct attribute *
19868 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19872 for (i = 0; i < die->num_attrs; ++i)
19873 if (die->attrs[i].name == name)
19874 return &die->attrs[i];
19879 /* Return the string associated with a string-typed attribute, or NULL if it
19880 is either not found or is of an incorrect type. */
19882 static const char *
19883 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19885 struct attribute *attr;
19886 const char *str = NULL;
19888 attr = dwarf2_attr (die, name, cu);
19892 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19893 || attr->form == DW_FORM_string
19894 || attr->form == DW_FORM_GNU_str_index
19895 || attr->form == DW_FORM_GNU_strp_alt)
19896 str = DW_STRING (attr);
19898 complaint (&symfile_complaints,
19899 _("string type expected for attribute %s for "
19900 "DIE at 0x%x in module %s"),
19901 dwarf_attr_name (name), to_underlying (die->sect_off),
19902 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19908 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19909 and holds a non-zero value. This function should only be used for
19910 DW_FORM_flag or DW_FORM_flag_present attributes. */
19913 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19915 struct attribute *attr = dwarf2_attr (die, name, cu);
19917 return (attr && DW_UNSND (attr));
19921 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19923 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19924 which value is non-zero. However, we have to be careful with
19925 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19926 (via dwarf2_flag_true_p) follows this attribute. So we may
19927 end up accidently finding a declaration attribute that belongs
19928 to a different DIE referenced by the specification attribute,
19929 even though the given DIE does not have a declaration attribute. */
19930 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19931 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19934 /* Return the die giving the specification for DIE, if there is
19935 one. *SPEC_CU is the CU containing DIE on input, and the CU
19936 containing the return value on output. If there is no
19937 specification, but there is an abstract origin, that is
19940 static struct die_info *
19941 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19943 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19946 if (spec_attr == NULL)
19947 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19949 if (spec_attr == NULL)
19952 return follow_die_ref (die, spec_attr, spec_cu);
19955 /* Stub for free_line_header to match void * callback types. */
19958 free_line_header_voidp (void *arg)
19960 struct line_header *lh = (struct line_header *) arg;
19966 line_header::add_include_dir (const char *include_dir)
19968 if (dwarf_line_debug >= 2)
19969 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19970 include_dirs.size () + 1, include_dir);
19972 include_dirs.push_back (include_dir);
19976 line_header::add_file_name (const char *name,
19978 unsigned int mod_time,
19979 unsigned int length)
19981 if (dwarf_line_debug >= 2)
19982 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
19983 (unsigned) file_names.size () + 1, name);
19985 file_names.emplace_back (name, d_index, mod_time, length);
19988 /* A convenience function to find the proper .debug_line section for a CU. */
19990 static struct dwarf2_section_info *
19991 get_debug_line_section (struct dwarf2_cu *cu)
19993 struct dwarf2_section_info *section;
19994 struct dwarf2_per_objfile *dwarf2_per_objfile
19995 = cu->per_cu->dwarf2_per_objfile;
19997 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19999 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20000 section = &cu->dwo_unit->dwo_file->sections.line;
20001 else if (cu->per_cu->is_dwz)
20003 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20005 section = &dwz->line;
20008 section = &dwarf2_per_objfile->line;
20013 /* Read directory or file name entry format, starting with byte of
20014 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20015 entries count and the entries themselves in the described entry
20019 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20020 bfd *abfd, const gdb_byte **bufp,
20021 struct line_header *lh,
20022 const struct comp_unit_head *cu_header,
20023 void (*callback) (struct line_header *lh,
20026 unsigned int mod_time,
20027 unsigned int length))
20029 gdb_byte format_count, formati;
20030 ULONGEST data_count, datai;
20031 const gdb_byte *buf = *bufp;
20032 const gdb_byte *format_header_data;
20033 unsigned int bytes_read;
20035 format_count = read_1_byte (abfd, buf);
20037 format_header_data = buf;
20038 for (formati = 0; formati < format_count; formati++)
20040 read_unsigned_leb128 (abfd, buf, &bytes_read);
20042 read_unsigned_leb128 (abfd, buf, &bytes_read);
20046 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20048 for (datai = 0; datai < data_count; datai++)
20050 const gdb_byte *format = format_header_data;
20051 struct file_entry fe;
20053 for (formati = 0; formati < format_count; formati++)
20055 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20056 format += bytes_read;
20058 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20059 format += bytes_read;
20061 gdb::optional<const char *> string;
20062 gdb::optional<unsigned int> uint;
20066 case DW_FORM_string:
20067 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20071 case DW_FORM_line_strp:
20072 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20079 case DW_FORM_data1:
20080 uint.emplace (read_1_byte (abfd, buf));
20084 case DW_FORM_data2:
20085 uint.emplace (read_2_bytes (abfd, buf));
20089 case DW_FORM_data4:
20090 uint.emplace (read_4_bytes (abfd, buf));
20094 case DW_FORM_data8:
20095 uint.emplace (read_8_bytes (abfd, buf));
20099 case DW_FORM_udata:
20100 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20104 case DW_FORM_block:
20105 /* It is valid only for DW_LNCT_timestamp which is ignored by
20110 switch (content_type)
20113 if (string.has_value ())
20116 case DW_LNCT_directory_index:
20117 if (uint.has_value ())
20118 fe.d_index = (dir_index) *uint;
20120 case DW_LNCT_timestamp:
20121 if (uint.has_value ())
20122 fe.mod_time = *uint;
20125 if (uint.has_value ())
20131 complaint (&symfile_complaints,
20132 _("Unknown format content type %s"),
20133 pulongest (content_type));
20137 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20143 /* Read the statement program header starting at OFFSET in
20144 .debug_line, or .debug_line.dwo. Return a pointer
20145 to a struct line_header, allocated using xmalloc.
20146 Returns NULL if there is a problem reading the header, e.g., if it
20147 has a version we don't understand.
20149 NOTE: the strings in the include directory and file name tables of
20150 the returned object point into the dwarf line section buffer,
20151 and must not be freed. */
20153 static line_header_up
20154 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20156 const gdb_byte *line_ptr;
20157 unsigned int bytes_read, offset_size;
20159 const char *cur_dir, *cur_file;
20160 struct dwarf2_section_info *section;
20162 struct dwarf2_per_objfile *dwarf2_per_objfile
20163 = cu->per_cu->dwarf2_per_objfile;
20165 section = get_debug_line_section (cu);
20166 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20167 if (section->buffer == NULL)
20169 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20170 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20172 complaint (&symfile_complaints, _("missing .debug_line section"));
20176 /* We can't do this until we know the section is non-empty.
20177 Only then do we know we have such a section. */
20178 abfd = get_section_bfd_owner (section);
20180 /* Make sure that at least there's room for the total_length field.
20181 That could be 12 bytes long, but we're just going to fudge that. */
20182 if (to_underlying (sect_off) + 4 >= section->size)
20184 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20188 line_header_up lh (new line_header ());
20190 lh->sect_off = sect_off;
20191 lh->offset_in_dwz = cu->per_cu->is_dwz;
20193 line_ptr = section->buffer + to_underlying (sect_off);
20195 /* Read in the header. */
20197 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20198 &bytes_read, &offset_size);
20199 line_ptr += bytes_read;
20200 if (line_ptr + lh->total_length > (section->buffer + section->size))
20202 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20205 lh->statement_program_end = line_ptr + lh->total_length;
20206 lh->version = read_2_bytes (abfd, line_ptr);
20208 if (lh->version > 5)
20210 /* This is a version we don't understand. The format could have
20211 changed in ways we don't handle properly so just punt. */
20212 complaint (&symfile_complaints,
20213 _("unsupported version in .debug_line section"));
20216 if (lh->version >= 5)
20218 gdb_byte segment_selector_size;
20220 /* Skip address size. */
20221 read_1_byte (abfd, line_ptr);
20224 segment_selector_size = read_1_byte (abfd, line_ptr);
20226 if (segment_selector_size != 0)
20228 complaint (&symfile_complaints,
20229 _("unsupported segment selector size %u "
20230 "in .debug_line section"),
20231 segment_selector_size);
20235 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20236 line_ptr += offset_size;
20237 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20239 if (lh->version >= 4)
20241 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20245 lh->maximum_ops_per_instruction = 1;
20247 if (lh->maximum_ops_per_instruction == 0)
20249 lh->maximum_ops_per_instruction = 1;
20250 complaint (&symfile_complaints,
20251 _("invalid maximum_ops_per_instruction "
20252 "in `.debug_line' section"));
20255 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20257 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20259 lh->line_range = read_1_byte (abfd, line_ptr);
20261 lh->opcode_base = read_1_byte (abfd, line_ptr);
20263 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20265 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20266 for (i = 1; i < lh->opcode_base; ++i)
20268 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20272 if (lh->version >= 5)
20274 /* Read directory table. */
20275 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20277 [] (struct line_header *lh, const char *name,
20278 dir_index d_index, unsigned int mod_time,
20279 unsigned int length)
20281 lh->add_include_dir (name);
20284 /* Read file name table. */
20285 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20287 [] (struct line_header *lh, const char *name,
20288 dir_index d_index, unsigned int mod_time,
20289 unsigned int length)
20291 lh->add_file_name (name, d_index, mod_time, length);
20296 /* Read directory table. */
20297 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20299 line_ptr += bytes_read;
20300 lh->add_include_dir (cur_dir);
20302 line_ptr += bytes_read;
20304 /* Read file name table. */
20305 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20307 unsigned int mod_time, length;
20310 line_ptr += bytes_read;
20311 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20312 line_ptr += bytes_read;
20313 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20314 line_ptr += bytes_read;
20315 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20316 line_ptr += bytes_read;
20318 lh->add_file_name (cur_file, d_index, mod_time, length);
20320 line_ptr += bytes_read;
20322 lh->statement_program_start = line_ptr;
20324 if (line_ptr > (section->buffer + section->size))
20325 complaint (&symfile_complaints,
20326 _("line number info header doesn't "
20327 "fit in `.debug_line' section"));
20332 /* Subroutine of dwarf_decode_lines to simplify it.
20333 Return the file name of the psymtab for included file FILE_INDEX
20334 in line header LH of PST.
20335 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20336 If space for the result is malloc'd, *NAME_HOLDER will be set.
20337 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20339 static const char *
20340 psymtab_include_file_name (const struct line_header *lh, int file_index,
20341 const struct partial_symtab *pst,
20342 const char *comp_dir,
20343 gdb::unique_xmalloc_ptr<char> *name_holder)
20345 const file_entry &fe = lh->file_names[file_index];
20346 const char *include_name = fe.name;
20347 const char *include_name_to_compare = include_name;
20348 const char *pst_filename;
20351 const char *dir_name = fe.include_dir (lh);
20353 gdb::unique_xmalloc_ptr<char> hold_compare;
20354 if (!IS_ABSOLUTE_PATH (include_name)
20355 && (dir_name != NULL || comp_dir != NULL))
20357 /* Avoid creating a duplicate psymtab for PST.
20358 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20359 Before we do the comparison, however, we need to account
20360 for DIR_NAME and COMP_DIR.
20361 First prepend dir_name (if non-NULL). If we still don't
20362 have an absolute path prepend comp_dir (if non-NULL).
20363 However, the directory we record in the include-file's
20364 psymtab does not contain COMP_DIR (to match the
20365 corresponding symtab(s)).
20370 bash$ gcc -g ./hello.c
20371 include_name = "hello.c"
20373 DW_AT_comp_dir = comp_dir = "/tmp"
20374 DW_AT_name = "./hello.c"
20378 if (dir_name != NULL)
20380 name_holder->reset (concat (dir_name, SLASH_STRING,
20381 include_name, (char *) NULL));
20382 include_name = name_holder->get ();
20383 include_name_to_compare = include_name;
20385 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20387 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20388 include_name, (char *) NULL));
20389 include_name_to_compare = hold_compare.get ();
20393 pst_filename = pst->filename;
20394 gdb::unique_xmalloc_ptr<char> copied_name;
20395 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20397 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20398 pst_filename, (char *) NULL));
20399 pst_filename = copied_name.get ();
20402 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20406 return include_name;
20409 /* State machine to track the state of the line number program. */
20411 class lnp_state_machine
20414 /* Initialize a machine state for the start of a line number
20416 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20418 file_entry *current_file ()
20420 /* lh->file_names is 0-based, but the file name numbers in the
20421 statement program are 1-based. */
20422 return m_line_header->file_name_at (m_file);
20425 /* Record the line in the state machine. END_SEQUENCE is true if
20426 we're processing the end of a sequence. */
20427 void record_line (bool end_sequence);
20429 /* Check address and if invalid nop-out the rest of the lines in this
20431 void check_line_address (struct dwarf2_cu *cu,
20432 const gdb_byte *line_ptr,
20433 CORE_ADDR lowpc, CORE_ADDR address);
20435 void handle_set_discriminator (unsigned int discriminator)
20437 m_discriminator = discriminator;
20438 m_line_has_non_zero_discriminator |= discriminator != 0;
20441 /* Handle DW_LNE_set_address. */
20442 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20445 address += baseaddr;
20446 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20449 /* Handle DW_LNS_advance_pc. */
20450 void handle_advance_pc (CORE_ADDR adjust);
20452 /* Handle a special opcode. */
20453 void handle_special_opcode (unsigned char op_code);
20455 /* Handle DW_LNS_advance_line. */
20456 void handle_advance_line (int line_delta)
20458 advance_line (line_delta);
20461 /* Handle DW_LNS_set_file. */
20462 void handle_set_file (file_name_index file);
20464 /* Handle DW_LNS_negate_stmt. */
20465 void handle_negate_stmt ()
20467 m_is_stmt = !m_is_stmt;
20470 /* Handle DW_LNS_const_add_pc. */
20471 void handle_const_add_pc ();
20473 /* Handle DW_LNS_fixed_advance_pc. */
20474 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20476 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20480 /* Handle DW_LNS_copy. */
20481 void handle_copy ()
20483 record_line (false);
20484 m_discriminator = 0;
20487 /* Handle DW_LNE_end_sequence. */
20488 void handle_end_sequence ()
20490 m_record_line_callback = ::record_line;
20494 /* Advance the line by LINE_DELTA. */
20495 void advance_line (int line_delta)
20497 m_line += line_delta;
20499 if (line_delta != 0)
20500 m_line_has_non_zero_discriminator = m_discriminator != 0;
20503 gdbarch *m_gdbarch;
20505 /* True if we're recording lines.
20506 Otherwise we're building partial symtabs and are just interested in
20507 finding include files mentioned by the line number program. */
20508 bool m_record_lines_p;
20510 /* The line number header. */
20511 line_header *m_line_header;
20513 /* These are part of the standard DWARF line number state machine,
20514 and initialized according to the DWARF spec. */
20516 unsigned char m_op_index = 0;
20517 /* The line table index (1-based) of the current file. */
20518 file_name_index m_file = (file_name_index) 1;
20519 unsigned int m_line = 1;
20521 /* These are initialized in the constructor. */
20523 CORE_ADDR m_address;
20525 unsigned int m_discriminator;
20527 /* Additional bits of state we need to track. */
20529 /* The last file that we called dwarf2_start_subfile for.
20530 This is only used for TLLs. */
20531 unsigned int m_last_file = 0;
20532 /* The last file a line number was recorded for. */
20533 struct subfile *m_last_subfile = NULL;
20535 /* The function to call to record a line. */
20536 record_line_ftype *m_record_line_callback = NULL;
20538 /* The last line number that was recorded, used to coalesce
20539 consecutive entries for the same line. This can happen, for
20540 example, when discriminators are present. PR 17276. */
20541 unsigned int m_last_line = 0;
20542 bool m_line_has_non_zero_discriminator = false;
20546 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20548 CORE_ADDR addr_adj = (((m_op_index + adjust)
20549 / m_line_header->maximum_ops_per_instruction)
20550 * m_line_header->minimum_instruction_length);
20551 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20552 m_op_index = ((m_op_index + adjust)
20553 % m_line_header->maximum_ops_per_instruction);
20557 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20559 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20560 CORE_ADDR addr_adj = (((m_op_index
20561 + (adj_opcode / m_line_header->line_range))
20562 / m_line_header->maximum_ops_per_instruction)
20563 * m_line_header->minimum_instruction_length);
20564 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20565 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20566 % m_line_header->maximum_ops_per_instruction);
20568 int line_delta = (m_line_header->line_base
20569 + (adj_opcode % m_line_header->line_range));
20570 advance_line (line_delta);
20571 record_line (false);
20572 m_discriminator = 0;
20576 lnp_state_machine::handle_set_file (file_name_index file)
20580 const file_entry *fe = current_file ();
20582 dwarf2_debug_line_missing_file_complaint ();
20583 else if (m_record_lines_p)
20585 const char *dir = fe->include_dir (m_line_header);
20587 m_last_subfile = current_subfile;
20588 m_line_has_non_zero_discriminator = m_discriminator != 0;
20589 dwarf2_start_subfile (fe->name, dir);
20594 lnp_state_machine::handle_const_add_pc ()
20597 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20600 = (((m_op_index + adjust)
20601 / m_line_header->maximum_ops_per_instruction)
20602 * m_line_header->minimum_instruction_length);
20604 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20605 m_op_index = ((m_op_index + adjust)
20606 % m_line_header->maximum_ops_per_instruction);
20609 /* Ignore this record_line request. */
20612 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20617 /* Return non-zero if we should add LINE to the line number table.
20618 LINE is the line to add, LAST_LINE is the last line that was added,
20619 LAST_SUBFILE is the subfile for LAST_LINE.
20620 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20621 had a non-zero discriminator.
20623 We have to be careful in the presence of discriminators.
20624 E.g., for this line:
20626 for (i = 0; i < 100000; i++);
20628 clang can emit four line number entries for that one line,
20629 each with a different discriminator.
20630 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20632 However, we want gdb to coalesce all four entries into one.
20633 Otherwise the user could stepi into the middle of the line and
20634 gdb would get confused about whether the pc really was in the
20635 middle of the line.
20637 Things are further complicated by the fact that two consecutive
20638 line number entries for the same line is a heuristic used by gcc
20639 to denote the end of the prologue. So we can't just discard duplicate
20640 entries, we have to be selective about it. The heuristic we use is
20641 that we only collapse consecutive entries for the same line if at least
20642 one of those entries has a non-zero discriminator. PR 17276.
20644 Note: Addresses in the line number state machine can never go backwards
20645 within one sequence, thus this coalescing is ok. */
20648 dwarf_record_line_p (unsigned int line, unsigned int last_line,
20649 int line_has_non_zero_discriminator,
20650 struct subfile *last_subfile)
20652 if (current_subfile != last_subfile)
20654 if (line != last_line)
20656 /* Same line for the same file that we've seen already.
20657 As a last check, for pr 17276, only record the line if the line
20658 has never had a non-zero discriminator. */
20659 if (!line_has_non_zero_discriminator)
20664 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20665 in the line table of subfile SUBFILE. */
20668 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20669 unsigned int line, CORE_ADDR address,
20670 record_line_ftype p_record_line)
20672 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20674 if (dwarf_line_debug)
20676 fprintf_unfiltered (gdb_stdlog,
20677 "Recording line %u, file %s, address %s\n",
20678 line, lbasename (subfile->name),
20679 paddress (gdbarch, address));
20682 (*p_record_line) (subfile, line, addr);
20685 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20686 Mark the end of a set of line number records.
20687 The arguments are the same as for dwarf_record_line_1.
20688 If SUBFILE is NULL the request is ignored. */
20691 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20692 CORE_ADDR address, record_line_ftype p_record_line)
20694 if (subfile == NULL)
20697 if (dwarf_line_debug)
20699 fprintf_unfiltered (gdb_stdlog,
20700 "Finishing current line, file %s, address %s\n",
20701 lbasename (subfile->name),
20702 paddress (gdbarch, address));
20705 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20709 lnp_state_machine::record_line (bool end_sequence)
20711 if (dwarf_line_debug)
20713 fprintf_unfiltered (gdb_stdlog,
20714 "Processing actual line %u: file %u,"
20715 " address %s, is_stmt %u, discrim %u\n",
20716 m_line, to_underlying (m_file),
20717 paddress (m_gdbarch, m_address),
20718 m_is_stmt, m_discriminator);
20721 file_entry *fe = current_file ();
20724 dwarf2_debug_line_missing_file_complaint ();
20725 /* For now we ignore lines not starting on an instruction boundary.
20726 But not when processing end_sequence for compatibility with the
20727 previous version of the code. */
20728 else if (m_op_index == 0 || end_sequence)
20730 fe->included_p = 1;
20731 if (m_record_lines_p && m_is_stmt)
20733 if (m_last_subfile != current_subfile || end_sequence)
20735 dwarf_finish_line (m_gdbarch, m_last_subfile,
20736 m_address, m_record_line_callback);
20741 if (dwarf_record_line_p (m_line, m_last_line,
20742 m_line_has_non_zero_discriminator,
20745 dwarf_record_line_1 (m_gdbarch, current_subfile,
20747 m_record_line_callback);
20749 m_last_subfile = current_subfile;
20750 m_last_line = m_line;
20756 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20757 bool record_lines_p)
20760 m_record_lines_p = record_lines_p;
20761 m_line_header = lh;
20763 m_record_line_callback = ::record_line;
20765 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20766 was a line entry for it so that the backend has a chance to adjust it
20767 and also record it in case it needs it. This is currently used by MIPS
20768 code, cf. `mips_adjust_dwarf2_line'. */
20769 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20770 m_is_stmt = lh->default_is_stmt;
20771 m_discriminator = 0;
20775 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20776 const gdb_byte *line_ptr,
20777 CORE_ADDR lowpc, CORE_ADDR address)
20779 /* If address < lowpc then it's not a usable value, it's outside the
20780 pc range of the CU. However, we restrict the test to only address
20781 values of zero to preserve GDB's previous behaviour which is to
20782 handle the specific case of a function being GC'd by the linker. */
20784 if (address == 0 && address < lowpc)
20786 /* This line table is for a function which has been
20787 GCd by the linker. Ignore it. PR gdb/12528 */
20789 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20790 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20792 complaint (&symfile_complaints,
20793 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20794 line_offset, objfile_name (objfile));
20795 m_record_line_callback = noop_record_line;
20796 /* Note: record_line_callback is left as noop_record_line until
20797 we see DW_LNE_end_sequence. */
20801 /* Subroutine of dwarf_decode_lines to simplify it.
20802 Process the line number information in LH.
20803 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20804 program in order to set included_p for every referenced header. */
20807 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20808 const int decode_for_pst_p, CORE_ADDR lowpc)
20810 const gdb_byte *line_ptr, *extended_end;
20811 const gdb_byte *line_end;
20812 unsigned int bytes_read, extended_len;
20813 unsigned char op_code, extended_op;
20814 CORE_ADDR baseaddr;
20815 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20816 bfd *abfd = objfile->obfd;
20817 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20818 /* True if we're recording line info (as opposed to building partial
20819 symtabs and just interested in finding include files mentioned by
20820 the line number program). */
20821 bool record_lines_p = !decode_for_pst_p;
20823 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20825 line_ptr = lh->statement_program_start;
20826 line_end = lh->statement_program_end;
20828 /* Read the statement sequences until there's nothing left. */
20829 while (line_ptr < line_end)
20831 /* The DWARF line number program state machine. Reset the state
20832 machine at the start of each sequence. */
20833 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20834 bool end_sequence = false;
20836 if (record_lines_p)
20838 /* Start a subfile for the current file of the state
20840 const file_entry *fe = state_machine.current_file ();
20843 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
20846 /* Decode the table. */
20847 while (line_ptr < line_end && !end_sequence)
20849 op_code = read_1_byte (abfd, line_ptr);
20852 if (op_code >= lh->opcode_base)
20854 /* Special opcode. */
20855 state_machine.handle_special_opcode (op_code);
20857 else switch (op_code)
20859 case DW_LNS_extended_op:
20860 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20862 line_ptr += bytes_read;
20863 extended_end = line_ptr + extended_len;
20864 extended_op = read_1_byte (abfd, line_ptr);
20866 switch (extended_op)
20868 case DW_LNE_end_sequence:
20869 state_machine.handle_end_sequence ();
20870 end_sequence = true;
20872 case DW_LNE_set_address:
20875 = read_address (abfd, line_ptr, cu, &bytes_read);
20876 line_ptr += bytes_read;
20878 state_machine.check_line_address (cu, line_ptr,
20880 state_machine.handle_set_address (baseaddr, address);
20883 case DW_LNE_define_file:
20885 const char *cur_file;
20886 unsigned int mod_time, length;
20889 cur_file = read_direct_string (abfd, line_ptr,
20891 line_ptr += bytes_read;
20892 dindex = (dir_index)
20893 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20894 line_ptr += bytes_read;
20896 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20897 line_ptr += bytes_read;
20899 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20900 line_ptr += bytes_read;
20901 lh->add_file_name (cur_file, dindex, mod_time, length);
20904 case DW_LNE_set_discriminator:
20906 /* The discriminator is not interesting to the
20907 debugger; just ignore it. We still need to
20908 check its value though:
20909 if there are consecutive entries for the same
20910 (non-prologue) line we want to coalesce them.
20913 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20914 line_ptr += bytes_read;
20916 state_machine.handle_set_discriminator (discr);
20920 complaint (&symfile_complaints,
20921 _("mangled .debug_line section"));
20924 /* Make sure that we parsed the extended op correctly. If e.g.
20925 we expected a different address size than the producer used,
20926 we may have read the wrong number of bytes. */
20927 if (line_ptr != extended_end)
20929 complaint (&symfile_complaints,
20930 _("mangled .debug_line section"));
20935 state_machine.handle_copy ();
20937 case DW_LNS_advance_pc:
20940 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20941 line_ptr += bytes_read;
20943 state_machine.handle_advance_pc (adjust);
20946 case DW_LNS_advance_line:
20949 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20950 line_ptr += bytes_read;
20952 state_machine.handle_advance_line (line_delta);
20955 case DW_LNS_set_file:
20957 file_name_index file
20958 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20960 line_ptr += bytes_read;
20962 state_machine.handle_set_file (file);
20965 case DW_LNS_set_column:
20966 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20967 line_ptr += bytes_read;
20969 case DW_LNS_negate_stmt:
20970 state_machine.handle_negate_stmt ();
20972 case DW_LNS_set_basic_block:
20974 /* Add to the address register of the state machine the
20975 address increment value corresponding to special opcode
20976 255. I.e., this value is scaled by the minimum
20977 instruction length since special opcode 255 would have
20978 scaled the increment. */
20979 case DW_LNS_const_add_pc:
20980 state_machine.handle_const_add_pc ();
20982 case DW_LNS_fixed_advance_pc:
20984 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20987 state_machine.handle_fixed_advance_pc (addr_adj);
20992 /* Unknown standard opcode, ignore it. */
20995 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20997 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20998 line_ptr += bytes_read;
21005 dwarf2_debug_line_missing_end_sequence_complaint ();
21007 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21008 in which case we still finish recording the last line). */
21009 state_machine.record_line (true);
21013 /* Decode the Line Number Program (LNP) for the given line_header
21014 structure and CU. The actual information extracted and the type
21015 of structures created from the LNP depends on the value of PST.
21017 1. If PST is NULL, then this procedure uses the data from the program
21018 to create all necessary symbol tables, and their linetables.
21020 2. If PST is not NULL, this procedure reads the program to determine
21021 the list of files included by the unit represented by PST, and
21022 builds all the associated partial symbol tables.
21024 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21025 It is used for relative paths in the line table.
21026 NOTE: When processing partial symtabs (pst != NULL),
21027 comp_dir == pst->dirname.
21029 NOTE: It is important that psymtabs have the same file name (via strcmp)
21030 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21031 symtab we don't use it in the name of the psymtabs we create.
21032 E.g. expand_line_sal requires this when finding psymtabs to expand.
21033 A good testcase for this is mb-inline.exp.
21035 LOWPC is the lowest address in CU (or 0 if not known).
21037 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21038 for its PC<->lines mapping information. Otherwise only the filename
21039 table is read in. */
21042 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21043 struct dwarf2_cu *cu, struct partial_symtab *pst,
21044 CORE_ADDR lowpc, int decode_mapping)
21046 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21047 const int decode_for_pst_p = (pst != NULL);
21049 if (decode_mapping)
21050 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21052 if (decode_for_pst_p)
21056 /* Now that we're done scanning the Line Header Program, we can
21057 create the psymtab of each included file. */
21058 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21059 if (lh->file_names[file_index].included_p == 1)
21061 gdb::unique_xmalloc_ptr<char> name_holder;
21062 const char *include_name =
21063 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21065 if (include_name != NULL)
21066 dwarf2_create_include_psymtab (include_name, pst, objfile);
21071 /* Make sure a symtab is created for every file, even files
21072 which contain only variables (i.e. no code with associated
21074 struct compunit_symtab *cust = buildsym_compunit_symtab ();
21077 for (i = 0; i < lh->file_names.size (); i++)
21079 file_entry &fe = lh->file_names[i];
21081 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
21083 if (current_subfile->symtab == NULL)
21085 current_subfile->symtab
21086 = allocate_symtab (cust, current_subfile->name);
21088 fe.symtab = current_subfile->symtab;
21093 /* Start a subfile for DWARF. FILENAME is the name of the file and
21094 DIRNAME the name of the source directory which contains FILENAME
21095 or NULL if not known.
21096 This routine tries to keep line numbers from identical absolute and
21097 relative file names in a common subfile.
21099 Using the `list' example from the GDB testsuite, which resides in
21100 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21101 of /srcdir/list0.c yields the following debugging information for list0.c:
21103 DW_AT_name: /srcdir/list0.c
21104 DW_AT_comp_dir: /compdir
21105 files.files[0].name: list0.h
21106 files.files[0].dir: /srcdir
21107 files.files[1].name: list0.c
21108 files.files[1].dir: /srcdir
21110 The line number information for list0.c has to end up in a single
21111 subfile, so that `break /srcdir/list0.c:1' works as expected.
21112 start_subfile will ensure that this happens provided that we pass the
21113 concatenation of files.files[1].dir and files.files[1].name as the
21117 dwarf2_start_subfile (const char *filename, const char *dirname)
21121 /* In order not to lose the line information directory,
21122 we concatenate it to the filename when it makes sense.
21123 Note that the Dwarf3 standard says (speaking of filenames in line
21124 information): ``The directory index is ignored for file names
21125 that represent full path names''. Thus ignoring dirname in the
21126 `else' branch below isn't an issue. */
21128 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21130 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21134 start_subfile (filename);
21140 /* Start a symtab for DWARF.
21141 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21143 static struct compunit_symtab *
21144 dwarf2_start_symtab (struct dwarf2_cu *cu,
21145 const char *name, const char *comp_dir, CORE_ADDR low_pc)
21147 struct compunit_symtab *cust
21148 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21149 low_pc, cu->language);
21151 record_debugformat ("DWARF 2");
21152 record_producer (cu->producer);
21154 /* We assume that we're processing GCC output. */
21155 processing_gcc_compilation = 2;
21157 cu->processing_has_namespace_info = 0;
21163 var_decode_location (struct attribute *attr, struct symbol *sym,
21164 struct dwarf2_cu *cu)
21166 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21167 struct comp_unit_head *cu_header = &cu->header;
21169 /* NOTE drow/2003-01-30: There used to be a comment and some special
21170 code here to turn a symbol with DW_AT_external and a
21171 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21172 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21173 with some versions of binutils) where shared libraries could have
21174 relocations against symbols in their debug information - the
21175 minimal symbol would have the right address, but the debug info
21176 would not. It's no longer necessary, because we will explicitly
21177 apply relocations when we read in the debug information now. */
21179 /* A DW_AT_location attribute with no contents indicates that a
21180 variable has been optimized away. */
21181 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21183 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21187 /* Handle one degenerate form of location expression specially, to
21188 preserve GDB's previous behavior when section offsets are
21189 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21190 then mark this symbol as LOC_STATIC. */
21192 if (attr_form_is_block (attr)
21193 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21194 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21195 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21196 && (DW_BLOCK (attr)->size
21197 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21199 unsigned int dummy;
21201 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21202 SYMBOL_VALUE_ADDRESS (sym) =
21203 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21205 SYMBOL_VALUE_ADDRESS (sym) =
21206 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21207 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21208 fixup_symbol_section (sym, objfile);
21209 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21210 SYMBOL_SECTION (sym));
21214 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21215 expression evaluator, and use LOC_COMPUTED only when necessary
21216 (i.e. when the value of a register or memory location is
21217 referenced, or a thread-local block, etc.). Then again, it might
21218 not be worthwhile. I'm assuming that it isn't unless performance
21219 or memory numbers show me otherwise. */
21221 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21223 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21224 cu->has_loclist = 1;
21227 /* Given a pointer to a DWARF information entry, figure out if we need
21228 to make a symbol table entry for it, and if so, create a new entry
21229 and return a pointer to it.
21230 If TYPE is NULL, determine symbol type from the die, otherwise
21231 used the passed type.
21232 If SPACE is not NULL, use it to hold the new symbol. If it is
21233 NULL, allocate a new symbol on the objfile's obstack. */
21235 static struct symbol *
21236 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21237 struct symbol *space)
21239 struct dwarf2_per_objfile *dwarf2_per_objfile
21240 = cu->per_cu->dwarf2_per_objfile;
21241 struct objfile *objfile = dwarf2_per_objfile->objfile;
21242 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21243 struct symbol *sym = NULL;
21245 struct attribute *attr = NULL;
21246 struct attribute *attr2 = NULL;
21247 CORE_ADDR baseaddr;
21248 struct pending **list_to_add = NULL;
21250 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21252 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21254 name = dwarf2_name (die, cu);
21257 const char *linkagename;
21258 int suppress_add = 0;
21263 sym = allocate_symbol (objfile);
21264 OBJSTAT (objfile, n_syms++);
21266 /* Cache this symbol's name and the name's demangled form (if any). */
21267 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21268 linkagename = dwarf2_physname (name, die, cu);
21269 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21271 /* Fortran does not have mangling standard and the mangling does differ
21272 between gfortran, iFort etc. */
21273 if (cu->language == language_fortran
21274 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21275 symbol_set_demangled_name (&(sym->ginfo),
21276 dwarf2_full_name (name, die, cu),
21279 /* Default assumptions.
21280 Use the passed type or decode it from the die. */
21281 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21282 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21284 SYMBOL_TYPE (sym) = type;
21286 SYMBOL_TYPE (sym) = die_type (die, cu);
21287 attr = dwarf2_attr (die,
21288 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21292 SYMBOL_LINE (sym) = DW_UNSND (attr);
21295 attr = dwarf2_attr (die,
21296 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21300 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21301 struct file_entry *fe;
21303 if (cu->line_header != NULL)
21304 fe = cu->line_header->file_name_at (file_index);
21309 complaint (&symfile_complaints,
21310 _("file index out of range"));
21312 symbol_set_symtab (sym, fe->symtab);
21318 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21323 addr = attr_value_as_address (attr);
21324 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21325 SYMBOL_VALUE_ADDRESS (sym) = addr;
21327 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21328 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21329 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21330 add_symbol_to_list (sym, cu->list_in_scope);
21332 case DW_TAG_subprogram:
21333 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21335 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21336 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21337 if ((attr2 && (DW_UNSND (attr2) != 0))
21338 || cu->language == language_ada)
21340 /* Subprograms marked external are stored as a global symbol.
21341 Ada subprograms, whether marked external or not, are always
21342 stored as a global symbol, because we want to be able to
21343 access them globally. For instance, we want to be able
21344 to break on a nested subprogram without having to
21345 specify the context. */
21346 list_to_add = &global_symbols;
21350 list_to_add = cu->list_in_scope;
21353 case DW_TAG_inlined_subroutine:
21354 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21356 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21357 SYMBOL_INLINED (sym) = 1;
21358 list_to_add = cu->list_in_scope;
21360 case DW_TAG_template_value_param:
21362 /* Fall through. */
21363 case DW_TAG_constant:
21364 case DW_TAG_variable:
21365 case DW_TAG_member:
21366 /* Compilation with minimal debug info may result in
21367 variables with missing type entries. Change the
21368 misleading `void' type to something sensible. */
21369 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21370 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21372 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21373 /* In the case of DW_TAG_member, we should only be called for
21374 static const members. */
21375 if (die->tag == DW_TAG_member)
21377 /* dwarf2_add_field uses die_is_declaration,
21378 so we do the same. */
21379 gdb_assert (die_is_declaration (die, cu));
21384 dwarf2_const_value (attr, sym, cu);
21385 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21388 if (attr2 && (DW_UNSND (attr2) != 0))
21389 list_to_add = &global_symbols;
21391 list_to_add = cu->list_in_scope;
21395 attr = dwarf2_attr (die, DW_AT_location, cu);
21398 var_decode_location (attr, sym, cu);
21399 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21401 /* Fortran explicitly imports any global symbols to the local
21402 scope by DW_TAG_common_block. */
21403 if (cu->language == language_fortran && die->parent
21404 && die->parent->tag == DW_TAG_common_block)
21407 if (SYMBOL_CLASS (sym) == LOC_STATIC
21408 && SYMBOL_VALUE_ADDRESS (sym) == 0
21409 && !dwarf2_per_objfile->has_section_at_zero)
21411 /* When a static variable is eliminated by the linker,
21412 the corresponding debug information is not stripped
21413 out, but the variable address is set to null;
21414 do not add such variables into symbol table. */
21416 else if (attr2 && (DW_UNSND (attr2) != 0))
21418 /* Workaround gfortran PR debug/40040 - it uses
21419 DW_AT_location for variables in -fPIC libraries which may
21420 get overriden by other libraries/executable and get
21421 a different address. Resolve it by the minimal symbol
21422 which may come from inferior's executable using copy
21423 relocation. Make this workaround only for gfortran as for
21424 other compilers GDB cannot guess the minimal symbol
21425 Fortran mangling kind. */
21426 if (cu->language == language_fortran && die->parent
21427 && die->parent->tag == DW_TAG_module
21429 && startswith (cu->producer, "GNU Fortran"))
21430 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21432 /* A variable with DW_AT_external is never static,
21433 but it may be block-scoped. */
21434 list_to_add = (cu->list_in_scope == &file_symbols
21435 ? &global_symbols : cu->list_in_scope);
21438 list_to_add = cu->list_in_scope;
21442 /* We do not know the address of this symbol.
21443 If it is an external symbol and we have type information
21444 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21445 The address of the variable will then be determined from
21446 the minimal symbol table whenever the variable is
21448 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21450 /* Fortran explicitly imports any global symbols to the local
21451 scope by DW_TAG_common_block. */
21452 if (cu->language == language_fortran && die->parent
21453 && die->parent->tag == DW_TAG_common_block)
21455 /* SYMBOL_CLASS doesn't matter here because
21456 read_common_block is going to reset it. */
21458 list_to_add = cu->list_in_scope;
21460 else if (attr2 && (DW_UNSND (attr2) != 0)
21461 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21463 /* A variable with DW_AT_external is never static, but it
21464 may be block-scoped. */
21465 list_to_add = (cu->list_in_scope == &file_symbols
21466 ? &global_symbols : cu->list_in_scope);
21468 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21470 else if (!die_is_declaration (die, cu))
21472 /* Use the default LOC_OPTIMIZED_OUT class. */
21473 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21475 list_to_add = cu->list_in_scope;
21479 case DW_TAG_formal_parameter:
21480 /* If we are inside a function, mark this as an argument. If
21481 not, we might be looking at an argument to an inlined function
21482 when we do not have enough information to show inlined frames;
21483 pretend it's a local variable in that case so that the user can
21485 if (context_stack_depth > 0
21486 && context_stack[context_stack_depth - 1].name != NULL)
21487 SYMBOL_IS_ARGUMENT (sym) = 1;
21488 attr = dwarf2_attr (die, DW_AT_location, cu);
21491 var_decode_location (attr, sym, cu);
21493 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21496 dwarf2_const_value (attr, sym, cu);
21499 list_to_add = cu->list_in_scope;
21501 case DW_TAG_unspecified_parameters:
21502 /* From varargs functions; gdb doesn't seem to have any
21503 interest in this information, so just ignore it for now.
21506 case DW_TAG_template_type_param:
21508 /* Fall through. */
21509 case DW_TAG_class_type:
21510 case DW_TAG_interface_type:
21511 case DW_TAG_structure_type:
21512 case DW_TAG_union_type:
21513 case DW_TAG_set_type:
21514 case DW_TAG_enumeration_type:
21515 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21516 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21519 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21520 really ever be static objects: otherwise, if you try
21521 to, say, break of a class's method and you're in a file
21522 which doesn't mention that class, it won't work unless
21523 the check for all static symbols in lookup_symbol_aux
21524 saves you. See the OtherFileClass tests in
21525 gdb.c++/namespace.exp. */
21529 list_to_add = (cu->list_in_scope == &file_symbols
21530 && cu->language == language_cplus
21531 ? &global_symbols : cu->list_in_scope);
21533 /* The semantics of C++ state that "struct foo {
21534 ... }" also defines a typedef for "foo". */
21535 if (cu->language == language_cplus
21536 || cu->language == language_ada
21537 || cu->language == language_d
21538 || cu->language == language_rust)
21540 /* The symbol's name is already allocated along
21541 with this objfile, so we don't need to
21542 duplicate it for the type. */
21543 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21544 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21549 case DW_TAG_typedef:
21550 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21551 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21552 list_to_add = cu->list_in_scope;
21554 case DW_TAG_base_type:
21555 case DW_TAG_subrange_type:
21556 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21557 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21558 list_to_add = cu->list_in_scope;
21560 case DW_TAG_enumerator:
21561 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21564 dwarf2_const_value (attr, sym, cu);
21567 /* NOTE: carlton/2003-11-10: See comment above in the
21568 DW_TAG_class_type, etc. block. */
21570 list_to_add = (cu->list_in_scope == &file_symbols
21571 && cu->language == language_cplus
21572 ? &global_symbols : cu->list_in_scope);
21575 case DW_TAG_imported_declaration:
21576 case DW_TAG_namespace:
21577 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21578 list_to_add = &global_symbols;
21580 case DW_TAG_module:
21581 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21582 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21583 list_to_add = &global_symbols;
21585 case DW_TAG_common_block:
21586 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21587 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21588 add_symbol_to_list (sym, cu->list_in_scope);
21591 /* Not a tag we recognize. Hopefully we aren't processing
21592 trash data, but since we must specifically ignore things
21593 we don't recognize, there is nothing else we should do at
21595 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
21596 dwarf_tag_name (die->tag));
21602 sym->hash_next = objfile->template_symbols;
21603 objfile->template_symbols = sym;
21604 list_to_add = NULL;
21607 if (list_to_add != NULL)
21608 add_symbol_to_list (sym, list_to_add);
21610 /* For the benefit of old versions of GCC, check for anonymous
21611 namespaces based on the demangled name. */
21612 if (!cu->processing_has_namespace_info
21613 && cu->language == language_cplus)
21614 cp_scan_for_anonymous_namespaces (sym, objfile);
21619 /* Given an attr with a DW_FORM_dataN value in host byte order,
21620 zero-extend it as appropriate for the symbol's type. The DWARF
21621 standard (v4) is not entirely clear about the meaning of using
21622 DW_FORM_dataN for a constant with a signed type, where the type is
21623 wider than the data. The conclusion of a discussion on the DWARF
21624 list was that this is unspecified. We choose to always zero-extend
21625 because that is the interpretation long in use by GCC. */
21628 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21629 struct dwarf2_cu *cu, LONGEST *value, int bits)
21631 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21632 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21633 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21634 LONGEST l = DW_UNSND (attr);
21636 if (bits < sizeof (*value) * 8)
21638 l &= ((LONGEST) 1 << bits) - 1;
21641 else if (bits == sizeof (*value) * 8)
21645 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21646 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21653 /* Read a constant value from an attribute. Either set *VALUE, or if
21654 the value does not fit in *VALUE, set *BYTES - either already
21655 allocated on the objfile obstack, or newly allocated on OBSTACK,
21656 or, set *BATON, if we translated the constant to a location
21660 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21661 const char *name, struct obstack *obstack,
21662 struct dwarf2_cu *cu,
21663 LONGEST *value, const gdb_byte **bytes,
21664 struct dwarf2_locexpr_baton **baton)
21666 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21667 struct comp_unit_head *cu_header = &cu->header;
21668 struct dwarf_block *blk;
21669 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21670 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21676 switch (attr->form)
21679 case DW_FORM_GNU_addr_index:
21683 if (TYPE_LENGTH (type) != cu_header->addr_size)
21684 dwarf2_const_value_length_mismatch_complaint (name,
21685 cu_header->addr_size,
21686 TYPE_LENGTH (type));
21687 /* Symbols of this form are reasonably rare, so we just
21688 piggyback on the existing location code rather than writing
21689 a new implementation of symbol_computed_ops. */
21690 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21691 (*baton)->per_cu = cu->per_cu;
21692 gdb_assert ((*baton)->per_cu);
21694 (*baton)->size = 2 + cu_header->addr_size;
21695 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21696 (*baton)->data = data;
21698 data[0] = DW_OP_addr;
21699 store_unsigned_integer (&data[1], cu_header->addr_size,
21700 byte_order, DW_ADDR (attr));
21701 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21704 case DW_FORM_string:
21706 case DW_FORM_GNU_str_index:
21707 case DW_FORM_GNU_strp_alt:
21708 /* DW_STRING is already allocated on the objfile obstack, point
21710 *bytes = (const gdb_byte *) DW_STRING (attr);
21712 case DW_FORM_block1:
21713 case DW_FORM_block2:
21714 case DW_FORM_block4:
21715 case DW_FORM_block:
21716 case DW_FORM_exprloc:
21717 case DW_FORM_data16:
21718 blk = DW_BLOCK (attr);
21719 if (TYPE_LENGTH (type) != blk->size)
21720 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21721 TYPE_LENGTH (type));
21722 *bytes = blk->data;
21725 /* The DW_AT_const_value attributes are supposed to carry the
21726 symbol's value "represented as it would be on the target
21727 architecture." By the time we get here, it's already been
21728 converted to host endianness, so we just need to sign- or
21729 zero-extend it as appropriate. */
21730 case DW_FORM_data1:
21731 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21733 case DW_FORM_data2:
21734 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21736 case DW_FORM_data4:
21737 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21739 case DW_FORM_data8:
21740 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21743 case DW_FORM_sdata:
21744 case DW_FORM_implicit_const:
21745 *value = DW_SND (attr);
21748 case DW_FORM_udata:
21749 *value = DW_UNSND (attr);
21753 complaint (&symfile_complaints,
21754 _("unsupported const value attribute form: '%s'"),
21755 dwarf_form_name (attr->form));
21762 /* Copy constant value from an attribute to a symbol. */
21765 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21766 struct dwarf2_cu *cu)
21768 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21770 const gdb_byte *bytes;
21771 struct dwarf2_locexpr_baton *baton;
21773 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21774 SYMBOL_PRINT_NAME (sym),
21775 &objfile->objfile_obstack, cu,
21776 &value, &bytes, &baton);
21780 SYMBOL_LOCATION_BATON (sym) = baton;
21781 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21783 else if (bytes != NULL)
21785 SYMBOL_VALUE_BYTES (sym) = bytes;
21786 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21790 SYMBOL_VALUE (sym) = value;
21791 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21795 /* Return the type of the die in question using its DW_AT_type attribute. */
21797 static struct type *
21798 die_type (struct die_info *die, struct dwarf2_cu *cu)
21800 struct attribute *type_attr;
21802 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21805 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21806 /* A missing DW_AT_type represents a void type. */
21807 return objfile_type (objfile)->builtin_void;
21810 return lookup_die_type (die, type_attr, cu);
21813 /* True iff CU's producer generates GNAT Ada auxiliary information
21814 that allows to find parallel types through that information instead
21815 of having to do expensive parallel lookups by type name. */
21818 need_gnat_info (struct dwarf2_cu *cu)
21820 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
21821 of GNAT produces this auxiliary information, without any indication
21822 that it is produced. Part of enhancing the FSF version of GNAT
21823 to produce that information will be to put in place an indicator
21824 that we can use in order to determine whether the descriptive type
21825 info is available or not. One suggestion that has been made is
21826 to use a new attribute, attached to the CU die. For now, assume
21827 that the descriptive type info is not available. */
21831 /* Return the auxiliary type of the die in question using its
21832 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21833 attribute is not present. */
21835 static struct type *
21836 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21838 struct attribute *type_attr;
21840 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21844 return lookup_die_type (die, type_attr, cu);
21847 /* If DIE has a descriptive_type attribute, then set the TYPE's
21848 descriptive type accordingly. */
21851 set_descriptive_type (struct type *type, struct die_info *die,
21852 struct dwarf2_cu *cu)
21854 struct type *descriptive_type = die_descriptive_type (die, cu);
21856 if (descriptive_type)
21858 ALLOCATE_GNAT_AUX_TYPE (type);
21859 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21863 /* Return the containing type of the die in question using its
21864 DW_AT_containing_type attribute. */
21866 static struct type *
21867 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21869 struct attribute *type_attr;
21870 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21872 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21874 error (_("Dwarf Error: Problem turning containing type into gdb type "
21875 "[in module %s]"), objfile_name (objfile));
21877 return lookup_die_type (die, type_attr, cu);
21880 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21882 static struct type *
21883 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21885 struct dwarf2_per_objfile *dwarf2_per_objfile
21886 = cu->per_cu->dwarf2_per_objfile;
21887 struct objfile *objfile = dwarf2_per_objfile->objfile;
21888 char *message, *saved;
21890 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
21891 objfile_name (objfile),
21892 to_underlying (cu->header.sect_off),
21893 to_underlying (die->sect_off));
21894 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21895 message, strlen (message));
21898 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21901 /* Look up the type of DIE in CU using its type attribute ATTR.
21902 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21903 DW_AT_containing_type.
21904 If there is no type substitute an error marker. */
21906 static struct type *
21907 lookup_die_type (struct die_info *die, const struct attribute *attr,
21908 struct dwarf2_cu *cu)
21910 struct dwarf2_per_objfile *dwarf2_per_objfile
21911 = cu->per_cu->dwarf2_per_objfile;
21912 struct objfile *objfile = dwarf2_per_objfile->objfile;
21913 struct type *this_type;
21915 gdb_assert (attr->name == DW_AT_type
21916 || attr->name == DW_AT_GNAT_descriptive_type
21917 || attr->name == DW_AT_containing_type);
21919 /* First see if we have it cached. */
21921 if (attr->form == DW_FORM_GNU_ref_alt)
21923 struct dwarf2_per_cu_data *per_cu;
21924 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21926 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21927 dwarf2_per_objfile);
21928 this_type = get_die_type_at_offset (sect_off, per_cu);
21930 else if (attr_form_is_ref (attr))
21932 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21934 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21936 else if (attr->form == DW_FORM_ref_sig8)
21938 ULONGEST signature = DW_SIGNATURE (attr);
21940 return get_signatured_type (die, signature, cu);
21944 complaint (&symfile_complaints,
21945 _("Dwarf Error: Bad type attribute %s in DIE"
21946 " at 0x%x [in module %s]"),
21947 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
21948 objfile_name (objfile));
21949 return build_error_marker_type (cu, die);
21952 /* If not cached we need to read it in. */
21954 if (this_type == NULL)
21956 struct die_info *type_die = NULL;
21957 struct dwarf2_cu *type_cu = cu;
21959 if (attr_form_is_ref (attr))
21960 type_die = follow_die_ref (die, attr, &type_cu);
21961 if (type_die == NULL)
21962 return build_error_marker_type (cu, die);
21963 /* If we find the type now, it's probably because the type came
21964 from an inter-CU reference and the type's CU got expanded before
21966 this_type = read_type_die (type_die, type_cu);
21969 /* If we still don't have a type use an error marker. */
21971 if (this_type == NULL)
21972 return build_error_marker_type (cu, die);
21977 /* Return the type in DIE, CU.
21978 Returns NULL for invalid types.
21980 This first does a lookup in die_type_hash,
21981 and only reads the die in if necessary.
21983 NOTE: This can be called when reading in partial or full symbols. */
21985 static struct type *
21986 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21988 struct type *this_type;
21990 this_type = get_die_type (die, cu);
21994 return read_type_die_1 (die, cu);
21997 /* Read the type in DIE, CU.
21998 Returns NULL for invalid types. */
22000 static struct type *
22001 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22003 struct type *this_type = NULL;
22007 case DW_TAG_class_type:
22008 case DW_TAG_interface_type:
22009 case DW_TAG_structure_type:
22010 case DW_TAG_union_type:
22011 this_type = read_structure_type (die, cu);
22013 case DW_TAG_enumeration_type:
22014 this_type = read_enumeration_type (die, cu);
22016 case DW_TAG_subprogram:
22017 case DW_TAG_subroutine_type:
22018 case DW_TAG_inlined_subroutine:
22019 this_type = read_subroutine_type (die, cu);
22021 case DW_TAG_array_type:
22022 this_type = read_array_type (die, cu);
22024 case DW_TAG_set_type:
22025 this_type = read_set_type (die, cu);
22027 case DW_TAG_pointer_type:
22028 this_type = read_tag_pointer_type (die, cu);
22030 case DW_TAG_ptr_to_member_type:
22031 this_type = read_tag_ptr_to_member_type (die, cu);
22033 case DW_TAG_reference_type:
22034 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22036 case DW_TAG_rvalue_reference_type:
22037 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22039 case DW_TAG_const_type:
22040 this_type = read_tag_const_type (die, cu);
22042 case DW_TAG_volatile_type:
22043 this_type = read_tag_volatile_type (die, cu);
22045 case DW_TAG_restrict_type:
22046 this_type = read_tag_restrict_type (die, cu);
22048 case DW_TAG_string_type:
22049 this_type = read_tag_string_type (die, cu);
22051 case DW_TAG_typedef:
22052 this_type = read_typedef (die, cu);
22054 case DW_TAG_subrange_type:
22055 this_type = read_subrange_type (die, cu);
22057 case DW_TAG_base_type:
22058 this_type = read_base_type (die, cu);
22060 case DW_TAG_unspecified_type:
22061 this_type = read_unspecified_type (die, cu);
22063 case DW_TAG_namespace:
22064 this_type = read_namespace_type (die, cu);
22066 case DW_TAG_module:
22067 this_type = read_module_type (die, cu);
22069 case DW_TAG_atomic_type:
22070 this_type = read_tag_atomic_type (die, cu);
22073 complaint (&symfile_complaints,
22074 _("unexpected tag in read_type_die: '%s'"),
22075 dwarf_tag_name (die->tag));
22082 /* See if we can figure out if the class lives in a namespace. We do
22083 this by looking for a member function; its demangled name will
22084 contain namespace info, if there is any.
22085 Return the computed name or NULL.
22086 Space for the result is allocated on the objfile's obstack.
22087 This is the full-die version of guess_partial_die_structure_name.
22088 In this case we know DIE has no useful parent. */
22091 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22093 struct die_info *spec_die;
22094 struct dwarf2_cu *spec_cu;
22095 struct die_info *child;
22096 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22099 spec_die = die_specification (die, &spec_cu);
22100 if (spec_die != NULL)
22106 for (child = die->child;
22108 child = child->sibling)
22110 if (child->tag == DW_TAG_subprogram)
22112 const char *linkage_name = dw2_linkage_name (child, cu);
22114 if (linkage_name != NULL)
22117 = language_class_name_from_physname (cu->language_defn,
22121 if (actual_name != NULL)
22123 const char *die_name = dwarf2_name (die, cu);
22125 if (die_name != NULL
22126 && strcmp (die_name, actual_name) != 0)
22128 /* Strip off the class name from the full name.
22129 We want the prefix. */
22130 int die_name_len = strlen (die_name);
22131 int actual_name_len = strlen (actual_name);
22133 /* Test for '::' as a sanity check. */
22134 if (actual_name_len > die_name_len + 2
22135 && actual_name[actual_name_len
22136 - die_name_len - 1] == ':')
22137 name = (char *) obstack_copy0 (
22138 &objfile->per_bfd->storage_obstack,
22139 actual_name, actual_name_len - die_name_len - 2);
22142 xfree (actual_name);
22151 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22152 prefix part in such case. See
22153 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22155 static const char *
22156 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22158 struct attribute *attr;
22161 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22162 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22165 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22168 attr = dw2_linkage_name_attr (die, cu);
22169 if (attr == NULL || DW_STRING (attr) == NULL)
22172 /* dwarf2_name had to be already called. */
22173 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22175 /* Strip the base name, keep any leading namespaces/classes. */
22176 base = strrchr (DW_STRING (attr), ':');
22177 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22180 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22181 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
22183 &base[-1] - DW_STRING (attr));
22186 /* Return the name of the namespace/class that DIE is defined within,
22187 or "" if we can't tell. The caller should not xfree the result.
22189 For example, if we're within the method foo() in the following
22199 then determine_prefix on foo's die will return "N::C". */
22201 static const char *
22202 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22204 struct dwarf2_per_objfile *dwarf2_per_objfile
22205 = cu->per_cu->dwarf2_per_objfile;
22206 struct die_info *parent, *spec_die;
22207 struct dwarf2_cu *spec_cu;
22208 struct type *parent_type;
22209 const char *retval;
22211 if (cu->language != language_cplus
22212 && cu->language != language_fortran && cu->language != language_d
22213 && cu->language != language_rust)
22216 retval = anonymous_struct_prefix (die, cu);
22220 /* We have to be careful in the presence of DW_AT_specification.
22221 For example, with GCC 3.4, given the code
22225 // Definition of N::foo.
22229 then we'll have a tree of DIEs like this:
22231 1: DW_TAG_compile_unit
22232 2: DW_TAG_namespace // N
22233 3: DW_TAG_subprogram // declaration of N::foo
22234 4: DW_TAG_subprogram // definition of N::foo
22235 DW_AT_specification // refers to die #3
22237 Thus, when processing die #4, we have to pretend that we're in
22238 the context of its DW_AT_specification, namely the contex of die
22241 spec_die = die_specification (die, &spec_cu);
22242 if (spec_die == NULL)
22243 parent = die->parent;
22246 parent = spec_die->parent;
22250 if (parent == NULL)
22252 else if (parent->building_fullname)
22255 const char *parent_name;
22257 /* It has been seen on RealView 2.2 built binaries,
22258 DW_TAG_template_type_param types actually _defined_ as
22259 children of the parent class:
22262 template class <class Enum> Class{};
22263 Class<enum E> class_e;
22265 1: DW_TAG_class_type (Class)
22266 2: DW_TAG_enumeration_type (E)
22267 3: DW_TAG_enumerator (enum1:0)
22268 3: DW_TAG_enumerator (enum2:1)
22270 2: DW_TAG_template_type_param
22271 DW_AT_type DW_FORM_ref_udata (E)
22273 Besides being broken debug info, it can put GDB into an
22274 infinite loop. Consider:
22276 When we're building the full name for Class<E>, we'll start
22277 at Class, and go look over its template type parameters,
22278 finding E. We'll then try to build the full name of E, and
22279 reach here. We're now trying to build the full name of E,
22280 and look over the parent DIE for containing scope. In the
22281 broken case, if we followed the parent DIE of E, we'd again
22282 find Class, and once again go look at its template type
22283 arguments, etc., etc. Simply don't consider such parent die
22284 as source-level parent of this die (it can't be, the language
22285 doesn't allow it), and break the loop here. */
22286 name = dwarf2_name (die, cu);
22287 parent_name = dwarf2_name (parent, cu);
22288 complaint (&symfile_complaints,
22289 _("template param type '%s' defined within parent '%s'"),
22290 name ? name : "<unknown>",
22291 parent_name ? parent_name : "<unknown>");
22295 switch (parent->tag)
22297 case DW_TAG_namespace:
22298 parent_type = read_type_die (parent, cu);
22299 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22300 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22301 Work around this problem here. */
22302 if (cu->language == language_cplus
22303 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22305 /* We give a name to even anonymous namespaces. */
22306 return TYPE_TAG_NAME (parent_type);
22307 case DW_TAG_class_type:
22308 case DW_TAG_interface_type:
22309 case DW_TAG_structure_type:
22310 case DW_TAG_union_type:
22311 case DW_TAG_module:
22312 parent_type = read_type_die (parent, cu);
22313 if (TYPE_TAG_NAME (parent_type) != NULL)
22314 return TYPE_TAG_NAME (parent_type);
22316 /* An anonymous structure is only allowed non-static data
22317 members; no typedefs, no member functions, et cetera.
22318 So it does not need a prefix. */
22320 case DW_TAG_compile_unit:
22321 case DW_TAG_partial_unit:
22322 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22323 if (cu->language == language_cplus
22324 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22325 && die->child != NULL
22326 && (die->tag == DW_TAG_class_type
22327 || die->tag == DW_TAG_structure_type
22328 || die->tag == DW_TAG_union_type))
22330 char *name = guess_full_die_structure_name (die, cu);
22335 case DW_TAG_enumeration_type:
22336 parent_type = read_type_die (parent, cu);
22337 if (TYPE_DECLARED_CLASS (parent_type))
22339 if (TYPE_TAG_NAME (parent_type) != NULL)
22340 return TYPE_TAG_NAME (parent_type);
22343 /* Fall through. */
22345 return determine_prefix (parent, cu);
22349 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22350 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22351 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22352 an obconcat, otherwise allocate storage for the result. The CU argument is
22353 used to determine the language and hence, the appropriate separator. */
22355 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22358 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22359 int physname, struct dwarf2_cu *cu)
22361 const char *lead = "";
22364 if (suffix == NULL || suffix[0] == '\0'
22365 || prefix == NULL || prefix[0] == '\0')
22367 else if (cu->language == language_d)
22369 /* For D, the 'main' function could be defined in any module, but it
22370 should never be prefixed. */
22371 if (strcmp (suffix, "D main") == 0)
22379 else if (cu->language == language_fortran && physname)
22381 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22382 DW_AT_MIPS_linkage_name is preferred and used instead. */
22390 if (prefix == NULL)
22392 if (suffix == NULL)
22399 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22401 strcpy (retval, lead);
22402 strcat (retval, prefix);
22403 strcat (retval, sep);
22404 strcat (retval, suffix);
22409 /* We have an obstack. */
22410 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22414 /* Return sibling of die, NULL if no sibling. */
22416 static struct die_info *
22417 sibling_die (struct die_info *die)
22419 return die->sibling;
22422 /* Get name of a die, return NULL if not found. */
22424 static const char *
22425 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22426 struct obstack *obstack)
22428 if (name && cu->language == language_cplus)
22430 std::string canon_name = cp_canonicalize_string (name);
22432 if (!canon_name.empty ())
22434 if (canon_name != name)
22435 name = (const char *) obstack_copy0 (obstack,
22436 canon_name.c_str (),
22437 canon_name.length ());
22444 /* Get name of a die, return NULL if not found.
22445 Anonymous namespaces are converted to their magic string. */
22447 static const char *
22448 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22450 struct attribute *attr;
22451 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22453 attr = dwarf2_attr (die, DW_AT_name, cu);
22454 if ((!attr || !DW_STRING (attr))
22455 && die->tag != DW_TAG_namespace
22456 && die->tag != DW_TAG_class_type
22457 && die->tag != DW_TAG_interface_type
22458 && die->tag != DW_TAG_structure_type
22459 && die->tag != DW_TAG_union_type)
22464 case DW_TAG_compile_unit:
22465 case DW_TAG_partial_unit:
22466 /* Compilation units have a DW_AT_name that is a filename, not
22467 a source language identifier. */
22468 case DW_TAG_enumeration_type:
22469 case DW_TAG_enumerator:
22470 /* These tags always have simple identifiers already; no need
22471 to canonicalize them. */
22472 return DW_STRING (attr);
22474 case DW_TAG_namespace:
22475 if (attr != NULL && DW_STRING (attr) != NULL)
22476 return DW_STRING (attr);
22477 return CP_ANONYMOUS_NAMESPACE_STR;
22479 case DW_TAG_class_type:
22480 case DW_TAG_interface_type:
22481 case DW_TAG_structure_type:
22482 case DW_TAG_union_type:
22483 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22484 structures or unions. These were of the form "._%d" in GCC 4.1,
22485 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22486 and GCC 4.4. We work around this problem by ignoring these. */
22487 if (attr && DW_STRING (attr)
22488 && (startswith (DW_STRING (attr), "._")
22489 || startswith (DW_STRING (attr), "<anonymous")))
22492 /* GCC might emit a nameless typedef that has a linkage name. See
22493 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22494 if (!attr || DW_STRING (attr) == NULL)
22496 char *demangled = NULL;
22498 attr = dw2_linkage_name_attr (die, cu);
22499 if (attr == NULL || DW_STRING (attr) == NULL)
22502 /* Avoid demangling DW_STRING (attr) the second time on a second
22503 call for the same DIE. */
22504 if (!DW_STRING_IS_CANONICAL (attr))
22505 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22511 /* FIXME: we already did this for the partial symbol... */
22514 obstack_copy0 (&objfile->per_bfd->storage_obstack,
22515 demangled, strlen (demangled)));
22516 DW_STRING_IS_CANONICAL (attr) = 1;
22519 /* Strip any leading namespaces/classes, keep only the base name.
22520 DW_AT_name for named DIEs does not contain the prefixes. */
22521 base = strrchr (DW_STRING (attr), ':');
22522 if (base && base > DW_STRING (attr) && base[-1] == ':')
22525 return DW_STRING (attr);
22534 if (!DW_STRING_IS_CANONICAL (attr))
22537 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22538 &objfile->per_bfd->storage_obstack);
22539 DW_STRING_IS_CANONICAL (attr) = 1;
22541 return DW_STRING (attr);
22544 /* Return the die that this die in an extension of, or NULL if there
22545 is none. *EXT_CU is the CU containing DIE on input, and the CU
22546 containing the return value on output. */
22548 static struct die_info *
22549 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22551 struct attribute *attr;
22553 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22557 return follow_die_ref (die, attr, ext_cu);
22560 /* Convert a DIE tag into its string name. */
22562 static const char *
22563 dwarf_tag_name (unsigned tag)
22565 const char *name = get_DW_TAG_name (tag);
22568 return "DW_TAG_<unknown>";
22573 /* Convert a DWARF attribute code into its string name. */
22575 static const char *
22576 dwarf_attr_name (unsigned attr)
22580 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22581 if (attr == DW_AT_MIPS_fde)
22582 return "DW_AT_MIPS_fde";
22584 if (attr == DW_AT_HP_block_index)
22585 return "DW_AT_HP_block_index";
22588 name = get_DW_AT_name (attr);
22591 return "DW_AT_<unknown>";
22596 /* Convert a DWARF value form code into its string name. */
22598 static const char *
22599 dwarf_form_name (unsigned form)
22601 const char *name = get_DW_FORM_name (form);
22604 return "DW_FORM_<unknown>";
22609 static const char *
22610 dwarf_bool_name (unsigned mybool)
22618 /* Convert a DWARF type code into its string name. */
22620 static const char *
22621 dwarf_type_encoding_name (unsigned enc)
22623 const char *name = get_DW_ATE_name (enc);
22626 return "DW_ATE_<unknown>";
22632 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22636 print_spaces (indent, f);
22637 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
22638 dwarf_tag_name (die->tag), die->abbrev,
22639 to_underlying (die->sect_off));
22641 if (die->parent != NULL)
22643 print_spaces (indent, f);
22644 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
22645 to_underlying (die->parent->sect_off));
22648 print_spaces (indent, f);
22649 fprintf_unfiltered (f, " has children: %s\n",
22650 dwarf_bool_name (die->child != NULL));
22652 print_spaces (indent, f);
22653 fprintf_unfiltered (f, " attributes:\n");
22655 for (i = 0; i < die->num_attrs; ++i)
22657 print_spaces (indent, f);
22658 fprintf_unfiltered (f, " %s (%s) ",
22659 dwarf_attr_name (die->attrs[i].name),
22660 dwarf_form_name (die->attrs[i].form));
22662 switch (die->attrs[i].form)
22665 case DW_FORM_GNU_addr_index:
22666 fprintf_unfiltered (f, "address: ");
22667 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22669 case DW_FORM_block2:
22670 case DW_FORM_block4:
22671 case DW_FORM_block:
22672 case DW_FORM_block1:
22673 fprintf_unfiltered (f, "block: size %s",
22674 pulongest (DW_BLOCK (&die->attrs[i])->size));
22676 case DW_FORM_exprloc:
22677 fprintf_unfiltered (f, "expression: size %s",
22678 pulongest (DW_BLOCK (&die->attrs[i])->size));
22680 case DW_FORM_data16:
22681 fprintf_unfiltered (f, "constant of 16 bytes");
22683 case DW_FORM_ref_addr:
22684 fprintf_unfiltered (f, "ref address: ");
22685 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22687 case DW_FORM_GNU_ref_alt:
22688 fprintf_unfiltered (f, "alt ref address: ");
22689 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22695 case DW_FORM_ref_udata:
22696 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22697 (long) (DW_UNSND (&die->attrs[i])));
22699 case DW_FORM_data1:
22700 case DW_FORM_data2:
22701 case DW_FORM_data4:
22702 case DW_FORM_data8:
22703 case DW_FORM_udata:
22704 case DW_FORM_sdata:
22705 fprintf_unfiltered (f, "constant: %s",
22706 pulongest (DW_UNSND (&die->attrs[i])));
22708 case DW_FORM_sec_offset:
22709 fprintf_unfiltered (f, "section offset: %s",
22710 pulongest (DW_UNSND (&die->attrs[i])));
22712 case DW_FORM_ref_sig8:
22713 fprintf_unfiltered (f, "signature: %s",
22714 hex_string (DW_SIGNATURE (&die->attrs[i])));
22716 case DW_FORM_string:
22718 case DW_FORM_line_strp:
22719 case DW_FORM_GNU_str_index:
22720 case DW_FORM_GNU_strp_alt:
22721 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22722 DW_STRING (&die->attrs[i])
22723 ? DW_STRING (&die->attrs[i]) : "",
22724 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22727 if (DW_UNSND (&die->attrs[i]))
22728 fprintf_unfiltered (f, "flag: TRUE");
22730 fprintf_unfiltered (f, "flag: FALSE");
22732 case DW_FORM_flag_present:
22733 fprintf_unfiltered (f, "flag: TRUE");
22735 case DW_FORM_indirect:
22736 /* The reader will have reduced the indirect form to
22737 the "base form" so this form should not occur. */
22738 fprintf_unfiltered (f,
22739 "unexpected attribute form: DW_FORM_indirect");
22741 case DW_FORM_implicit_const:
22742 fprintf_unfiltered (f, "constant: %s",
22743 plongest (DW_SND (&die->attrs[i])));
22746 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22747 die->attrs[i].form);
22750 fprintf_unfiltered (f, "\n");
22755 dump_die_for_error (struct die_info *die)
22757 dump_die_shallow (gdb_stderr, 0, die);
22761 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22763 int indent = level * 4;
22765 gdb_assert (die != NULL);
22767 if (level >= max_level)
22770 dump_die_shallow (f, indent, die);
22772 if (die->child != NULL)
22774 print_spaces (indent, f);
22775 fprintf_unfiltered (f, " Children:");
22776 if (level + 1 < max_level)
22778 fprintf_unfiltered (f, "\n");
22779 dump_die_1 (f, level + 1, max_level, die->child);
22783 fprintf_unfiltered (f,
22784 " [not printed, max nesting level reached]\n");
22788 if (die->sibling != NULL && level > 0)
22790 dump_die_1 (f, level, max_level, die->sibling);
22794 /* This is called from the pdie macro in gdbinit.in.
22795 It's not static so gcc will keep a copy callable from gdb. */
22798 dump_die (struct die_info *die, int max_level)
22800 dump_die_1 (gdb_stdlog, 0, max_level, die);
22804 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22808 slot = htab_find_slot_with_hash (cu->die_hash, die,
22809 to_underlying (die->sect_off),
22815 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22819 dwarf2_get_ref_die_offset (const struct attribute *attr)
22821 if (attr_form_is_ref (attr))
22822 return (sect_offset) DW_UNSND (attr);
22824 complaint (&symfile_complaints,
22825 _("unsupported die ref attribute form: '%s'"),
22826 dwarf_form_name (attr->form));
22830 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22831 * the value held by the attribute is not constant. */
22834 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22836 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22837 return DW_SND (attr);
22838 else if (attr->form == DW_FORM_udata
22839 || attr->form == DW_FORM_data1
22840 || attr->form == DW_FORM_data2
22841 || attr->form == DW_FORM_data4
22842 || attr->form == DW_FORM_data8)
22843 return DW_UNSND (attr);
22846 /* For DW_FORM_data16 see attr_form_is_constant. */
22847 complaint (&symfile_complaints,
22848 _("Attribute value is not a constant (%s)"),
22849 dwarf_form_name (attr->form));
22850 return default_value;
22854 /* Follow reference or signature attribute ATTR of SRC_DIE.
22855 On entry *REF_CU is the CU of SRC_DIE.
22856 On exit *REF_CU is the CU of the result. */
22858 static struct die_info *
22859 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22860 struct dwarf2_cu **ref_cu)
22862 struct die_info *die;
22864 if (attr_form_is_ref (attr))
22865 die = follow_die_ref (src_die, attr, ref_cu);
22866 else if (attr->form == DW_FORM_ref_sig8)
22867 die = follow_die_sig (src_die, attr, ref_cu);
22870 dump_die_for_error (src_die);
22871 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22872 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22878 /* Follow reference OFFSET.
22879 On entry *REF_CU is the CU of the source die referencing OFFSET.
22880 On exit *REF_CU is the CU of the result.
22881 Returns NULL if OFFSET is invalid. */
22883 static struct die_info *
22884 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22885 struct dwarf2_cu **ref_cu)
22887 struct die_info temp_die;
22888 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22889 struct dwarf2_per_objfile *dwarf2_per_objfile
22890 = cu->per_cu->dwarf2_per_objfile;
22891 struct objfile *objfile = dwarf2_per_objfile->objfile;
22893 gdb_assert (cu->per_cu != NULL);
22897 if (cu->per_cu->is_debug_types)
22899 /* .debug_types CUs cannot reference anything outside their CU.
22900 If they need to, they have to reference a signatured type via
22901 DW_FORM_ref_sig8. */
22902 if (!offset_in_cu_p (&cu->header, sect_off))
22905 else if (offset_in_dwz != cu->per_cu->is_dwz
22906 || !offset_in_cu_p (&cu->header, sect_off))
22908 struct dwarf2_per_cu_data *per_cu;
22910 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22911 dwarf2_per_objfile);
22913 /* If necessary, add it to the queue and load its DIEs. */
22914 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22915 load_full_comp_unit (per_cu, cu->language);
22917 target_cu = per_cu->cu;
22919 else if (cu->dies == NULL)
22921 /* We're loading full DIEs during partial symbol reading. */
22922 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22923 load_full_comp_unit (cu->per_cu, language_minimal);
22926 *ref_cu = target_cu;
22927 temp_die.sect_off = sect_off;
22928 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22930 to_underlying (sect_off));
22933 /* Follow reference attribute ATTR of SRC_DIE.
22934 On entry *REF_CU is the CU of SRC_DIE.
22935 On exit *REF_CU is the CU of the result. */
22937 static struct die_info *
22938 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22939 struct dwarf2_cu **ref_cu)
22941 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22942 struct dwarf2_cu *cu = *ref_cu;
22943 struct die_info *die;
22945 die = follow_die_offset (sect_off,
22946 (attr->form == DW_FORM_GNU_ref_alt
22947 || cu->per_cu->is_dwz),
22950 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
22951 "at 0x%x [in module %s]"),
22952 to_underlying (sect_off), to_underlying (src_die->sect_off),
22953 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22958 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22959 Returned value is intended for DW_OP_call*. Returned
22960 dwarf2_locexpr_baton->data has lifetime of
22961 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
22963 struct dwarf2_locexpr_baton
22964 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22965 struct dwarf2_per_cu_data *per_cu,
22966 CORE_ADDR (*get_frame_pc) (void *baton),
22969 struct dwarf2_cu *cu;
22970 struct die_info *die;
22971 struct attribute *attr;
22972 struct dwarf2_locexpr_baton retval;
22973 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22974 struct dwarf2_per_objfile *dwarf2_per_objfile
22975 = get_dwarf2_per_objfile (objfile);
22977 if (per_cu->cu == NULL)
22982 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22983 Instead just throw an error, not much else we can do. */
22984 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
22985 to_underlying (sect_off), objfile_name (objfile));
22988 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22990 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
22991 to_underlying (sect_off), objfile_name (objfile));
22993 attr = dwarf2_attr (die, DW_AT_location, cu);
22996 /* DWARF: "If there is no such attribute, then there is no effect.".
22997 DATA is ignored if SIZE is 0. */
22999 retval.data = NULL;
23002 else if (attr_form_is_section_offset (attr))
23004 struct dwarf2_loclist_baton loclist_baton;
23005 CORE_ADDR pc = (*get_frame_pc) (baton);
23008 fill_in_loclist_baton (cu, &loclist_baton, attr);
23010 retval.data = dwarf2_find_location_expression (&loclist_baton,
23012 retval.size = size;
23016 if (!attr_form_is_block (attr))
23017 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
23018 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23019 to_underlying (sect_off), objfile_name (objfile));
23021 retval.data = DW_BLOCK (attr)->data;
23022 retval.size = DW_BLOCK (attr)->size;
23024 retval.per_cu = cu->per_cu;
23026 age_cached_comp_units (dwarf2_per_objfile);
23031 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23034 struct dwarf2_locexpr_baton
23035 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23036 struct dwarf2_per_cu_data *per_cu,
23037 CORE_ADDR (*get_frame_pc) (void *baton),
23040 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23042 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23045 /* Write a constant of a given type as target-ordered bytes into
23048 static const gdb_byte *
23049 write_constant_as_bytes (struct obstack *obstack,
23050 enum bfd_endian byte_order,
23057 *len = TYPE_LENGTH (type);
23058 result = (gdb_byte *) obstack_alloc (obstack, *len);
23059 store_unsigned_integer (result, *len, byte_order, value);
23064 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23065 pointer to the constant bytes and set LEN to the length of the
23066 data. If memory is needed, allocate it on OBSTACK. If the DIE
23067 does not have a DW_AT_const_value, return NULL. */
23070 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23071 struct dwarf2_per_cu_data *per_cu,
23072 struct obstack *obstack,
23075 struct dwarf2_cu *cu;
23076 struct die_info *die;
23077 struct attribute *attr;
23078 const gdb_byte *result = NULL;
23081 enum bfd_endian byte_order;
23082 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23084 if (per_cu->cu == NULL)
23089 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23090 Instead just throw an error, not much else we can do. */
23091 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
23092 to_underlying (sect_off), objfile_name (objfile));
23095 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23097 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
23098 to_underlying (sect_off), objfile_name (objfile));
23101 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23105 byte_order = (bfd_big_endian (objfile->obfd)
23106 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23108 switch (attr->form)
23111 case DW_FORM_GNU_addr_index:
23115 *len = cu->header.addr_size;
23116 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23117 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23121 case DW_FORM_string:
23123 case DW_FORM_GNU_str_index:
23124 case DW_FORM_GNU_strp_alt:
23125 /* DW_STRING is already allocated on the objfile obstack, point
23127 result = (const gdb_byte *) DW_STRING (attr);
23128 *len = strlen (DW_STRING (attr));
23130 case DW_FORM_block1:
23131 case DW_FORM_block2:
23132 case DW_FORM_block4:
23133 case DW_FORM_block:
23134 case DW_FORM_exprloc:
23135 case DW_FORM_data16:
23136 result = DW_BLOCK (attr)->data;
23137 *len = DW_BLOCK (attr)->size;
23140 /* The DW_AT_const_value attributes are supposed to carry the
23141 symbol's value "represented as it would be on the target
23142 architecture." By the time we get here, it's already been
23143 converted to host endianness, so we just need to sign- or
23144 zero-extend it as appropriate. */
23145 case DW_FORM_data1:
23146 type = die_type (die, cu);
23147 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23148 if (result == NULL)
23149 result = write_constant_as_bytes (obstack, byte_order,
23152 case DW_FORM_data2:
23153 type = die_type (die, cu);
23154 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23155 if (result == NULL)
23156 result = write_constant_as_bytes (obstack, byte_order,
23159 case DW_FORM_data4:
23160 type = die_type (die, cu);
23161 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23162 if (result == NULL)
23163 result = write_constant_as_bytes (obstack, byte_order,
23166 case DW_FORM_data8:
23167 type = die_type (die, cu);
23168 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23169 if (result == NULL)
23170 result = write_constant_as_bytes (obstack, byte_order,
23174 case DW_FORM_sdata:
23175 case DW_FORM_implicit_const:
23176 type = die_type (die, cu);
23177 result = write_constant_as_bytes (obstack, byte_order,
23178 type, DW_SND (attr), len);
23181 case DW_FORM_udata:
23182 type = die_type (die, cu);
23183 result = write_constant_as_bytes (obstack, byte_order,
23184 type, DW_UNSND (attr), len);
23188 complaint (&symfile_complaints,
23189 _("unsupported const value attribute form: '%s'"),
23190 dwarf_form_name (attr->form));
23197 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23198 valid type for this die is found. */
23201 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23202 struct dwarf2_per_cu_data *per_cu)
23204 struct dwarf2_cu *cu;
23205 struct die_info *die;
23207 if (per_cu->cu == NULL)
23213 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23217 return die_type (die, cu);
23220 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23224 dwarf2_get_die_type (cu_offset die_offset,
23225 struct dwarf2_per_cu_data *per_cu)
23227 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23228 return get_die_type_at_offset (die_offset_sect, per_cu);
23231 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23232 On entry *REF_CU is the CU of SRC_DIE.
23233 On exit *REF_CU is the CU of the result.
23234 Returns NULL if the referenced DIE isn't found. */
23236 static struct die_info *
23237 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23238 struct dwarf2_cu **ref_cu)
23240 struct die_info temp_die;
23241 struct dwarf2_cu *sig_cu;
23242 struct die_info *die;
23244 /* While it might be nice to assert sig_type->type == NULL here,
23245 we can get here for DW_AT_imported_declaration where we need
23246 the DIE not the type. */
23248 /* If necessary, add it to the queue and load its DIEs. */
23250 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23251 read_signatured_type (sig_type);
23253 sig_cu = sig_type->per_cu.cu;
23254 gdb_assert (sig_cu != NULL);
23255 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23256 temp_die.sect_off = sig_type->type_offset_in_section;
23257 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23258 to_underlying (temp_die.sect_off));
23261 struct dwarf2_per_objfile *dwarf2_per_objfile
23262 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23264 /* For .gdb_index version 7 keep track of included TUs.
23265 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23266 if (dwarf2_per_objfile->index_table != NULL
23267 && dwarf2_per_objfile->index_table->version <= 7)
23269 VEC_safe_push (dwarf2_per_cu_ptr,
23270 (*ref_cu)->per_cu->imported_symtabs,
23281 /* Follow signatured type referenced by ATTR in SRC_DIE.
23282 On entry *REF_CU is the CU of SRC_DIE.
23283 On exit *REF_CU is the CU of the result.
23284 The result is the DIE of the type.
23285 If the referenced type cannot be found an error is thrown. */
23287 static struct die_info *
23288 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23289 struct dwarf2_cu **ref_cu)
23291 ULONGEST signature = DW_SIGNATURE (attr);
23292 struct signatured_type *sig_type;
23293 struct die_info *die;
23295 gdb_assert (attr->form == DW_FORM_ref_sig8);
23297 sig_type = lookup_signatured_type (*ref_cu, signature);
23298 /* sig_type will be NULL if the signatured type is missing from
23300 if (sig_type == NULL)
23302 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23303 " from DIE at 0x%x [in module %s]"),
23304 hex_string (signature), to_underlying (src_die->sect_off),
23305 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23308 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23311 dump_die_for_error (src_die);
23312 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23313 " from DIE at 0x%x [in module %s]"),
23314 hex_string (signature), to_underlying (src_die->sect_off),
23315 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23321 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23322 reading in and processing the type unit if necessary. */
23324 static struct type *
23325 get_signatured_type (struct die_info *die, ULONGEST signature,
23326 struct dwarf2_cu *cu)
23328 struct dwarf2_per_objfile *dwarf2_per_objfile
23329 = cu->per_cu->dwarf2_per_objfile;
23330 struct signatured_type *sig_type;
23331 struct dwarf2_cu *type_cu;
23332 struct die_info *type_die;
23335 sig_type = lookup_signatured_type (cu, signature);
23336 /* sig_type will be NULL if the signatured type is missing from
23338 if (sig_type == NULL)
23340 complaint (&symfile_complaints,
23341 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23342 " from DIE at 0x%x [in module %s]"),
23343 hex_string (signature), to_underlying (die->sect_off),
23344 objfile_name (dwarf2_per_objfile->objfile));
23345 return build_error_marker_type (cu, die);
23348 /* If we already know the type we're done. */
23349 if (sig_type->type != NULL)
23350 return sig_type->type;
23353 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23354 if (type_die != NULL)
23356 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23357 is created. This is important, for example, because for c++ classes
23358 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23359 type = read_type_die (type_die, type_cu);
23362 complaint (&symfile_complaints,
23363 _("Dwarf Error: Cannot build signatured type %s"
23364 " referenced from DIE at 0x%x [in module %s]"),
23365 hex_string (signature), to_underlying (die->sect_off),
23366 objfile_name (dwarf2_per_objfile->objfile));
23367 type = build_error_marker_type (cu, die);
23372 complaint (&symfile_complaints,
23373 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23374 " from DIE at 0x%x [in module %s]"),
23375 hex_string (signature), to_underlying (die->sect_off),
23376 objfile_name (dwarf2_per_objfile->objfile));
23377 type = build_error_marker_type (cu, die);
23379 sig_type->type = type;
23384 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23385 reading in and processing the type unit if necessary. */
23387 static struct type *
23388 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23389 struct dwarf2_cu *cu) /* ARI: editCase function */
23391 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23392 if (attr_form_is_ref (attr))
23394 struct dwarf2_cu *type_cu = cu;
23395 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23397 return read_type_die (type_die, type_cu);
23399 else if (attr->form == DW_FORM_ref_sig8)
23401 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23405 struct dwarf2_per_objfile *dwarf2_per_objfile
23406 = cu->per_cu->dwarf2_per_objfile;
23408 complaint (&symfile_complaints,
23409 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23410 " at 0x%x [in module %s]"),
23411 dwarf_form_name (attr->form), to_underlying (die->sect_off),
23412 objfile_name (dwarf2_per_objfile->objfile));
23413 return build_error_marker_type (cu, die);
23417 /* Load the DIEs associated with type unit PER_CU into memory. */
23420 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23422 struct signatured_type *sig_type;
23424 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23425 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23427 /* We have the per_cu, but we need the signatured_type.
23428 Fortunately this is an easy translation. */
23429 gdb_assert (per_cu->is_debug_types);
23430 sig_type = (struct signatured_type *) per_cu;
23432 gdb_assert (per_cu->cu == NULL);
23434 read_signatured_type (sig_type);
23436 gdb_assert (per_cu->cu != NULL);
23439 /* die_reader_func for read_signatured_type.
23440 This is identical to load_full_comp_unit_reader,
23441 but is kept separate for now. */
23444 read_signatured_type_reader (const struct die_reader_specs *reader,
23445 const gdb_byte *info_ptr,
23446 struct die_info *comp_unit_die,
23450 struct dwarf2_cu *cu = reader->cu;
23452 gdb_assert (cu->die_hash == NULL);
23454 htab_create_alloc_ex (cu->header.length / 12,
23458 &cu->comp_unit_obstack,
23459 hashtab_obstack_allocate,
23460 dummy_obstack_deallocate);
23463 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23464 &info_ptr, comp_unit_die);
23465 cu->dies = comp_unit_die;
23466 /* comp_unit_die is not stored in die_hash, no need. */
23468 /* We try not to read any attributes in this function, because not
23469 all CUs needed for references have been loaded yet, and symbol
23470 table processing isn't initialized. But we have to set the CU language,
23471 or we won't be able to build types correctly.
23472 Similarly, if we do not read the producer, we can not apply
23473 producer-specific interpretation. */
23474 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23477 /* Read in a signatured type and build its CU and DIEs.
23478 If the type is a stub for the real type in a DWO file,
23479 read in the real type from the DWO file as well. */
23482 read_signatured_type (struct signatured_type *sig_type)
23484 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23486 gdb_assert (per_cu->is_debug_types);
23487 gdb_assert (per_cu->cu == NULL);
23489 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23490 read_signatured_type_reader, NULL);
23491 sig_type->per_cu.tu_read = 1;
23494 /* Decode simple location descriptions.
23495 Given a pointer to a dwarf block that defines a location, compute
23496 the location and return the value.
23498 NOTE drow/2003-11-18: This function is called in two situations
23499 now: for the address of static or global variables (partial symbols
23500 only) and for offsets into structures which are expected to be
23501 (more or less) constant. The partial symbol case should go away,
23502 and only the constant case should remain. That will let this
23503 function complain more accurately. A few special modes are allowed
23504 without complaint for global variables (for instance, global
23505 register values and thread-local values).
23507 A location description containing no operations indicates that the
23508 object is optimized out. The return value is 0 for that case.
23509 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23510 callers will only want a very basic result and this can become a
23513 Note that stack[0] is unused except as a default error return. */
23516 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23518 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23520 size_t size = blk->size;
23521 const gdb_byte *data = blk->data;
23522 CORE_ADDR stack[64];
23524 unsigned int bytes_read, unsnd;
23530 stack[++stacki] = 0;
23569 stack[++stacki] = op - DW_OP_lit0;
23604 stack[++stacki] = op - DW_OP_reg0;
23606 dwarf2_complex_location_expr_complaint ();
23610 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23612 stack[++stacki] = unsnd;
23614 dwarf2_complex_location_expr_complaint ();
23618 stack[++stacki] = read_address (objfile->obfd, &data[i],
23623 case DW_OP_const1u:
23624 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23628 case DW_OP_const1s:
23629 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23633 case DW_OP_const2u:
23634 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23638 case DW_OP_const2s:
23639 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23643 case DW_OP_const4u:
23644 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23648 case DW_OP_const4s:
23649 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23653 case DW_OP_const8u:
23654 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23659 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23665 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23670 stack[stacki + 1] = stack[stacki];
23675 stack[stacki - 1] += stack[stacki];
23679 case DW_OP_plus_uconst:
23680 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23686 stack[stacki - 1] -= stack[stacki];
23691 /* If we're not the last op, then we definitely can't encode
23692 this using GDB's address_class enum. This is valid for partial
23693 global symbols, although the variable's address will be bogus
23696 dwarf2_complex_location_expr_complaint ();
23699 case DW_OP_GNU_push_tls_address:
23700 case DW_OP_form_tls_address:
23701 /* The top of the stack has the offset from the beginning
23702 of the thread control block at which the variable is located. */
23703 /* Nothing should follow this operator, so the top of stack would
23705 /* This is valid for partial global symbols, but the variable's
23706 address will be bogus in the psymtab. Make it always at least
23707 non-zero to not look as a variable garbage collected by linker
23708 which have DW_OP_addr 0. */
23710 dwarf2_complex_location_expr_complaint ();
23714 case DW_OP_GNU_uninit:
23717 case DW_OP_GNU_addr_index:
23718 case DW_OP_GNU_const_index:
23719 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23726 const char *name = get_DW_OP_name (op);
23729 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23732 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23736 return (stack[stacki]);
23739 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23740 outside of the allocated space. Also enforce minimum>0. */
23741 if (stacki >= ARRAY_SIZE (stack) - 1)
23743 complaint (&symfile_complaints,
23744 _("location description stack overflow"));
23750 complaint (&symfile_complaints,
23751 _("location description stack underflow"));
23755 return (stack[stacki]);
23758 /* memory allocation interface */
23760 static struct dwarf_block *
23761 dwarf_alloc_block (struct dwarf2_cu *cu)
23763 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23766 static struct die_info *
23767 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23769 struct die_info *die;
23770 size_t size = sizeof (struct die_info);
23773 size += (num_attrs - 1) * sizeof (struct attribute);
23775 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23776 memset (die, 0, sizeof (struct die_info));
23781 /* Macro support. */
23783 /* Return file name relative to the compilation directory of file number I in
23784 *LH's file name table. The result is allocated using xmalloc; the caller is
23785 responsible for freeing it. */
23788 file_file_name (int file, struct line_header *lh)
23790 /* Is the file number a valid index into the line header's file name
23791 table? Remember that file numbers start with one, not zero. */
23792 if (1 <= file && file <= lh->file_names.size ())
23794 const file_entry &fe = lh->file_names[file - 1];
23796 if (!IS_ABSOLUTE_PATH (fe.name))
23798 const char *dir = fe.include_dir (lh);
23800 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23802 return xstrdup (fe.name);
23806 /* The compiler produced a bogus file number. We can at least
23807 record the macro definitions made in the file, even if we
23808 won't be able to find the file by name. */
23809 char fake_name[80];
23811 xsnprintf (fake_name, sizeof (fake_name),
23812 "<bad macro file number %d>", file);
23814 complaint (&symfile_complaints,
23815 _("bad file number in macro information (%d)"),
23818 return xstrdup (fake_name);
23822 /* Return the full name of file number I in *LH's file name table.
23823 Use COMP_DIR as the name of the current directory of the
23824 compilation. The result is allocated using xmalloc; the caller is
23825 responsible for freeing it. */
23827 file_full_name (int file, struct line_header *lh, const char *comp_dir)
23829 /* Is the file number a valid index into the line header's file name
23830 table? Remember that file numbers start with one, not zero. */
23831 if (1 <= file && file <= lh->file_names.size ())
23833 char *relative = file_file_name (file, lh);
23835 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23837 return reconcat (relative, comp_dir, SLASH_STRING,
23838 relative, (char *) NULL);
23841 return file_file_name (file, lh);
23845 static struct macro_source_file *
23846 macro_start_file (int file, int line,
23847 struct macro_source_file *current_file,
23848 struct line_header *lh)
23850 /* File name relative to the compilation directory of this source file. */
23851 char *file_name = file_file_name (file, lh);
23853 if (! current_file)
23855 /* Note: We don't create a macro table for this compilation unit
23856 at all until we actually get a filename. */
23857 struct macro_table *macro_table = get_macro_table ();
23859 /* If we have no current file, then this must be the start_file
23860 directive for the compilation unit's main source file. */
23861 current_file = macro_set_main (macro_table, file_name);
23862 macro_define_special (macro_table);
23865 current_file = macro_include (current_file, line, file_name);
23869 return current_file;
23872 static const char *
23873 consume_improper_spaces (const char *p, const char *body)
23877 complaint (&symfile_complaints,
23878 _("macro definition contains spaces "
23879 "in formal argument list:\n`%s'"),
23891 parse_macro_definition (struct macro_source_file *file, int line,
23896 /* The body string takes one of two forms. For object-like macro
23897 definitions, it should be:
23899 <macro name> " " <definition>
23901 For function-like macro definitions, it should be:
23903 <macro name> "() " <definition>
23905 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23907 Spaces may appear only where explicitly indicated, and in the
23910 The Dwarf 2 spec says that an object-like macro's name is always
23911 followed by a space, but versions of GCC around March 2002 omit
23912 the space when the macro's definition is the empty string.
23914 The Dwarf 2 spec says that there should be no spaces between the
23915 formal arguments in a function-like macro's formal argument list,
23916 but versions of GCC around March 2002 include spaces after the
23920 /* Find the extent of the macro name. The macro name is terminated
23921 by either a space or null character (for an object-like macro) or
23922 an opening paren (for a function-like macro). */
23923 for (p = body; *p; p++)
23924 if (*p == ' ' || *p == '(')
23927 if (*p == ' ' || *p == '\0')
23929 /* It's an object-like macro. */
23930 int name_len = p - body;
23931 char *name = savestring (body, name_len);
23932 const char *replacement;
23935 replacement = body + name_len + 1;
23938 dwarf2_macro_malformed_definition_complaint (body);
23939 replacement = body + name_len;
23942 macro_define_object (file, line, name, replacement);
23946 else if (*p == '(')
23948 /* It's a function-like macro. */
23949 char *name = savestring (body, p - body);
23952 char **argv = XNEWVEC (char *, argv_size);
23956 p = consume_improper_spaces (p, body);
23958 /* Parse the formal argument list. */
23959 while (*p && *p != ')')
23961 /* Find the extent of the current argument name. */
23962 const char *arg_start = p;
23964 while (*p && *p != ',' && *p != ')' && *p != ' ')
23967 if (! *p || p == arg_start)
23968 dwarf2_macro_malformed_definition_complaint (body);
23971 /* Make sure argv has room for the new argument. */
23972 if (argc >= argv_size)
23975 argv = XRESIZEVEC (char *, argv, argv_size);
23978 argv[argc++] = savestring (arg_start, p - arg_start);
23981 p = consume_improper_spaces (p, body);
23983 /* Consume the comma, if present. */
23988 p = consume_improper_spaces (p, body);
23997 /* Perfectly formed definition, no complaints. */
23998 macro_define_function (file, line, name,
23999 argc, (const char **) argv,
24001 else if (*p == '\0')
24003 /* Complain, but do define it. */
24004 dwarf2_macro_malformed_definition_complaint (body);
24005 macro_define_function (file, line, name,
24006 argc, (const char **) argv,
24010 /* Just complain. */
24011 dwarf2_macro_malformed_definition_complaint (body);
24014 /* Just complain. */
24015 dwarf2_macro_malformed_definition_complaint (body);
24021 for (i = 0; i < argc; i++)
24027 dwarf2_macro_malformed_definition_complaint (body);
24030 /* Skip some bytes from BYTES according to the form given in FORM.
24031 Returns the new pointer. */
24033 static const gdb_byte *
24034 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24035 enum dwarf_form form,
24036 unsigned int offset_size,
24037 struct dwarf2_section_info *section)
24039 unsigned int bytes_read;
24043 case DW_FORM_data1:
24048 case DW_FORM_data2:
24052 case DW_FORM_data4:
24056 case DW_FORM_data8:
24060 case DW_FORM_data16:
24064 case DW_FORM_string:
24065 read_direct_string (abfd, bytes, &bytes_read);
24066 bytes += bytes_read;
24069 case DW_FORM_sec_offset:
24071 case DW_FORM_GNU_strp_alt:
24072 bytes += offset_size;
24075 case DW_FORM_block:
24076 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24077 bytes += bytes_read;
24080 case DW_FORM_block1:
24081 bytes += 1 + read_1_byte (abfd, bytes);
24083 case DW_FORM_block2:
24084 bytes += 2 + read_2_bytes (abfd, bytes);
24086 case DW_FORM_block4:
24087 bytes += 4 + read_4_bytes (abfd, bytes);
24090 case DW_FORM_sdata:
24091 case DW_FORM_udata:
24092 case DW_FORM_GNU_addr_index:
24093 case DW_FORM_GNU_str_index:
24094 bytes = gdb_skip_leb128 (bytes, buffer_end);
24097 dwarf2_section_buffer_overflow_complaint (section);
24102 case DW_FORM_implicit_const:
24107 complaint (&symfile_complaints,
24108 _("invalid form 0x%x in `%s'"),
24109 form, get_section_name (section));
24117 /* A helper for dwarf_decode_macros that handles skipping an unknown
24118 opcode. Returns an updated pointer to the macro data buffer; or,
24119 on error, issues a complaint and returns NULL. */
24121 static const gdb_byte *
24122 skip_unknown_opcode (unsigned int opcode,
24123 const gdb_byte **opcode_definitions,
24124 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24126 unsigned int offset_size,
24127 struct dwarf2_section_info *section)
24129 unsigned int bytes_read, i;
24131 const gdb_byte *defn;
24133 if (opcode_definitions[opcode] == NULL)
24135 complaint (&symfile_complaints,
24136 _("unrecognized DW_MACFINO opcode 0x%x"),
24141 defn = opcode_definitions[opcode];
24142 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24143 defn += bytes_read;
24145 for (i = 0; i < arg; ++i)
24147 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24148 (enum dwarf_form) defn[i], offset_size,
24150 if (mac_ptr == NULL)
24152 /* skip_form_bytes already issued the complaint. */
24160 /* A helper function which parses the header of a macro section.
24161 If the macro section is the extended (for now called "GNU") type,
24162 then this updates *OFFSET_SIZE. Returns a pointer to just after
24163 the header, or issues a complaint and returns NULL on error. */
24165 static const gdb_byte *
24166 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24168 const gdb_byte *mac_ptr,
24169 unsigned int *offset_size,
24170 int section_is_gnu)
24172 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24174 if (section_is_gnu)
24176 unsigned int version, flags;
24178 version = read_2_bytes (abfd, mac_ptr);
24179 if (version != 4 && version != 5)
24181 complaint (&symfile_complaints,
24182 _("unrecognized version `%d' in .debug_macro section"),
24188 flags = read_1_byte (abfd, mac_ptr);
24190 *offset_size = (flags & 1) ? 8 : 4;
24192 if ((flags & 2) != 0)
24193 /* We don't need the line table offset. */
24194 mac_ptr += *offset_size;
24196 /* Vendor opcode descriptions. */
24197 if ((flags & 4) != 0)
24199 unsigned int i, count;
24201 count = read_1_byte (abfd, mac_ptr);
24203 for (i = 0; i < count; ++i)
24205 unsigned int opcode, bytes_read;
24208 opcode = read_1_byte (abfd, mac_ptr);
24210 opcode_definitions[opcode] = mac_ptr;
24211 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24212 mac_ptr += bytes_read;
24221 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24222 including DW_MACRO_import. */
24225 dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24227 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24228 struct macro_source_file *current_file,
24229 struct line_header *lh,
24230 struct dwarf2_section_info *section,
24231 int section_is_gnu, int section_is_dwz,
24232 unsigned int offset_size,
24233 htab_t include_hash)
24235 struct objfile *objfile = dwarf2_per_objfile->objfile;
24236 enum dwarf_macro_record_type macinfo_type;
24237 int at_commandline;
24238 const gdb_byte *opcode_definitions[256];
24240 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24241 &offset_size, section_is_gnu);
24242 if (mac_ptr == NULL)
24244 /* We already issued a complaint. */
24248 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24249 GDB is still reading the definitions from command line. First
24250 DW_MACINFO_start_file will need to be ignored as it was already executed
24251 to create CURRENT_FILE for the main source holding also the command line
24252 definitions. On first met DW_MACINFO_start_file this flag is reset to
24253 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24255 at_commandline = 1;
24259 /* Do we at least have room for a macinfo type byte? */
24260 if (mac_ptr >= mac_end)
24262 dwarf2_section_buffer_overflow_complaint (section);
24266 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24269 /* Note that we rely on the fact that the corresponding GNU and
24270 DWARF constants are the same. */
24272 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24273 switch (macinfo_type)
24275 /* A zero macinfo type indicates the end of the macro
24280 case DW_MACRO_define:
24281 case DW_MACRO_undef:
24282 case DW_MACRO_define_strp:
24283 case DW_MACRO_undef_strp:
24284 case DW_MACRO_define_sup:
24285 case DW_MACRO_undef_sup:
24287 unsigned int bytes_read;
24292 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24293 mac_ptr += bytes_read;
24295 if (macinfo_type == DW_MACRO_define
24296 || macinfo_type == DW_MACRO_undef)
24298 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24299 mac_ptr += bytes_read;
24303 LONGEST str_offset;
24305 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24306 mac_ptr += offset_size;
24308 if (macinfo_type == DW_MACRO_define_sup
24309 || macinfo_type == DW_MACRO_undef_sup
24312 struct dwz_file *dwz
24313 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24315 body = read_indirect_string_from_dwz (objfile,
24319 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24323 is_define = (macinfo_type == DW_MACRO_define
24324 || macinfo_type == DW_MACRO_define_strp
24325 || macinfo_type == DW_MACRO_define_sup);
24326 if (! current_file)
24328 /* DWARF violation as no main source is present. */
24329 complaint (&symfile_complaints,
24330 _("debug info with no main source gives macro %s "
24332 is_define ? _("definition") : _("undefinition"),
24336 if ((line == 0 && !at_commandline)
24337 || (line != 0 && at_commandline))
24338 complaint (&symfile_complaints,
24339 _("debug info gives %s macro %s with %s line %d: %s"),
24340 at_commandline ? _("command-line") : _("in-file"),
24341 is_define ? _("definition") : _("undefinition"),
24342 line == 0 ? _("zero") : _("non-zero"), line, body);
24345 parse_macro_definition (current_file, line, body);
24348 gdb_assert (macinfo_type == DW_MACRO_undef
24349 || macinfo_type == DW_MACRO_undef_strp
24350 || macinfo_type == DW_MACRO_undef_sup);
24351 macro_undef (current_file, line, body);
24356 case DW_MACRO_start_file:
24358 unsigned int bytes_read;
24361 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24362 mac_ptr += bytes_read;
24363 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24364 mac_ptr += bytes_read;
24366 if ((line == 0 && !at_commandline)
24367 || (line != 0 && at_commandline))
24368 complaint (&symfile_complaints,
24369 _("debug info gives source %d included "
24370 "from %s at %s line %d"),
24371 file, at_commandline ? _("command-line") : _("file"),
24372 line == 0 ? _("zero") : _("non-zero"), line);
24374 if (at_commandline)
24376 /* This DW_MACRO_start_file was executed in the
24378 at_commandline = 0;
24381 current_file = macro_start_file (file, line, current_file, lh);
24385 case DW_MACRO_end_file:
24386 if (! current_file)
24387 complaint (&symfile_complaints,
24388 _("macro debug info has an unmatched "
24389 "`close_file' directive"));
24392 current_file = current_file->included_by;
24393 if (! current_file)
24395 enum dwarf_macro_record_type next_type;
24397 /* GCC circa March 2002 doesn't produce the zero
24398 type byte marking the end of the compilation
24399 unit. Complain if it's not there, but exit no
24402 /* Do we at least have room for a macinfo type byte? */
24403 if (mac_ptr >= mac_end)
24405 dwarf2_section_buffer_overflow_complaint (section);
24409 /* We don't increment mac_ptr here, so this is just
24412 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24414 if (next_type != 0)
24415 complaint (&symfile_complaints,
24416 _("no terminating 0-type entry for "
24417 "macros in `.debug_macinfo' section"));
24424 case DW_MACRO_import:
24425 case DW_MACRO_import_sup:
24429 bfd *include_bfd = abfd;
24430 struct dwarf2_section_info *include_section = section;
24431 const gdb_byte *include_mac_end = mac_end;
24432 int is_dwz = section_is_dwz;
24433 const gdb_byte *new_mac_ptr;
24435 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24436 mac_ptr += offset_size;
24438 if (macinfo_type == DW_MACRO_import_sup)
24440 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24442 dwarf2_read_section (objfile, &dwz->macro);
24444 include_section = &dwz->macro;
24445 include_bfd = get_section_bfd_owner (include_section);
24446 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24450 new_mac_ptr = include_section->buffer + offset;
24451 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24455 /* This has actually happened; see
24456 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24457 complaint (&symfile_complaints,
24458 _("recursive DW_MACRO_import in "
24459 ".debug_macro section"));
24463 *slot = (void *) new_mac_ptr;
24465 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24466 include_bfd, new_mac_ptr,
24467 include_mac_end, current_file, lh,
24468 section, section_is_gnu, is_dwz,
24469 offset_size, include_hash);
24471 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24476 case DW_MACINFO_vendor_ext:
24477 if (!section_is_gnu)
24479 unsigned int bytes_read;
24481 /* This reads the constant, but since we don't recognize
24482 any vendor extensions, we ignore it. */
24483 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24484 mac_ptr += bytes_read;
24485 read_direct_string (abfd, mac_ptr, &bytes_read);
24486 mac_ptr += bytes_read;
24488 /* We don't recognize any vendor extensions. */
24494 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24495 mac_ptr, mac_end, abfd, offset_size,
24497 if (mac_ptr == NULL)
24502 } while (macinfo_type != 0);
24506 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24507 int section_is_gnu)
24509 struct dwarf2_per_objfile *dwarf2_per_objfile
24510 = cu->per_cu->dwarf2_per_objfile;
24511 struct objfile *objfile = dwarf2_per_objfile->objfile;
24512 struct line_header *lh = cu->line_header;
24514 const gdb_byte *mac_ptr, *mac_end;
24515 struct macro_source_file *current_file = 0;
24516 enum dwarf_macro_record_type macinfo_type;
24517 unsigned int offset_size = cu->header.offset_size;
24518 const gdb_byte *opcode_definitions[256];
24520 struct dwarf2_section_info *section;
24521 const char *section_name;
24523 if (cu->dwo_unit != NULL)
24525 if (section_is_gnu)
24527 section = &cu->dwo_unit->dwo_file->sections.macro;
24528 section_name = ".debug_macro.dwo";
24532 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24533 section_name = ".debug_macinfo.dwo";
24538 if (section_is_gnu)
24540 section = &dwarf2_per_objfile->macro;
24541 section_name = ".debug_macro";
24545 section = &dwarf2_per_objfile->macinfo;
24546 section_name = ".debug_macinfo";
24550 dwarf2_read_section (objfile, section);
24551 if (section->buffer == NULL)
24553 complaint (&symfile_complaints, _("missing %s section"), section_name);
24556 abfd = get_section_bfd_owner (section);
24558 /* First pass: Find the name of the base filename.
24559 This filename is needed in order to process all macros whose definition
24560 (or undefinition) comes from the command line. These macros are defined
24561 before the first DW_MACINFO_start_file entry, and yet still need to be
24562 associated to the base file.
24564 To determine the base file name, we scan the macro definitions until we
24565 reach the first DW_MACINFO_start_file entry. We then initialize
24566 CURRENT_FILE accordingly so that any macro definition found before the
24567 first DW_MACINFO_start_file can still be associated to the base file. */
24569 mac_ptr = section->buffer + offset;
24570 mac_end = section->buffer + section->size;
24572 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24573 &offset_size, section_is_gnu);
24574 if (mac_ptr == NULL)
24576 /* We already issued a complaint. */
24582 /* Do we at least have room for a macinfo type byte? */
24583 if (mac_ptr >= mac_end)
24585 /* Complaint is printed during the second pass as GDB will probably
24586 stop the first pass earlier upon finding
24587 DW_MACINFO_start_file. */
24591 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24594 /* Note that we rely on the fact that the corresponding GNU and
24595 DWARF constants are the same. */
24597 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24598 switch (macinfo_type)
24600 /* A zero macinfo type indicates the end of the macro
24605 case DW_MACRO_define:
24606 case DW_MACRO_undef:
24607 /* Only skip the data by MAC_PTR. */
24609 unsigned int bytes_read;
24611 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24612 mac_ptr += bytes_read;
24613 read_direct_string (abfd, mac_ptr, &bytes_read);
24614 mac_ptr += bytes_read;
24618 case DW_MACRO_start_file:
24620 unsigned int bytes_read;
24623 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24624 mac_ptr += bytes_read;
24625 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24626 mac_ptr += bytes_read;
24628 current_file = macro_start_file (file, line, current_file, lh);
24632 case DW_MACRO_end_file:
24633 /* No data to skip by MAC_PTR. */
24636 case DW_MACRO_define_strp:
24637 case DW_MACRO_undef_strp:
24638 case DW_MACRO_define_sup:
24639 case DW_MACRO_undef_sup:
24641 unsigned int bytes_read;
24643 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24644 mac_ptr += bytes_read;
24645 mac_ptr += offset_size;
24649 case DW_MACRO_import:
24650 case DW_MACRO_import_sup:
24651 /* Note that, according to the spec, a transparent include
24652 chain cannot call DW_MACRO_start_file. So, we can just
24653 skip this opcode. */
24654 mac_ptr += offset_size;
24657 case DW_MACINFO_vendor_ext:
24658 /* Only skip the data by MAC_PTR. */
24659 if (!section_is_gnu)
24661 unsigned int bytes_read;
24663 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24664 mac_ptr += bytes_read;
24665 read_direct_string (abfd, mac_ptr, &bytes_read);
24666 mac_ptr += bytes_read;
24671 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24672 mac_ptr, mac_end, abfd, offset_size,
24674 if (mac_ptr == NULL)
24679 } while (macinfo_type != 0 && current_file == NULL);
24681 /* Second pass: Process all entries.
24683 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24684 command-line macro definitions/undefinitions. This flag is unset when we
24685 reach the first DW_MACINFO_start_file entry. */
24687 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24689 NULL, xcalloc, xfree));
24690 mac_ptr = section->buffer + offset;
24691 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24692 *slot = (void *) mac_ptr;
24693 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24694 abfd, mac_ptr, mac_end,
24695 current_file, lh, section,
24696 section_is_gnu, 0, offset_size,
24697 include_hash.get ());
24700 /* Check if the attribute's form is a DW_FORM_block*
24701 if so return true else false. */
24704 attr_form_is_block (const struct attribute *attr)
24706 return (attr == NULL ? 0 :
24707 attr->form == DW_FORM_block1
24708 || attr->form == DW_FORM_block2
24709 || attr->form == DW_FORM_block4
24710 || attr->form == DW_FORM_block
24711 || attr->form == DW_FORM_exprloc);
24714 /* Return non-zero if ATTR's value is a section offset --- classes
24715 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24716 You may use DW_UNSND (attr) to retrieve such offsets.
24718 Section 7.5.4, "Attribute Encodings", explains that no attribute
24719 may have a value that belongs to more than one of these classes; it
24720 would be ambiguous if we did, because we use the same forms for all
24724 attr_form_is_section_offset (const struct attribute *attr)
24726 return (attr->form == DW_FORM_data4
24727 || attr->form == DW_FORM_data8
24728 || attr->form == DW_FORM_sec_offset);
24731 /* Return non-zero if ATTR's value falls in the 'constant' class, or
24732 zero otherwise. When this function returns true, you can apply
24733 dwarf2_get_attr_constant_value to it.
24735 However, note that for some attributes you must check
24736 attr_form_is_section_offset before using this test. DW_FORM_data4
24737 and DW_FORM_data8 are members of both the constant class, and of
24738 the classes that contain offsets into other debug sections
24739 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24740 that, if an attribute's can be either a constant or one of the
24741 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
24742 taken as section offsets, not constants.
24744 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24745 cannot handle that. */
24748 attr_form_is_constant (const struct attribute *attr)
24750 switch (attr->form)
24752 case DW_FORM_sdata:
24753 case DW_FORM_udata:
24754 case DW_FORM_data1:
24755 case DW_FORM_data2:
24756 case DW_FORM_data4:
24757 case DW_FORM_data8:
24758 case DW_FORM_implicit_const:
24766 /* DW_ADDR is always stored already as sect_offset; despite for the forms
24767 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24770 attr_form_is_ref (const struct attribute *attr)
24772 switch (attr->form)
24774 case DW_FORM_ref_addr:
24779 case DW_FORM_ref_udata:
24780 case DW_FORM_GNU_ref_alt:
24787 /* Return the .debug_loc section to use for CU.
24788 For DWO files use .debug_loc.dwo. */
24790 static struct dwarf2_section_info *
24791 cu_debug_loc_section (struct dwarf2_cu *cu)
24793 struct dwarf2_per_objfile *dwarf2_per_objfile
24794 = cu->per_cu->dwarf2_per_objfile;
24798 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24800 return cu->header.version >= 5 ? §ions->loclists : §ions->loc;
24802 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24803 : &dwarf2_per_objfile->loc);
24806 /* A helper function that fills in a dwarf2_loclist_baton. */
24809 fill_in_loclist_baton (struct dwarf2_cu *cu,
24810 struct dwarf2_loclist_baton *baton,
24811 const struct attribute *attr)
24813 struct dwarf2_per_objfile *dwarf2_per_objfile
24814 = cu->per_cu->dwarf2_per_objfile;
24815 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24817 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
24819 baton->per_cu = cu->per_cu;
24820 gdb_assert (baton->per_cu);
24821 /* We don't know how long the location list is, but make sure we
24822 don't run off the edge of the section. */
24823 baton->size = section->size - DW_UNSND (attr);
24824 baton->data = section->buffer + DW_UNSND (attr);
24825 baton->base_address = cu->base_address;
24826 baton->from_dwo = cu->dwo_unit != NULL;
24830 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24831 struct dwarf2_cu *cu, int is_block)
24833 struct dwarf2_per_objfile *dwarf2_per_objfile
24834 = cu->per_cu->dwarf2_per_objfile;
24835 struct objfile *objfile = dwarf2_per_objfile->objfile;
24836 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24838 if (attr_form_is_section_offset (attr)
24839 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24840 the section. If so, fall through to the complaint in the
24842 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
24844 struct dwarf2_loclist_baton *baton;
24846 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24848 fill_in_loclist_baton (cu, baton, attr);
24850 if (cu->base_known == 0)
24851 complaint (&symfile_complaints,
24852 _("Location list used without "
24853 "specifying the CU base address."));
24855 SYMBOL_ACLASS_INDEX (sym) = (is_block
24856 ? dwarf2_loclist_block_index
24857 : dwarf2_loclist_index);
24858 SYMBOL_LOCATION_BATON (sym) = baton;
24862 struct dwarf2_locexpr_baton *baton;
24864 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24865 baton->per_cu = cu->per_cu;
24866 gdb_assert (baton->per_cu);
24868 if (attr_form_is_block (attr))
24870 /* Note that we're just copying the block's data pointer
24871 here, not the actual data. We're still pointing into the
24872 info_buffer for SYM's objfile; right now we never release
24873 that buffer, but when we do clean up properly this may
24875 baton->size = DW_BLOCK (attr)->size;
24876 baton->data = DW_BLOCK (attr)->data;
24880 dwarf2_invalid_attrib_class_complaint ("location description",
24881 SYMBOL_NATURAL_NAME (sym));
24885 SYMBOL_ACLASS_INDEX (sym) = (is_block
24886 ? dwarf2_locexpr_block_index
24887 : dwarf2_locexpr_index);
24888 SYMBOL_LOCATION_BATON (sym) = baton;
24892 /* Return the OBJFILE associated with the compilation unit CU. If CU
24893 came from a separate debuginfo file, then the master objfile is
24897 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24899 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24901 /* Return the master objfile, so that we can report and look up the
24902 correct file containing this variable. */
24903 if (objfile->separate_debug_objfile_backlink)
24904 objfile = objfile->separate_debug_objfile_backlink;
24909 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24910 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24911 CU_HEADERP first. */
24913 static const struct comp_unit_head *
24914 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24915 struct dwarf2_per_cu_data *per_cu)
24917 const gdb_byte *info_ptr;
24920 return &per_cu->cu->header;
24922 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24924 memset (cu_headerp, 0, sizeof (*cu_headerp));
24925 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24926 rcuh_kind::COMPILE);
24931 /* Return the address size given in the compilation unit header for CU. */
24934 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24936 struct comp_unit_head cu_header_local;
24937 const struct comp_unit_head *cu_headerp;
24939 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24941 return cu_headerp->addr_size;
24944 /* Return the offset size given in the compilation unit header for CU. */
24947 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24949 struct comp_unit_head cu_header_local;
24950 const struct comp_unit_head *cu_headerp;
24952 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24954 return cu_headerp->offset_size;
24957 /* See its dwarf2loc.h declaration. */
24960 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24962 struct comp_unit_head cu_header_local;
24963 const struct comp_unit_head *cu_headerp;
24965 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24967 if (cu_headerp->version == 2)
24968 return cu_headerp->addr_size;
24970 return cu_headerp->offset_size;
24973 /* Return the text offset of the CU. The returned offset comes from
24974 this CU's objfile. If this objfile came from a separate debuginfo
24975 file, then the offset may be different from the corresponding
24976 offset in the parent objfile. */
24979 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24981 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24983 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24986 /* Return DWARF version number of PER_CU. */
24989 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24991 return per_cu->dwarf_version;
24994 /* Locate the .debug_info compilation unit from CU's objfile which contains
24995 the DIE at OFFSET. Raises an error on failure. */
24997 static struct dwarf2_per_cu_data *
24998 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24999 unsigned int offset_in_dwz,
25000 struct dwarf2_per_objfile *dwarf2_per_objfile)
25002 struct dwarf2_per_cu_data *this_cu;
25004 const sect_offset *cu_off;
25007 high = dwarf2_per_objfile->n_comp_units - 1;
25010 struct dwarf2_per_cu_data *mid_cu;
25011 int mid = low + (high - low) / 2;
25013 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25014 cu_off = &mid_cu->sect_off;
25015 if (mid_cu->is_dwz > offset_in_dwz
25016 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
25021 gdb_assert (low == high);
25022 this_cu = dwarf2_per_objfile->all_comp_units[low];
25023 cu_off = &this_cu->sect_off;
25024 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
25026 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25027 error (_("Dwarf Error: could not find partial DIE containing "
25028 "offset 0x%x [in module %s]"),
25029 to_underlying (sect_off),
25030 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25032 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25034 return dwarf2_per_objfile->all_comp_units[low-1];
25038 this_cu = dwarf2_per_objfile->all_comp_units[low];
25039 if (low == dwarf2_per_objfile->n_comp_units - 1
25040 && sect_off >= this_cu->sect_off + this_cu->length)
25041 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
25042 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25047 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25049 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25050 : per_cu (per_cu_),
25053 checked_producer (0),
25054 producer_is_gxx_lt_4_6 (0),
25055 producer_is_gcc_lt_4_3 (0),
25056 producer_is_icc_lt_14 (0),
25057 processing_has_namespace_info (0)
25062 /* Destroy a dwarf2_cu. */
25064 dwarf2_cu::~dwarf2_cu ()
25069 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25072 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25073 enum language pretend_language)
25075 struct attribute *attr;
25077 /* Set the language we're debugging. */
25078 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25080 set_cu_language (DW_UNSND (attr), cu);
25083 cu->language = pretend_language;
25084 cu->language_defn = language_def (cu->language);
25087 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25090 /* Free all cached compilation units. */
25093 free_cached_comp_units (void *data)
25095 struct dwarf2_per_objfile *dwarf2_per_objfile
25096 = (struct dwarf2_per_objfile *) data;
25098 dwarf2_per_objfile->free_cached_comp_units ();
25101 /* Increase the age counter on each cached compilation unit, and free
25102 any that are too old. */
25105 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25107 struct dwarf2_per_cu_data *per_cu, **last_chain;
25109 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25110 per_cu = dwarf2_per_objfile->read_in_chain;
25111 while (per_cu != NULL)
25113 per_cu->cu->last_used ++;
25114 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25115 dwarf2_mark (per_cu->cu);
25116 per_cu = per_cu->cu->read_in_chain;
25119 per_cu = dwarf2_per_objfile->read_in_chain;
25120 last_chain = &dwarf2_per_objfile->read_in_chain;
25121 while (per_cu != NULL)
25123 struct dwarf2_per_cu_data *next_cu;
25125 next_cu = per_cu->cu->read_in_chain;
25127 if (!per_cu->cu->mark)
25130 *last_chain = next_cu;
25133 last_chain = &per_cu->cu->read_in_chain;
25139 /* Remove a single compilation unit from the cache. */
25142 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25144 struct dwarf2_per_cu_data *per_cu, **last_chain;
25145 struct dwarf2_per_objfile *dwarf2_per_objfile
25146 = target_per_cu->dwarf2_per_objfile;
25148 per_cu = dwarf2_per_objfile->read_in_chain;
25149 last_chain = &dwarf2_per_objfile->read_in_chain;
25150 while (per_cu != NULL)
25152 struct dwarf2_per_cu_data *next_cu;
25154 next_cu = per_cu->cu->read_in_chain;
25156 if (per_cu == target_per_cu)
25160 *last_chain = next_cu;
25164 last_chain = &per_cu->cu->read_in_chain;
25170 /* Release all extra memory associated with OBJFILE. */
25173 dwarf2_free_objfile (struct objfile *objfile)
25175 struct dwarf2_per_objfile *dwarf2_per_objfile
25176 = get_dwarf2_per_objfile (objfile);
25178 if (dwarf2_per_objfile == NULL)
25181 dwarf2_per_objfile->~dwarf2_per_objfile ();
25184 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25185 We store these in a hash table separate from the DIEs, and preserve them
25186 when the DIEs are flushed out of cache.
25188 The CU "per_cu" pointer is needed because offset alone is not enough to
25189 uniquely identify the type. A file may have multiple .debug_types sections,
25190 or the type may come from a DWO file. Furthermore, while it's more logical
25191 to use per_cu->section+offset, with Fission the section with the data is in
25192 the DWO file but we don't know that section at the point we need it.
25193 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25194 because we can enter the lookup routine, get_die_type_at_offset, from
25195 outside this file, and thus won't necessarily have PER_CU->cu.
25196 Fortunately, PER_CU is stable for the life of the objfile. */
25198 struct dwarf2_per_cu_offset_and_type
25200 const struct dwarf2_per_cu_data *per_cu;
25201 sect_offset sect_off;
25205 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25208 per_cu_offset_and_type_hash (const void *item)
25210 const struct dwarf2_per_cu_offset_and_type *ofs
25211 = (const struct dwarf2_per_cu_offset_and_type *) item;
25213 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25216 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25219 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25221 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25222 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25223 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25224 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25226 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25227 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25230 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25231 table if necessary. For convenience, return TYPE.
25233 The DIEs reading must have careful ordering to:
25234 * Not cause infite loops trying to read in DIEs as a prerequisite for
25235 reading current DIE.
25236 * Not trying to dereference contents of still incompletely read in types
25237 while reading in other DIEs.
25238 * Enable referencing still incompletely read in types just by a pointer to
25239 the type without accessing its fields.
25241 Therefore caller should follow these rules:
25242 * Try to fetch any prerequisite types we may need to build this DIE type
25243 before building the type and calling set_die_type.
25244 * After building type call set_die_type for current DIE as soon as
25245 possible before fetching more types to complete the current type.
25246 * Make the type as complete as possible before fetching more types. */
25248 static struct type *
25249 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25251 struct dwarf2_per_objfile *dwarf2_per_objfile
25252 = cu->per_cu->dwarf2_per_objfile;
25253 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25254 struct objfile *objfile = dwarf2_per_objfile->objfile;
25255 struct attribute *attr;
25256 struct dynamic_prop prop;
25258 /* For Ada types, make sure that the gnat-specific data is always
25259 initialized (if not already set). There are a few types where
25260 we should not be doing so, because the type-specific area is
25261 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25262 where the type-specific area is used to store the floatformat).
25263 But this is not a problem, because the gnat-specific information
25264 is actually not needed for these types. */
25265 if (need_gnat_info (cu)
25266 && TYPE_CODE (type) != TYPE_CODE_FUNC
25267 && TYPE_CODE (type) != TYPE_CODE_FLT
25268 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25269 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25270 && TYPE_CODE (type) != TYPE_CODE_METHOD
25271 && !HAVE_GNAT_AUX_INFO (type))
25272 INIT_GNAT_SPECIFIC (type);
25274 /* Read DW_AT_allocated and set in type. */
25275 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25276 if (attr_form_is_block (attr))
25278 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25279 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
25281 else if (attr != NULL)
25283 complaint (&symfile_complaints,
25284 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
25285 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25286 to_underlying (die->sect_off));
25289 /* Read DW_AT_associated and set in type. */
25290 attr = dwarf2_attr (die, DW_AT_associated, cu);
25291 if (attr_form_is_block (attr))
25293 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25294 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
25296 else if (attr != NULL)
25298 complaint (&symfile_complaints,
25299 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
25300 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25301 to_underlying (die->sect_off));
25304 /* Read DW_AT_data_location and set in type. */
25305 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25306 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25307 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
25309 if (dwarf2_per_objfile->die_type_hash == NULL)
25311 dwarf2_per_objfile->die_type_hash =
25312 htab_create_alloc_ex (127,
25313 per_cu_offset_and_type_hash,
25314 per_cu_offset_and_type_eq,
25316 &objfile->objfile_obstack,
25317 hashtab_obstack_allocate,
25318 dummy_obstack_deallocate);
25321 ofs.per_cu = cu->per_cu;
25322 ofs.sect_off = die->sect_off;
25324 slot = (struct dwarf2_per_cu_offset_and_type **)
25325 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25327 complaint (&symfile_complaints,
25328 _("A problem internal to GDB: DIE 0x%x has type already set"),
25329 to_underlying (die->sect_off));
25330 *slot = XOBNEW (&objfile->objfile_obstack,
25331 struct dwarf2_per_cu_offset_and_type);
25336 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25337 or return NULL if the die does not have a saved type. */
25339 static struct type *
25340 get_die_type_at_offset (sect_offset sect_off,
25341 struct dwarf2_per_cu_data *per_cu)
25343 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25344 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25346 if (dwarf2_per_objfile->die_type_hash == NULL)
25349 ofs.per_cu = per_cu;
25350 ofs.sect_off = sect_off;
25351 slot = ((struct dwarf2_per_cu_offset_and_type *)
25352 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25359 /* Look up the type for DIE in CU in die_type_hash,
25360 or return NULL if DIE does not have a saved type. */
25362 static struct type *
25363 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25365 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25368 /* Add a dependence relationship from CU to REF_PER_CU. */
25371 dwarf2_add_dependence (struct dwarf2_cu *cu,
25372 struct dwarf2_per_cu_data *ref_per_cu)
25376 if (cu->dependencies == NULL)
25378 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25379 NULL, &cu->comp_unit_obstack,
25380 hashtab_obstack_allocate,
25381 dummy_obstack_deallocate);
25383 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25385 *slot = ref_per_cu;
25388 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25389 Set the mark field in every compilation unit in the
25390 cache that we must keep because we are keeping CU. */
25393 dwarf2_mark_helper (void **slot, void *data)
25395 struct dwarf2_per_cu_data *per_cu;
25397 per_cu = (struct dwarf2_per_cu_data *) *slot;
25399 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25400 reading of the chain. As such dependencies remain valid it is not much
25401 useful to track and undo them during QUIT cleanups. */
25402 if (per_cu->cu == NULL)
25405 if (per_cu->cu->mark)
25407 per_cu->cu->mark = 1;
25409 if (per_cu->cu->dependencies != NULL)
25410 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25415 /* Set the mark field in CU and in every other compilation unit in the
25416 cache that we must keep because we are keeping CU. */
25419 dwarf2_mark (struct dwarf2_cu *cu)
25424 if (cu->dependencies != NULL)
25425 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25429 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25433 per_cu->cu->mark = 0;
25434 per_cu = per_cu->cu->read_in_chain;
25438 /* Trivial hash function for partial_die_info: the hash value of a DIE
25439 is its offset in .debug_info for this objfile. */
25442 partial_die_hash (const void *item)
25444 const struct partial_die_info *part_die
25445 = (const struct partial_die_info *) item;
25447 return to_underlying (part_die->sect_off);
25450 /* Trivial comparison function for partial_die_info structures: two DIEs
25451 are equal if they have the same offset. */
25454 partial_die_eq (const void *item_lhs, const void *item_rhs)
25456 const struct partial_die_info *part_die_lhs
25457 = (const struct partial_die_info *) item_lhs;
25458 const struct partial_die_info *part_die_rhs
25459 = (const struct partial_die_info *) item_rhs;
25461 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25464 static struct cmd_list_element *set_dwarf_cmdlist;
25465 static struct cmd_list_element *show_dwarf_cmdlist;
25468 set_dwarf_cmd (const char *args, int from_tty)
25470 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25475 show_dwarf_cmd (const char *args, int from_tty)
25477 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25480 /* Free data associated with OBJFILE, if necessary. */
25483 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
25485 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
25488 for (ix = 0; ix < data->n_comp_units; ++ix)
25489 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
25491 for (ix = 0; ix < data->n_type_units; ++ix)
25492 VEC_free (dwarf2_per_cu_ptr,
25493 data->all_type_units[ix]->per_cu.imported_symtabs);
25494 xfree (data->all_type_units);
25496 VEC_free (dwarf2_section_info_def, data->types);
25498 if (data->dwo_files)
25499 free_dwo_files (data->dwo_files, objfile);
25500 if (data->dwp_file)
25501 gdb_bfd_unref (data->dwp_file->dbfd);
25503 if (data->dwz_file && data->dwz_file->dwz_bfd)
25504 gdb_bfd_unref (data->dwz_file->dwz_bfd);
25506 if (data->index_table != NULL)
25507 data->index_table->~mapped_index ();
25511 /* The "save gdb-index" command. */
25513 /* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
25517 file_write (FILE *file, const void *data, size_t size)
25519 if (fwrite (data, 1, size, file) != size)
25520 error (_("couldn't data write to file"));
25523 /* Write the contents of VEC to FILE, with error checking. */
25525 template<typename Elem, typename Alloc>
25527 file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
25529 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
25532 /* In-memory buffer to prepare data to be written later to a file. */
25536 /* Copy DATA to the end of the buffer. */
25537 template<typename T>
25538 void append_data (const T &data)
25540 std::copy (reinterpret_cast<const gdb_byte *> (&data),
25541 reinterpret_cast<const gdb_byte *> (&data + 1),
25542 grow (sizeof (data)));
25545 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
25546 terminating zero is appended too. */
25547 void append_cstr0 (const char *cstr)
25549 const size_t size = strlen (cstr) + 1;
25550 std::copy (cstr, cstr + size, grow (size));
25553 /* Store INPUT as ULEB128 to the end of buffer. */
25554 void append_unsigned_leb128 (ULONGEST input)
25558 gdb_byte output = input & 0x7f;
25562 append_data (output);
25568 /* Accept a host-format integer in VAL and append it to the buffer
25569 as a target-format integer which is LEN bytes long. */
25570 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
25572 ::store_unsigned_integer (grow (len), len, byte_order, val);
25575 /* Return the size of the buffer. */
25576 size_t size () const
25578 return m_vec.size ();
25581 /* Return true iff the buffer is empty. */
25582 bool empty () const
25584 return m_vec.empty ();
25587 /* Write the buffer to FILE. */
25588 void file_write (FILE *file) const
25590 ::file_write (file, m_vec);
25594 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
25595 the start of the new block. */
25596 gdb_byte *grow (size_t size)
25598 m_vec.resize (m_vec.size () + size);
25599 return &*m_vec.end () - size;
25602 gdb::byte_vector m_vec;
25605 /* An entry in the symbol table. */
25606 struct symtab_index_entry
25608 /* The name of the symbol. */
25610 /* The offset of the name in the constant pool. */
25611 offset_type index_offset;
25612 /* A sorted vector of the indices of all the CUs that hold an object
25614 std::vector<offset_type> cu_indices;
25617 /* The symbol table. This is a power-of-2-sized hash table. */
25618 struct mapped_symtab
25622 data.resize (1024);
25625 offset_type n_elements = 0;
25626 std::vector<symtab_index_entry> data;
25629 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
25632 Function is used only during write_hash_table so no index format backward
25633 compatibility is needed. */
25635 static symtab_index_entry &
25636 find_slot (struct mapped_symtab *symtab, const char *name)
25638 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
25640 index = hash & (symtab->data.size () - 1);
25641 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
25645 if (symtab->data[index].name == NULL
25646 || strcmp (name, symtab->data[index].name) == 0)
25647 return symtab->data[index];
25648 index = (index + step) & (symtab->data.size () - 1);
25652 /* Expand SYMTAB's hash table. */
25655 hash_expand (struct mapped_symtab *symtab)
25657 auto old_entries = std::move (symtab->data);
25659 symtab->data.clear ();
25660 symtab->data.resize (old_entries.size () * 2);
25662 for (auto &it : old_entries)
25663 if (it.name != NULL)
25665 auto &ref = find_slot (symtab, it.name);
25666 ref = std::move (it);
25670 /* Add an entry to SYMTAB. NAME is the name of the symbol.
25671 CU_INDEX is the index of the CU in which the symbol appears.
25672 IS_STATIC is one if the symbol is static, otherwise zero (global). */
25675 add_index_entry (struct mapped_symtab *symtab, const char *name,
25676 int is_static, gdb_index_symbol_kind kind,
25677 offset_type cu_index)
25679 offset_type cu_index_and_attrs;
25681 ++symtab->n_elements;
25682 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
25683 hash_expand (symtab);
25685 symtab_index_entry &slot = find_slot (symtab, name);
25686 if (slot.name == NULL)
25689 /* index_offset is set later. */
25692 cu_index_and_attrs = 0;
25693 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
25694 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
25695 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
25697 /* We don't want to record an index value twice as we want to avoid the
25699 We process all global symbols and then all static symbols
25700 (which would allow us to avoid the duplication by only having to check
25701 the last entry pushed), but a symbol could have multiple kinds in one CU.
25702 To keep things simple we don't worry about the duplication here and
25703 sort and uniqufy the list after we've processed all symbols. */
25704 slot.cu_indices.push_back (cu_index_and_attrs);
25707 /* Sort and remove duplicates of all symbols' cu_indices lists. */
25710 uniquify_cu_indices (struct mapped_symtab *symtab)
25712 for (auto &entry : symtab->data)
25714 if (entry.name != NULL && !entry.cu_indices.empty ())
25716 auto &cu_indices = entry.cu_indices;
25717 std::sort (cu_indices.begin (), cu_indices.end ());
25718 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
25719 cu_indices.erase (from, cu_indices.end ());
25724 /* A form of 'const char *' suitable for container keys. Only the
25725 pointer is stored. The strings themselves are compared, not the
25730 c_str_view (const char *cstr)
25734 bool operator== (const c_str_view &other) const
25736 return strcmp (m_cstr, other.m_cstr) == 0;
25739 /* Return the underlying C string. Note, the returned string is
25740 only a reference with lifetime of this object. */
25741 const char *c_str () const
25747 friend class c_str_view_hasher;
25748 const char *const m_cstr;
25751 /* A std::unordered_map::hasher for c_str_view that uses the right
25752 hash function for strings in a mapped index. */
25753 class c_str_view_hasher
25756 size_t operator () (const c_str_view &x) const
25758 return mapped_index_string_hash (INT_MAX, x.m_cstr);
25762 /* A std::unordered_map::hasher for std::vector<>. */
25763 template<typename T>
25764 class vector_hasher
25767 size_t operator () (const std::vector<T> &key) const
25769 return iterative_hash (key.data (),
25770 sizeof (key.front ()) * key.size (), 0);
25774 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
25775 constant pool entries going into the data buffer CPOOL. */
25778 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
25781 /* Elements are sorted vectors of the indices of all the CUs that
25782 hold an object of this name. */
25783 std::unordered_map<std::vector<offset_type>, offset_type,
25784 vector_hasher<offset_type>>
25787 /* We add all the index vectors to the constant pool first, to
25788 ensure alignment is ok. */
25789 for (symtab_index_entry &entry : symtab->data)
25791 if (entry.name == NULL)
25793 gdb_assert (entry.index_offset == 0);
25795 /* Finding before inserting is faster than always trying to
25796 insert, because inserting always allocates a node, does the
25797 lookup, and then destroys the new node if another node
25798 already had the same key. C++17 try_emplace will avoid
25801 = symbol_hash_table.find (entry.cu_indices);
25802 if (found != symbol_hash_table.end ())
25804 entry.index_offset = found->second;
25808 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
25809 entry.index_offset = cpool.size ();
25810 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
25811 for (const auto index : entry.cu_indices)
25812 cpool.append_data (MAYBE_SWAP (index));
25816 /* Now write out the hash table. */
25817 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
25818 for (const auto &entry : symtab->data)
25820 offset_type str_off, vec_off;
25822 if (entry.name != NULL)
25824 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
25825 if (insertpair.second)
25826 cpool.append_cstr0 (entry.name);
25827 str_off = insertpair.first->second;
25828 vec_off = entry.index_offset;
25832 /* While 0 is a valid constant pool index, it is not valid
25833 to have 0 for both offsets. */
25838 output.append_data (MAYBE_SWAP (str_off));
25839 output.append_data (MAYBE_SWAP (vec_off));
25843 typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
25845 /* Helper struct for building the address table. */
25846 struct addrmap_index_data
25848 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
25849 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
25852 struct objfile *objfile;
25853 data_buf &addr_vec;
25854 psym_index_map &cu_index_htab;
25856 /* Non-zero if the previous_* fields are valid.
25857 We can't write an entry until we see the next entry (since it is only then
25858 that we know the end of the entry). */
25859 int previous_valid;
25860 /* Index of the CU in the table of all CUs in the index file. */
25861 unsigned int previous_cu_index;
25862 /* Start address of the CU. */
25863 CORE_ADDR previous_cu_start;
25866 /* Write an address entry to ADDR_VEC. */
25869 add_address_entry (struct objfile *objfile, data_buf &addr_vec,
25870 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
25872 CORE_ADDR baseaddr;
25874 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25876 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
25877 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
25878 addr_vec.append_data (MAYBE_SWAP (cu_index));
25881 /* Worker function for traversing an addrmap to build the address table. */
25884 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
25886 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
25887 struct partial_symtab *pst = (struct partial_symtab *) obj;
25889 if (data->previous_valid)
25890 add_address_entry (data->objfile, data->addr_vec,
25891 data->previous_cu_start, start_addr,
25892 data->previous_cu_index);
25894 data->previous_cu_start = start_addr;
25897 const auto it = data->cu_index_htab.find (pst);
25898 gdb_assert (it != data->cu_index_htab.cend ());
25899 data->previous_cu_index = it->second;
25900 data->previous_valid = 1;
25903 data->previous_valid = 0;
25908 /* Write OBJFILE's address map to ADDR_VEC.
25909 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
25910 in the index file. */
25913 write_address_map (struct objfile *objfile, data_buf &addr_vec,
25914 psym_index_map &cu_index_htab)
25916 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
25918 /* When writing the address table, we have to cope with the fact that
25919 the addrmap iterator only provides the start of a region; we have to
25920 wait until the next invocation to get the start of the next region. */
25922 addrmap_index_data.objfile = objfile;
25923 addrmap_index_data.previous_valid = 0;
25925 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
25926 &addrmap_index_data);
25928 /* It's highly unlikely the last entry (end address = 0xff...ff)
25929 is valid, but we should still handle it.
25930 The end address is recorded as the start of the next region, but that
25931 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
25933 if (addrmap_index_data.previous_valid)
25934 add_address_entry (objfile, addr_vec,
25935 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
25936 addrmap_index_data.previous_cu_index);
25939 /* Return the symbol kind of PSYM. */
25941 static gdb_index_symbol_kind
25942 symbol_kind (struct partial_symbol *psym)
25944 domain_enum domain = PSYMBOL_DOMAIN (psym);
25945 enum address_class aclass = PSYMBOL_CLASS (psym);
25953 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
25955 return GDB_INDEX_SYMBOL_KIND_TYPE;
25957 case LOC_CONST_BYTES:
25958 case LOC_OPTIMIZED_OUT:
25960 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25962 /* Note: It's currently impossible to recognize psyms as enum values
25963 short of reading the type info. For now punt. */
25964 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25966 /* There are other LOC_FOO values that one might want to classify
25967 as variables, but dwarf2read.c doesn't currently use them. */
25968 return GDB_INDEX_SYMBOL_KIND_OTHER;
25970 case STRUCT_DOMAIN:
25971 return GDB_INDEX_SYMBOL_KIND_TYPE;
25973 return GDB_INDEX_SYMBOL_KIND_OTHER;
25977 /* Add a list of partial symbols to SYMTAB. */
25980 write_psymbols (struct mapped_symtab *symtab,
25981 std::unordered_set<partial_symbol *> &psyms_seen,
25982 struct partial_symbol **psymp,
25984 offset_type cu_index,
25987 for (; count-- > 0; ++psymp)
25989 struct partial_symbol *psym = *psymp;
25991 if (SYMBOL_LANGUAGE (psym) == language_ada)
25992 error (_("Ada is not currently supported by the index"));
25994 /* Only add a given psymbol once. */
25995 if (psyms_seen.insert (psym).second)
25997 gdb_index_symbol_kind kind = symbol_kind (psym);
25999 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
26000 is_static, kind, cu_index);
26005 /* A helper struct used when iterating over debug_types. */
26006 struct signatured_type_index_data
26008 signatured_type_index_data (data_buf &types_list_,
26009 std::unordered_set<partial_symbol *> &psyms_seen_)
26010 : types_list (types_list_), psyms_seen (psyms_seen_)
26013 struct objfile *objfile;
26014 struct mapped_symtab *symtab;
26015 data_buf &types_list;
26016 std::unordered_set<partial_symbol *> &psyms_seen;
26020 /* A helper function that writes a single signatured_type to an
26024 write_one_signatured_type (void **slot, void *d)
26026 struct signatured_type_index_data *info
26027 = (struct signatured_type_index_data *) d;
26028 struct signatured_type *entry = (struct signatured_type *) *slot;
26029 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
26031 write_psymbols (info->symtab,
26033 &info->objfile->global_psymbols[psymtab->globals_offset],
26034 psymtab->n_global_syms, info->cu_index,
26036 write_psymbols (info->symtab,
26038 &info->objfile->static_psymbols[psymtab->statics_offset],
26039 psymtab->n_static_syms, info->cu_index,
26042 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26043 to_underlying (entry->per_cu.sect_off));
26044 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26045 to_underlying (entry->type_offset_in_tu));
26046 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
26053 /* Recurse into all "included" dependencies and count their symbols as
26054 if they appeared in this psymtab. */
26057 recursively_count_psymbols (struct partial_symtab *psymtab,
26058 size_t &psyms_seen)
26060 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26061 if (psymtab->dependencies[i]->user != NULL)
26062 recursively_count_psymbols (psymtab->dependencies[i],
26065 psyms_seen += psymtab->n_global_syms;
26066 psyms_seen += psymtab->n_static_syms;
26069 /* Recurse into all "included" dependencies and write their symbols as
26070 if they appeared in this psymtab. */
26073 recursively_write_psymbols (struct objfile *objfile,
26074 struct partial_symtab *psymtab,
26075 struct mapped_symtab *symtab,
26076 std::unordered_set<partial_symbol *> &psyms_seen,
26077 offset_type cu_index)
26081 for (i = 0; i < psymtab->number_of_dependencies; ++i)
26082 if (psymtab->dependencies[i]->user != NULL)
26083 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26084 symtab, psyms_seen, cu_index);
26086 write_psymbols (symtab,
26088 &objfile->global_psymbols[psymtab->globals_offset],
26089 psymtab->n_global_syms, cu_index,
26091 write_psymbols (symtab,
26093 &objfile->static_psymbols[psymtab->statics_offset],
26094 psymtab->n_static_syms, cu_index,
26098 /* DWARF-5 .debug_names builder. */
26102 debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile, bool is_dwarf64,
26103 bfd_endian dwarf5_byte_order)
26104 : m_dwarf5_byte_order (dwarf5_byte_order),
26105 m_dwarf32 (dwarf5_byte_order),
26106 m_dwarf64 (dwarf5_byte_order),
26107 m_dwarf (is_dwarf64
26108 ? static_cast<dwarf &> (m_dwarf64)
26109 : static_cast<dwarf &> (m_dwarf32)),
26110 m_name_table_string_offs (m_dwarf.name_table_string_offs),
26111 m_name_table_entry_offs (m_dwarf.name_table_entry_offs),
26112 m_debugstrlookup (dwarf2_per_objfile)
26115 int dwarf5_offset_size () const
26117 const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64;
26118 return dwarf5_is_dwarf64 ? 8 : 4;
26121 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
26122 enum class unit_kind { cu, tu };
26124 /* Insert one symbol. */
26125 void insert (const partial_symbol *psym, int cu_index, bool is_static,
26128 const int dwarf_tag = psymbol_tag (psym);
26129 if (dwarf_tag == 0)
26131 const char *const name = SYMBOL_SEARCH_NAME (psym);
26132 const auto insertpair
26133 = m_name_to_value_set.emplace (c_str_view (name),
26134 std::set<symbol_value> ());
26135 std::set<symbol_value> &value_set = insertpair.first->second;
26136 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static, kind));
26139 /* Build all the tables. All symbols must be already inserted.
26140 This function does not call file_write, caller has to do it
26144 /* Verify the build method has not be called twice. */
26145 gdb_assert (m_abbrev_table.empty ());
26146 const size_t name_count = m_name_to_value_set.size ();
26147 m_bucket_table.resize
26148 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
26149 m_hash_table.reserve (name_count);
26150 m_name_table_string_offs.reserve (name_count);
26151 m_name_table_entry_offs.reserve (name_count);
26153 /* Map each hash of symbol to its name and value. */
26154 struct hash_it_pair
26157 decltype (m_name_to_value_set)::const_iterator it;
26159 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
26160 bucket_hash.resize (m_bucket_table.size ());
26161 for (decltype (m_name_to_value_set)::const_iterator it
26162 = m_name_to_value_set.cbegin ();
26163 it != m_name_to_value_set.cend ();
26166 const char *const name = it->first.c_str ();
26167 const uint32_t hash = dwarf5_djb_hash (name);
26168 hash_it_pair hashitpair;
26169 hashitpair.hash = hash;
26170 hashitpair.it = it;
26171 auto &slot = bucket_hash[hash % bucket_hash.size()];
26172 slot.push_front (std::move (hashitpair));
26174 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
26176 const std::forward_list<hash_it_pair> &hashitlist
26177 = bucket_hash[bucket_ix];
26178 if (hashitlist.empty ())
26180 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
26181 /* The hashes array is indexed starting at 1. */
26182 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
26183 sizeof (bucket_slot), m_dwarf5_byte_order,
26184 m_hash_table.size () + 1);
26185 for (const hash_it_pair &hashitpair : hashitlist)
26187 m_hash_table.push_back (0);
26188 store_unsigned_integer (reinterpret_cast<gdb_byte *>
26189 (&m_hash_table.back ()),
26190 sizeof (m_hash_table.back ()),
26191 m_dwarf5_byte_order, hashitpair.hash);
26192 const c_str_view &name = hashitpair.it->first;
26193 const std::set<symbol_value> &value_set = hashitpair.it->second;
26194 m_name_table_string_offs.push_back_reorder
26195 (m_debugstrlookup.lookup (name.c_str ()));
26196 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
26197 gdb_assert (!value_set.empty ());
26198 for (const symbol_value &value : value_set)
26200 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
26205 idx = m_idx_next++;
26206 m_abbrev_table.append_unsigned_leb128 (idx);
26207 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
26208 m_abbrev_table.append_unsigned_leb128
26209 (value.kind == unit_kind::cu ? DW_IDX_compile_unit
26210 : DW_IDX_type_unit);
26211 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
26212 m_abbrev_table.append_unsigned_leb128 (value.is_static
26213 ? DW_IDX_GNU_internal
26214 : DW_IDX_GNU_external);
26215 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
26217 /* Terminate attributes list. */
26218 m_abbrev_table.append_unsigned_leb128 (0);
26219 m_abbrev_table.append_unsigned_leb128 (0);
26222 m_entry_pool.append_unsigned_leb128 (idx);
26223 m_entry_pool.append_unsigned_leb128 (value.cu_index);
26226 /* Terminate the list of CUs. */
26227 m_entry_pool.append_unsigned_leb128 (0);
26230 gdb_assert (m_hash_table.size () == name_count);
26232 /* Terminate tags list. */
26233 m_abbrev_table.append_unsigned_leb128 (0);
26236 /* Return .debug_names bucket count. This must be called only after
26237 calling the build method. */
26238 uint32_t bucket_count () const
26240 /* Verify the build method has been already called. */
26241 gdb_assert (!m_abbrev_table.empty ());
26242 const uint32_t retval = m_bucket_table.size ();
26244 /* Check for overflow. */
26245 gdb_assert (retval == m_bucket_table.size ());
26249 /* Return .debug_names names count. This must be called only after
26250 calling the build method. */
26251 uint32_t name_count () const
26253 /* Verify the build method has been already called. */
26254 gdb_assert (!m_abbrev_table.empty ());
26255 const uint32_t retval = m_hash_table.size ();
26257 /* Check for overflow. */
26258 gdb_assert (retval == m_hash_table.size ());
26262 /* Return number of bytes of .debug_names abbreviation table. This
26263 must be called only after calling the build method. */
26264 uint32_t abbrev_table_bytes () const
26266 gdb_assert (!m_abbrev_table.empty ());
26267 return m_abbrev_table.size ();
26270 /* Recurse into all "included" dependencies and store their symbols
26271 as if they appeared in this psymtab. */
26272 void recursively_write_psymbols
26273 (struct objfile *objfile,
26274 struct partial_symtab *psymtab,
26275 std::unordered_set<partial_symbol *> &psyms_seen,
26278 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26279 if (psymtab->dependencies[i]->user != NULL)
26280 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26281 psyms_seen, cu_index);
26283 write_psymbols (psyms_seen,
26284 &objfile->global_psymbols[psymtab->globals_offset],
26285 psymtab->n_global_syms, cu_index, false, unit_kind::cu);
26286 write_psymbols (psyms_seen,
26287 &objfile->static_psymbols[psymtab->statics_offset],
26288 psymtab->n_static_syms, cu_index, true, unit_kind::cu);
26291 /* Return number of bytes the .debug_names section will have. This
26292 must be called only after calling the build method. */
26293 size_t bytes () const
26295 /* Verify the build method has been already called. */
26296 gdb_assert (!m_abbrev_table.empty ());
26297 size_t expected_bytes = 0;
26298 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
26299 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
26300 expected_bytes += m_name_table_string_offs.bytes ();
26301 expected_bytes += m_name_table_entry_offs.bytes ();
26302 expected_bytes += m_abbrev_table.size ();
26303 expected_bytes += m_entry_pool.size ();
26304 return expected_bytes;
26307 /* Write .debug_names to FILE_NAMES and .debug_str addition to
26308 FILE_STR. This must be called only after calling the build
26310 void file_write (FILE *file_names, FILE *file_str) const
26312 /* Verify the build method has been already called. */
26313 gdb_assert (!m_abbrev_table.empty ());
26314 ::file_write (file_names, m_bucket_table);
26315 ::file_write (file_names, m_hash_table);
26316 m_name_table_string_offs.file_write (file_names);
26317 m_name_table_entry_offs.file_write (file_names);
26318 m_abbrev_table.file_write (file_names);
26319 m_entry_pool.file_write (file_names);
26320 m_debugstrlookup.file_write (file_str);
26323 /* A helper user data for write_one_signatured_type. */
26324 class write_one_signatured_type_data
26327 write_one_signatured_type_data (debug_names &nametable_,
26328 signatured_type_index_data &&info_)
26329 : nametable (nametable_), info (std::move (info_))
26331 debug_names &nametable;
26332 struct signatured_type_index_data info;
26335 /* A helper function to pass write_one_signatured_type to
26336 htab_traverse_noresize. */
26338 write_one_signatured_type (void **slot, void *d)
26340 write_one_signatured_type_data *data = (write_one_signatured_type_data *) d;
26341 struct signatured_type_index_data *info = &data->info;
26342 struct signatured_type *entry = (struct signatured_type *) *slot;
26344 data->nametable.write_one_signatured_type (entry, info);
26351 /* Storage for symbol names mapping them to their .debug_str section
26353 class debug_str_lookup
26357 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
26358 All .debug_str section strings are automatically stored. */
26359 debug_str_lookup (struct dwarf2_per_objfile *dwarf2_per_objfile)
26360 : m_abfd (dwarf2_per_objfile->objfile->obfd),
26361 m_dwarf2_per_objfile (dwarf2_per_objfile)
26363 dwarf2_read_section (dwarf2_per_objfile->objfile,
26364 &dwarf2_per_objfile->str);
26365 if (dwarf2_per_objfile->str.buffer == NULL)
26367 for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
26368 data < (dwarf2_per_objfile->str.buffer
26369 + dwarf2_per_objfile->str.size);)
26371 const char *const s = reinterpret_cast<const char *> (data);
26372 const auto insertpair
26373 = m_str_table.emplace (c_str_view (s),
26374 data - dwarf2_per_objfile->str.buffer);
26375 if (!insertpair.second)
26376 complaint (&symfile_complaints,
26377 _("Duplicate string \"%s\" in "
26378 ".debug_str section [in module %s]"),
26379 s, bfd_get_filename (m_abfd));
26380 data += strlen (s) + 1;
26384 /* Return offset of symbol name S in the .debug_str section. Add
26385 such symbol to the section's end if it does not exist there
26387 size_t lookup (const char *s)
26389 const auto it = m_str_table.find (c_str_view (s));
26390 if (it != m_str_table.end ())
26392 const size_t offset = (m_dwarf2_per_objfile->str.size
26393 + m_str_add_buf.size ());
26394 m_str_table.emplace (c_str_view (s), offset);
26395 m_str_add_buf.append_cstr0 (s);
26399 /* Append the end of the .debug_str section to FILE. */
26400 void file_write (FILE *file) const
26402 m_str_add_buf.file_write (file);
26406 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
26408 struct dwarf2_per_objfile *m_dwarf2_per_objfile;
26410 /* Data to add at the end of .debug_str for new needed symbol names. */
26411 data_buf m_str_add_buf;
26414 /* Container to map used DWARF tags to their .debug_names abbreviation
26419 index_key (int dwarf_tag_, bool is_static_, unit_kind kind_)
26420 : dwarf_tag (dwarf_tag_), is_static (is_static_), kind (kind_)
26425 operator== (const index_key &other) const
26427 return (dwarf_tag == other.dwarf_tag && is_static == other.is_static
26428 && kind == other.kind);
26431 const int dwarf_tag;
26432 const bool is_static;
26433 const unit_kind kind;
26436 /* Provide std::unordered_map::hasher for index_key. */
26437 class index_key_hasher
26441 operator () (const index_key &key) const
26443 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
26447 /* Parameters of one symbol entry. */
26451 const int dwarf_tag, cu_index;
26452 const bool is_static;
26453 const unit_kind kind;
26455 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_,
26457 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_),
26462 operator< (const symbol_value &other) const
26482 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
26487 const bfd_endian dwarf5_byte_order;
26489 explicit offset_vec (bfd_endian dwarf5_byte_order_)
26490 : dwarf5_byte_order (dwarf5_byte_order_)
26493 /* Call std::vector::reserve for NELEM elements. */
26494 virtual void reserve (size_t nelem) = 0;
26496 /* Call std::vector::push_back with store_unsigned_integer byte
26497 reordering for ELEM. */
26498 virtual void push_back_reorder (size_t elem) = 0;
26500 /* Return expected output size in bytes. */
26501 virtual size_t bytes () const = 0;
26503 /* Write name table to FILE. */
26504 virtual void file_write (FILE *file) const = 0;
26507 /* Template to unify DWARF-32 and DWARF-64 output. */
26508 template<typename OffsetSize>
26509 class offset_vec_tmpl : public offset_vec
26512 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
26513 : offset_vec (dwarf5_byte_order_)
26516 /* Implement offset_vec::reserve. */
26517 void reserve (size_t nelem) override
26519 m_vec.reserve (nelem);
26522 /* Implement offset_vec::push_back_reorder. */
26523 void push_back_reorder (size_t elem) override
26525 m_vec.push_back (elem);
26526 /* Check for overflow. */
26527 gdb_assert (m_vec.back () == elem);
26528 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
26529 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
26532 /* Implement offset_vec::bytes. */
26533 size_t bytes () const override
26535 return m_vec.size () * sizeof (m_vec[0]);
26538 /* Implement offset_vec::file_write. */
26539 void file_write (FILE *file) const override
26541 ::file_write (file, m_vec);
26545 std::vector<OffsetSize> m_vec;
26548 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
26549 respecting name table width. */
26553 offset_vec &name_table_string_offs, &name_table_entry_offs;
26555 dwarf (offset_vec &name_table_string_offs_,
26556 offset_vec &name_table_entry_offs_)
26557 : name_table_string_offs (name_table_string_offs_),
26558 name_table_entry_offs (name_table_entry_offs_)
26563 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
26564 respecting name table width. */
26565 template<typename OffsetSize>
26566 class dwarf_tmpl : public dwarf
26569 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
26570 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
26571 m_name_table_string_offs (dwarf5_byte_order_),
26572 m_name_table_entry_offs (dwarf5_byte_order_)
26576 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
26577 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
26580 /* Try to reconstruct original DWARF tag for given partial_symbol.
26581 This function is not DWARF-5 compliant but it is sufficient for
26582 GDB as a DWARF-5 index consumer. */
26583 static int psymbol_tag (const struct partial_symbol *psym)
26585 domain_enum domain = PSYMBOL_DOMAIN (psym);
26586 enum address_class aclass = PSYMBOL_CLASS (psym);
26594 return DW_TAG_subprogram;
26596 return DW_TAG_typedef;
26598 case LOC_CONST_BYTES:
26599 case LOC_OPTIMIZED_OUT:
26601 return DW_TAG_variable;
26603 /* Note: It's currently impossible to recognize psyms as enum values
26604 short of reading the type info. For now punt. */
26605 return DW_TAG_variable;
26607 /* There are other LOC_FOO values that one might want to classify
26608 as variables, but dwarf2read.c doesn't currently use them. */
26609 return DW_TAG_variable;
26611 case STRUCT_DOMAIN:
26612 return DW_TAG_structure_type;
26618 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
26619 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
26620 struct partial_symbol **psymp, int count, int cu_index,
26621 bool is_static, unit_kind kind)
26623 for (; count-- > 0; ++psymp)
26625 struct partial_symbol *psym = *psymp;
26627 if (SYMBOL_LANGUAGE (psym) == language_ada)
26628 error (_("Ada is not currently supported by the index"));
26630 /* Only add a given psymbol once. */
26631 if (psyms_seen.insert (psym).second)
26632 insert (psym, cu_index, is_static, kind);
26636 /* A helper function that writes a single signatured_type
26637 to a debug_names. */
26639 write_one_signatured_type (struct signatured_type *entry,
26640 struct signatured_type_index_data *info)
26642 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
26644 write_psymbols (info->psyms_seen,
26645 &info->objfile->global_psymbols[psymtab->globals_offset],
26646 psymtab->n_global_syms, info->cu_index, false,
26648 write_psymbols (info->psyms_seen,
26649 &info->objfile->static_psymbols[psymtab->statics_offset],
26650 psymtab->n_static_syms, info->cu_index, true,
26653 info->types_list.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order,
26654 to_underlying (entry->per_cu.sect_off));
26659 /* Store value of each symbol. */
26660 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
26661 m_name_to_value_set;
26663 /* Tables of DWARF-5 .debug_names. They are in object file byte
26665 std::vector<uint32_t> m_bucket_table;
26666 std::vector<uint32_t> m_hash_table;
26668 const bfd_endian m_dwarf5_byte_order;
26669 dwarf_tmpl<uint32_t> m_dwarf32;
26670 dwarf_tmpl<uint64_t> m_dwarf64;
26672 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
26673 debug_str_lookup m_debugstrlookup;
26675 /* Map each used .debug_names abbreviation tag parameter to its
26677 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
26679 /* Next unused .debug_names abbreviation tag for
26680 m_indexkey_to_idx. */
26681 int m_idx_next = 1;
26683 /* .debug_names abbreviation table. */
26684 data_buf m_abbrev_table;
26686 /* .debug_names entry pool. */
26687 data_buf m_entry_pool;
26690 /* Return iff any of the needed offsets does not fit into 32-bit
26691 .debug_names section. */
26694 check_dwarf64_offsets (struct dwarf2_per_objfile *dwarf2_per_objfile)
26696 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26698 const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
26700 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26703 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
26705 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
26706 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
26708 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26714 /* The psyms_seen set is potentially going to be largish (~40k
26715 elements when indexing a -g3 build of GDB itself). Estimate the
26716 number of elements in order to avoid too many rehashes, which
26717 require rebuilding buckets and thus many trips to
26721 psyms_seen_size (struct dwarf2_per_objfile *dwarf2_per_objfile)
26723 size_t psyms_count = 0;
26724 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26726 struct dwarf2_per_cu_data *per_cu
26727 = dwarf2_per_objfile->all_comp_units[i];
26728 struct partial_symtab *psymtab = per_cu->v.psymtab;
26730 if (psymtab != NULL && psymtab->user == NULL)
26731 recursively_count_psymbols (psymtab, psyms_count);
26733 /* Generating an index for gdb itself shows a ratio of
26734 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
26735 return psyms_count / 4;
26738 /* Write new .gdb_index section for OBJFILE into OUT_FILE.
26739 Return how many bytes were expected to be written into OUT_FILE. */
26742 write_gdbindex (struct dwarf2_per_objfile *dwarf2_per_objfile, FILE *out_file)
26744 struct objfile *objfile = dwarf2_per_objfile->objfile;
26745 mapped_symtab symtab;
26748 /* While we're scanning CU's create a table that maps a psymtab pointer
26749 (which is what addrmap records) to its index (which is what is recorded
26750 in the index file). This will later be needed to write the address
26752 psym_index_map cu_index_htab;
26753 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
26755 /* The CU list is already sorted, so we don't need to do additional
26756 work here. Also, the debug_types entries do not appear in
26757 all_comp_units, but only in their own hash table. */
26759 std::unordered_set<partial_symbol *> psyms_seen
26760 (psyms_seen_size (dwarf2_per_objfile));
26761 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26763 struct dwarf2_per_cu_data *per_cu
26764 = dwarf2_per_objfile->all_comp_units[i];
26765 struct partial_symtab *psymtab = per_cu->v.psymtab;
26767 /* CU of a shared file from 'dwz -m' may be unused by this main file.
26768 It may be referenced from a local scope but in such case it does not
26769 need to be present in .gdb_index. */
26770 if (psymtab == NULL)
26773 if (psymtab->user == NULL)
26774 recursively_write_psymbols (objfile, psymtab, &symtab,
26777 const auto insertpair = cu_index_htab.emplace (psymtab, i);
26778 gdb_assert (insertpair.second);
26780 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
26781 to_underlying (per_cu->sect_off));
26782 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
26785 /* Dump the address map. */
26787 write_address_map (objfile, addr_vec, cu_index_htab);
26789 /* Write out the .debug_type entries, if any. */
26790 data_buf types_cu_list;
26791 if (dwarf2_per_objfile->signatured_types)
26793 signatured_type_index_data sig_data (types_cu_list,
26796 sig_data.objfile = objfile;
26797 sig_data.symtab = &symtab;
26798 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
26799 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26800 write_one_signatured_type, &sig_data);
26803 /* Now that we've processed all symbols we can shrink their cu_indices
26805 uniquify_cu_indices (&symtab);
26807 data_buf symtab_vec, constant_pool;
26808 write_hash_table (&symtab, symtab_vec, constant_pool);
26811 const offset_type size_of_contents = 6 * sizeof (offset_type);
26812 offset_type total_len = size_of_contents;
26814 /* The version number. */
26815 contents.append_data (MAYBE_SWAP (8));
26817 /* The offset of the CU list from the start of the file. */
26818 contents.append_data (MAYBE_SWAP (total_len));
26819 total_len += cu_list.size ();
26821 /* The offset of the types CU list from the start of the file. */
26822 contents.append_data (MAYBE_SWAP (total_len));
26823 total_len += types_cu_list.size ();
26825 /* The offset of the address table from the start of the file. */
26826 contents.append_data (MAYBE_SWAP (total_len));
26827 total_len += addr_vec.size ();
26829 /* The offset of the symbol table from the start of the file. */
26830 contents.append_data (MAYBE_SWAP (total_len));
26831 total_len += symtab_vec.size ();
26833 /* The offset of the constant pool from the start of the file. */
26834 contents.append_data (MAYBE_SWAP (total_len));
26835 total_len += constant_pool.size ();
26837 gdb_assert (contents.size () == size_of_contents);
26839 contents.file_write (out_file);
26840 cu_list.file_write (out_file);
26841 types_cu_list.file_write (out_file);
26842 addr_vec.file_write (out_file);
26843 symtab_vec.file_write (out_file);
26844 constant_pool.file_write (out_file);
26849 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
26850 static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
26852 /* Write a new .debug_names section for OBJFILE into OUT_FILE, write
26853 needed addition to .debug_str section to OUT_FILE_STR. Return how
26854 many bytes were expected to be written into OUT_FILE. */
26857 write_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
26858 FILE *out_file, FILE *out_file_str)
26860 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets (dwarf2_per_objfile);
26861 struct objfile *objfile = dwarf2_per_objfile->objfile;
26862 const enum bfd_endian dwarf5_byte_order
26863 = gdbarch_byte_order (get_objfile_arch (objfile));
26865 /* The CU list is already sorted, so we don't need to do additional
26866 work here. Also, the debug_types entries do not appear in
26867 all_comp_units, but only in their own hash table. */
26869 debug_names nametable (dwarf2_per_objfile, dwarf5_is_dwarf64,
26870 dwarf5_byte_order);
26871 std::unordered_set<partial_symbol *>
26872 psyms_seen (psyms_seen_size (dwarf2_per_objfile));
26873 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26875 const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
26876 partial_symtab *psymtab = per_cu->v.psymtab;
26878 /* CU of a shared file from 'dwz -m' may be unused by this main
26879 file. It may be referenced from a local scope but in such
26880 case it does not need to be present in .debug_names. */
26881 if (psymtab == NULL)
26884 if (psymtab->user == NULL)
26885 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
26887 cu_list.append_uint (nametable.dwarf5_offset_size (), dwarf5_byte_order,
26888 to_underlying (per_cu->sect_off));
26891 /* Write out the .debug_type entries, if any. */
26892 data_buf types_cu_list;
26893 if (dwarf2_per_objfile->signatured_types)
26895 debug_names::write_one_signatured_type_data sig_data (nametable,
26896 signatured_type_index_data (types_cu_list, psyms_seen));
26898 sig_data.info.objfile = objfile;
26899 /* It is used only for gdb_index. */
26900 sig_data.info.symtab = nullptr;
26901 sig_data.info.cu_index = 0;
26902 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26903 debug_names::write_one_signatured_type,
26907 nametable.build ();
26909 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
26911 const offset_type bytes_of_header
26912 = ((dwarf5_is_dwarf64 ? 12 : 4)
26914 + sizeof (dwarf5_gdb_augmentation));
26915 size_t expected_bytes = 0;
26916 expected_bytes += bytes_of_header;
26917 expected_bytes += cu_list.size ();
26918 expected_bytes += types_cu_list.size ();
26919 expected_bytes += nametable.bytes ();
26922 if (!dwarf5_is_dwarf64)
26924 const uint64_t size64 = expected_bytes - 4;
26925 gdb_assert (size64 < 0xfffffff0);
26926 header.append_uint (4, dwarf5_byte_order, size64);
26930 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
26931 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
26934 /* The version number. */
26935 header.append_uint (2, dwarf5_byte_order, 5);
26938 header.append_uint (2, dwarf5_byte_order, 0);
26940 /* comp_unit_count - The number of CUs in the CU list. */
26941 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
26943 /* local_type_unit_count - The number of TUs in the local TU
26945 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
26947 /* foreign_type_unit_count - The number of TUs in the foreign TU
26949 header.append_uint (4, dwarf5_byte_order, 0);
26951 /* bucket_count - The number of hash buckets in the hash lookup
26953 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
26955 /* name_count - The number of unique names in the index. */
26956 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
26958 /* abbrev_table_size - The size in bytes of the abbreviations
26960 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
26962 /* augmentation_string_size - The size in bytes of the augmentation
26963 string. This value is rounded up to a multiple of 4. */
26964 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
26965 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
26966 header.append_data (dwarf5_gdb_augmentation);
26968 gdb_assert (header.size () == bytes_of_header);
26970 header.file_write (out_file);
26971 cu_list.file_write (out_file);
26972 types_cu_list.file_write (out_file);
26973 nametable.file_write (out_file, out_file_str);
26975 return expected_bytes;
26978 /* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
26979 position is at the end of the file. */
26982 assert_file_size (FILE *file, const char *filename, size_t expected_size)
26984 const auto file_size = ftell (file);
26985 if (file_size == -1)
26986 error (_("Can't get `%s' size"), filename);
26987 gdb_assert (file_size == expected_size);
26990 /* Create an index file for OBJFILE in the directory DIR. */
26993 write_psymtabs_to_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
26995 dw_index_kind index_kind)
26997 struct objfile *objfile = dwarf2_per_objfile->objfile;
26999 if (dwarf2_per_objfile->using_index)
27000 error (_("Cannot use an index to create the index"));
27002 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
27003 error (_("Cannot make an index when the file has multiple .debug_types sections"));
27005 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
27009 if (stat (objfile_name (objfile), &st) < 0)
27010 perror_with_name (objfile_name (objfile));
27012 std::string filename (std::string (dir) + SLASH_STRING
27013 + lbasename (objfile_name (objfile))
27014 + (index_kind == dw_index_kind::DEBUG_NAMES
27015 ? INDEX5_SUFFIX : INDEX4_SUFFIX));
27017 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
27019 error (_("Can't open `%s' for writing"), filename.c_str ());
27021 /* Order matters here; we want FILE to be closed before FILENAME is
27022 unlinked, because on MS-Windows one cannot delete a file that is
27023 still open. (Don't call anything here that might throw until
27024 file_closer is created.) */
27025 gdb::unlinker unlink_file (filename.c_str ());
27026 gdb_file_up close_out_file (out_file);
27028 if (index_kind == dw_index_kind::DEBUG_NAMES)
27030 std::string filename_str (std::string (dir) + SLASH_STRING
27031 + lbasename (objfile_name (objfile))
27032 + DEBUG_STR_SUFFIX);
27034 = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
27036 error (_("Can't open `%s' for writing"), filename_str.c_str ());
27037 gdb::unlinker unlink_file_str (filename_str.c_str ());
27038 gdb_file_up close_out_file_str (out_file_str);
27040 const size_t total_len
27041 = write_debug_names (dwarf2_per_objfile, out_file, out_file_str);
27042 assert_file_size (out_file, filename.c_str (), total_len);
27044 /* We want to keep the file .debug_str file too. */
27045 unlink_file_str.keep ();
27049 const size_t total_len
27050 = write_gdbindex (dwarf2_per_objfile, out_file);
27051 assert_file_size (out_file, filename.c_str (), total_len);
27054 /* We want to keep the file. */
27055 unlink_file.keep ();
27058 /* Implementation of the `save gdb-index' command.
27060 Note that the .gdb_index file format used by this command is
27061 documented in the GDB manual. Any changes here must be documented
27065 save_gdb_index_command (const char *arg, int from_tty)
27067 struct objfile *objfile;
27068 const char dwarf5space[] = "-dwarf-5 ";
27069 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
27074 arg = skip_spaces (arg);
27075 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
27077 index_kind = dw_index_kind::DEBUG_NAMES;
27078 arg += strlen (dwarf5space);
27079 arg = skip_spaces (arg);
27083 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
27085 ALL_OBJFILES (objfile)
27089 /* If the objfile does not correspond to an actual file, skip it. */
27090 if (stat (objfile_name (objfile), &st) < 0)
27093 struct dwarf2_per_objfile *dwarf2_per_objfile
27094 = get_dwarf2_per_objfile (objfile);
27096 if (dwarf2_per_objfile != NULL)
27100 write_psymtabs_to_index (dwarf2_per_objfile, arg, index_kind);
27102 CATCH (except, RETURN_MASK_ERROR)
27104 exception_fprintf (gdb_stderr, except,
27105 _("Error while writing index for `%s': "),
27106 objfile_name (objfile));
27116 int dwarf_always_disassemble;
27119 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
27120 struct cmd_list_element *c, const char *value)
27122 fprintf_filtered (file,
27123 _("Whether to always disassemble "
27124 "DWARF expressions is %s.\n"),
27129 show_check_physname (struct ui_file *file, int from_tty,
27130 struct cmd_list_element *c, const char *value)
27132 fprintf_filtered (file,
27133 _("Whether to check \"physname\" is %s.\n"),
27138 _initialize_dwarf2_read (void)
27140 struct cmd_list_element *c;
27142 dwarf2_objfile_data_key
27143 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
27145 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
27146 Set DWARF specific variables.\n\
27147 Configure DWARF variables such as the cache size"),
27148 &set_dwarf_cmdlist, "maintenance set dwarf ",
27149 0/*allow-unknown*/, &maintenance_set_cmdlist);
27151 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
27152 Show DWARF specific variables\n\
27153 Show DWARF variables such as the cache size"),
27154 &show_dwarf_cmdlist, "maintenance show dwarf ",
27155 0/*allow-unknown*/, &maintenance_show_cmdlist);
27157 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
27158 &dwarf_max_cache_age, _("\
27159 Set the upper bound on the age of cached DWARF compilation units."), _("\
27160 Show the upper bound on the age of cached DWARF compilation units."), _("\
27161 A higher limit means that cached compilation units will be stored\n\
27162 in memory longer, and more total memory will be used. Zero disables\n\
27163 caching, which can slow down startup."),
27165 show_dwarf_max_cache_age,
27166 &set_dwarf_cmdlist,
27167 &show_dwarf_cmdlist);
27169 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
27170 &dwarf_always_disassemble, _("\
27171 Set whether `info address' always disassembles DWARF expressions."), _("\
27172 Show whether `info address' always disassembles DWARF expressions."), _("\
27173 When enabled, DWARF expressions are always printed in an assembly-like\n\
27174 syntax. When disabled, expressions will be printed in a more\n\
27175 conversational style, when possible."),
27177 show_dwarf_always_disassemble,
27178 &set_dwarf_cmdlist,
27179 &show_dwarf_cmdlist);
27181 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
27182 Set debugging of the DWARF reader."), _("\
27183 Show debugging of the DWARF reader."), _("\
27184 When enabled (non-zero), debugging messages are printed during DWARF\n\
27185 reading and symtab expansion. A value of 1 (one) provides basic\n\
27186 information. A value greater than 1 provides more verbose information."),
27189 &setdebuglist, &showdebuglist);
27191 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
27192 Set debugging of the DWARF DIE reader."), _("\
27193 Show debugging of the DWARF DIE reader."), _("\
27194 When enabled (non-zero), DIEs are dumped after they are read in.\n\
27195 The value is the maximum depth to print."),
27198 &setdebuglist, &showdebuglist);
27200 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
27201 Set debugging of the dwarf line reader."), _("\
27202 Show debugging of the dwarf line reader."), _("\
27203 When enabled (non-zero), line number entries are dumped as they are read in.\n\
27204 A value of 1 (one) provides basic information.\n\
27205 A value greater than 1 provides more verbose information."),
27208 &setdebuglist, &showdebuglist);
27210 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
27211 Set cross-checking of \"physname\" code against demangler."), _("\
27212 Show cross-checking of \"physname\" code against demangler."), _("\
27213 When enabled, GDB's internal \"physname\" code is checked against\n\
27215 NULL, show_check_physname,
27216 &setdebuglist, &showdebuglist);
27218 add_setshow_boolean_cmd ("use-deprecated-index-sections",
27219 no_class, &use_deprecated_index_sections, _("\
27220 Set whether to use deprecated gdb_index sections."), _("\
27221 Show whether to use deprecated gdb_index sections."), _("\
27222 When enabled, deprecated .gdb_index sections are used anyway.\n\
27223 Normally they are ignored either because of a missing feature or\n\
27224 performance issue.\n\
27225 Warning: This option must be enabled before gdb reads the file."),
27228 &setlist, &showlist);
27230 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
27232 Save a gdb-index file.\n\
27233 Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
27235 No options create one file with .gdb-index extension for pre-DWARF-5\n\
27236 compatible .gdb_index section. With -dwarf-5 creates two files with\n\
27237 extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
27239 set_cmd_completer (c, filename_completer);
27241 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
27242 &dwarf2_locexpr_funcs);
27243 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
27244 &dwarf2_loclist_funcs);
27246 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
27247 &dwarf2_block_frame_base_locexpr_funcs);
27248 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
27249 &dwarf2_block_frame_base_loclist_funcs);
27252 selftests::register_test ("dw2_expand_symtabs_matching",
27253 selftests::dw2_expand_symtabs_matching::run_test);