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 bfd_endian dwarf5_byte_order;
324 bool dwarf5_is_dwarf64;
325 bool augmentation_is_gdb;
327 uint32_t cu_count = 0;
328 uint32_t tu_count, bucket_count, name_count;
329 const gdb_byte *cu_table_reordered, *tu_table_reordered;
330 const uint32_t *bucket_table_reordered, *hash_table_reordered;
331 const gdb_byte *name_table_string_offs_reordered;
332 const gdb_byte *name_table_entry_offs_reordered;
333 const gdb_byte *entry_pool;
340 /* Attribute name DW_IDX_*. */
343 /* Attribute form DW_FORM_*. */
346 /* Value if FORM is DW_FORM_implicit_const. */
347 LONGEST implicit_const;
349 std::vector<attr> attr_vec;
352 std::unordered_map<ULONGEST, index_val> abbrev_map;
354 const char *namei_to_name (uint32_t namei) const;
356 /* Implementation of the mapped_index_base virtual interface, for
357 the name_components cache. */
359 const char *symbol_name_at (offset_type idx) const override
360 { return namei_to_name (idx); }
362 size_t symbol_name_count () const override
363 { return this->name_count; }
366 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
367 DEF_VEC_P (dwarf2_per_cu_ptr);
371 int nr_uniq_abbrev_tables;
373 int nr_symtab_sharers;
374 int nr_stmt_less_type_units;
375 int nr_all_type_units_reallocs;
378 /* Collection of data recorded per objfile.
379 This hangs off of dwarf2_objfile_data_key. */
381 struct dwarf2_per_objfile
383 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
384 dwarf2 section names, or is NULL if the standard ELF names are
386 dwarf2_per_objfile (struct objfile *objfile,
387 const dwarf2_debug_sections *names);
389 ~dwarf2_per_objfile ();
391 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
393 /* Free all cached compilation units. */
394 void free_cached_comp_units ();
396 /* This function is mapped across the sections and remembers the
397 offset and size of each of the debugging sections we are
399 void locate_sections (bfd *abfd, asection *sectp,
400 const dwarf2_debug_sections &names);
403 dwarf2_section_info info {};
404 dwarf2_section_info abbrev {};
405 dwarf2_section_info line {};
406 dwarf2_section_info loc {};
407 dwarf2_section_info loclists {};
408 dwarf2_section_info macinfo {};
409 dwarf2_section_info macro {};
410 dwarf2_section_info str {};
411 dwarf2_section_info line_str {};
412 dwarf2_section_info ranges {};
413 dwarf2_section_info rnglists {};
414 dwarf2_section_info addr {};
415 dwarf2_section_info frame {};
416 dwarf2_section_info eh_frame {};
417 dwarf2_section_info gdb_index {};
418 dwarf2_section_info debug_names {};
419 dwarf2_section_info debug_aranges {};
421 VEC (dwarf2_section_info_def) *types = NULL;
424 struct objfile *objfile = NULL;
426 /* Table of all the compilation units. This is used to locate
427 the target compilation unit of a particular reference. */
428 struct dwarf2_per_cu_data **all_comp_units = NULL;
430 /* The number of compilation units in ALL_COMP_UNITS. */
431 int n_comp_units = 0;
433 /* The number of .debug_types-related CUs. */
434 int n_type_units = 0;
436 /* The number of elements allocated in all_type_units.
437 If there are skeleton-less TUs, we add them to all_type_units lazily. */
438 int n_allocated_type_units = 0;
440 /* The .debug_types-related CUs (TUs).
441 This is stored in malloc space because we may realloc it. */
442 struct signatured_type **all_type_units = NULL;
444 /* Table of struct type_unit_group objects.
445 The hash key is the DW_AT_stmt_list value. */
446 htab_t type_unit_groups {};
448 /* A table mapping .debug_types signatures to its signatured_type entry.
449 This is NULL if the .debug_types section hasn't been read in yet. */
450 htab_t signatured_types {};
452 /* Type unit statistics, to see how well the scaling improvements
454 struct tu_stats tu_stats {};
456 /* A chain of compilation units that are currently read in, so that
457 they can be freed later. */
458 dwarf2_per_cu_data *read_in_chain = NULL;
460 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
461 This is NULL if the table hasn't been allocated yet. */
464 /* True if we've checked for whether there is a DWP file. */
465 bool dwp_checked = false;
467 /* The DWP file if there is one, or NULL. */
468 struct dwp_file *dwp_file = NULL;
470 /* The shared '.dwz' file, if one exists. This is used when the
471 original data was compressed using 'dwz -m'. */
472 struct dwz_file *dwz_file = NULL;
474 /* A flag indicating whether this objfile has a section loaded at a
476 bool has_section_at_zero = false;
478 /* True if we are using the mapped index,
479 or we are faking it for OBJF_READNOW's sake. */
480 bool using_index = false;
482 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
483 mapped_index *index_table = NULL;
485 /* The mapped index, or NULL if .debug_names is missing or not being used. */
486 std::unique_ptr<mapped_debug_names> debug_names_table;
488 /* When using index_table, this keeps track of all quick_file_names entries.
489 TUs typically share line table entries with a CU, so we maintain a
490 separate table of all line table entries to support the sharing.
491 Note that while there can be way more TUs than CUs, we've already
492 sorted all the TUs into "type unit groups", grouped by their
493 DW_AT_stmt_list value. Therefore the only sharing done here is with a
494 CU and its associated TU group if there is one. */
495 htab_t quick_file_names_table {};
497 /* Set during partial symbol reading, to prevent queueing of full
499 bool reading_partial_symbols = false;
501 /* Table mapping type DIEs to their struct type *.
502 This is NULL if not allocated yet.
503 The mapping is done via (CU/TU + DIE offset) -> type. */
504 htab_t die_type_hash {};
506 /* The CUs we recently read. */
507 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
509 /* Table containing line_header indexed by offset and offset_in_dwz. */
510 htab_t line_header_hash {};
512 /* Table containing all filenames. This is an optional because the
513 table is lazily constructed on first access. */
514 gdb::optional<filename_seen_cache> filenames_cache;
517 static struct dwarf2_per_objfile *dwarf2_per_objfile;
519 /* Default names of the debugging sections. */
521 /* Note that if the debugging section has been compressed, it might
522 have a name like .zdebug_info. */
524 static const struct dwarf2_debug_sections dwarf2_elf_names =
526 { ".debug_info", ".zdebug_info" },
527 { ".debug_abbrev", ".zdebug_abbrev" },
528 { ".debug_line", ".zdebug_line" },
529 { ".debug_loc", ".zdebug_loc" },
530 { ".debug_loclists", ".zdebug_loclists" },
531 { ".debug_macinfo", ".zdebug_macinfo" },
532 { ".debug_macro", ".zdebug_macro" },
533 { ".debug_str", ".zdebug_str" },
534 { ".debug_line_str", ".zdebug_line_str" },
535 { ".debug_ranges", ".zdebug_ranges" },
536 { ".debug_rnglists", ".zdebug_rnglists" },
537 { ".debug_types", ".zdebug_types" },
538 { ".debug_addr", ".zdebug_addr" },
539 { ".debug_frame", ".zdebug_frame" },
540 { ".eh_frame", NULL },
541 { ".gdb_index", ".zgdb_index" },
542 { ".debug_names", ".zdebug_names" },
543 { ".debug_aranges", ".zdebug_aranges" },
547 /* List of DWO/DWP sections. */
549 static const struct dwop_section_names
551 struct dwarf2_section_names abbrev_dwo;
552 struct dwarf2_section_names info_dwo;
553 struct dwarf2_section_names line_dwo;
554 struct dwarf2_section_names loc_dwo;
555 struct dwarf2_section_names loclists_dwo;
556 struct dwarf2_section_names macinfo_dwo;
557 struct dwarf2_section_names macro_dwo;
558 struct dwarf2_section_names str_dwo;
559 struct dwarf2_section_names str_offsets_dwo;
560 struct dwarf2_section_names types_dwo;
561 struct dwarf2_section_names cu_index;
562 struct dwarf2_section_names tu_index;
566 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
567 { ".debug_info.dwo", ".zdebug_info.dwo" },
568 { ".debug_line.dwo", ".zdebug_line.dwo" },
569 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
570 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
571 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
572 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
573 { ".debug_str.dwo", ".zdebug_str.dwo" },
574 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
575 { ".debug_types.dwo", ".zdebug_types.dwo" },
576 { ".debug_cu_index", ".zdebug_cu_index" },
577 { ".debug_tu_index", ".zdebug_tu_index" },
580 /* local data types */
582 /* The data in a compilation unit header, after target2host
583 translation, looks like this. */
584 struct comp_unit_head
588 unsigned char addr_size;
589 unsigned char signed_addr_p;
590 sect_offset abbrev_sect_off;
592 /* Size of file offsets; either 4 or 8. */
593 unsigned int offset_size;
595 /* Size of the length field; either 4 or 12. */
596 unsigned int initial_length_size;
598 enum dwarf_unit_type unit_type;
600 /* Offset to the first byte of this compilation unit header in the
601 .debug_info section, for resolving relative reference dies. */
602 sect_offset sect_off;
604 /* Offset to first die in this cu from the start of the cu.
605 This will be the first byte following the compilation unit header. */
606 cu_offset first_die_cu_offset;
608 /* 64-bit signature of this type unit - it is valid only for
609 UNIT_TYPE DW_UT_type. */
612 /* For types, offset in the type's DIE of the type defined by this TU. */
613 cu_offset type_cu_offset_in_tu;
616 /* Type used for delaying computation of method physnames.
617 See comments for compute_delayed_physnames. */
618 struct delayed_method_info
620 /* The type to which the method is attached, i.e., its parent class. */
623 /* The index of the method in the type's function fieldlists. */
626 /* The index of the method in the fieldlist. */
629 /* The name of the DIE. */
632 /* The DIE associated with this method. */
633 struct die_info *die;
636 typedef struct delayed_method_info delayed_method_info;
637 DEF_VEC_O (delayed_method_info);
639 /* Internal state when decoding a particular compilation unit. */
642 /* The objfile containing this compilation unit. */
643 struct objfile *objfile;
645 /* The header of the compilation unit. */
646 struct comp_unit_head header;
648 /* Base address of this compilation unit. */
649 CORE_ADDR base_address;
651 /* Non-zero if base_address has been set. */
654 /* The language we are debugging. */
655 enum language language;
656 const struct language_defn *language_defn;
658 const char *producer;
660 /* The generic symbol table building routines have separate lists for
661 file scope symbols and all all other scopes (local scopes). So
662 we need to select the right one to pass to add_symbol_to_list().
663 We do it by keeping a pointer to the correct list in list_in_scope.
665 FIXME: The original dwarf code just treated the file scope as the
666 first local scope, and all other local scopes as nested local
667 scopes, and worked fine. Check to see if we really need to
668 distinguish these in buildsym.c. */
669 struct pending **list_in_scope;
671 /* The abbrev table for this CU.
672 Normally this points to the abbrev table in the objfile.
673 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
674 struct abbrev_table *abbrev_table;
676 /* Hash table holding all the loaded partial DIEs
677 with partial_die->offset.SECT_OFF as hash. */
680 /* Storage for things with the same lifetime as this read-in compilation
681 unit, including partial DIEs. */
682 struct obstack comp_unit_obstack;
684 /* When multiple dwarf2_cu structures are living in memory, this field
685 chains them all together, so that they can be released efficiently.
686 We will probably also want a generation counter so that most-recently-used
687 compilation units are cached... */
688 struct dwarf2_per_cu_data *read_in_chain;
690 /* Backlink to our per_cu entry. */
691 struct dwarf2_per_cu_data *per_cu;
693 /* How many compilation units ago was this CU last referenced? */
696 /* A hash table of DIE cu_offset for following references with
697 die_info->offset.sect_off as hash. */
700 /* Full DIEs if read in. */
701 struct die_info *dies;
703 /* A set of pointers to dwarf2_per_cu_data objects for compilation
704 units referenced by this one. Only set during full symbol processing;
705 partial symbol tables do not have dependencies. */
708 /* Header data from the line table, during full symbol processing. */
709 struct line_header *line_header;
710 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
711 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
712 this is the DW_TAG_compile_unit die for this CU. We'll hold on
713 to the line header as long as this DIE is being processed. See
714 process_die_scope. */
715 die_info *line_header_die_owner;
717 /* A list of methods which need to have physnames computed
718 after all type information has been read. */
719 VEC (delayed_method_info) *method_list;
721 /* To be copied to symtab->call_site_htab. */
722 htab_t call_site_htab;
724 /* Non-NULL if this CU came from a DWO file.
725 There is an invariant here that is important to remember:
726 Except for attributes copied from the top level DIE in the "main"
727 (or "stub") file in preparation for reading the DWO file
728 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
729 Either there isn't a DWO file (in which case this is NULL and the point
730 is moot), or there is and either we're not going to read it (in which
731 case this is NULL) or there is and we are reading it (in which case this
733 struct dwo_unit *dwo_unit;
735 /* The DW_AT_addr_base attribute if present, zero otherwise
736 (zero is a valid value though).
737 Note this value comes from the Fission stub CU/TU's DIE. */
740 /* The DW_AT_ranges_base attribute if present, zero otherwise
741 (zero is a valid value though).
742 Note this value comes from the Fission stub CU/TU's DIE.
743 Also note that the value is zero in the non-DWO case so this value can
744 be used without needing to know whether DWO files are in use or not.
745 N.B. This does not apply to DW_AT_ranges appearing in
746 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
747 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
748 DW_AT_ranges_base *would* have to be applied, and we'd have to care
749 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
750 ULONGEST ranges_base;
752 /* Mark used when releasing cached dies. */
753 unsigned int mark : 1;
755 /* This CU references .debug_loc. See the symtab->locations_valid field.
756 This test is imperfect as there may exist optimized debug code not using
757 any location list and still facing inlining issues if handled as
758 unoptimized code. For a future better test see GCC PR other/32998. */
759 unsigned int has_loclist : 1;
761 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
762 if all the producer_is_* fields are valid. This information is cached
763 because profiling CU expansion showed excessive time spent in
764 producer_is_gxx_lt_4_6. */
765 unsigned int checked_producer : 1;
766 unsigned int producer_is_gxx_lt_4_6 : 1;
767 unsigned int producer_is_gcc_lt_4_3 : 1;
768 unsigned int producer_is_icc_lt_14 : 1;
770 /* When set, the file that we're processing is known to have
771 debugging info for C++ namespaces. GCC 3.3.x did not produce
772 this information, but later versions do. */
774 unsigned int processing_has_namespace_info : 1;
777 /* Persistent data held for a compilation unit, even when not
778 processing it. We put a pointer to this structure in the
779 read_symtab_private field of the psymtab. */
781 struct dwarf2_per_cu_data
783 /* The start offset and length of this compilation unit.
784 NOTE: Unlike comp_unit_head.length, this length includes
786 If the DIE refers to a DWO file, this is always of the original die,
788 sect_offset sect_off;
791 /* DWARF standard version this data has been read from (such as 4 or 5). */
794 /* Flag indicating this compilation unit will be read in before
795 any of the current compilation units are processed. */
796 unsigned int queued : 1;
798 /* This flag will be set when reading partial DIEs if we need to load
799 absolutely all DIEs for this compilation unit, instead of just the ones
800 we think are interesting. It gets set if we look for a DIE in the
801 hash table and don't find it. */
802 unsigned int load_all_dies : 1;
804 /* Non-zero if this CU is from .debug_types.
805 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
807 unsigned int is_debug_types : 1;
809 /* Non-zero if this CU is from the .dwz file. */
810 unsigned int is_dwz : 1;
812 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
813 This flag is only valid if is_debug_types is true.
814 We can't read a CU directly from a DWO file: There are required
815 attributes in the stub. */
816 unsigned int reading_dwo_directly : 1;
818 /* Non-zero if the TU has been read.
819 This is used to assist the "Stay in DWO Optimization" for Fission:
820 When reading a DWO, it's faster to read TUs from the DWO instead of
821 fetching them from random other DWOs (due to comdat folding).
822 If the TU has already been read, the optimization is unnecessary
823 (and unwise - we don't want to change where gdb thinks the TU lives
825 This flag is only valid if is_debug_types is true. */
826 unsigned int tu_read : 1;
828 /* The section this CU/TU lives in.
829 If the DIE refers to a DWO file, this is always the original die,
831 struct dwarf2_section_info *section;
833 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
834 of the CU cache it gets reset to NULL again. This is left as NULL for
835 dummy CUs (a CU header, but nothing else). */
836 struct dwarf2_cu *cu;
838 /* The corresponding objfile.
839 Normally we can get the objfile from dwarf2_per_objfile.
840 However we can enter this file with just a "per_cu" handle. */
841 struct objfile *objfile;
843 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
844 is active. Otherwise, the 'psymtab' field is active. */
847 /* The partial symbol table associated with this compilation unit,
848 or NULL for unread partial units. */
849 struct partial_symtab *psymtab;
851 /* Data needed by the "quick" functions. */
852 struct dwarf2_per_cu_quick_data *quick;
855 /* The CUs we import using DW_TAG_imported_unit. This is filled in
856 while reading psymtabs, used to compute the psymtab dependencies,
857 and then cleared. Then it is filled in again while reading full
858 symbols, and only deleted when the objfile is destroyed.
860 This is also used to work around a difference between the way gold
861 generates .gdb_index version <=7 and the way gdb does. Arguably this
862 is a gold bug. For symbols coming from TUs, gold records in the index
863 the CU that includes the TU instead of the TU itself. This breaks
864 dw2_lookup_symbol: It assumes that if the index says symbol X lives
865 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
866 will find X. Alas TUs live in their own symtab, so after expanding CU Y
867 we need to look in TU Z to find X. Fortunately, this is akin to
868 DW_TAG_imported_unit, so we just use the same mechanism: For
869 .gdb_index version <=7 this also records the TUs that the CU referred
870 to. Concurrently with this change gdb was modified to emit version 8
871 indices so we only pay a price for gold generated indices.
872 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
873 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
876 /* Entry in the signatured_types hash table. */
878 struct signatured_type
880 /* The "per_cu" object of this type.
881 This struct is used iff per_cu.is_debug_types.
882 N.B.: This is the first member so that it's easy to convert pointers
884 struct dwarf2_per_cu_data per_cu;
886 /* The type's signature. */
889 /* Offset in the TU of the type's DIE, as read from the TU header.
890 If this TU is a DWO stub and the definition lives in a DWO file
891 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
892 cu_offset type_offset_in_tu;
894 /* Offset in the section of the type's DIE.
895 If the definition lives in a DWO file, this is the offset in the
896 .debug_types.dwo section.
897 The value is zero until the actual value is known.
898 Zero is otherwise not a valid section offset. */
899 sect_offset type_offset_in_section;
901 /* Type units are grouped by their DW_AT_stmt_list entry so that they
902 can share them. This points to the containing symtab. */
903 struct type_unit_group *type_unit_group;
906 The first time we encounter this type we fully read it in and install it
907 in the symbol tables. Subsequent times we only need the type. */
910 /* Containing DWO unit.
911 This field is valid iff per_cu.reading_dwo_directly. */
912 struct dwo_unit *dwo_unit;
915 typedef struct signatured_type *sig_type_ptr;
916 DEF_VEC_P (sig_type_ptr);
918 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
919 This includes type_unit_group and quick_file_names. */
921 struct stmt_list_hash
923 /* The DWO unit this table is from or NULL if there is none. */
924 struct dwo_unit *dwo_unit;
926 /* Offset in .debug_line or .debug_line.dwo. */
927 sect_offset line_sect_off;
930 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
931 an object of this type. */
933 struct type_unit_group
935 /* dwarf2read.c's main "handle" on a TU symtab.
936 To simplify things we create an artificial CU that "includes" all the
937 type units using this stmt_list so that the rest of the code still has
938 a "per_cu" handle on the symtab.
939 This PER_CU is recognized by having no section. */
940 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
941 struct dwarf2_per_cu_data per_cu;
943 /* The TUs that share this DW_AT_stmt_list entry.
944 This is added to while parsing type units to build partial symtabs,
945 and is deleted afterwards and not used again. */
946 VEC (sig_type_ptr) *tus;
948 /* The compunit symtab.
949 Type units in a group needn't all be defined in the same source file,
950 so we create an essentially anonymous symtab as the compunit symtab. */
951 struct compunit_symtab *compunit_symtab;
953 /* The data used to construct the hash key. */
954 struct stmt_list_hash hash;
956 /* The number of symtabs from the line header.
957 The value here must match line_header.num_file_names. */
958 unsigned int num_symtabs;
960 /* The symbol tables for this TU (obtained from the files listed in
962 WARNING: The order of entries here must match the order of entries
963 in the line header. After the first TU using this type_unit_group, the
964 line header for the subsequent TUs is recreated from this. This is done
965 because we need to use the same symtabs for each TU using the same
966 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
967 there's no guarantee the line header doesn't have duplicate entries. */
968 struct symtab **symtabs;
971 /* These sections are what may appear in a (real or virtual) DWO file. */
975 struct dwarf2_section_info abbrev;
976 struct dwarf2_section_info line;
977 struct dwarf2_section_info loc;
978 struct dwarf2_section_info loclists;
979 struct dwarf2_section_info macinfo;
980 struct dwarf2_section_info macro;
981 struct dwarf2_section_info str;
982 struct dwarf2_section_info str_offsets;
983 /* In the case of a virtual DWO file, these two are unused. */
984 struct dwarf2_section_info info;
985 VEC (dwarf2_section_info_def) *types;
988 /* CUs/TUs in DWP/DWO files. */
992 /* Backlink to the containing struct dwo_file. */
993 struct dwo_file *dwo_file;
995 /* The "id" that distinguishes this CU/TU.
996 .debug_info calls this "dwo_id", .debug_types calls this "signature".
997 Since signatures came first, we stick with it for consistency. */
1000 /* The section this CU/TU lives in, in the DWO file. */
1001 struct dwarf2_section_info *section;
1003 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
1004 sect_offset sect_off;
1005 unsigned int length;
1007 /* For types, offset in the type's DIE of the type defined by this TU. */
1008 cu_offset type_offset_in_tu;
1011 /* include/dwarf2.h defines the DWP section codes.
1012 It defines a max value but it doesn't define a min value, which we
1013 use for error checking, so provide one. */
1015 enum dwp_v2_section_ids
1020 /* Data for one DWO file.
1022 This includes virtual DWO files (a virtual DWO file is a DWO file as it
1023 appears in a DWP file). DWP files don't really have DWO files per se -
1024 comdat folding of types "loses" the DWO file they came from, and from
1025 a high level view DWP files appear to contain a mass of random types.
1026 However, to maintain consistency with the non-DWP case we pretend DWP
1027 files contain virtual DWO files, and we assign each TU with one virtual
1028 DWO file (generally based on the line and abbrev section offsets -
1029 a heuristic that seems to work in practice). */
1033 /* The DW_AT_GNU_dwo_name attribute.
1034 For virtual DWO files the name is constructed from the section offsets
1035 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
1036 from related CU+TUs. */
1037 const char *dwo_name;
1039 /* The DW_AT_comp_dir attribute. */
1040 const char *comp_dir;
1042 /* The bfd, when the file is open. Otherwise this is NULL.
1043 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
1046 /* The sections that make up this DWO file.
1047 Remember that for virtual DWO files in DWP V2, these are virtual
1048 sections (for lack of a better name). */
1049 struct dwo_sections sections;
1051 /* The CUs in the file.
1052 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
1053 an extension to handle LLVM's Link Time Optimization output (where
1054 multiple source files may be compiled into a single object/dwo pair). */
1057 /* Table of TUs in the file.
1058 Each element is a struct dwo_unit. */
1062 /* These sections are what may appear in a DWP file. */
1066 /* These are used by both DWP version 1 and 2. */
1067 struct dwarf2_section_info str;
1068 struct dwarf2_section_info cu_index;
1069 struct dwarf2_section_info tu_index;
1071 /* These are only used by DWP version 2 files.
1072 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
1073 sections are referenced by section number, and are not recorded here.
1074 In DWP version 2 there is at most one copy of all these sections, each
1075 section being (effectively) comprised of the concatenation of all of the
1076 individual sections that exist in the version 1 format.
1077 To keep the code simple we treat each of these concatenated pieces as a
1078 section itself (a virtual section?). */
1079 struct dwarf2_section_info abbrev;
1080 struct dwarf2_section_info info;
1081 struct dwarf2_section_info line;
1082 struct dwarf2_section_info loc;
1083 struct dwarf2_section_info macinfo;
1084 struct dwarf2_section_info macro;
1085 struct dwarf2_section_info str_offsets;
1086 struct dwarf2_section_info types;
1089 /* These sections are what may appear in a virtual DWO file in DWP version 1.
1090 A virtual DWO file is a DWO file as it appears in a DWP file. */
1092 struct virtual_v1_dwo_sections
1094 struct dwarf2_section_info abbrev;
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 /* Each DWP hash table entry records one CU or one TU.
1101 That is recorded here, and copied to dwo_unit.section. */
1102 struct dwarf2_section_info info_or_types;
1105 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
1106 In version 2, the sections of the DWO files are concatenated together
1107 and stored in one section of that name. Thus each ELF section contains
1108 several "virtual" sections. */
1110 struct virtual_v2_dwo_sections
1112 bfd_size_type abbrev_offset;
1113 bfd_size_type abbrev_size;
1115 bfd_size_type line_offset;
1116 bfd_size_type line_size;
1118 bfd_size_type loc_offset;
1119 bfd_size_type loc_size;
1121 bfd_size_type macinfo_offset;
1122 bfd_size_type macinfo_size;
1124 bfd_size_type macro_offset;
1125 bfd_size_type macro_size;
1127 bfd_size_type str_offsets_offset;
1128 bfd_size_type str_offsets_size;
1130 /* Each DWP hash table entry records one CU or one TU.
1131 That is recorded here, and copied to dwo_unit.section. */
1132 bfd_size_type info_or_types_offset;
1133 bfd_size_type info_or_types_size;
1136 /* Contents of DWP hash tables. */
1138 struct dwp_hash_table
1140 uint32_t version, nr_columns;
1141 uint32_t nr_units, nr_slots;
1142 const gdb_byte *hash_table, *unit_table;
1147 const gdb_byte *indices;
1151 /* This is indexed by column number and gives the id of the section
1153 #define MAX_NR_V2_DWO_SECTIONS \
1154 (1 /* .debug_info or .debug_types */ \
1155 + 1 /* .debug_abbrev */ \
1156 + 1 /* .debug_line */ \
1157 + 1 /* .debug_loc */ \
1158 + 1 /* .debug_str_offsets */ \
1159 + 1 /* .debug_macro or .debug_macinfo */)
1160 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1161 const gdb_byte *offsets;
1162 const gdb_byte *sizes;
1167 /* Data for one DWP file. */
1171 /* Name of the file. */
1174 /* File format version. */
1180 /* Section info for this file. */
1181 struct dwp_sections sections;
1183 /* Table of CUs in the file. */
1184 const struct dwp_hash_table *cus;
1186 /* Table of TUs in the file. */
1187 const struct dwp_hash_table *tus;
1189 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1193 /* Table to map ELF section numbers to their sections.
1194 This is only needed for the DWP V1 file format. */
1195 unsigned int num_sections;
1196 asection **elf_sections;
1199 /* This represents a '.dwz' file. */
1203 /* A dwz file can only contain a few sections. */
1204 struct dwarf2_section_info abbrev;
1205 struct dwarf2_section_info info;
1206 struct dwarf2_section_info str;
1207 struct dwarf2_section_info line;
1208 struct dwarf2_section_info macro;
1209 struct dwarf2_section_info gdb_index;
1210 struct dwarf2_section_info debug_names;
1212 /* The dwz's BFD. */
1216 /* Struct used to pass misc. parameters to read_die_and_children, et
1217 al. which are used for both .debug_info and .debug_types dies.
1218 All parameters here are unchanging for the life of the call. This
1219 struct exists to abstract away the constant parameters of die reading. */
1221 struct die_reader_specs
1223 /* The bfd of die_section. */
1226 /* The CU of the DIE we are parsing. */
1227 struct dwarf2_cu *cu;
1229 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1230 struct dwo_file *dwo_file;
1232 /* The section the die comes from.
1233 This is either .debug_info or .debug_types, or the .dwo variants. */
1234 struct dwarf2_section_info *die_section;
1236 /* die_section->buffer. */
1237 const gdb_byte *buffer;
1239 /* The end of the buffer. */
1240 const gdb_byte *buffer_end;
1242 /* The value of the DW_AT_comp_dir attribute. */
1243 const char *comp_dir;
1246 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1247 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1248 const gdb_byte *info_ptr,
1249 struct die_info *comp_unit_die,
1253 /* A 1-based directory index. This is a strong typedef to prevent
1254 accidentally using a directory index as a 0-based index into an
1256 enum class dir_index : unsigned int {};
1258 /* Likewise, a 1-based file name index. */
1259 enum class file_name_index : unsigned int {};
1263 file_entry () = default;
1265 file_entry (const char *name_, dir_index d_index_,
1266 unsigned int mod_time_, unsigned int length_)
1269 mod_time (mod_time_),
1273 /* Return the include directory at D_INDEX stored in LH. Returns
1274 NULL if D_INDEX is out of bounds. */
1275 const char *include_dir (const line_header *lh) const;
1277 /* The file name. Note this is an observing pointer. The memory is
1278 owned by debug_line_buffer. */
1279 const char *name {};
1281 /* The directory index (1-based). */
1282 dir_index d_index {};
1284 unsigned int mod_time {};
1286 unsigned int length {};
1288 /* True if referenced by the Line Number Program. */
1291 /* The associated symbol table, if any. */
1292 struct symtab *symtab {};
1295 /* The line number information for a compilation unit (found in the
1296 .debug_line section) begins with a "statement program header",
1297 which contains the following information. */
1304 /* Add an entry to the include directory table. */
1305 void add_include_dir (const char *include_dir);
1307 /* Add an entry to the file name table. */
1308 void add_file_name (const char *name, dir_index d_index,
1309 unsigned int mod_time, unsigned int length);
1311 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
1312 is out of bounds. */
1313 const char *include_dir_at (dir_index index) const
1315 /* Convert directory index number (1-based) to vector index
1317 size_t vec_index = to_underlying (index) - 1;
1319 if (vec_index >= include_dirs.size ())
1321 return include_dirs[vec_index];
1324 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
1325 is out of bounds. */
1326 file_entry *file_name_at (file_name_index index)
1328 /* Convert file name index number (1-based) to vector index
1330 size_t vec_index = to_underlying (index) - 1;
1332 if (vec_index >= file_names.size ())
1334 return &file_names[vec_index];
1337 /* Const version of the above. */
1338 const file_entry *file_name_at (unsigned int index) const
1340 if (index >= file_names.size ())
1342 return &file_names[index];
1345 /* Offset of line number information in .debug_line section. */
1346 sect_offset sect_off {};
1348 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1349 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1351 unsigned int total_length {};
1352 unsigned short version {};
1353 unsigned int header_length {};
1354 unsigned char minimum_instruction_length {};
1355 unsigned char maximum_ops_per_instruction {};
1356 unsigned char default_is_stmt {};
1358 unsigned char line_range {};
1359 unsigned char opcode_base {};
1361 /* standard_opcode_lengths[i] is the number of operands for the
1362 standard opcode whose value is i. This means that
1363 standard_opcode_lengths[0] is unused, and the last meaningful
1364 element is standard_opcode_lengths[opcode_base - 1]. */
1365 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1367 /* The include_directories table. Note these are observing
1368 pointers. The memory is owned by debug_line_buffer. */
1369 std::vector<const char *> include_dirs;
1371 /* The file_names table. */
1372 std::vector<file_entry> file_names;
1374 /* The start and end of the statement program following this
1375 header. These point into dwarf2_per_objfile->line_buffer. */
1376 const gdb_byte *statement_program_start {}, *statement_program_end {};
1379 typedef std::unique_ptr<line_header> line_header_up;
1382 file_entry::include_dir (const line_header *lh) const
1384 return lh->include_dir_at (d_index);
1387 /* When we construct a partial symbol table entry we only
1388 need this much information. */
1389 struct partial_die_info
1391 /* Offset of this DIE. */
1392 sect_offset sect_off;
1394 /* DWARF-2 tag for this DIE. */
1395 ENUM_BITFIELD(dwarf_tag) tag : 16;
1397 /* Assorted flags describing the data found in this DIE. */
1398 unsigned int has_children : 1;
1399 unsigned int is_external : 1;
1400 unsigned int is_declaration : 1;
1401 unsigned int has_type : 1;
1402 unsigned int has_specification : 1;
1403 unsigned int has_pc_info : 1;
1404 unsigned int may_be_inlined : 1;
1406 /* This DIE has been marked DW_AT_main_subprogram. */
1407 unsigned int main_subprogram : 1;
1409 /* Flag set if the SCOPE field of this structure has been
1411 unsigned int scope_set : 1;
1413 /* Flag set if the DIE has a byte_size attribute. */
1414 unsigned int has_byte_size : 1;
1416 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1417 unsigned int has_const_value : 1;
1419 /* Flag set if any of the DIE's children are template arguments. */
1420 unsigned int has_template_arguments : 1;
1422 /* Flag set if fixup_partial_die has been called on this die. */
1423 unsigned int fixup_called : 1;
1425 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1426 unsigned int is_dwz : 1;
1428 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1429 unsigned int spec_is_dwz : 1;
1431 /* The name of this DIE. Normally the value of DW_AT_name, but
1432 sometimes a default name for unnamed DIEs. */
1435 /* The linkage name, if present. */
1436 const char *linkage_name;
1438 /* The scope to prepend to our children. This is generally
1439 allocated on the comp_unit_obstack, so will disappear
1440 when this compilation unit leaves the cache. */
1443 /* Some data associated with the partial DIE. The tag determines
1444 which field is live. */
1447 /* The location description associated with this DIE, if any. */
1448 struct dwarf_block *locdesc;
1449 /* The offset of an import, for DW_TAG_imported_unit. */
1450 sect_offset sect_off;
1453 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1457 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1458 DW_AT_sibling, if any. */
1459 /* NOTE: This member isn't strictly necessary, read_partial_die could
1460 return DW_AT_sibling values to its caller load_partial_dies. */
1461 const gdb_byte *sibling;
1463 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1464 DW_AT_specification (or DW_AT_abstract_origin or
1465 DW_AT_extension). */
1466 sect_offset spec_offset;
1468 /* Pointers to this DIE's parent, first child, and next sibling,
1470 struct partial_die_info *die_parent, *die_child, *die_sibling;
1473 /* This data structure holds the information of an abbrev. */
1476 unsigned int number; /* number identifying abbrev */
1477 enum dwarf_tag tag; /* dwarf tag */
1478 unsigned short has_children; /* boolean */
1479 unsigned short num_attrs; /* number of attributes */
1480 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1481 struct abbrev_info *next; /* next in chain */
1486 ENUM_BITFIELD(dwarf_attribute) name : 16;
1487 ENUM_BITFIELD(dwarf_form) form : 16;
1489 /* It is valid only if FORM is DW_FORM_implicit_const. */
1490 LONGEST implicit_const;
1493 /* Size of abbrev_table.abbrev_hash_table. */
1494 #define ABBREV_HASH_SIZE 121
1496 /* Top level data structure to contain an abbreviation table. */
1500 /* Where the abbrev table came from.
1501 This is used as a sanity check when the table is used. */
1502 sect_offset sect_off;
1504 /* Storage for the abbrev table. */
1505 struct obstack abbrev_obstack;
1507 /* Hash table of abbrevs.
1508 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1509 It could be statically allocated, but the previous code didn't so we
1511 struct abbrev_info **abbrevs;
1514 /* Attributes have a name and a value. */
1517 ENUM_BITFIELD(dwarf_attribute) name : 16;
1518 ENUM_BITFIELD(dwarf_form) form : 15;
1520 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1521 field should be in u.str (existing only for DW_STRING) but it is kept
1522 here for better struct attribute alignment. */
1523 unsigned int string_is_canonical : 1;
1528 struct dwarf_block *blk;
1537 /* This data structure holds a complete die structure. */
1540 /* DWARF-2 tag for this DIE. */
1541 ENUM_BITFIELD(dwarf_tag) tag : 16;
1543 /* Number of attributes */
1544 unsigned char num_attrs;
1546 /* True if we're presently building the full type name for the
1547 type derived from this DIE. */
1548 unsigned char building_fullname : 1;
1550 /* True if this die is in process. PR 16581. */
1551 unsigned char in_process : 1;
1554 unsigned int abbrev;
1556 /* Offset in .debug_info or .debug_types section. */
1557 sect_offset sect_off;
1559 /* The dies in a compilation unit form an n-ary tree. PARENT
1560 points to this die's parent; CHILD points to the first child of
1561 this node; and all the children of a given node are chained
1562 together via their SIBLING fields. */
1563 struct die_info *child; /* Its first child, if any. */
1564 struct die_info *sibling; /* Its next sibling, if any. */
1565 struct die_info *parent; /* Its parent, if any. */
1567 /* An array of attributes, with NUM_ATTRS elements. There may be
1568 zero, but it's not common and zero-sized arrays are not
1569 sufficiently portable C. */
1570 struct attribute attrs[1];
1573 /* Get at parts of an attribute structure. */
1575 #define DW_STRING(attr) ((attr)->u.str)
1576 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1577 #define DW_UNSND(attr) ((attr)->u.unsnd)
1578 #define DW_BLOCK(attr) ((attr)->u.blk)
1579 #define DW_SND(attr) ((attr)->u.snd)
1580 #define DW_ADDR(attr) ((attr)->u.addr)
1581 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1583 /* Blocks are a bunch of untyped bytes. */
1588 /* Valid only if SIZE is not zero. */
1589 const gdb_byte *data;
1592 #ifndef ATTR_ALLOC_CHUNK
1593 #define ATTR_ALLOC_CHUNK 4
1596 /* Allocate fields for structs, unions and enums in this size. */
1597 #ifndef DW_FIELD_ALLOC_CHUNK
1598 #define DW_FIELD_ALLOC_CHUNK 4
1601 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1602 but this would require a corresponding change in unpack_field_as_long
1604 static int bits_per_byte = 8;
1608 struct nextfield *next;
1616 struct nextfnfield *next;
1617 struct fn_field fnfield;
1624 struct nextfnfield *head;
1627 struct decl_field_list
1629 struct decl_field field;
1630 struct decl_field_list *next;
1633 /* The routines that read and process dies for a C struct or C++ class
1634 pass lists of data member fields and lists of member function fields
1635 in an instance of a field_info structure, as defined below. */
1638 /* List of data member and baseclasses fields. */
1639 struct nextfield *fields, *baseclasses;
1641 /* Number of fields (including baseclasses). */
1644 /* Number of baseclasses. */
1647 /* Set if the accesibility of one of the fields is not public. */
1648 int non_public_fields;
1650 /* Member function fieldlist array, contains name of possibly overloaded
1651 member function, number of overloaded member functions and a pointer
1652 to the head of the member function field chain. */
1653 struct fnfieldlist *fnfieldlists;
1655 /* Number of entries in the fnfieldlists array. */
1658 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1659 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1660 struct decl_field_list *typedef_field_list;
1661 unsigned typedef_field_list_count;
1663 /* Nested types defined by this class and the number of elements in this
1665 struct decl_field_list *nested_types_list;
1666 unsigned nested_types_list_count;
1669 /* One item on the queue of compilation units to read in full symbols
1671 struct dwarf2_queue_item
1673 struct dwarf2_per_cu_data *per_cu;
1674 enum language pretend_language;
1675 struct dwarf2_queue_item *next;
1678 /* The current queue. */
1679 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1681 /* Loaded secondary compilation units are kept in memory until they
1682 have not been referenced for the processing of this many
1683 compilation units. Set this to zero to disable caching. Cache
1684 sizes of up to at least twenty will improve startup time for
1685 typical inter-CU-reference binaries, at an obvious memory cost. */
1686 static int dwarf_max_cache_age = 5;
1688 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1689 struct cmd_list_element *c, const char *value)
1691 fprintf_filtered (file, _("The upper bound on the age of cached "
1692 "DWARF compilation units is %s.\n"),
1696 /* local function prototypes */
1698 static const char *get_section_name (const struct dwarf2_section_info *);
1700 static const char *get_section_file_name (const struct dwarf2_section_info *);
1702 static void dwarf2_find_base_address (struct die_info *die,
1703 struct dwarf2_cu *cu);
1705 static struct partial_symtab *create_partial_symtab
1706 (struct dwarf2_per_cu_data *per_cu, const char *name);
1708 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1709 const gdb_byte *info_ptr,
1710 struct die_info *type_unit_die,
1711 int has_children, void *data);
1713 static void dwarf2_build_psymtabs_hard (struct objfile *);
1715 static void scan_partial_symbols (struct partial_die_info *,
1716 CORE_ADDR *, CORE_ADDR *,
1717 int, struct dwarf2_cu *);
1719 static void add_partial_symbol (struct partial_die_info *,
1720 struct dwarf2_cu *);
1722 static void add_partial_namespace (struct partial_die_info *pdi,
1723 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1724 int set_addrmap, struct dwarf2_cu *cu);
1726 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1727 CORE_ADDR *highpc, int set_addrmap,
1728 struct dwarf2_cu *cu);
1730 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1731 struct dwarf2_cu *cu);
1733 static void add_partial_subprogram (struct partial_die_info *pdi,
1734 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1735 int need_pc, struct dwarf2_cu *cu);
1737 static void dwarf2_read_symtab (struct partial_symtab *,
1740 static void psymtab_to_symtab_1 (struct partial_symtab *);
1742 static struct abbrev_info *abbrev_table_lookup_abbrev
1743 (const struct abbrev_table *, unsigned int);
1745 static struct abbrev_table *abbrev_table_read_table
1746 (struct dwarf2_section_info *, sect_offset);
1748 static void abbrev_table_free (struct abbrev_table *);
1750 static void abbrev_table_free_cleanup (void *);
1752 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1753 struct dwarf2_section_info *);
1755 static void dwarf2_free_abbrev_table (void *);
1757 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1759 static struct partial_die_info *load_partial_dies
1760 (const struct die_reader_specs *, const gdb_byte *, int);
1762 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1763 struct partial_die_info *,
1764 struct abbrev_info *,
1768 static struct partial_die_info *find_partial_die (sect_offset, int,
1769 struct dwarf2_cu *);
1771 static void fixup_partial_die (struct partial_die_info *,
1772 struct dwarf2_cu *);
1774 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1775 struct attribute *, struct attr_abbrev *,
1778 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1780 static int read_1_signed_byte (bfd *, const gdb_byte *);
1782 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1784 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1786 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1788 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1791 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1793 static LONGEST read_checked_initial_length_and_offset
1794 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1795 unsigned int *, unsigned int *);
1797 static LONGEST read_offset (bfd *, const gdb_byte *,
1798 const struct comp_unit_head *,
1801 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1803 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1806 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1808 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1810 static const char *read_indirect_string (bfd *, const gdb_byte *,
1811 const struct comp_unit_head *,
1814 static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1815 const struct comp_unit_head *,
1818 static const char *read_indirect_string_at_offset (bfd *abfd,
1819 LONGEST str_offset);
1821 static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1823 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1825 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1829 static const char *read_str_index (const struct die_reader_specs *reader,
1830 ULONGEST str_index);
1832 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1834 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1835 struct dwarf2_cu *);
1837 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1840 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1841 struct dwarf2_cu *cu);
1843 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1844 struct dwarf2_cu *cu);
1846 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1848 static struct die_info *die_specification (struct die_info *die,
1849 struct dwarf2_cu **);
1851 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1852 struct dwarf2_cu *cu);
1854 static void dwarf_decode_lines (struct line_header *, const char *,
1855 struct dwarf2_cu *, struct partial_symtab *,
1856 CORE_ADDR, int decode_mapping);
1858 static void dwarf2_start_subfile (const char *, const char *);
1860 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1861 const char *, const char *,
1864 static struct symbol *new_symbol (struct die_info *, struct type *,
1865 struct dwarf2_cu *);
1867 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1868 struct dwarf2_cu *, struct symbol *);
1870 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1871 struct dwarf2_cu *);
1873 static void dwarf2_const_value_attr (const struct attribute *attr,
1876 struct obstack *obstack,
1877 struct dwarf2_cu *cu, LONGEST *value,
1878 const gdb_byte **bytes,
1879 struct dwarf2_locexpr_baton **baton);
1881 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1883 static int need_gnat_info (struct dwarf2_cu *);
1885 static struct type *die_descriptive_type (struct die_info *,
1886 struct dwarf2_cu *);
1888 static void set_descriptive_type (struct type *, struct die_info *,
1889 struct dwarf2_cu *);
1891 static struct type *die_containing_type (struct die_info *,
1892 struct dwarf2_cu *);
1894 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1895 struct dwarf2_cu *);
1897 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1899 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1901 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1903 static char *typename_concat (struct obstack *obs, const char *prefix,
1904 const char *suffix, int physname,
1905 struct dwarf2_cu *cu);
1907 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1909 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1911 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1913 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1915 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1917 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1919 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1920 struct dwarf2_cu *, struct partial_symtab *);
1922 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1923 values. Keep the items ordered with increasing constraints compliance. */
1926 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1927 PC_BOUNDS_NOT_PRESENT,
1929 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1930 were present but they do not form a valid range of PC addresses. */
1933 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1936 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1940 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1941 CORE_ADDR *, CORE_ADDR *,
1943 struct partial_symtab *);
1945 static void get_scope_pc_bounds (struct die_info *,
1946 CORE_ADDR *, CORE_ADDR *,
1947 struct dwarf2_cu *);
1949 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1950 CORE_ADDR, struct dwarf2_cu *);
1952 static void dwarf2_add_field (struct field_info *, struct die_info *,
1953 struct dwarf2_cu *);
1955 static void dwarf2_attach_fields_to_type (struct field_info *,
1956 struct type *, struct dwarf2_cu *);
1958 static void dwarf2_add_member_fn (struct field_info *,
1959 struct die_info *, struct type *,
1960 struct dwarf2_cu *);
1962 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1964 struct dwarf2_cu *);
1966 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1968 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1970 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1972 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1974 static struct using_direct **using_directives (enum language);
1976 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1978 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1980 static struct type *read_module_type (struct die_info *die,
1981 struct dwarf2_cu *cu);
1983 static const char *namespace_name (struct die_info *die,
1984 int *is_anonymous, struct dwarf2_cu *);
1986 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1988 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1990 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1991 struct dwarf2_cu *);
1993 static struct die_info *read_die_and_siblings_1
1994 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1997 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1998 const gdb_byte *info_ptr,
1999 const gdb_byte **new_info_ptr,
2000 struct die_info *parent);
2002 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
2003 struct die_info **, const gdb_byte *,
2006 static const gdb_byte *read_full_die (const struct die_reader_specs *,
2007 struct die_info **, const gdb_byte *,
2010 static void process_die (struct die_info *, struct dwarf2_cu *);
2012 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
2015 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
2017 static const char *dwarf2_full_name (const char *name,
2018 struct die_info *die,
2019 struct dwarf2_cu *cu);
2021 static const char *dwarf2_physname (const char *name, struct die_info *die,
2022 struct dwarf2_cu *cu);
2024 static struct die_info *dwarf2_extension (struct die_info *die,
2025 struct dwarf2_cu **);
2027 static const char *dwarf_tag_name (unsigned int);
2029 static const char *dwarf_attr_name (unsigned int);
2031 static const char *dwarf_form_name (unsigned int);
2033 static const char *dwarf_bool_name (unsigned int);
2035 static const char *dwarf_type_encoding_name (unsigned int);
2037 static struct die_info *sibling_die (struct die_info *);
2039 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
2041 static void dump_die_for_error (struct die_info *);
2043 static void dump_die_1 (struct ui_file *, int level, int max_level,
2046 /*static*/ void dump_die (struct die_info *, int max_level);
2048 static void store_in_ref_table (struct die_info *,
2049 struct dwarf2_cu *);
2051 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
2053 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
2055 static struct die_info *follow_die_ref_or_sig (struct die_info *,
2056 const struct attribute *,
2057 struct dwarf2_cu **);
2059 static struct die_info *follow_die_ref (struct die_info *,
2060 const struct attribute *,
2061 struct dwarf2_cu **);
2063 static struct die_info *follow_die_sig (struct die_info *,
2064 const struct attribute *,
2065 struct dwarf2_cu **);
2067 static struct type *get_signatured_type (struct die_info *, ULONGEST,
2068 struct dwarf2_cu *);
2070 static struct type *get_DW_AT_signature_type (struct die_info *,
2071 const struct attribute *,
2072 struct dwarf2_cu *);
2074 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
2076 static void read_signatured_type (struct signatured_type *);
2078 static int attr_to_dynamic_prop (const struct attribute *attr,
2079 struct die_info *die, struct dwarf2_cu *cu,
2080 struct dynamic_prop *prop);
2082 /* memory allocation interface */
2084 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
2086 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
2088 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2090 static int attr_form_is_block (const struct attribute *);
2092 static int attr_form_is_section_offset (const struct attribute *);
2094 static int attr_form_is_constant (const struct attribute *);
2096 static int attr_form_is_ref (const struct attribute *);
2098 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
2099 struct dwarf2_loclist_baton *baton,
2100 const struct attribute *attr);
2102 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
2104 struct dwarf2_cu *cu,
2107 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
2108 const gdb_byte *info_ptr,
2109 struct abbrev_info *abbrev);
2111 static void free_stack_comp_unit (void *);
2113 static hashval_t partial_die_hash (const void *item);
2115 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2117 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
2118 (sect_offset sect_off, unsigned int offset_in_dwz, struct objfile *objfile);
2120 static void init_one_comp_unit (struct dwarf2_cu *cu,
2121 struct dwarf2_per_cu_data *per_cu);
2123 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
2124 struct die_info *comp_unit_die,
2125 enum language pretend_language);
2127 static void free_heap_comp_unit (void *);
2129 static void free_cached_comp_units (void *);
2131 static void age_cached_comp_units (void);
2133 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
2135 static struct type *set_die_type (struct die_info *, struct type *,
2136 struct dwarf2_cu *);
2138 static void create_all_comp_units (struct objfile *);
2140 static int create_all_type_units (struct objfile *);
2142 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2145 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2148 static void process_full_type_unit (struct dwarf2_per_cu_data *,
2151 static void dwarf2_add_dependence (struct dwarf2_cu *,
2152 struct dwarf2_per_cu_data *);
2154 static void dwarf2_mark (struct dwarf2_cu *);
2156 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2158 static struct type *get_die_type_at_offset (sect_offset,
2159 struct dwarf2_per_cu_data *);
2161 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
2163 static void dwarf2_release_queue (void *dummy);
2165 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2166 enum language pretend_language);
2168 static void process_queue (void);
2170 /* The return type of find_file_and_directory. Note, the enclosed
2171 string pointers are only valid while this object is valid. */
2173 struct file_and_directory
2175 /* The filename. This is never NULL. */
2178 /* The compilation directory. NULL if not known. If we needed to
2179 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2180 points directly to the DW_AT_comp_dir string attribute owned by
2181 the obstack that owns the DIE. */
2182 const char *comp_dir;
2184 /* If we needed to build a new string for comp_dir, this is what
2185 owns the storage. */
2186 std::string comp_dir_storage;
2189 static file_and_directory find_file_and_directory (struct die_info *die,
2190 struct dwarf2_cu *cu);
2192 static char *file_full_name (int file, struct line_header *lh,
2193 const char *comp_dir);
2195 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
2196 enum class rcuh_kind { COMPILE, TYPE };
2198 static const gdb_byte *read_and_check_comp_unit_head
2199 (struct comp_unit_head *header,
2200 struct dwarf2_section_info *section,
2201 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
2202 rcuh_kind section_kind);
2204 static void init_cutu_and_read_dies
2205 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2206 int use_existing_cu, int keep,
2207 die_reader_func_ftype *die_reader_func, void *data);
2209 static void init_cutu_and_read_dies_simple
2210 (struct dwarf2_per_cu_data *this_cu,
2211 die_reader_func_ftype *die_reader_func, void *data);
2213 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2215 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2217 static struct dwo_unit *lookup_dwo_unit_in_dwp
2218 (struct dwp_file *dwp_file, const char *comp_dir,
2219 ULONGEST signature, int is_debug_types);
2221 static struct dwp_file *get_dwp_file (void);
2223 static struct dwo_unit *lookup_dwo_comp_unit
2224 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2226 static struct dwo_unit *lookup_dwo_type_unit
2227 (struct signatured_type *, const char *, const char *);
2229 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2231 static void free_dwo_file_cleanup (void *);
2233 static void process_cu_includes (void);
2235 static void check_producer (struct dwarf2_cu *cu);
2237 static void free_line_header_voidp (void *arg);
2239 /* Various complaints about symbol reading that don't abort the process. */
2242 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2244 complaint (&symfile_complaints,
2245 _("statement list doesn't fit in .debug_line section"));
2249 dwarf2_debug_line_missing_file_complaint (void)
2251 complaint (&symfile_complaints,
2252 _(".debug_line section has line data without a file"));
2256 dwarf2_debug_line_missing_end_sequence_complaint (void)
2258 complaint (&symfile_complaints,
2259 _(".debug_line section has line "
2260 "program sequence without an end"));
2264 dwarf2_complex_location_expr_complaint (void)
2266 complaint (&symfile_complaints, _("location expression too complex"));
2270 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2273 complaint (&symfile_complaints,
2274 _("const value length mismatch for '%s', got %d, expected %d"),
2279 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2281 complaint (&symfile_complaints,
2282 _("debug info runs off end of %s section"
2284 get_section_name (section),
2285 get_section_file_name (section));
2289 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2291 complaint (&symfile_complaints,
2292 _("macro debug info contains a "
2293 "malformed macro definition:\n`%s'"),
2298 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2300 complaint (&symfile_complaints,
2301 _("invalid attribute class or form for '%s' in '%s'"),
2305 /* Hash function for line_header_hash. */
2308 line_header_hash (const struct line_header *ofs)
2310 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2313 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2316 line_header_hash_voidp (const void *item)
2318 const struct line_header *ofs = (const struct line_header *) item;
2320 return line_header_hash (ofs);
2323 /* Equality function for line_header_hash. */
2326 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2328 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2329 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2331 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2332 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2337 /* Read the given attribute value as an address, taking the attribute's
2338 form into account. */
2341 attr_value_as_address (struct attribute *attr)
2345 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2347 /* Aside from a few clearly defined exceptions, attributes that
2348 contain an address must always be in DW_FORM_addr form.
2349 Unfortunately, some compilers happen to be violating this
2350 requirement by encoding addresses using other forms, such
2351 as DW_FORM_data4 for example. For those broken compilers,
2352 we try to do our best, without any guarantee of success,
2353 to interpret the address correctly. It would also be nice
2354 to generate a complaint, but that would require us to maintain
2355 a list of legitimate cases where a non-address form is allowed,
2356 as well as update callers to pass in at least the CU's DWARF
2357 version. This is more overhead than what we're willing to
2358 expand for a pretty rare case. */
2359 addr = DW_UNSND (attr);
2362 addr = DW_ADDR (attr);
2367 /* The suffix for an index file. */
2368 #define INDEX4_SUFFIX ".gdb-index"
2369 #define INDEX5_SUFFIX ".debug_names"
2370 #define DEBUG_STR_SUFFIX ".debug_str"
2372 /* See declaration. */
2374 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2375 const dwarf2_debug_sections *names)
2376 : objfile (objfile_)
2379 names = &dwarf2_elf_names;
2381 bfd *obfd = objfile->obfd;
2383 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2384 locate_sections (obfd, sec, *names);
2387 dwarf2_per_objfile::~dwarf2_per_objfile ()
2389 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2390 free_cached_comp_units ();
2392 if (quick_file_names_table)
2393 htab_delete (quick_file_names_table);
2395 if (line_header_hash)
2396 htab_delete (line_header_hash);
2398 /* Everything else should be on the objfile obstack. */
2401 /* See declaration. */
2404 dwarf2_per_objfile::free_cached_comp_units ()
2406 dwarf2_per_cu_data *per_cu = read_in_chain;
2407 dwarf2_per_cu_data **last_chain = &read_in_chain;
2408 while (per_cu != NULL)
2410 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2412 free_heap_comp_unit (per_cu->cu);
2413 *last_chain = next_cu;
2418 /* Try to locate the sections we need for DWARF 2 debugging
2419 information and return true if we have enough to do something.
2420 NAMES points to the dwarf2 section names, or is NULL if the standard
2421 ELF names are used. */
2424 dwarf2_has_info (struct objfile *objfile,
2425 const struct dwarf2_debug_sections *names)
2427 if (objfile->flags & OBJF_READNEVER)
2430 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2431 objfile_data (objfile, dwarf2_objfile_data_key));
2432 if (!dwarf2_per_objfile)
2434 /* Initialize per-objfile state. */
2435 struct dwarf2_per_objfile *data
2436 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
2438 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
2439 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
2441 return (!dwarf2_per_objfile->info.is_virtual
2442 && dwarf2_per_objfile->info.s.section != NULL
2443 && !dwarf2_per_objfile->abbrev.is_virtual
2444 && dwarf2_per_objfile->abbrev.s.section != NULL);
2447 /* Return the containing section of virtual section SECTION. */
2449 static struct dwarf2_section_info *
2450 get_containing_section (const struct dwarf2_section_info *section)
2452 gdb_assert (section->is_virtual);
2453 return section->s.containing_section;
2456 /* Return the bfd owner of SECTION. */
2459 get_section_bfd_owner (const struct dwarf2_section_info *section)
2461 if (section->is_virtual)
2463 section = get_containing_section (section);
2464 gdb_assert (!section->is_virtual);
2466 return section->s.section->owner;
2469 /* Return the bfd section of SECTION.
2470 Returns NULL if the section is not present. */
2473 get_section_bfd_section (const struct dwarf2_section_info *section)
2475 if (section->is_virtual)
2477 section = get_containing_section (section);
2478 gdb_assert (!section->is_virtual);
2480 return section->s.section;
2483 /* Return the name of SECTION. */
2486 get_section_name (const struct dwarf2_section_info *section)
2488 asection *sectp = get_section_bfd_section (section);
2490 gdb_assert (sectp != NULL);
2491 return bfd_section_name (get_section_bfd_owner (section), sectp);
2494 /* Return the name of the file SECTION is in. */
2497 get_section_file_name (const struct dwarf2_section_info *section)
2499 bfd *abfd = get_section_bfd_owner (section);
2501 return bfd_get_filename (abfd);
2504 /* Return the id of SECTION.
2505 Returns 0 if SECTION doesn't exist. */
2508 get_section_id (const struct dwarf2_section_info *section)
2510 asection *sectp = get_section_bfd_section (section);
2517 /* Return the flags of SECTION.
2518 SECTION (or containing section if this is a virtual section) must exist. */
2521 get_section_flags (const struct dwarf2_section_info *section)
2523 asection *sectp = get_section_bfd_section (section);
2525 gdb_assert (sectp != NULL);
2526 return bfd_get_section_flags (sectp->owner, sectp);
2529 /* When loading sections, we look either for uncompressed section or for
2530 compressed section names. */
2533 section_is_p (const char *section_name,
2534 const struct dwarf2_section_names *names)
2536 if (names->normal != NULL
2537 && strcmp (section_name, names->normal) == 0)
2539 if (names->compressed != NULL
2540 && strcmp (section_name, names->compressed) == 0)
2545 /* See declaration. */
2548 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2549 const dwarf2_debug_sections &names)
2551 flagword aflag = bfd_get_section_flags (abfd, sectp);
2553 if ((aflag & SEC_HAS_CONTENTS) == 0)
2556 else if (section_is_p (sectp->name, &names.info))
2558 this->info.s.section = sectp;
2559 this->info.size = bfd_get_section_size (sectp);
2561 else if (section_is_p (sectp->name, &names.abbrev))
2563 this->abbrev.s.section = sectp;
2564 this->abbrev.size = bfd_get_section_size (sectp);
2566 else if (section_is_p (sectp->name, &names.line))
2568 this->line.s.section = sectp;
2569 this->line.size = bfd_get_section_size (sectp);
2571 else if (section_is_p (sectp->name, &names.loc))
2573 this->loc.s.section = sectp;
2574 this->loc.size = bfd_get_section_size (sectp);
2576 else if (section_is_p (sectp->name, &names.loclists))
2578 this->loclists.s.section = sectp;
2579 this->loclists.size = bfd_get_section_size (sectp);
2581 else if (section_is_p (sectp->name, &names.macinfo))
2583 this->macinfo.s.section = sectp;
2584 this->macinfo.size = bfd_get_section_size (sectp);
2586 else if (section_is_p (sectp->name, &names.macro))
2588 this->macro.s.section = sectp;
2589 this->macro.size = bfd_get_section_size (sectp);
2591 else if (section_is_p (sectp->name, &names.str))
2593 this->str.s.section = sectp;
2594 this->str.size = bfd_get_section_size (sectp);
2596 else if (section_is_p (sectp->name, &names.line_str))
2598 this->line_str.s.section = sectp;
2599 this->line_str.size = bfd_get_section_size (sectp);
2601 else if (section_is_p (sectp->name, &names.addr))
2603 this->addr.s.section = sectp;
2604 this->addr.size = bfd_get_section_size (sectp);
2606 else if (section_is_p (sectp->name, &names.frame))
2608 this->frame.s.section = sectp;
2609 this->frame.size = bfd_get_section_size (sectp);
2611 else if (section_is_p (sectp->name, &names.eh_frame))
2613 this->eh_frame.s.section = sectp;
2614 this->eh_frame.size = bfd_get_section_size (sectp);
2616 else if (section_is_p (sectp->name, &names.ranges))
2618 this->ranges.s.section = sectp;
2619 this->ranges.size = bfd_get_section_size (sectp);
2621 else if (section_is_p (sectp->name, &names.rnglists))
2623 this->rnglists.s.section = sectp;
2624 this->rnglists.size = bfd_get_section_size (sectp);
2626 else if (section_is_p (sectp->name, &names.types))
2628 struct dwarf2_section_info type_section;
2630 memset (&type_section, 0, sizeof (type_section));
2631 type_section.s.section = sectp;
2632 type_section.size = bfd_get_section_size (sectp);
2634 VEC_safe_push (dwarf2_section_info_def, this->types,
2637 else if (section_is_p (sectp->name, &names.gdb_index))
2639 this->gdb_index.s.section = sectp;
2640 this->gdb_index.size = bfd_get_section_size (sectp);
2642 else if (section_is_p (sectp->name, &names.debug_names))
2644 this->debug_names.s.section = sectp;
2645 this->debug_names.size = bfd_get_section_size (sectp);
2647 else if (section_is_p (sectp->name, &names.debug_aranges))
2649 this->debug_aranges.s.section = sectp;
2650 this->debug_aranges.size = bfd_get_section_size (sectp);
2653 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2654 && bfd_section_vma (abfd, sectp) == 0)
2655 this->has_section_at_zero = true;
2658 /* A helper function that decides whether a section is empty,
2662 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2664 if (section->is_virtual)
2665 return section->size == 0;
2666 return section->s.section == NULL || section->size == 0;
2669 /* Read the contents of the section INFO.
2670 OBJFILE is the main object file, but not necessarily the file where
2671 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2673 If the section is compressed, uncompress it before returning. */
2676 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2680 gdb_byte *buf, *retbuf;
2684 info->buffer = NULL;
2687 if (dwarf2_section_empty_p (info))
2690 sectp = get_section_bfd_section (info);
2692 /* If this is a virtual section we need to read in the real one first. */
2693 if (info->is_virtual)
2695 struct dwarf2_section_info *containing_section =
2696 get_containing_section (info);
2698 gdb_assert (sectp != NULL);
2699 if ((sectp->flags & SEC_RELOC) != 0)
2701 error (_("Dwarf Error: DWP format V2 with relocations is not"
2702 " supported in section %s [in module %s]"),
2703 get_section_name (info), get_section_file_name (info));
2705 dwarf2_read_section (objfile, containing_section);
2706 /* Other code should have already caught virtual sections that don't
2708 gdb_assert (info->virtual_offset + info->size
2709 <= containing_section->size);
2710 /* If the real section is empty or there was a problem reading the
2711 section we shouldn't get here. */
2712 gdb_assert (containing_section->buffer != NULL);
2713 info->buffer = containing_section->buffer + info->virtual_offset;
2717 /* If the section has relocations, we must read it ourselves.
2718 Otherwise we attach it to the BFD. */
2719 if ((sectp->flags & SEC_RELOC) == 0)
2721 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2725 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2728 /* When debugging .o files, we may need to apply relocations; see
2729 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2730 We never compress sections in .o files, so we only need to
2731 try this when the section is not compressed. */
2732 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2735 info->buffer = retbuf;
2739 abfd = get_section_bfd_owner (info);
2740 gdb_assert (abfd != NULL);
2742 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2743 || bfd_bread (buf, info->size, abfd) != info->size)
2745 error (_("Dwarf Error: Can't read DWARF data"
2746 " in section %s [in module %s]"),
2747 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2751 /* A helper function that returns the size of a section in a safe way.
2752 If you are positive that the section has been read before using the
2753 size, then it is safe to refer to the dwarf2_section_info object's
2754 "size" field directly. In other cases, you must call this
2755 function, because for compressed sections the size field is not set
2756 correctly until the section has been read. */
2758 static bfd_size_type
2759 dwarf2_section_size (struct objfile *objfile,
2760 struct dwarf2_section_info *info)
2763 dwarf2_read_section (objfile, info);
2767 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2771 dwarf2_get_section_info (struct objfile *objfile,
2772 enum dwarf2_section_enum sect,
2773 asection **sectp, const gdb_byte **bufp,
2774 bfd_size_type *sizep)
2776 struct dwarf2_per_objfile *data
2777 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2778 dwarf2_objfile_data_key);
2779 struct dwarf2_section_info *info;
2781 /* We may see an objfile without any DWARF, in which case we just
2792 case DWARF2_DEBUG_FRAME:
2793 info = &data->frame;
2795 case DWARF2_EH_FRAME:
2796 info = &data->eh_frame;
2799 gdb_assert_not_reached ("unexpected section");
2802 dwarf2_read_section (objfile, info);
2804 *sectp = get_section_bfd_section (info);
2805 *bufp = info->buffer;
2806 *sizep = info->size;
2809 /* A helper function to find the sections for a .dwz file. */
2812 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2814 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2816 /* Note that we only support the standard ELF names, because .dwz
2817 is ELF-only (at the time of writing). */
2818 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2820 dwz_file->abbrev.s.section = sectp;
2821 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2823 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2825 dwz_file->info.s.section = sectp;
2826 dwz_file->info.size = bfd_get_section_size (sectp);
2828 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2830 dwz_file->str.s.section = sectp;
2831 dwz_file->str.size = bfd_get_section_size (sectp);
2833 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2835 dwz_file->line.s.section = sectp;
2836 dwz_file->line.size = bfd_get_section_size (sectp);
2838 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2840 dwz_file->macro.s.section = sectp;
2841 dwz_file->macro.size = bfd_get_section_size (sectp);
2843 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2845 dwz_file->gdb_index.s.section = sectp;
2846 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2848 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2850 dwz_file->debug_names.s.section = sectp;
2851 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2855 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2856 there is no .gnu_debugaltlink section in the file. Error if there
2857 is such a section but the file cannot be found. */
2859 static struct dwz_file *
2860 dwarf2_get_dwz_file (void)
2862 const char *filename;
2863 struct dwz_file *result;
2864 bfd_size_type buildid_len_arg;
2868 if (dwarf2_per_objfile->dwz_file != NULL)
2869 return dwarf2_per_objfile->dwz_file;
2871 bfd_set_error (bfd_error_no_error);
2872 gdb::unique_xmalloc_ptr<char> data
2873 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2874 &buildid_len_arg, &buildid));
2877 if (bfd_get_error () == bfd_error_no_error)
2879 error (_("could not read '.gnu_debugaltlink' section: %s"),
2880 bfd_errmsg (bfd_get_error ()));
2883 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2885 buildid_len = (size_t) buildid_len_arg;
2887 filename = data.get ();
2889 std::string abs_storage;
2890 if (!IS_ABSOLUTE_PATH (filename))
2892 gdb::unique_xmalloc_ptr<char> abs
2893 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2895 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2896 filename = abs_storage.c_str ();
2899 /* First try the file name given in the section. If that doesn't
2900 work, try to use the build-id instead. */
2901 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2902 if (dwz_bfd != NULL)
2904 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2908 if (dwz_bfd == NULL)
2909 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2911 if (dwz_bfd == NULL)
2912 error (_("could not find '.gnu_debugaltlink' file for %s"),
2913 objfile_name (dwarf2_per_objfile->objfile));
2915 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2917 result->dwz_bfd = dwz_bfd.release ();
2919 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
2921 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
2922 dwarf2_per_objfile->dwz_file = result;
2926 /* DWARF quick_symbols_functions support. */
2928 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2929 unique line tables, so we maintain a separate table of all .debug_line
2930 derived entries to support the sharing.
2931 All the quick functions need is the list of file names. We discard the
2932 line_header when we're done and don't need to record it here. */
2933 struct quick_file_names
2935 /* The data used to construct the hash key. */
2936 struct stmt_list_hash hash;
2938 /* The number of entries in file_names, real_names. */
2939 unsigned int num_file_names;
2941 /* The file names from the line table, after being run through
2943 const char **file_names;
2945 /* The file names from the line table after being run through
2946 gdb_realpath. These are computed lazily. */
2947 const char **real_names;
2950 /* When using the index (and thus not using psymtabs), each CU has an
2951 object of this type. This is used to hold information needed by
2952 the various "quick" methods. */
2953 struct dwarf2_per_cu_quick_data
2955 /* The file table. This can be NULL if there was no file table
2956 or it's currently not read in.
2957 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2958 struct quick_file_names *file_names;
2960 /* The corresponding symbol table. This is NULL if symbols for this
2961 CU have not yet been read. */
2962 struct compunit_symtab *compunit_symtab;
2964 /* A temporary mark bit used when iterating over all CUs in
2965 expand_symtabs_matching. */
2966 unsigned int mark : 1;
2968 /* True if we've tried to read the file table and found there isn't one.
2969 There will be no point in trying to read it again next time. */
2970 unsigned int no_file_data : 1;
2973 /* Utility hash function for a stmt_list_hash. */
2976 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2980 if (stmt_list_hash->dwo_unit != NULL)
2981 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2982 v += to_underlying (stmt_list_hash->line_sect_off);
2986 /* Utility equality function for a stmt_list_hash. */
2989 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2990 const struct stmt_list_hash *rhs)
2992 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2994 if (lhs->dwo_unit != NULL
2995 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2998 return lhs->line_sect_off == rhs->line_sect_off;
3001 /* Hash function for a quick_file_names. */
3004 hash_file_name_entry (const void *e)
3006 const struct quick_file_names *file_data
3007 = (const struct quick_file_names *) e;
3009 return hash_stmt_list_entry (&file_data->hash);
3012 /* Equality function for a quick_file_names. */
3015 eq_file_name_entry (const void *a, const void *b)
3017 const struct quick_file_names *ea = (const struct quick_file_names *) a;
3018 const struct quick_file_names *eb = (const struct quick_file_names *) b;
3020 return eq_stmt_list_entry (&ea->hash, &eb->hash);
3023 /* Delete function for a quick_file_names. */
3026 delete_file_name_entry (void *e)
3028 struct quick_file_names *file_data = (struct quick_file_names *) e;
3031 for (i = 0; i < file_data->num_file_names; ++i)
3033 xfree ((void*) file_data->file_names[i]);
3034 if (file_data->real_names)
3035 xfree ((void*) file_data->real_names[i]);
3038 /* The space for the struct itself lives on objfile_obstack,
3039 so we don't free it here. */
3042 /* Create a quick_file_names hash table. */
3045 create_quick_file_names_table (unsigned int nr_initial_entries)
3047 return htab_create_alloc (nr_initial_entries,
3048 hash_file_name_entry, eq_file_name_entry,
3049 delete_file_name_entry, xcalloc, xfree);
3052 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
3053 have to be created afterwards. You should call age_cached_comp_units after
3054 processing PER_CU->CU. dw2_setup must have been already called. */
3057 load_cu (struct dwarf2_per_cu_data *per_cu)
3059 if (per_cu->is_debug_types)
3060 load_full_type_unit (per_cu);
3062 load_full_comp_unit (per_cu, language_minimal);
3064 if (per_cu->cu == NULL)
3065 return; /* Dummy CU. */
3067 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
3070 /* Read in the symbols for PER_CU. */
3073 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3075 struct cleanup *back_to;
3077 /* Skip type_unit_groups, reading the type units they contain
3078 is handled elsewhere. */
3079 if (IS_TYPE_UNIT_GROUP (per_cu))
3082 back_to = make_cleanup (dwarf2_release_queue, NULL);
3084 if (dwarf2_per_objfile->using_index
3085 ? per_cu->v.quick->compunit_symtab == NULL
3086 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
3088 queue_comp_unit (per_cu, language_minimal);
3091 /* If we just loaded a CU from a DWO, and we're working with an index
3092 that may badly handle TUs, load all the TUs in that DWO as well.
3093 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
3094 if (!per_cu->is_debug_types
3095 && per_cu->cu != NULL
3096 && per_cu->cu->dwo_unit != NULL
3097 && dwarf2_per_objfile->index_table != NULL
3098 && dwarf2_per_objfile->index_table->version <= 7
3099 /* DWP files aren't supported yet. */
3100 && get_dwp_file () == NULL)
3101 queue_and_load_all_dwo_tus (per_cu);
3106 /* Age the cache, releasing compilation units that have not
3107 been used recently. */
3108 age_cached_comp_units ();
3110 do_cleanups (back_to);
3113 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
3114 the objfile from which this CU came. Returns the resulting symbol
3117 static struct compunit_symtab *
3118 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3120 gdb_assert (dwarf2_per_objfile->using_index);
3121 if (!per_cu->v.quick->compunit_symtab)
3123 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
3124 scoped_restore decrementer = increment_reading_symtab ();
3125 dw2_do_instantiate_symtab (per_cu);
3126 process_cu_includes ();
3127 do_cleanups (back_to);
3130 return per_cu->v.quick->compunit_symtab;
3133 /* Return the CU/TU given its index.
3135 This is intended for loops like:
3137 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3138 + dwarf2_per_objfile->n_type_units); ++i)
3140 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3146 static struct dwarf2_per_cu_data *
3147 dw2_get_cutu (int index)
3149 if (index >= dwarf2_per_objfile->n_comp_units)
3151 index -= dwarf2_per_objfile->n_comp_units;
3152 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3153 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
3156 return dwarf2_per_objfile->all_comp_units[index];
3159 /* Return the CU given its index.
3160 This differs from dw2_get_cutu in that it's for when you know INDEX
3163 static struct dwarf2_per_cu_data *
3164 dw2_get_cu (int index)
3166 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
3168 return dwarf2_per_objfile->all_comp_units[index];
3171 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3172 objfile_obstack, and constructed with the specified field
3175 static dwarf2_per_cu_data *
3176 create_cu_from_index_list (struct objfile *objfile,
3177 struct dwarf2_section_info *section,
3179 sect_offset sect_off, ULONGEST length)
3181 dwarf2_per_cu_data *the_cu
3182 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3183 struct dwarf2_per_cu_data);
3184 the_cu->sect_off = sect_off;
3185 the_cu->length = length;
3186 the_cu->objfile = objfile;
3187 the_cu->section = section;
3188 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3189 struct dwarf2_per_cu_quick_data);
3190 the_cu->is_dwz = is_dwz;
3194 /* A helper for create_cus_from_index that handles a given list of
3198 create_cus_from_index_list (struct objfile *objfile,
3199 const gdb_byte *cu_list, offset_type n_elements,
3200 struct dwarf2_section_info *section,
3206 for (i = 0; i < n_elements; i += 2)
3208 gdb_static_assert (sizeof (ULONGEST) >= 8);
3210 sect_offset sect_off
3211 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3212 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3215 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
3216 = create_cu_from_index_list (objfile, section, is_dwz, sect_off, length);
3220 /* Read the CU list from the mapped index, and use it to create all
3221 the CU objects for this objfile. */
3224 create_cus_from_index (struct objfile *objfile,
3225 const gdb_byte *cu_list, offset_type cu_list_elements,
3226 const gdb_byte *dwz_list, offset_type dwz_elements)
3228 struct dwz_file *dwz;
3230 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
3231 dwarf2_per_objfile->all_comp_units =
3232 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3233 dwarf2_per_objfile->n_comp_units);
3235 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3236 &dwarf2_per_objfile->info, 0, 0);
3238 if (dwz_elements == 0)
3241 dwz = dwarf2_get_dwz_file ();
3242 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3243 cu_list_elements / 2);
3246 /* Create the signatured type hash table from the index. */
3249 create_signatured_type_table_from_index (struct objfile *objfile,
3250 struct dwarf2_section_info *section,
3251 const gdb_byte *bytes,
3252 offset_type elements)
3255 htab_t sig_types_hash;
3257 dwarf2_per_objfile->n_type_units
3258 = dwarf2_per_objfile->n_allocated_type_units
3260 dwarf2_per_objfile->all_type_units =
3261 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3263 sig_types_hash = allocate_signatured_type_table (objfile);
3265 for (i = 0; i < elements; i += 3)
3267 struct signatured_type *sig_type;
3270 cu_offset type_offset_in_tu;
3272 gdb_static_assert (sizeof (ULONGEST) >= 8);
3273 sect_offset sect_off
3274 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3276 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3278 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3281 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3282 struct signatured_type);
3283 sig_type->signature = signature;
3284 sig_type->type_offset_in_tu = type_offset_in_tu;
3285 sig_type->per_cu.is_debug_types = 1;
3286 sig_type->per_cu.section = section;
3287 sig_type->per_cu.sect_off = sect_off;
3288 sig_type->per_cu.objfile = objfile;
3289 sig_type->per_cu.v.quick
3290 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3291 struct dwarf2_per_cu_quick_data);
3293 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3296 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3299 dwarf2_per_objfile->signatured_types = sig_types_hash;
3302 /* Create the signatured type hash table from .debug_names. */
3305 create_signatured_type_table_from_debug_names
3306 (struct objfile *objfile,
3307 const mapped_debug_names &map,
3308 struct dwarf2_section_info *section,
3309 struct dwarf2_section_info *abbrev_section)
3311 dwarf2_read_section (objfile, section);
3312 dwarf2_read_section (objfile, abbrev_section);
3314 dwarf2_per_objfile->n_type_units
3315 = dwarf2_per_objfile->n_allocated_type_units
3317 dwarf2_per_objfile->all_type_units
3318 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3320 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3322 for (uint32_t i = 0; i < map.tu_count; ++i)
3324 struct signatured_type *sig_type;
3327 cu_offset type_offset_in_tu;
3329 sect_offset sect_off
3330 = (sect_offset) (extract_unsigned_integer
3331 (map.tu_table_reordered + i * map.offset_size,
3333 map.dwarf5_byte_order));
3335 comp_unit_head cu_header;
3336 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
3337 section->buffer + to_underlying (sect_off),
3340 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3341 struct signatured_type);
3342 sig_type->signature = cu_header.signature;
3343 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3344 sig_type->per_cu.is_debug_types = 1;
3345 sig_type->per_cu.section = section;
3346 sig_type->per_cu.sect_off = sect_off;
3347 sig_type->per_cu.objfile = objfile;
3348 sig_type->per_cu.v.quick
3349 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3350 struct dwarf2_per_cu_quick_data);
3352 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3355 dwarf2_per_objfile->all_type_units[i] = sig_type;
3358 dwarf2_per_objfile->signatured_types = sig_types_hash;
3361 /* Read the address map data from the mapped index, and use it to
3362 populate the objfile's psymtabs_addrmap. */
3365 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3367 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3368 const gdb_byte *iter, *end;
3369 struct addrmap *mutable_map;
3372 auto_obstack temp_obstack;
3374 mutable_map = addrmap_create_mutable (&temp_obstack);
3376 iter = index->address_table.data ();
3377 end = iter + index->address_table.size ();
3379 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3383 ULONGEST hi, lo, cu_index;
3384 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3386 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3388 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3393 complaint (&symfile_complaints,
3394 _(".gdb_index address table has invalid range (%s - %s)"),
3395 hex_string (lo), hex_string (hi));
3399 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3401 complaint (&symfile_complaints,
3402 _(".gdb_index address table has invalid CU number %u"),
3403 (unsigned) cu_index);
3407 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3408 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3409 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
3412 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3413 &objfile->objfile_obstack);
3416 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3417 populate the objfile's psymtabs_addrmap. */
3420 create_addrmap_from_aranges (struct objfile *objfile,
3421 struct dwarf2_section_info *section)
3423 bfd *abfd = objfile->obfd;
3424 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3425 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3426 SECT_OFF_TEXT (objfile));
3428 auto_obstack temp_obstack;
3429 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3431 std::unordered_map<sect_offset,
3432 dwarf2_per_cu_data *,
3433 gdb::hash_enum<sect_offset>>
3434 debug_info_offset_to_per_cu;
3435 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3437 dwarf2_per_cu_data *per_cu = dw2_get_cutu (cui);
3438 const auto insertpair
3439 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3440 if (!insertpair.second)
3442 warning (_("Section .debug_aranges in %s has duplicate "
3443 "debug_info_offset %u, ignoring .debug_aranges."),
3444 objfile_name (objfile), to_underlying (per_cu->sect_off));
3449 dwarf2_read_section (objfile, section);
3451 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3453 const gdb_byte *addr = section->buffer;
3455 while (addr < section->buffer + section->size)
3457 const gdb_byte *const entry_addr = addr;
3458 unsigned int bytes_read;
3460 const LONGEST entry_length = read_initial_length (abfd, addr,
3464 const gdb_byte *const entry_end = addr + entry_length;
3465 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3466 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3467 if (addr + entry_length > section->buffer + section->size)
3469 warning (_("Section .debug_aranges in %s entry at offset %zu "
3470 "length %s exceeds section length %s, "
3471 "ignoring .debug_aranges."),
3472 objfile_name (objfile), entry_addr - section->buffer,
3473 plongest (bytes_read + entry_length),
3474 pulongest (section->size));
3478 /* The version number. */
3479 const uint16_t version = read_2_bytes (abfd, addr);
3483 warning (_("Section .debug_aranges in %s entry at offset %zu "
3484 "has unsupported version %d, ignoring .debug_aranges."),
3485 objfile_name (objfile), entry_addr - section->buffer,
3490 const uint64_t debug_info_offset
3491 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3492 addr += offset_size;
3493 const auto per_cu_it
3494 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3495 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3497 warning (_("Section .debug_aranges in %s entry at offset %zu "
3498 "debug_info_offset %s does not exists, "
3499 "ignoring .debug_aranges."),
3500 objfile_name (objfile), entry_addr - section->buffer,
3501 pulongest (debug_info_offset));
3504 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3506 const uint8_t address_size = *addr++;
3507 if (address_size < 1 || address_size > 8)
3509 warning (_("Section .debug_aranges in %s entry at offset %zu "
3510 "address_size %u is invalid, ignoring .debug_aranges."),
3511 objfile_name (objfile), entry_addr - section->buffer,
3516 const uint8_t segment_selector_size = *addr++;
3517 if (segment_selector_size != 0)
3519 warning (_("Section .debug_aranges in %s entry at offset %zu "
3520 "segment_selector_size %u is not supported, "
3521 "ignoring .debug_aranges."),
3522 objfile_name (objfile), entry_addr - section->buffer,
3523 segment_selector_size);
3527 /* Must pad to an alignment boundary that is twice the address
3528 size. It is undocumented by the DWARF standard but GCC does
3530 for (size_t padding = ((-(addr - section->buffer))
3531 & (2 * address_size - 1));
3532 padding > 0; padding--)
3535 warning (_("Section .debug_aranges in %s entry at offset %zu "
3536 "padding is not zero, ignoring .debug_aranges."),
3537 objfile_name (objfile), entry_addr - section->buffer);
3543 if (addr + 2 * address_size > entry_end)
3545 warning (_("Section .debug_aranges in %s entry at offset %zu "
3546 "address list is not properly terminated, "
3547 "ignoring .debug_aranges."),
3548 objfile_name (objfile), entry_addr - section->buffer);
3551 ULONGEST start = extract_unsigned_integer (addr, address_size,
3553 addr += address_size;
3554 ULONGEST length = extract_unsigned_integer (addr, address_size,
3556 addr += address_size;
3557 if (start == 0 && length == 0)
3559 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3561 /* Symbol was eliminated due to a COMDAT group. */
3564 ULONGEST end = start + length;
3565 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3566 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3567 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3571 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3572 &objfile->objfile_obstack);
3575 /* The hash function for strings in the mapped index. This is the same as
3576 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3577 implementation. This is necessary because the hash function is tied to the
3578 format of the mapped index file. The hash values do not have to match with
3581 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3584 mapped_index_string_hash (int index_version, const void *p)
3586 const unsigned char *str = (const unsigned char *) p;
3590 while ((c = *str++) != 0)
3592 if (index_version >= 5)
3594 r = r * 67 + c - 113;
3600 /* Find a slot in the mapped index INDEX for the object named NAME.
3601 If NAME is found, set *VEC_OUT to point to the CU vector in the
3602 constant pool and return true. If NAME cannot be found, return
3606 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3607 offset_type **vec_out)
3610 offset_type slot, step;
3611 int (*cmp) (const char *, const char *);
3613 gdb::unique_xmalloc_ptr<char> without_params;
3614 if (current_language->la_language == language_cplus
3615 || current_language->la_language == language_fortran
3616 || current_language->la_language == language_d)
3618 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3621 if (strchr (name, '(') != NULL)
3623 without_params = cp_remove_params (name);
3625 if (without_params != NULL)
3626 name = without_params.get ();
3630 /* Index version 4 did not support case insensitive searches. But the
3631 indices for case insensitive languages are built in lowercase, therefore
3632 simulate our NAME being searched is also lowercased. */
3633 hash = mapped_index_string_hash ((index->version == 4
3634 && case_sensitivity == case_sensitive_off
3635 ? 5 : index->version),
3638 slot = hash & (index->symbol_table.size () - 1);
3639 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3640 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3646 const auto &bucket = index->symbol_table[slot];
3647 if (bucket.name == 0 && bucket.vec == 0)
3650 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3651 if (!cmp (name, str))
3653 *vec_out = (offset_type *) (index->constant_pool
3654 + MAYBE_SWAP (bucket.vec));
3658 slot = (slot + step) & (index->symbol_table.size () - 1);
3662 /* A helper function that reads the .gdb_index from SECTION and fills
3663 in MAP. FILENAME is the name of the file containing the section;
3664 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3665 ok to use deprecated sections.
3667 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3668 out parameters that are filled in with information about the CU and
3669 TU lists in the section.
3671 Returns 1 if all went well, 0 otherwise. */
3674 read_index_from_section (struct objfile *objfile,
3675 const char *filename,
3677 struct dwarf2_section_info *section,
3678 struct mapped_index *map,
3679 const gdb_byte **cu_list,
3680 offset_type *cu_list_elements,
3681 const gdb_byte **types_list,
3682 offset_type *types_list_elements)
3684 const gdb_byte *addr;
3685 offset_type version;
3686 offset_type *metadata;
3689 if (dwarf2_section_empty_p (section))
3692 /* Older elfutils strip versions could keep the section in the main
3693 executable while splitting it for the separate debug info file. */
3694 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3697 dwarf2_read_section (objfile, section);
3699 addr = section->buffer;
3700 /* Version check. */
3701 version = MAYBE_SWAP (*(offset_type *) addr);
3702 /* Versions earlier than 3 emitted every copy of a psymbol. This
3703 causes the index to behave very poorly for certain requests. Version 3
3704 contained incomplete addrmap. So, it seems better to just ignore such
3708 static int warning_printed = 0;
3709 if (!warning_printed)
3711 warning (_("Skipping obsolete .gdb_index section in %s."),
3713 warning_printed = 1;
3717 /* Index version 4 uses a different hash function than index version
3720 Versions earlier than 6 did not emit psymbols for inlined
3721 functions. Using these files will cause GDB not to be able to
3722 set breakpoints on inlined functions by name, so we ignore these
3723 indices unless the user has done
3724 "set use-deprecated-index-sections on". */
3725 if (version < 6 && !deprecated_ok)
3727 static int warning_printed = 0;
3728 if (!warning_printed)
3731 Skipping deprecated .gdb_index section in %s.\n\
3732 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3733 to use the section anyway."),
3735 warning_printed = 1;
3739 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3740 of the TU (for symbols coming from TUs),
3741 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3742 Plus gold-generated indices can have duplicate entries for global symbols,
3743 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3744 These are just performance bugs, and we can't distinguish gdb-generated
3745 indices from gold-generated ones, so issue no warning here. */
3747 /* Indexes with higher version than the one supported by GDB may be no
3748 longer backward compatible. */
3752 map->version = version;
3753 map->total_size = section->size;
3755 metadata = (offset_type *) (addr + sizeof (offset_type));
3758 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3759 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3763 *types_list = addr + MAYBE_SWAP (metadata[i]);
3764 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3765 - MAYBE_SWAP (metadata[i]))
3769 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3770 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3772 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3775 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3776 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3778 = gdb::array_view<mapped_index::symbol_table_slot>
3779 ((mapped_index::symbol_table_slot *) symbol_table,
3780 (mapped_index::symbol_table_slot *) symbol_table_end);
3783 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3788 /* Read .gdb_index. If everything went ok, initialize the "quick"
3789 elements of all the CUs and return 1. Otherwise, return 0. */
3792 dwarf2_read_index (struct objfile *objfile)
3794 struct mapped_index local_map, *map;
3795 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3796 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3797 struct dwz_file *dwz;
3799 if (!read_index_from_section (objfile, objfile_name (objfile),
3800 use_deprecated_index_sections,
3801 &dwarf2_per_objfile->gdb_index, &local_map,
3802 &cu_list, &cu_list_elements,
3803 &types_list, &types_list_elements))
3806 /* Don't use the index if it's empty. */
3807 if (local_map.symbol_table.empty ())
3810 /* If there is a .dwz file, read it so we can get its CU list as
3812 dwz = dwarf2_get_dwz_file ();
3815 struct mapped_index dwz_map;
3816 const gdb_byte *dwz_types_ignore;
3817 offset_type dwz_types_elements_ignore;
3819 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3821 &dwz->gdb_index, &dwz_map,
3822 &dwz_list, &dwz_list_elements,
3824 &dwz_types_elements_ignore))
3826 warning (_("could not read '.gdb_index' section from %s; skipping"),
3827 bfd_get_filename (dwz->dwz_bfd));
3832 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3835 if (types_list_elements)
3837 struct dwarf2_section_info *section;
3839 /* We can only handle a single .debug_types when we have an
3841 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3844 section = VEC_index (dwarf2_section_info_def,
3845 dwarf2_per_objfile->types, 0);
3847 create_signatured_type_table_from_index (objfile, section, types_list,
3848 types_list_elements);
3851 create_addrmap_from_index (objfile, &local_map);
3853 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3854 map = new (map) mapped_index ();
3857 dwarf2_per_objfile->index_table = map;
3858 dwarf2_per_objfile->using_index = 1;
3859 dwarf2_per_objfile->quick_file_names_table =
3860 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3865 /* A helper for the "quick" functions which sets the global
3866 dwarf2_per_objfile according to OBJFILE. */
3869 dw2_setup (struct objfile *objfile)
3871 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3872 objfile_data (objfile, dwarf2_objfile_data_key));
3873 gdb_assert (dwarf2_per_objfile);
3876 /* die_reader_func for dw2_get_file_names. */
3879 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3880 const gdb_byte *info_ptr,
3881 struct die_info *comp_unit_die,
3885 struct dwarf2_cu *cu = reader->cu;
3886 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3887 struct objfile *objfile = dwarf2_per_objfile->objfile;
3888 struct dwarf2_per_cu_data *lh_cu;
3889 struct attribute *attr;
3892 struct quick_file_names *qfn;
3894 gdb_assert (! this_cu->is_debug_types);
3896 /* Our callers never want to match partial units -- instead they
3897 will match the enclosing full CU. */
3898 if (comp_unit_die->tag == DW_TAG_partial_unit)
3900 this_cu->v.quick->no_file_data = 1;
3908 sect_offset line_offset {};
3910 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3913 struct quick_file_names find_entry;
3915 line_offset = (sect_offset) DW_UNSND (attr);
3917 /* We may have already read in this line header (TU line header sharing).
3918 If we have we're done. */
3919 find_entry.hash.dwo_unit = cu->dwo_unit;
3920 find_entry.hash.line_sect_off = line_offset;
3921 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3922 &find_entry, INSERT);
3925 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3929 lh = dwarf_decode_line_header (line_offset, cu);
3933 lh_cu->v.quick->no_file_data = 1;
3937 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3938 qfn->hash.dwo_unit = cu->dwo_unit;
3939 qfn->hash.line_sect_off = line_offset;
3940 gdb_assert (slot != NULL);
3943 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3945 qfn->num_file_names = lh->file_names.size ();
3947 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3948 for (i = 0; i < lh->file_names.size (); ++i)
3949 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3950 qfn->real_names = NULL;
3952 lh_cu->v.quick->file_names = qfn;
3955 /* A helper for the "quick" functions which attempts to read the line
3956 table for THIS_CU. */
3958 static struct quick_file_names *
3959 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3961 /* This should never be called for TUs. */
3962 gdb_assert (! this_cu->is_debug_types);
3963 /* Nor type unit groups. */
3964 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3966 if (this_cu->v.quick->file_names != NULL)
3967 return this_cu->v.quick->file_names;
3968 /* If we know there is no line data, no point in looking again. */
3969 if (this_cu->v.quick->no_file_data)
3972 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3974 if (this_cu->v.quick->no_file_data)
3976 return this_cu->v.quick->file_names;
3979 /* A helper for the "quick" functions which computes and caches the
3980 real path for a given file name from the line table. */
3983 dw2_get_real_path (struct objfile *objfile,
3984 struct quick_file_names *qfn, int index)
3986 if (qfn->real_names == NULL)
3987 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3988 qfn->num_file_names, const char *);
3990 if (qfn->real_names[index] == NULL)
3991 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3993 return qfn->real_names[index];
3996 static struct symtab *
3997 dw2_find_last_source_symtab (struct objfile *objfile)
3999 struct compunit_symtab *cust;
4002 dw2_setup (objfile);
4003 index = dwarf2_per_objfile->n_comp_units - 1;
4004 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
4007 return compunit_primary_filetab (cust);
4010 /* Traversal function for dw2_forget_cached_source_info. */
4013 dw2_free_cached_file_names (void **slot, void *info)
4015 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
4017 if (file_data->real_names)
4021 for (i = 0; i < file_data->num_file_names; ++i)
4023 xfree ((void*) file_data->real_names[i]);
4024 file_data->real_names[i] = NULL;
4032 dw2_forget_cached_source_info (struct objfile *objfile)
4034 dw2_setup (objfile);
4036 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
4037 dw2_free_cached_file_names, NULL);
4040 /* Helper function for dw2_map_symtabs_matching_filename that expands
4041 the symtabs and calls the iterator. */
4044 dw2_map_expand_apply (struct objfile *objfile,
4045 struct dwarf2_per_cu_data *per_cu,
4046 const char *name, const char *real_path,
4047 gdb::function_view<bool (symtab *)> callback)
4049 struct compunit_symtab *last_made = objfile->compunit_symtabs;
4051 /* Don't visit already-expanded CUs. */
4052 if (per_cu->v.quick->compunit_symtab)
4055 /* This may expand more than one symtab, and we want to iterate over
4057 dw2_instantiate_symtab (per_cu);
4059 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
4060 last_made, callback);
4063 /* Implementation of the map_symtabs_matching_filename method. */
4066 dw2_map_symtabs_matching_filename
4067 (struct objfile *objfile, const char *name, const char *real_path,
4068 gdb::function_view<bool (symtab *)> callback)
4071 const char *name_basename = lbasename (name);
4073 dw2_setup (objfile);
4075 /* The rule is CUs specify all the files, including those used by
4076 any TU, so there's no need to scan TUs here. */
4078 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4081 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4082 struct quick_file_names *file_data;
4084 /* We only need to look at symtabs not already expanded. */
4085 if (per_cu->v.quick->compunit_symtab)
4088 file_data = dw2_get_file_names (per_cu);
4089 if (file_data == NULL)
4092 for (j = 0; j < file_data->num_file_names; ++j)
4094 const char *this_name = file_data->file_names[j];
4095 const char *this_real_name;
4097 if (compare_filenames_for_search (this_name, name))
4099 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4105 /* Before we invoke realpath, which can get expensive when many
4106 files are involved, do a quick comparison of the basenames. */
4107 if (! basenames_may_differ
4108 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
4111 this_real_name = dw2_get_real_path (objfile, file_data, j);
4112 if (compare_filenames_for_search (this_real_name, name))
4114 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4120 if (real_path != NULL)
4122 gdb_assert (IS_ABSOLUTE_PATH (real_path));
4123 gdb_assert (IS_ABSOLUTE_PATH (name));
4124 if (this_real_name != NULL
4125 && FILENAME_CMP (real_path, this_real_name) == 0)
4127 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4139 /* Struct used to manage iterating over all CUs looking for a symbol. */
4141 struct dw2_symtab_iterator
4143 /* The internalized form of .gdb_index. */
4144 struct mapped_index *index;
4145 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
4146 int want_specific_block;
4147 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
4148 Unused if !WANT_SPECIFIC_BLOCK. */
4150 /* The kind of symbol we're looking for. */
4152 /* The list of CUs from the index entry of the symbol,
4153 or NULL if not found. */
4155 /* The next element in VEC to look at. */
4157 /* The number of elements in VEC, or zero if there is no match. */
4159 /* Have we seen a global version of the symbol?
4160 If so we can ignore all further global instances.
4161 This is to work around gold/15646, inefficient gold-generated
4166 /* Initialize the index symtab iterator ITER.
4167 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
4168 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
4171 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
4172 struct mapped_index *index,
4173 int want_specific_block,
4178 iter->index = index;
4179 iter->want_specific_block = want_specific_block;
4180 iter->block_index = block_index;
4181 iter->domain = domain;
4183 iter->global_seen = 0;
4185 if (find_slot_in_mapped_hash (index, name, &iter->vec))
4186 iter->length = MAYBE_SWAP (*iter->vec);
4194 /* Return the next matching CU or NULL if there are no more. */
4196 static struct dwarf2_per_cu_data *
4197 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4199 for ( ; iter->next < iter->length; ++iter->next)
4201 offset_type cu_index_and_attrs =
4202 MAYBE_SWAP (iter->vec[iter->next + 1]);
4203 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4204 struct dwarf2_per_cu_data *per_cu;
4205 int want_static = iter->block_index != GLOBAL_BLOCK;
4206 /* This value is only valid for index versions >= 7. */
4207 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4208 gdb_index_symbol_kind symbol_kind =
4209 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4210 /* Only check the symbol attributes if they're present.
4211 Indices prior to version 7 don't record them,
4212 and indices >= 7 may elide them for certain symbols
4213 (gold does this). */
4215 (iter->index->version >= 7
4216 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4218 /* Don't crash on bad data. */
4219 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4220 + dwarf2_per_objfile->n_type_units))
4222 complaint (&symfile_complaints,
4223 _(".gdb_index entry has bad CU index"
4225 objfile_name (dwarf2_per_objfile->objfile));
4229 per_cu = dw2_get_cutu (cu_index);
4231 /* Skip if already read in. */
4232 if (per_cu->v.quick->compunit_symtab)
4235 /* Check static vs global. */
4238 if (iter->want_specific_block
4239 && want_static != is_static)
4241 /* Work around gold/15646. */
4242 if (!is_static && iter->global_seen)
4245 iter->global_seen = 1;
4248 /* Only check the symbol's kind if it has one. */
4251 switch (iter->domain)
4254 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4255 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4256 /* Some types are also in VAR_DOMAIN. */
4257 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4261 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4265 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4280 static struct compunit_symtab *
4281 dw2_lookup_symbol (struct objfile *objfile, int block_index,
4282 const char *name, domain_enum domain)
4284 struct compunit_symtab *stab_best = NULL;
4285 struct mapped_index *index;
4287 dw2_setup (objfile);
4289 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4291 index = dwarf2_per_objfile->index_table;
4293 /* index is NULL if OBJF_READNOW. */
4296 struct dw2_symtab_iterator iter;
4297 struct dwarf2_per_cu_data *per_cu;
4299 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
4301 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4303 struct symbol *sym, *with_opaque = NULL;
4304 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4305 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4306 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4308 sym = block_find_symbol (block, name, domain,
4309 block_find_non_opaque_type_preferred,
4312 /* Some caution must be observed with overloaded functions
4313 and methods, since the index will not contain any overload
4314 information (but NAME might contain it). */
4317 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4319 if (with_opaque != NULL
4320 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4323 /* Keep looking through other CUs. */
4331 dw2_print_stats (struct objfile *objfile)
4333 int i, total, count;
4335 dw2_setup (objfile);
4336 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4338 for (i = 0; i < total; ++i)
4340 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4342 if (!per_cu->v.quick->compunit_symtab)
4345 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4346 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4349 /* This dumps minimal information about the index.
4350 It is called via "mt print objfiles".
4351 One use is to verify .gdb_index has been loaded by the
4352 gdb.dwarf2/gdb-index.exp testcase. */
4355 dw2_dump (struct objfile *objfile)
4357 dw2_setup (objfile);
4358 gdb_assert (dwarf2_per_objfile->using_index);
4359 printf_filtered (".gdb_index:");
4360 if (dwarf2_per_objfile->index_table != NULL)
4362 printf_filtered (" version %d\n",
4363 dwarf2_per_objfile->index_table->version);
4366 printf_filtered (" faked for \"readnow\"\n");
4367 printf_filtered ("\n");
4371 dw2_relocate (struct objfile *objfile,
4372 const struct section_offsets *new_offsets,
4373 const struct section_offsets *delta)
4375 /* There's nothing to relocate here. */
4379 dw2_expand_symtabs_for_function (struct objfile *objfile,
4380 const char *func_name)
4382 struct mapped_index *index;
4384 dw2_setup (objfile);
4386 index = dwarf2_per_objfile->index_table;
4388 /* index is NULL if OBJF_READNOW. */
4391 struct dw2_symtab_iterator iter;
4392 struct dwarf2_per_cu_data *per_cu;
4394 /* Note: It doesn't matter what we pass for block_index here. */
4395 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4398 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4399 dw2_instantiate_symtab (per_cu);
4404 dw2_expand_all_symtabs (struct objfile *objfile)
4408 dw2_setup (objfile);
4410 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
4411 + dwarf2_per_objfile->n_type_units); ++i)
4413 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4415 dw2_instantiate_symtab (per_cu);
4420 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4421 const char *fullname)
4425 dw2_setup (objfile);
4427 /* We don't need to consider type units here.
4428 This is only called for examining code, e.g. expand_line_sal.
4429 There can be an order of magnitude (or more) more type units
4430 than comp units, and we avoid them if we can. */
4432 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4435 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4436 struct quick_file_names *file_data;
4438 /* We only need to look at symtabs not already expanded. */
4439 if (per_cu->v.quick->compunit_symtab)
4442 file_data = dw2_get_file_names (per_cu);
4443 if (file_data == NULL)
4446 for (j = 0; j < file_data->num_file_names; ++j)
4448 const char *this_fullname = file_data->file_names[j];
4450 if (filename_cmp (this_fullname, fullname) == 0)
4452 dw2_instantiate_symtab (per_cu);
4460 dw2_map_matching_symbols (struct objfile *objfile,
4461 const char * name, domain_enum domain,
4463 int (*callback) (struct block *,
4464 struct symbol *, void *),
4465 void *data, symbol_name_match_type match,
4466 symbol_compare_ftype *ordered_compare)
4468 /* Currently unimplemented; used for Ada. The function can be called if the
4469 current language is Ada for a non-Ada objfile using GNU index. As Ada
4470 does not look for non-Ada symbols this function should just return. */
4473 /* Symbol name matcher for .gdb_index names.
4475 Symbol names in .gdb_index have a few particularities:
4477 - There's no indication of which is the language of each symbol.
4479 Since each language has its own symbol name matching algorithm,
4480 and we don't know which language is the right one, we must match
4481 each symbol against all languages. This would be a potential
4482 performance problem if it were not mitigated by the
4483 mapped_index::name_components lookup table, which significantly
4484 reduces the number of times we need to call into this matcher,
4485 making it a non-issue.
4487 - Symbol names in the index have no overload (parameter)
4488 information. I.e., in C++, "foo(int)" and "foo(long)" both
4489 appear as "foo" in the index, for example.
4491 This means that the lookup names passed to the symbol name
4492 matcher functions must have no parameter information either
4493 because (e.g.) symbol search name "foo" does not match
4494 lookup-name "foo(int)" [while swapping search name for lookup
4497 class gdb_index_symbol_name_matcher
4500 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4501 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4503 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4504 Returns true if any matcher matches. */
4505 bool matches (const char *symbol_name);
4508 /* A reference to the lookup name we're matching against. */
4509 const lookup_name_info &m_lookup_name;
4511 /* A vector holding all the different symbol name matchers, for all
4513 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4516 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4517 (const lookup_name_info &lookup_name)
4518 : m_lookup_name (lookup_name)
4520 /* Prepare the vector of comparison functions upfront, to avoid
4521 doing the same work for each symbol. Care is taken to avoid
4522 matching with the same matcher more than once if/when multiple
4523 languages use the same matcher function. */
4524 auto &matchers = m_symbol_name_matcher_funcs;
4525 matchers.reserve (nr_languages);
4527 matchers.push_back (default_symbol_name_matcher);
4529 for (int i = 0; i < nr_languages; i++)
4531 const language_defn *lang = language_def ((enum language) i);
4532 if (lang->la_get_symbol_name_matcher != NULL)
4534 symbol_name_matcher_ftype *name_matcher
4535 = lang->la_get_symbol_name_matcher (m_lookup_name);
4537 /* Don't insert the same comparison routine more than once.
4538 Note that we do this linear walk instead of a cheaper
4539 sorted insert, or use a std::set or something like that,
4540 because relative order of function addresses is not
4541 stable. This is not a problem in practice because the
4542 number of supported languages is low, and the cost here
4543 is tiny compared to the number of searches we'll do
4544 afterwards using this object. */
4545 if (std::find (matchers.begin (), matchers.end (), name_matcher)
4547 matchers.push_back (name_matcher);
4553 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4555 for (auto matches_name : m_symbol_name_matcher_funcs)
4556 if (matches_name (symbol_name, m_lookup_name, NULL))
4562 /* Starting from a search name, return the string that finds the upper
4563 bound of all strings that start with SEARCH_NAME in a sorted name
4564 list. Returns the empty string to indicate that the upper bound is
4565 the end of the list. */
4568 make_sort_after_prefix_name (const char *search_name)
4570 /* When looking to complete "func", we find the upper bound of all
4571 symbols that start with "func" by looking for where we'd insert
4572 the closest string that would follow "func" in lexicographical
4573 order. Usually, that's "func"-with-last-character-incremented,
4574 i.e. "fund". Mind non-ASCII characters, though. Usually those
4575 will be UTF-8 multi-byte sequences, but we can't be certain.
4576 Especially mind the 0xff character, which is a valid character in
4577 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4578 rule out compilers allowing it in identifiers. Note that
4579 conveniently, strcmp/strcasecmp are specified to compare
4580 characters interpreted as unsigned char. So what we do is treat
4581 the whole string as a base 256 number composed of a sequence of
4582 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4583 to 0, and carries 1 to the following more-significant position.
4584 If the very first character in SEARCH_NAME ends up incremented
4585 and carries/overflows, then the upper bound is the end of the
4586 list. The string after the empty string is also the empty
4589 Some examples of this operation:
4591 SEARCH_NAME => "+1" RESULT
4595 "\xff" "a" "\xff" => "\xff" "b"
4600 Then, with these symbols for example:
4606 completing "func" looks for symbols between "func" and
4607 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4608 which finds "func" and "func1", but not "fund".
4612 funcÿ (Latin1 'ÿ' [0xff])
4616 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4617 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4621 ÿÿ (Latin1 'ÿ' [0xff])
4624 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4625 the end of the list.
4627 std::string after = search_name;
4628 while (!after.empty () && (unsigned char) after.back () == 0xff)
4630 if (!after.empty ())
4631 after.back () = (unsigned char) after.back () + 1;
4635 /* See declaration. */
4637 std::pair<std::vector<name_component>::const_iterator,
4638 std::vector<name_component>::const_iterator>
4639 mapped_index_base::find_name_components_bounds
4640 (const lookup_name_info &lookup_name_without_params) const
4643 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4646 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4648 /* Comparison function object for lower_bound that matches against a
4649 given symbol name. */
4650 auto lookup_compare_lower = [&] (const name_component &elem,
4653 const char *elem_qualified = this->symbol_name_at (elem.idx);
4654 const char *elem_name = elem_qualified + elem.name_offset;
4655 return name_cmp (elem_name, name) < 0;
4658 /* Comparison function object for upper_bound that matches against a
4659 given symbol name. */
4660 auto lookup_compare_upper = [&] (const char *name,
4661 const name_component &elem)
4663 const char *elem_qualified = this->symbol_name_at (elem.idx);
4664 const char *elem_name = elem_qualified + elem.name_offset;
4665 return name_cmp (name, elem_name) < 0;
4668 auto begin = this->name_components.begin ();
4669 auto end = this->name_components.end ();
4671 /* Find the lower bound. */
4674 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4677 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4680 /* Find the upper bound. */
4683 if (lookup_name_without_params.completion_mode ())
4685 /* In completion mode, we want UPPER to point past all
4686 symbols names that have the same prefix. I.e., with
4687 these symbols, and completing "func":
4689 function << lower bound
4691 other_function << upper bound
4693 We find the upper bound by looking for the insertion
4694 point of "func"-with-last-character-incremented,
4696 std::string after = make_sort_after_prefix_name (cplus);
4699 return std::lower_bound (lower, end, after.c_str (),
4700 lookup_compare_lower);
4703 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4706 return {lower, upper};
4709 /* See declaration. */
4712 mapped_index_base::build_name_components ()
4714 if (!this->name_components.empty ())
4717 this->name_components_casing = case_sensitivity;
4719 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4721 /* The code below only knows how to break apart components of C++
4722 symbol names (and other languages that use '::' as
4723 namespace/module separator). If we add support for wild matching
4724 to some language that uses some other operator (E.g., Ada, Go and
4725 D use '.'), then we'll need to try splitting the symbol name
4726 according to that language too. Note that Ada does support wild
4727 matching, but doesn't currently support .gdb_index. */
4728 auto count = this->symbol_name_count ();
4729 for (offset_type idx = 0; idx < count; idx++)
4731 if (this->symbol_name_slot_invalid (idx))
4734 const char *name = this->symbol_name_at (idx);
4736 /* Add each name component to the name component table. */
4737 unsigned int previous_len = 0;
4738 for (unsigned int current_len = cp_find_first_component (name);
4739 name[current_len] != '\0';
4740 current_len += cp_find_first_component (name + current_len))
4742 gdb_assert (name[current_len] == ':');
4743 this->name_components.push_back ({previous_len, idx});
4744 /* Skip the '::'. */
4746 previous_len = current_len;
4748 this->name_components.push_back ({previous_len, idx});
4751 /* Sort name_components elements by name. */
4752 auto name_comp_compare = [&] (const name_component &left,
4753 const name_component &right)
4755 const char *left_qualified = this->symbol_name_at (left.idx);
4756 const char *right_qualified = this->symbol_name_at (right.idx);
4758 const char *left_name = left_qualified + left.name_offset;
4759 const char *right_name = right_qualified + right.name_offset;
4761 return name_cmp (left_name, right_name) < 0;
4764 std::sort (this->name_components.begin (),
4765 this->name_components.end (),
4769 /* Helper for dw2_expand_symtabs_matching that works with a
4770 mapped_index_base instead of the containing objfile. This is split
4771 to a separate function in order to be able to unit test the
4772 name_components matching using a mock mapped_index_base. For each
4773 symbol name that matches, calls MATCH_CALLBACK, passing it the
4774 symbol's index in the mapped_index_base symbol table. */
4777 dw2_expand_symtabs_matching_symbol
4778 (mapped_index_base &index,
4779 const lookup_name_info &lookup_name_in,
4780 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4781 enum search_domain kind,
4782 gdb::function_view<void (offset_type)> match_callback)
4784 lookup_name_info lookup_name_without_params
4785 = lookup_name_in.make_ignore_params ();
4786 gdb_index_symbol_name_matcher lookup_name_matcher
4787 (lookup_name_without_params);
4789 /* Build the symbol name component sorted vector, if we haven't
4791 index.build_name_components ();
4793 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4795 /* Now for each symbol name in range, check to see if we have a name
4796 match, and if so, call the MATCH_CALLBACK callback. */
4798 /* The same symbol may appear more than once in the range though.
4799 E.g., if we're looking for symbols that complete "w", and we have
4800 a symbol named "w1::w2", we'll find the two name components for
4801 that same symbol in the range. To be sure we only call the
4802 callback once per symbol, we first collect the symbol name
4803 indexes that matched in a temporary vector and ignore
4805 std::vector<offset_type> matches;
4806 matches.reserve (std::distance (bounds.first, bounds.second));
4808 for (; bounds.first != bounds.second; ++bounds.first)
4810 const char *qualified = index.symbol_name_at (bounds.first->idx);
4812 if (!lookup_name_matcher.matches (qualified)
4813 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4816 matches.push_back (bounds.first->idx);
4819 std::sort (matches.begin (), matches.end ());
4821 /* Finally call the callback, once per match. */
4823 for (offset_type idx : matches)
4827 match_callback (idx);
4832 /* Above we use a type wider than idx's for 'prev', since 0 and
4833 (offset_type)-1 are both possible values. */
4834 static_assert (sizeof (prev) > sizeof (offset_type), "");
4839 namespace selftests { namespace dw2_expand_symtabs_matching {
4841 /* A mock .gdb_index/.debug_names-like name index table, enough to
4842 exercise dw2_expand_symtabs_matching_symbol, which works with the
4843 mapped_index_base interface. Builds an index from the symbol list
4844 passed as parameter to the constructor. */
4845 class mock_mapped_index : public mapped_index_base
4848 mock_mapped_index (gdb::array_view<const char *> symbols)
4849 : m_symbol_table (symbols)
4852 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4854 /* Return the number of names in the symbol table. */
4855 virtual size_t symbol_name_count () const
4857 return m_symbol_table.size ();
4860 /* Get the name of the symbol at IDX in the symbol table. */
4861 virtual const char *symbol_name_at (offset_type idx) const
4863 return m_symbol_table[idx];
4867 gdb::array_view<const char *> m_symbol_table;
4870 /* Convenience function that converts a NULL pointer to a "<null>"
4871 string, to pass to print routines. */
4874 string_or_null (const char *str)
4876 return str != NULL ? str : "<null>";
4879 /* Check if a lookup_name_info built from
4880 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4881 index. EXPECTED_LIST is the list of expected matches, in expected
4882 matching order. If no match expected, then an empty list is
4883 specified. Returns true on success. On failure prints a warning
4884 indicating the file:line that failed, and returns false. */
4887 check_match (const char *file, int line,
4888 mock_mapped_index &mock_index,
4889 const char *name, symbol_name_match_type match_type,
4890 bool completion_mode,
4891 std::initializer_list<const char *> expected_list)
4893 lookup_name_info lookup_name (name, match_type, completion_mode);
4895 bool matched = true;
4897 auto mismatch = [&] (const char *expected_str,
4900 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4901 "expected=\"%s\", got=\"%s\"\n"),
4903 (match_type == symbol_name_match_type::FULL
4905 name, string_or_null (expected_str), string_or_null (got));
4909 auto expected_it = expected_list.begin ();
4910 auto expected_end = expected_list.end ();
4912 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4914 [&] (offset_type idx)
4916 const char *matched_name = mock_index.symbol_name_at (idx);
4917 const char *expected_str
4918 = expected_it == expected_end ? NULL : *expected_it++;
4920 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4921 mismatch (expected_str, matched_name);
4924 const char *expected_str
4925 = expected_it == expected_end ? NULL : *expected_it++;
4926 if (expected_str != NULL)
4927 mismatch (expected_str, NULL);
4932 /* The symbols added to the mock mapped_index for testing (in
4934 static const char *test_symbols[] = {
4943 "ns2::tmpl<int>::foo2",
4944 "(anonymous namespace)::A::B::C",
4946 /* These are used to check that the increment-last-char in the
4947 matching algorithm for completion doesn't match "t1_fund" when
4948 completing "t1_func". */
4954 /* A UTF-8 name with multi-byte sequences to make sure that
4955 cp-name-parser understands this as a single identifier ("função"
4956 is "function" in PT). */
4959 /* \377 (0xff) is Latin1 'ÿ'. */
4962 /* \377 (0xff) is Latin1 'ÿ'. */
4966 /* A name with all sorts of complications. Starts with "z" to make
4967 it easier for the completion tests below. */
4968 #define Z_SYM_NAME \
4969 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4970 "::tuple<(anonymous namespace)::ui*, " \
4971 "std::default_delete<(anonymous namespace)::ui>, void>"
4976 /* Returns true if the mapped_index_base::find_name_component_bounds
4977 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4978 in completion mode. */
4981 check_find_bounds_finds (mapped_index_base &index,
4982 const char *search_name,
4983 gdb::array_view<const char *> expected_syms)
4985 lookup_name_info lookup_name (search_name,
4986 symbol_name_match_type::FULL, true);
4988 auto bounds = index.find_name_components_bounds (lookup_name);
4990 size_t distance = std::distance (bounds.first, bounds.second);
4991 if (distance != expected_syms.size ())
4994 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4996 auto nc_elem = bounds.first + exp_elem;
4997 const char *qualified = index.symbol_name_at (nc_elem->idx);
4998 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
5005 /* Test the lower-level mapped_index::find_name_component_bounds
5009 test_mapped_index_find_name_component_bounds ()
5011 mock_mapped_index mock_index (test_symbols);
5013 mock_index.build_name_components ();
5015 /* Test the lower-level mapped_index::find_name_component_bounds
5016 method in completion mode. */
5018 static const char *expected_syms[] = {
5023 SELF_CHECK (check_find_bounds_finds (mock_index,
5024 "t1_func", expected_syms));
5027 /* Check that the increment-last-char in the name matching algorithm
5028 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
5030 static const char *expected_syms1[] = {
5034 SELF_CHECK (check_find_bounds_finds (mock_index,
5035 "\377", expected_syms1));
5037 static const char *expected_syms2[] = {
5040 SELF_CHECK (check_find_bounds_finds (mock_index,
5041 "\377\377", expected_syms2));
5045 /* Test dw2_expand_symtabs_matching_symbol. */
5048 test_dw2_expand_symtabs_matching_symbol ()
5050 mock_mapped_index mock_index (test_symbols);
5052 /* We let all tests run until the end even if some fails, for debug
5054 bool any_mismatch = false;
5056 /* Create the expected symbols list (an initializer_list). Needed
5057 because lists have commas, and we need to pass them to CHECK,
5058 which is a macro. */
5059 #define EXPECT(...) { __VA_ARGS__ }
5061 /* Wrapper for check_match that passes down the current
5062 __FILE__/__LINE__. */
5063 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
5064 any_mismatch |= !check_match (__FILE__, __LINE__, \
5066 NAME, MATCH_TYPE, COMPLETION_MODE, \
5069 /* Identity checks. */
5070 for (const char *sym : test_symbols)
5072 /* Should be able to match all existing symbols. */
5073 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
5076 /* Should be able to match all existing symbols with
5078 std::string with_params = std::string (sym) + "(int)";
5079 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5082 /* Should be able to match all existing symbols with
5083 parameters and qualifiers. */
5084 with_params = std::string (sym) + " ( int ) const";
5085 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5088 /* This should really find sym, but cp-name-parser.y doesn't
5089 know about lvalue/rvalue qualifiers yet. */
5090 with_params = std::string (sym) + " ( int ) &&";
5091 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5095 /* Check that the name matching algorithm for completion doesn't get
5096 confused with Latin1 'ÿ' / 0xff. */
5098 static const char str[] = "\377";
5099 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5100 EXPECT ("\377", "\377\377123"));
5103 /* Check that the increment-last-char in the matching algorithm for
5104 completion doesn't match "t1_fund" when completing "t1_func". */
5106 static const char str[] = "t1_func";
5107 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5108 EXPECT ("t1_func", "t1_func1"));
5111 /* Check that completion mode works at each prefix of the expected
5114 static const char str[] = "function(int)";
5115 size_t len = strlen (str);
5118 for (size_t i = 1; i < len; i++)
5120 lookup.assign (str, i);
5121 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5122 EXPECT ("function"));
5126 /* While "w" is a prefix of both components, the match function
5127 should still only be called once. */
5129 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
5131 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
5135 /* Same, with a "complicated" symbol. */
5137 static const char str[] = Z_SYM_NAME;
5138 size_t len = strlen (str);
5141 for (size_t i = 1; i < len; i++)
5143 lookup.assign (str, i);
5144 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5145 EXPECT (Z_SYM_NAME));
5149 /* In FULL mode, an incomplete symbol doesn't match. */
5151 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
5155 /* A complete symbol with parameters matches any overload, since the
5156 index has no overload info. */
5158 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
5159 EXPECT ("std::zfunction", "std::zfunction2"));
5160 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
5161 EXPECT ("std::zfunction", "std::zfunction2"));
5162 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
5163 EXPECT ("std::zfunction", "std::zfunction2"));
5166 /* Check that whitespace is ignored appropriately. A symbol with a
5167 template argument list. */
5169 static const char expected[] = "ns::foo<int>";
5170 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
5172 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
5176 /* Check that whitespace is ignored appropriately. A symbol with a
5177 template argument list that includes a pointer. */
5179 static const char expected[] = "ns::foo<char*>";
5180 /* Try both completion and non-completion modes. */
5181 static const bool completion_mode[2] = {false, true};
5182 for (size_t i = 0; i < 2; i++)
5184 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
5185 completion_mode[i], EXPECT (expected));
5186 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
5187 completion_mode[i], EXPECT (expected));
5189 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
5190 completion_mode[i], EXPECT (expected));
5191 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
5192 completion_mode[i], EXPECT (expected));
5197 /* Check method qualifiers are ignored. */
5198 static const char expected[] = "ns::foo<char*>";
5199 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
5200 symbol_name_match_type::FULL, true, EXPECT (expected));
5201 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
5202 symbol_name_match_type::FULL, true, EXPECT (expected));
5203 CHECK_MATCH ("foo < char * > ( int ) const",
5204 symbol_name_match_type::WILD, true, EXPECT (expected));
5205 CHECK_MATCH ("foo < char * > ( int ) &&",
5206 symbol_name_match_type::WILD, true, EXPECT (expected));
5209 /* Test lookup names that don't match anything. */
5211 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
5214 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
5218 /* Some wild matching tests, exercising "(anonymous namespace)",
5219 which should not be confused with a parameter list. */
5221 static const char *syms[] = {
5225 "A :: B :: C ( int )",
5230 for (const char *s : syms)
5232 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
5233 EXPECT ("(anonymous namespace)::A::B::C"));
5238 static const char expected[] = "ns2::tmpl<int>::foo2";
5239 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5241 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5245 SELF_CHECK (!any_mismatch);
5254 test_mapped_index_find_name_component_bounds ();
5255 test_dw2_expand_symtabs_matching_symbol ();
5258 }} // namespace selftests::dw2_expand_symtabs_matching
5260 #endif /* GDB_SELF_TEST */
5262 /* If FILE_MATCHER is NULL or if PER_CU has
5263 dwarf2_per_cu_quick_data::MARK set (see
5264 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5265 EXPANSION_NOTIFY on it. */
5268 dw2_expand_symtabs_matching_one
5269 (struct dwarf2_per_cu_data *per_cu,
5270 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5271 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5273 if (file_matcher == NULL || per_cu->v.quick->mark)
5275 bool symtab_was_null
5276 = (per_cu->v.quick->compunit_symtab == NULL);
5278 dw2_instantiate_symtab (per_cu);
5280 if (expansion_notify != NULL
5282 && per_cu->v.quick->compunit_symtab != NULL)
5283 expansion_notify (per_cu->v.quick->compunit_symtab);
5287 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5288 matched, to expand corresponding CUs that were marked. IDX is the
5289 index of the symbol name that matched. */
5292 dw2_expand_marked_cus
5293 (mapped_index &index, offset_type idx,
5294 struct objfile *objfile,
5295 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5296 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5299 offset_type *vec, vec_len, vec_idx;
5300 bool global_seen = false;
5302 vec = (offset_type *) (index.constant_pool
5303 + MAYBE_SWAP (index.symbol_table[idx].vec));
5304 vec_len = MAYBE_SWAP (vec[0]);
5305 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5307 struct dwarf2_per_cu_data *per_cu;
5308 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5309 /* This value is only valid for index versions >= 7. */
5310 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5311 gdb_index_symbol_kind symbol_kind =
5312 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5313 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5314 /* Only check the symbol attributes if they're present.
5315 Indices prior to version 7 don't record them,
5316 and indices >= 7 may elide them for certain symbols
5317 (gold does this). */
5320 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5322 /* Work around gold/15646. */
5325 if (!is_static && global_seen)
5331 /* Only check the symbol's kind if it has one. */
5336 case VARIABLES_DOMAIN:
5337 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5340 case FUNCTIONS_DOMAIN:
5341 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5345 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5353 /* Don't crash on bad data. */
5354 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5355 + dwarf2_per_objfile->n_type_units))
5357 complaint (&symfile_complaints,
5358 _(".gdb_index entry has bad CU index"
5359 " [in module %s]"), objfile_name (objfile));
5363 per_cu = dw2_get_cutu (cu_index);
5364 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5369 /* If FILE_MATCHER is non-NULL, set all the
5370 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5371 that match FILE_MATCHER. */
5374 dw_expand_symtabs_matching_file_matcher
5375 (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5377 if (file_matcher == NULL)
5380 objfile *const objfile = dwarf2_per_objfile->objfile;
5382 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5384 NULL, xcalloc, xfree));
5385 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5387 NULL, xcalloc, xfree));
5389 /* The rule is CUs specify all the files, including those used by
5390 any TU, so there's no need to scan TUs here. */
5392 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5395 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5396 struct quick_file_names *file_data;
5401 per_cu->v.quick->mark = 0;
5403 /* We only need to look at symtabs not already expanded. */
5404 if (per_cu->v.quick->compunit_symtab)
5407 file_data = dw2_get_file_names (per_cu);
5408 if (file_data == NULL)
5411 if (htab_find (visited_not_found.get (), file_data) != NULL)
5413 else if (htab_find (visited_found.get (), file_data) != NULL)
5415 per_cu->v.quick->mark = 1;
5419 for (j = 0; j < file_data->num_file_names; ++j)
5421 const char *this_real_name;
5423 if (file_matcher (file_data->file_names[j], false))
5425 per_cu->v.quick->mark = 1;
5429 /* Before we invoke realpath, which can get expensive when many
5430 files are involved, do a quick comparison of the basenames. */
5431 if (!basenames_may_differ
5432 && !file_matcher (lbasename (file_data->file_names[j]),
5436 this_real_name = dw2_get_real_path (objfile, file_data, j);
5437 if (file_matcher (this_real_name, false))
5439 per_cu->v.quick->mark = 1;
5444 slot = htab_find_slot (per_cu->v.quick->mark
5445 ? visited_found.get ()
5446 : visited_not_found.get (),
5453 dw2_expand_symtabs_matching
5454 (struct objfile *objfile,
5455 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5456 const lookup_name_info &lookup_name,
5457 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5458 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5459 enum search_domain kind)
5461 dw2_setup (objfile);
5463 /* index_table is NULL if OBJF_READNOW. */
5464 if (!dwarf2_per_objfile->index_table)
5467 dw_expand_symtabs_matching_file_matcher (file_matcher);
5469 mapped_index &index = *dwarf2_per_objfile->index_table;
5471 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5473 kind, [&] (offset_type idx)
5475 dw2_expand_marked_cus (index, idx, objfile, file_matcher,
5476 expansion_notify, kind);
5480 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5483 static struct compunit_symtab *
5484 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5489 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5490 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5493 if (cust->includes == NULL)
5496 for (i = 0; cust->includes[i]; ++i)
5498 struct compunit_symtab *s = cust->includes[i];
5500 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5508 static struct compunit_symtab *
5509 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5510 struct bound_minimal_symbol msymbol,
5512 struct obj_section *section,
5515 struct dwarf2_per_cu_data *data;
5516 struct compunit_symtab *result;
5518 dw2_setup (objfile);
5520 if (!objfile->psymtabs_addrmap)
5523 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5528 if (warn_if_readin && data->v.quick->compunit_symtab)
5529 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5530 paddress (get_objfile_arch (objfile), pc));
5533 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5535 gdb_assert (result != NULL);
5540 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5541 void *data, int need_fullname)
5543 dw2_setup (objfile);
5545 if (!dwarf2_per_objfile->filenames_cache)
5547 dwarf2_per_objfile->filenames_cache.emplace ();
5549 htab_up visited (htab_create_alloc (10,
5550 htab_hash_pointer, htab_eq_pointer,
5551 NULL, xcalloc, xfree));
5553 /* The rule is CUs specify all the files, including those used
5554 by any TU, so there's no need to scan TUs here. We can
5555 ignore file names coming from already-expanded CUs. */
5557 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5559 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
5561 if (per_cu->v.quick->compunit_symtab)
5563 void **slot = htab_find_slot (visited.get (),
5564 per_cu->v.quick->file_names,
5567 *slot = per_cu->v.quick->file_names;
5571 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5573 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5574 struct quick_file_names *file_data;
5577 /* We only need to look at symtabs not already expanded. */
5578 if (per_cu->v.quick->compunit_symtab)
5581 file_data = dw2_get_file_names (per_cu);
5582 if (file_data == NULL)
5585 slot = htab_find_slot (visited.get (), file_data, INSERT);
5588 /* Already visited. */
5593 for (int j = 0; j < file_data->num_file_names; ++j)
5595 const char *filename = file_data->file_names[j];
5596 dwarf2_per_objfile->filenames_cache->seen (filename);
5601 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5603 gdb::unique_xmalloc_ptr<char> this_real_name;
5606 this_real_name = gdb_realpath (filename);
5607 (*fun) (filename, this_real_name.get (), data);
5612 dw2_has_symbols (struct objfile *objfile)
5617 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5620 dw2_find_last_source_symtab,
5621 dw2_forget_cached_source_info,
5622 dw2_map_symtabs_matching_filename,
5627 dw2_expand_symtabs_for_function,
5628 dw2_expand_all_symtabs,
5629 dw2_expand_symtabs_with_fullname,
5630 dw2_map_matching_symbols,
5631 dw2_expand_symtabs_matching,
5632 dw2_find_pc_sect_compunit_symtab,
5634 dw2_map_symbol_filenames
5637 /* DWARF-5 debug_names reader. */
5639 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5640 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5642 /* A helper function that reads the .debug_names section in SECTION
5643 and fills in MAP. FILENAME is the name of the file containing the
5644 section; it is used for error reporting.
5646 Returns true if all went well, false otherwise. */
5649 read_debug_names_from_section (struct objfile *objfile,
5650 const char *filename,
5651 struct dwarf2_section_info *section,
5652 mapped_debug_names &map)
5654 if (dwarf2_section_empty_p (section))
5657 /* Older elfutils strip versions could keep the section in the main
5658 executable while splitting it for the separate debug info file. */
5659 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5662 dwarf2_read_section (objfile, section);
5664 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5666 const gdb_byte *addr = section->buffer;
5668 bfd *const abfd = get_section_bfd_owner (section);
5670 unsigned int bytes_read;
5671 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5674 map.dwarf5_is_dwarf64 = bytes_read != 4;
5675 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5676 if (bytes_read + length != section->size)
5678 /* There may be multiple per-CU indices. */
5679 warning (_("Section .debug_names in %s length %s does not match "
5680 "section length %s, ignoring .debug_names."),
5681 filename, plongest (bytes_read + length),
5682 pulongest (section->size));
5686 /* The version number. */
5687 uint16_t version = read_2_bytes (abfd, addr);
5691 warning (_("Section .debug_names in %s has unsupported version %d, "
5692 "ignoring .debug_names."),
5698 uint16_t padding = read_2_bytes (abfd, addr);
5702 warning (_("Section .debug_names in %s has unsupported padding %d, "
5703 "ignoring .debug_names."),
5708 /* comp_unit_count - The number of CUs in the CU list. */
5709 map.cu_count = read_4_bytes (abfd, addr);
5712 /* local_type_unit_count - The number of TUs in the local TU
5714 map.tu_count = read_4_bytes (abfd, addr);
5717 /* foreign_type_unit_count - The number of TUs in the foreign TU
5719 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5721 if (foreign_tu_count != 0)
5723 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5724 "ignoring .debug_names."),
5725 filename, static_cast<unsigned long> (foreign_tu_count));
5729 /* bucket_count - The number of hash buckets in the hash lookup
5731 map.bucket_count = read_4_bytes (abfd, addr);
5734 /* name_count - The number of unique names in the index. */
5735 map.name_count = read_4_bytes (abfd, addr);
5738 /* abbrev_table_size - The size in bytes of the abbreviations
5740 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5743 /* augmentation_string_size - The size in bytes of the augmentation
5744 string. This value is rounded up to a multiple of 4. */
5745 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5747 map.augmentation_is_gdb = ((augmentation_string_size
5748 == sizeof (dwarf5_augmentation))
5749 && memcmp (addr, dwarf5_augmentation,
5750 sizeof (dwarf5_augmentation)) == 0);
5751 augmentation_string_size += (-augmentation_string_size) & 3;
5752 addr += augmentation_string_size;
5755 map.cu_table_reordered = addr;
5756 addr += map.cu_count * map.offset_size;
5758 /* List of Local TUs */
5759 map.tu_table_reordered = addr;
5760 addr += map.tu_count * map.offset_size;
5762 /* Hash Lookup Table */
5763 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5764 addr += map.bucket_count * 4;
5765 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5766 addr += map.name_count * 4;
5769 map.name_table_string_offs_reordered = addr;
5770 addr += map.name_count * map.offset_size;
5771 map.name_table_entry_offs_reordered = addr;
5772 addr += map.name_count * map.offset_size;
5774 const gdb_byte *abbrev_table_start = addr;
5777 unsigned int bytes_read;
5778 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5783 const auto insertpair
5784 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5785 if (!insertpair.second)
5787 warning (_("Section .debug_names in %s has duplicate index %s, "
5788 "ignoring .debug_names."),
5789 filename, pulongest (index_num));
5792 mapped_debug_names::index_val &indexval = insertpair.first->second;
5793 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5798 mapped_debug_names::index_val::attr attr;
5799 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5801 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5803 if (attr.form == DW_FORM_implicit_const)
5805 attr.implicit_const = read_signed_leb128 (abfd, addr,
5809 if (attr.dw_idx == 0 && attr.form == 0)
5811 indexval.attr_vec.push_back (std::move (attr));
5814 if (addr != abbrev_table_start + abbrev_table_size)
5816 warning (_("Section .debug_names in %s has abbreviation_table "
5817 "of size %zu vs. written as %u, ignoring .debug_names."),
5818 filename, addr - abbrev_table_start, abbrev_table_size);
5821 map.entry_pool = addr;
5826 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5830 create_cus_from_debug_names_list (struct objfile *objfile,
5831 const mapped_debug_names &map,
5832 dwarf2_section_info §ion,
5833 bool is_dwz, int base_offset)
5835 sect_offset sect_off_prev;
5836 for (uint32_t i = 0; i <= map.cu_count; ++i)
5838 sect_offset sect_off_next;
5839 if (i < map.cu_count)
5842 = (sect_offset) (extract_unsigned_integer
5843 (map.cu_table_reordered + i * map.offset_size,
5845 map.dwarf5_byte_order));
5848 sect_off_next = (sect_offset) section.size;
5851 const ULONGEST length = sect_off_next - sect_off_prev;
5852 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
5853 = create_cu_from_index_list (objfile, §ion, is_dwz,
5854 sect_off_prev, length);
5856 sect_off_prev = sect_off_next;
5860 /* Read the CU list from the mapped index, and use it to create all
5861 the CU objects for this objfile. */
5864 create_cus_from_debug_names (struct objfile *objfile,
5865 const mapped_debug_names &map,
5866 const mapped_debug_names &dwz_map)
5869 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
5870 dwarf2_per_objfile->all_comp_units
5871 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
5872 dwarf2_per_objfile->n_comp_units);
5874 create_cus_from_debug_names_list (objfile, map, dwarf2_per_objfile->info,
5876 0 /* base_offset */);
5878 if (dwz_map.cu_count == 0)
5881 dwz_file *dwz = dwarf2_get_dwz_file ();
5882 create_cus_from_debug_names_list (objfile, dwz_map, dwz->info,
5884 map.cu_count /* base_offset */);
5887 /* Read .debug_names. If everything went ok, initialize the "quick"
5888 elements of all the CUs and return true. Otherwise, return false. */
5891 dwarf2_read_debug_names (struct objfile *objfile)
5893 mapped_debug_names local_map, dwz_map;
5895 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5896 &dwarf2_per_objfile->debug_names,
5900 /* Don't use the index if it's empty. */
5901 if (local_map.name_count == 0)
5904 /* If there is a .dwz file, read it so we can get its CU list as
5906 dwz_file *dwz = dwarf2_get_dwz_file ();
5909 if (!read_debug_names_from_section (objfile,
5910 bfd_get_filename (dwz->dwz_bfd),
5911 &dwz->debug_names, dwz_map))
5913 warning (_("could not read '.debug_names' section from %s; skipping"),
5914 bfd_get_filename (dwz->dwz_bfd));
5919 create_cus_from_debug_names (objfile, local_map, dwz_map);
5921 if (local_map.tu_count != 0)
5923 /* We can only handle a single .debug_types when we have an
5925 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5928 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5929 dwarf2_per_objfile->types, 0);
5931 create_signatured_type_table_from_debug_names
5932 (objfile, local_map, section, &dwarf2_per_objfile->abbrev);
5935 create_addrmap_from_aranges (objfile, &dwarf2_per_objfile->debug_aranges);
5937 dwarf2_per_objfile->debug_names_table.reset (new mapped_debug_names);
5938 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
5939 dwarf2_per_objfile->using_index = 1;
5940 dwarf2_per_objfile->quick_file_names_table =
5941 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
5946 /* Symbol name hashing function as specified by DWARF-5. */
5949 dwarf5_djb_hash (const char *str_)
5951 const unsigned char *str = (const unsigned char *) str_;
5953 /* Note: tolower here ignores UTF-8, which isn't fully compliant.
5954 See http://dwarfstd.org/ShowIssue.php?issue=161027.1. */
5956 uint32_t hash = 5381;
5957 while (int c = *str++)
5958 hash = hash * 33 + tolower (c);
5962 /* Type used to manage iterating over all CUs looking for a symbol for
5965 class dw2_debug_names_iterator
5968 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
5969 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
5970 dw2_debug_names_iterator (const mapped_debug_names &map,
5971 bool want_specific_block,
5972 block_enum block_index, domain_enum domain,
5974 : m_map (map), m_want_specific_block (want_specific_block),
5975 m_block_index (block_index), m_domain (domain),
5976 m_addr (find_vec_in_debug_names (map, name))
5979 dw2_debug_names_iterator (const mapped_debug_names &map,
5980 search_domain search, uint32_t namei)
5983 m_addr (find_vec_in_debug_names (map, namei))
5986 /* Return the next matching CU or NULL if there are no more. */
5987 dwarf2_per_cu_data *next ();
5990 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5992 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5995 /* The internalized form of .debug_names. */
5996 const mapped_debug_names &m_map;
5998 /* If true, only look for symbols that match BLOCK_INDEX. */
5999 const bool m_want_specific_block = false;
6001 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
6002 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
6004 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
6006 /* The kind of symbol we're looking for. */
6007 const domain_enum m_domain = UNDEF_DOMAIN;
6008 const search_domain m_search = ALL_DOMAIN;
6010 /* The list of CUs from the index entry of the symbol, or NULL if
6012 const gdb_byte *m_addr;
6016 mapped_debug_names::namei_to_name (uint32_t namei) const
6018 const ULONGEST namei_string_offs
6019 = extract_unsigned_integer ((name_table_string_offs_reordered
6020 + namei * offset_size),
6023 return read_indirect_string_at_offset
6024 (dwarf2_per_objfile->objfile->obfd, namei_string_offs);
6027 /* Find a slot in .debug_names for the object named NAME. If NAME is
6028 found, return pointer to its pool data. If NAME cannot be found,
6032 dw2_debug_names_iterator::find_vec_in_debug_names
6033 (const mapped_debug_names &map, const char *name)
6035 int (*cmp) (const char *, const char *);
6037 if (current_language->la_language == language_cplus
6038 || current_language->la_language == language_fortran
6039 || current_language->la_language == language_d)
6041 /* NAME is already canonical. Drop any qualifiers as
6042 .debug_names does not contain any. */
6044 if (strchr (name, '(') != NULL)
6046 gdb::unique_xmalloc_ptr<char> without_params
6047 = cp_remove_params (name);
6049 if (without_params != NULL)
6051 name = without_params.get();
6056 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
6058 const uint32_t full_hash = dwarf5_djb_hash (name);
6060 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6061 (map.bucket_table_reordered
6062 + (full_hash % map.bucket_count)), 4,
6063 map.dwarf5_byte_order);
6067 if (namei >= map.name_count)
6069 complaint (&symfile_complaints,
6070 _("Wrong .debug_names with name index %u but name_count=%u "
6072 namei, map.name_count,
6073 objfile_name (dwarf2_per_objfile->objfile));
6079 const uint32_t namei_full_hash
6080 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6081 (map.hash_table_reordered + namei), 4,
6082 map.dwarf5_byte_order);
6083 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
6086 if (full_hash == namei_full_hash)
6088 const char *const namei_string = map.namei_to_name (namei);
6090 #if 0 /* An expensive sanity check. */
6091 if (namei_full_hash != dwarf5_djb_hash (namei_string))
6093 complaint (&symfile_complaints,
6094 _("Wrong .debug_names hash for string at index %u "
6096 namei, objfile_name (dwarf2_per_objfile->objfile));
6101 if (cmp (namei_string, name) == 0)
6103 const ULONGEST namei_entry_offs
6104 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6105 + namei * map.offset_size),
6106 map.offset_size, map.dwarf5_byte_order);
6107 return map.entry_pool + namei_entry_offs;
6112 if (namei >= map.name_count)
6118 dw2_debug_names_iterator::find_vec_in_debug_names
6119 (const mapped_debug_names &map, uint32_t namei)
6121 if (namei >= map.name_count)
6123 complaint (&symfile_complaints,
6124 _("Wrong .debug_names with name index %u but name_count=%u "
6126 namei, map.name_count,
6127 objfile_name (dwarf2_per_objfile->objfile));
6131 const ULONGEST namei_entry_offs
6132 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6133 + namei * map.offset_size),
6134 map.offset_size, map.dwarf5_byte_order);
6135 return map.entry_pool + namei_entry_offs;
6138 /* See dw2_debug_names_iterator. */
6140 dwarf2_per_cu_data *
6141 dw2_debug_names_iterator::next ()
6146 bfd *const abfd = dwarf2_per_objfile->objfile->obfd;
6150 unsigned int bytes_read;
6151 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6152 m_addr += bytes_read;
6156 const auto indexval_it = m_map.abbrev_map.find (abbrev);
6157 if (indexval_it == m_map.abbrev_map.cend ())
6159 complaint (&symfile_complaints,
6160 _("Wrong .debug_names undefined abbrev code %s "
6162 pulongest (abbrev), objfile_name (dwarf2_per_objfile->objfile));
6165 const mapped_debug_names::index_val &indexval = indexval_it->second;
6166 bool have_is_static = false;
6168 dwarf2_per_cu_data *per_cu = NULL;
6169 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
6174 case DW_FORM_implicit_const:
6175 ull = attr.implicit_const;
6177 case DW_FORM_flag_present:
6181 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6182 m_addr += bytes_read;
6185 complaint (&symfile_complaints,
6186 _("Unsupported .debug_names form %s [in module %s]"),
6187 dwarf_form_name (attr.form),
6188 objfile_name (dwarf2_per_objfile->objfile));
6191 switch (attr.dw_idx)
6193 case DW_IDX_compile_unit:
6194 /* Don't crash on bad data. */
6195 if (ull >= dwarf2_per_objfile->n_comp_units)
6197 complaint (&symfile_complaints,
6198 _(".debug_names entry has bad CU index %s"
6201 objfile_name (dwarf2_per_objfile->objfile));
6204 per_cu = dw2_get_cutu (ull);
6206 case DW_IDX_type_unit:
6207 /* Don't crash on bad data. */
6208 if (ull >= dwarf2_per_objfile->n_type_units)
6210 complaint (&symfile_complaints,
6211 _(".debug_names entry has bad TU index %s"
6214 objfile_name (dwarf2_per_objfile->objfile));
6217 per_cu = dw2_get_cutu (dwarf2_per_objfile->n_comp_units + ull);
6219 case DW_IDX_GNU_internal:
6220 if (!m_map.augmentation_is_gdb)
6222 have_is_static = true;
6225 case DW_IDX_GNU_external:
6226 if (!m_map.augmentation_is_gdb)
6228 have_is_static = true;
6234 /* Skip if already read in. */
6235 if (per_cu->v.quick->compunit_symtab)
6238 /* Check static vs global. */
6241 const bool want_static = m_block_index != GLOBAL_BLOCK;
6242 if (m_want_specific_block && want_static != is_static)
6246 /* Match dw2_symtab_iter_next, symbol_kind
6247 and debug_names::psymbol_tag. */
6251 switch (indexval.dwarf_tag)
6253 case DW_TAG_variable:
6254 case DW_TAG_subprogram:
6255 /* Some types are also in VAR_DOMAIN. */
6256 case DW_TAG_typedef:
6257 case DW_TAG_structure_type:
6264 switch (indexval.dwarf_tag)
6266 case DW_TAG_typedef:
6267 case DW_TAG_structure_type:
6274 switch (indexval.dwarf_tag)
6277 case DW_TAG_variable:
6287 /* Match dw2_expand_symtabs_matching, symbol_kind and
6288 debug_names::psymbol_tag. */
6291 case VARIABLES_DOMAIN:
6292 switch (indexval.dwarf_tag)
6294 case DW_TAG_variable:
6300 case FUNCTIONS_DOMAIN:
6301 switch (indexval.dwarf_tag)
6303 case DW_TAG_subprogram:
6310 switch (indexval.dwarf_tag)
6312 case DW_TAG_typedef:
6313 case DW_TAG_structure_type:
6326 static struct compunit_symtab *
6327 dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6328 const char *name, domain_enum domain)
6330 const block_enum block_index = static_cast<block_enum> (block_index_int);
6331 dw2_setup (objfile);
6333 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6336 /* index is NULL if OBJF_READNOW. */
6339 const auto &map = *mapp;
6341 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6342 block_index, domain, name);
6344 struct compunit_symtab *stab_best = NULL;
6345 struct dwarf2_per_cu_data *per_cu;
6346 while ((per_cu = iter.next ()) != NULL)
6348 struct symbol *sym, *with_opaque = NULL;
6349 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6350 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6351 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6353 sym = block_find_symbol (block, name, domain,
6354 block_find_non_opaque_type_preferred,
6357 /* Some caution must be observed with overloaded functions and
6358 methods, since the index will not contain any overload
6359 information (but NAME might contain it). */
6362 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6364 if (with_opaque != NULL
6365 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6368 /* Keep looking through other CUs. */
6374 /* This dumps minimal information about .debug_names. It is called
6375 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6376 uses this to verify that .debug_names has been loaded. */
6379 dw2_debug_names_dump (struct objfile *objfile)
6381 dw2_setup (objfile);
6382 gdb_assert (dwarf2_per_objfile->using_index);
6383 printf_filtered (".debug_names:");
6384 if (dwarf2_per_objfile->debug_names_table)
6385 printf_filtered (" exists\n");
6387 printf_filtered (" faked for \"readnow\"\n");
6388 printf_filtered ("\n");
6392 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6393 const char *func_name)
6395 dw2_setup (objfile);
6397 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6398 if (dwarf2_per_objfile->debug_names_table)
6400 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6402 /* Note: It doesn't matter what we pass for block_index here. */
6403 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6404 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
6406 struct dwarf2_per_cu_data *per_cu;
6407 while ((per_cu = iter.next ()) != NULL)
6408 dw2_instantiate_symtab (per_cu);
6413 dw2_debug_names_expand_symtabs_matching
6414 (struct objfile *objfile,
6415 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6416 const lookup_name_info &lookup_name,
6417 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6418 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6419 enum search_domain kind)
6421 dw2_setup (objfile);
6423 /* debug_names_table is NULL if OBJF_READNOW. */
6424 if (!dwarf2_per_objfile->debug_names_table)
6427 dw_expand_symtabs_matching_file_matcher (file_matcher);
6429 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6431 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6433 kind, [&] (offset_type namei)
6435 /* The name was matched, now expand corresponding CUs that were
6437 dw2_debug_names_iterator iter (map, kind, namei);
6439 struct dwarf2_per_cu_data *per_cu;
6440 while ((per_cu = iter.next ()) != NULL)
6441 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6446 const struct quick_symbol_functions dwarf2_debug_names_functions =
6449 dw2_find_last_source_symtab,
6450 dw2_forget_cached_source_info,
6451 dw2_map_symtabs_matching_filename,
6452 dw2_debug_names_lookup_symbol,
6454 dw2_debug_names_dump,
6456 dw2_debug_names_expand_symtabs_for_function,
6457 dw2_expand_all_symtabs,
6458 dw2_expand_symtabs_with_fullname,
6459 dw2_map_matching_symbols,
6460 dw2_debug_names_expand_symtabs_matching,
6461 dw2_find_pc_sect_compunit_symtab,
6463 dw2_map_symbol_filenames
6466 /* See symfile.h. */
6469 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6471 /* If we're about to read full symbols, don't bother with the
6472 indices. In this case we also don't care if some other debug
6473 format is making psymtabs, because they are all about to be
6475 if ((objfile->flags & OBJF_READNOW))
6479 dwarf2_per_objfile->using_index = 1;
6480 create_all_comp_units (objfile);
6481 create_all_type_units (objfile);
6482 dwarf2_per_objfile->quick_file_names_table =
6483 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6485 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
6486 + dwarf2_per_objfile->n_type_units); ++i)
6488 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6490 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6491 struct dwarf2_per_cu_quick_data);
6494 /* Return 1 so that gdb sees the "quick" functions. However,
6495 these functions will be no-ops because we will have expanded
6497 *index_kind = dw_index_kind::GDB_INDEX;
6501 if (dwarf2_read_debug_names (objfile))
6503 *index_kind = dw_index_kind::DEBUG_NAMES;
6507 if (dwarf2_read_index (objfile))
6509 *index_kind = dw_index_kind::GDB_INDEX;
6518 /* Build a partial symbol table. */
6521 dwarf2_build_psymtabs (struct objfile *objfile)
6524 if (objfile->global_psymbols.capacity () == 0
6525 && objfile->static_psymbols.capacity () == 0)
6526 init_psymbol_list (objfile, 1024);
6530 /* This isn't really ideal: all the data we allocate on the
6531 objfile's obstack is still uselessly kept around. However,
6532 freeing it seems unsafe. */
6533 psymtab_discarder psymtabs (objfile);
6534 dwarf2_build_psymtabs_hard (objfile);
6537 CATCH (except, RETURN_MASK_ERROR)
6539 exception_print (gdb_stderr, except);
6544 /* Return the total length of the CU described by HEADER. */
6547 get_cu_length (const struct comp_unit_head *header)
6549 return header->initial_length_size + header->length;
6552 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6555 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6557 sect_offset bottom = cu_header->sect_off;
6558 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6560 return sect_off >= bottom && sect_off < top;
6563 /* Find the base address of the compilation unit for range lists and
6564 location lists. It will normally be specified by DW_AT_low_pc.
6565 In DWARF-3 draft 4, the base address could be overridden by
6566 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6567 compilation units with discontinuous ranges. */
6570 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6572 struct attribute *attr;
6575 cu->base_address = 0;
6577 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6580 cu->base_address = attr_value_as_address (attr);
6585 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6588 cu->base_address = attr_value_as_address (attr);
6594 /* Read in the comp unit header information from the debug_info at info_ptr.
6595 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6596 NOTE: This leaves members offset, first_die_offset to be filled in
6599 static const gdb_byte *
6600 read_comp_unit_head (struct comp_unit_head *cu_header,
6601 const gdb_byte *info_ptr,
6602 struct dwarf2_section_info *section,
6603 rcuh_kind section_kind)
6606 unsigned int bytes_read;
6607 const char *filename = get_section_file_name (section);
6608 bfd *abfd = get_section_bfd_owner (section);
6610 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6611 cu_header->initial_length_size = bytes_read;
6612 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6613 info_ptr += bytes_read;
6614 cu_header->version = read_2_bytes (abfd, info_ptr);
6616 if (cu_header->version < 5)
6617 switch (section_kind)
6619 case rcuh_kind::COMPILE:
6620 cu_header->unit_type = DW_UT_compile;
6622 case rcuh_kind::TYPE:
6623 cu_header->unit_type = DW_UT_type;
6626 internal_error (__FILE__, __LINE__,
6627 _("read_comp_unit_head: invalid section_kind"));
6631 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6632 (read_1_byte (abfd, info_ptr));
6634 switch (cu_header->unit_type)
6637 if (section_kind != rcuh_kind::COMPILE)
6638 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6639 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6643 section_kind = rcuh_kind::TYPE;
6646 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6647 "(is %d, should be %d or %d) [in module %s]"),
6648 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6651 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6654 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6657 info_ptr += bytes_read;
6658 if (cu_header->version < 5)
6660 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6663 signed_addr = bfd_get_sign_extend_vma (abfd);
6664 if (signed_addr < 0)
6665 internal_error (__FILE__, __LINE__,
6666 _("read_comp_unit_head: dwarf from non elf file"));
6667 cu_header->signed_addr_p = signed_addr;
6669 if (section_kind == rcuh_kind::TYPE)
6671 LONGEST type_offset;
6673 cu_header->signature = read_8_bytes (abfd, info_ptr);
6676 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6677 info_ptr += bytes_read;
6678 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6679 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6680 error (_("Dwarf Error: Too big type_offset in compilation unit "
6681 "header (is %s) [in module %s]"), plongest (type_offset),
6688 /* Helper function that returns the proper abbrev section for
6691 static struct dwarf2_section_info *
6692 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6694 struct dwarf2_section_info *abbrev;
6696 if (this_cu->is_dwz)
6697 abbrev = &dwarf2_get_dwz_file ()->abbrev;
6699 abbrev = &dwarf2_per_objfile->abbrev;
6704 /* Subroutine of read_and_check_comp_unit_head and
6705 read_and_check_type_unit_head to simplify them.
6706 Perform various error checking on the header. */
6709 error_check_comp_unit_head (struct comp_unit_head *header,
6710 struct dwarf2_section_info *section,
6711 struct dwarf2_section_info *abbrev_section)
6713 const char *filename = get_section_file_name (section);
6715 if (header->version < 2 || header->version > 5)
6716 error (_("Dwarf Error: wrong version in compilation unit header "
6717 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
6720 if (to_underlying (header->abbrev_sect_off)
6721 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6722 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
6723 "(offset 0x%x + 6) [in module %s]"),
6724 to_underlying (header->abbrev_sect_off),
6725 to_underlying (header->sect_off),
6728 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6729 avoid potential 32-bit overflow. */
6730 if (((ULONGEST) header->sect_off + get_cu_length (header))
6732 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6733 "(offset 0x%x + 0) [in module %s]"),
6734 header->length, to_underlying (header->sect_off),
6738 /* Read in a CU/TU header and perform some basic error checking.
6739 The contents of the header are stored in HEADER.
6740 The result is a pointer to the start of the first DIE. */
6742 static const gdb_byte *
6743 read_and_check_comp_unit_head (struct comp_unit_head *header,
6744 struct dwarf2_section_info *section,
6745 struct dwarf2_section_info *abbrev_section,
6746 const gdb_byte *info_ptr,
6747 rcuh_kind section_kind)
6749 const gdb_byte *beg_of_comp_unit = info_ptr;
6751 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6753 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6755 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6757 error_check_comp_unit_head (header, section, abbrev_section);
6762 /* Fetch the abbreviation table offset from a comp or type unit header. */
6765 read_abbrev_offset (struct dwarf2_section_info *section,
6766 sect_offset sect_off)
6768 bfd *abfd = get_section_bfd_owner (section);
6769 const gdb_byte *info_ptr;
6770 unsigned int initial_length_size, offset_size;
6773 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6774 info_ptr = section->buffer + to_underlying (sect_off);
6775 read_initial_length (abfd, info_ptr, &initial_length_size);
6776 offset_size = initial_length_size == 4 ? 4 : 8;
6777 info_ptr += initial_length_size;
6779 version = read_2_bytes (abfd, info_ptr);
6783 /* Skip unit type and address size. */
6787 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6790 /* Allocate a new partial symtab for file named NAME and mark this new
6791 partial symtab as being an include of PST. */
6794 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6795 struct objfile *objfile)
6797 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6799 if (!IS_ABSOLUTE_PATH (subpst->filename))
6801 /* It shares objfile->objfile_obstack. */
6802 subpst->dirname = pst->dirname;
6805 subpst->textlow = 0;
6806 subpst->texthigh = 0;
6808 subpst->dependencies
6809 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
6810 subpst->dependencies[0] = pst;
6811 subpst->number_of_dependencies = 1;
6813 subpst->globals_offset = 0;
6814 subpst->n_global_syms = 0;
6815 subpst->statics_offset = 0;
6816 subpst->n_static_syms = 0;
6817 subpst->compunit_symtab = NULL;
6818 subpst->read_symtab = pst->read_symtab;
6821 /* No private part is necessary for include psymtabs. This property
6822 can be used to differentiate between such include psymtabs and
6823 the regular ones. */
6824 subpst->read_symtab_private = NULL;
6827 /* Read the Line Number Program data and extract the list of files
6828 included by the source file represented by PST. Build an include
6829 partial symtab for each of these included files. */
6832 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6833 struct die_info *die,
6834 struct partial_symtab *pst)
6837 struct attribute *attr;
6839 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6841 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6843 return; /* No linetable, so no includes. */
6845 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
6846 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
6850 hash_signatured_type (const void *item)
6852 const struct signatured_type *sig_type
6853 = (const struct signatured_type *) item;
6855 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6856 return sig_type->signature;
6860 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6862 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6863 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6865 return lhs->signature == rhs->signature;
6868 /* Allocate a hash table for signatured types. */
6871 allocate_signatured_type_table (struct objfile *objfile)
6873 return htab_create_alloc_ex (41,
6874 hash_signatured_type,
6877 &objfile->objfile_obstack,
6878 hashtab_obstack_allocate,
6879 dummy_obstack_deallocate);
6882 /* A helper function to add a signatured type CU to a table. */
6885 add_signatured_type_cu_to_table (void **slot, void *datum)
6887 struct signatured_type *sigt = (struct signatured_type *) *slot;
6888 struct signatured_type ***datap = (struct signatured_type ***) datum;
6896 /* A helper for create_debug_types_hash_table. Read types from SECTION
6897 and fill them into TYPES_HTAB. It will process only type units,
6898 therefore DW_UT_type. */
6901 create_debug_type_hash_table (struct dwo_file *dwo_file,
6902 dwarf2_section_info *section, htab_t &types_htab,
6903 rcuh_kind section_kind)
6905 struct objfile *objfile = dwarf2_per_objfile->objfile;
6906 struct dwarf2_section_info *abbrev_section;
6908 const gdb_byte *info_ptr, *end_ptr;
6910 abbrev_section = (dwo_file != NULL
6911 ? &dwo_file->sections.abbrev
6912 : &dwarf2_per_objfile->abbrev);
6914 if (dwarf_read_debug)
6915 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6916 get_section_name (section),
6917 get_section_file_name (abbrev_section));
6919 dwarf2_read_section (objfile, section);
6920 info_ptr = section->buffer;
6922 if (info_ptr == NULL)
6925 /* We can't set abfd until now because the section may be empty or
6926 not present, in which case the bfd is unknown. */
6927 abfd = get_section_bfd_owner (section);
6929 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6930 because we don't need to read any dies: the signature is in the
6933 end_ptr = info_ptr + section->size;
6934 while (info_ptr < end_ptr)
6936 struct signatured_type *sig_type;
6937 struct dwo_unit *dwo_tu;
6939 const gdb_byte *ptr = info_ptr;
6940 struct comp_unit_head header;
6941 unsigned int length;
6943 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6945 /* Initialize it due to a false compiler warning. */
6946 header.signature = -1;
6947 header.type_cu_offset_in_tu = (cu_offset) -1;
6949 /* We need to read the type's signature in order to build the hash
6950 table, but we don't need anything else just yet. */
6952 ptr = read_and_check_comp_unit_head (&header, section,
6953 abbrev_section, ptr, section_kind);
6955 length = get_cu_length (&header);
6957 /* Skip dummy type units. */
6958 if (ptr >= info_ptr + length
6959 || peek_abbrev_code (abfd, ptr) == 0
6960 || header.unit_type != DW_UT_type)
6966 if (types_htab == NULL)
6969 types_htab = allocate_dwo_unit_table (objfile);
6971 types_htab = allocate_signatured_type_table (objfile);
6977 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6979 dwo_tu->dwo_file = dwo_file;
6980 dwo_tu->signature = header.signature;
6981 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6982 dwo_tu->section = section;
6983 dwo_tu->sect_off = sect_off;
6984 dwo_tu->length = length;
6988 /* N.B.: type_offset is not usable if this type uses a DWO file.
6989 The real type_offset is in the DWO file. */
6991 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6992 struct signatured_type);
6993 sig_type->signature = header.signature;
6994 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6995 sig_type->per_cu.objfile = objfile;
6996 sig_type->per_cu.is_debug_types = 1;
6997 sig_type->per_cu.section = section;
6998 sig_type->per_cu.sect_off = sect_off;
6999 sig_type->per_cu.length = length;
7002 slot = htab_find_slot (types_htab,
7003 dwo_file ? (void*) dwo_tu : (void *) sig_type,
7005 gdb_assert (slot != NULL);
7008 sect_offset dup_sect_off;
7012 const struct dwo_unit *dup_tu
7013 = (const struct dwo_unit *) *slot;
7015 dup_sect_off = dup_tu->sect_off;
7019 const struct signatured_type *dup_tu
7020 = (const struct signatured_type *) *slot;
7022 dup_sect_off = dup_tu->per_cu.sect_off;
7025 complaint (&symfile_complaints,
7026 _("debug type entry at offset 0x%x is duplicate to"
7027 " the entry at offset 0x%x, signature %s"),
7028 to_underlying (sect_off), to_underlying (dup_sect_off),
7029 hex_string (header.signature));
7031 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
7033 if (dwarf_read_debug > 1)
7034 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
7035 to_underlying (sect_off),
7036 hex_string (header.signature));
7042 /* Create the hash table of all entries in the .debug_types
7043 (or .debug_types.dwo) section(s).
7044 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
7045 otherwise it is NULL.
7047 The result is a pointer to the hash table or NULL if there are no types.
7049 Note: This function processes DWO files only, not DWP files. */
7052 create_debug_types_hash_table (struct dwo_file *dwo_file,
7053 VEC (dwarf2_section_info_def) *types,
7057 struct dwarf2_section_info *section;
7059 if (VEC_empty (dwarf2_section_info_def, types))
7063 VEC_iterate (dwarf2_section_info_def, types, ix, section);
7065 create_debug_type_hash_table (dwo_file, section, types_htab,
7069 /* Create the hash table of all entries in the .debug_types section,
7070 and initialize all_type_units.
7071 The result is zero if there is an error (e.g. missing .debug_types section),
7072 otherwise non-zero. */
7075 create_all_type_units (struct objfile *objfile)
7077 htab_t types_htab = NULL;
7078 struct signatured_type **iter;
7080 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
7081 rcuh_kind::COMPILE);
7082 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
7083 if (types_htab == NULL)
7085 dwarf2_per_objfile->signatured_types = NULL;
7089 dwarf2_per_objfile->signatured_types = types_htab;
7091 dwarf2_per_objfile->n_type_units
7092 = dwarf2_per_objfile->n_allocated_type_units
7093 = htab_elements (types_htab);
7094 dwarf2_per_objfile->all_type_units =
7095 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
7096 iter = &dwarf2_per_objfile->all_type_units[0];
7097 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
7098 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
7099 == dwarf2_per_objfile->n_type_units);
7104 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
7105 If SLOT is non-NULL, it is the entry to use in the hash table.
7106 Otherwise we find one. */
7108 static struct signatured_type *
7109 add_type_unit (ULONGEST sig, void **slot)
7111 struct objfile *objfile = dwarf2_per_objfile->objfile;
7112 int n_type_units = dwarf2_per_objfile->n_type_units;
7113 struct signatured_type *sig_type;
7115 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
7117 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
7119 if (dwarf2_per_objfile->n_allocated_type_units == 0)
7120 dwarf2_per_objfile->n_allocated_type_units = 1;
7121 dwarf2_per_objfile->n_allocated_type_units *= 2;
7122 dwarf2_per_objfile->all_type_units
7123 = XRESIZEVEC (struct signatured_type *,
7124 dwarf2_per_objfile->all_type_units,
7125 dwarf2_per_objfile->n_allocated_type_units);
7126 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
7128 dwarf2_per_objfile->n_type_units = n_type_units;
7130 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7131 struct signatured_type);
7132 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
7133 sig_type->signature = sig;
7134 sig_type->per_cu.is_debug_types = 1;
7135 if (dwarf2_per_objfile->using_index)
7137 sig_type->per_cu.v.quick =
7138 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7139 struct dwarf2_per_cu_quick_data);
7144 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7147 gdb_assert (*slot == NULL);
7149 /* The rest of sig_type must be filled in by the caller. */
7153 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7154 Fill in SIG_ENTRY with DWO_ENTRY. */
7157 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
7158 struct signatured_type *sig_entry,
7159 struct dwo_unit *dwo_entry)
7161 /* Make sure we're not clobbering something we don't expect to. */
7162 gdb_assert (! sig_entry->per_cu.queued);
7163 gdb_assert (sig_entry->per_cu.cu == NULL);
7164 if (dwarf2_per_objfile->using_index)
7166 gdb_assert (sig_entry->per_cu.v.quick != NULL);
7167 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
7170 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7171 gdb_assert (sig_entry->signature == dwo_entry->signature);
7172 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7173 gdb_assert (sig_entry->type_unit_group == NULL);
7174 gdb_assert (sig_entry->dwo_unit == NULL);
7176 sig_entry->per_cu.section = dwo_entry->section;
7177 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7178 sig_entry->per_cu.length = dwo_entry->length;
7179 sig_entry->per_cu.reading_dwo_directly = 1;
7180 sig_entry->per_cu.objfile = objfile;
7181 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7182 sig_entry->dwo_unit = dwo_entry;
7185 /* Subroutine of lookup_signatured_type.
7186 If we haven't read the TU yet, create the signatured_type data structure
7187 for a TU to be read in directly from a DWO file, bypassing the stub.
7188 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7189 using .gdb_index, then when reading a CU we want to stay in the DWO file
7190 containing that CU. Otherwise we could end up reading several other DWO
7191 files (due to comdat folding) to process the transitive closure of all the
7192 mentioned TUs, and that can be slow. The current DWO file will have every
7193 type signature that it needs.
7194 We only do this for .gdb_index because in the psymtab case we already have
7195 to read all the DWOs to build the type unit groups. */
7197 static struct signatured_type *
7198 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7200 struct objfile *objfile = dwarf2_per_objfile->objfile;
7201 struct dwo_file *dwo_file;
7202 struct dwo_unit find_dwo_entry, *dwo_entry;
7203 struct signatured_type find_sig_entry, *sig_entry;
7206 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7208 /* If TU skeletons have been removed then we may not have read in any
7210 if (dwarf2_per_objfile->signatured_types == NULL)
7212 dwarf2_per_objfile->signatured_types
7213 = allocate_signatured_type_table (objfile);
7216 /* We only ever need to read in one copy of a signatured type.
7217 Use the global signatured_types array to do our own comdat-folding
7218 of types. If this is the first time we're reading this TU, and
7219 the TU has an entry in .gdb_index, replace the recorded data from
7220 .gdb_index with this TU. */
7222 find_sig_entry.signature = sig;
7223 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7224 &find_sig_entry, INSERT);
7225 sig_entry = (struct signatured_type *) *slot;
7227 /* We can get here with the TU already read, *or* in the process of being
7228 read. Don't reassign the global entry to point to this DWO if that's
7229 the case. Also note that if the TU is already being read, it may not
7230 have come from a DWO, the program may be a mix of Fission-compiled
7231 code and non-Fission-compiled code. */
7233 /* Have we already tried to read this TU?
7234 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7235 needn't exist in the global table yet). */
7236 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7239 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7240 dwo_unit of the TU itself. */
7241 dwo_file = cu->dwo_unit->dwo_file;
7243 /* Ok, this is the first time we're reading this TU. */
7244 if (dwo_file->tus == NULL)
7246 find_dwo_entry.signature = sig;
7247 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7248 if (dwo_entry == NULL)
7251 /* If the global table doesn't have an entry for this TU, add one. */
7252 if (sig_entry == NULL)
7253 sig_entry = add_type_unit (sig, slot);
7255 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
7256 sig_entry->per_cu.tu_read = 1;
7260 /* Subroutine of lookup_signatured_type.
7261 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7262 then try the DWP file. If the TU stub (skeleton) has been removed then
7263 it won't be in .gdb_index. */
7265 static struct signatured_type *
7266 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7268 struct objfile *objfile = dwarf2_per_objfile->objfile;
7269 struct dwp_file *dwp_file = get_dwp_file ();
7270 struct dwo_unit *dwo_entry;
7271 struct signatured_type find_sig_entry, *sig_entry;
7274 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7275 gdb_assert (dwp_file != NULL);
7277 /* If TU skeletons have been removed then we may not have read in any
7279 if (dwarf2_per_objfile->signatured_types == NULL)
7281 dwarf2_per_objfile->signatured_types
7282 = allocate_signatured_type_table (objfile);
7285 find_sig_entry.signature = sig;
7286 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7287 &find_sig_entry, INSERT);
7288 sig_entry = (struct signatured_type *) *slot;
7290 /* Have we already tried to read this TU?
7291 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7292 needn't exist in the global table yet). */
7293 if (sig_entry != NULL)
7296 if (dwp_file->tus == NULL)
7298 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
7299 sig, 1 /* is_debug_types */);
7300 if (dwo_entry == NULL)
7303 sig_entry = add_type_unit (sig, slot);
7304 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
7309 /* Lookup a signature based type for DW_FORM_ref_sig8.
7310 Returns NULL if signature SIG is not present in the table.
7311 It is up to the caller to complain about this. */
7313 static struct signatured_type *
7314 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7317 && dwarf2_per_objfile->using_index)
7319 /* We're in a DWO/DWP file, and we're using .gdb_index.
7320 These cases require special processing. */
7321 if (get_dwp_file () == NULL)
7322 return lookup_dwo_signatured_type (cu, sig);
7324 return lookup_dwp_signatured_type (cu, sig);
7328 struct signatured_type find_entry, *entry;
7330 if (dwarf2_per_objfile->signatured_types == NULL)
7332 find_entry.signature = sig;
7333 entry = ((struct signatured_type *)
7334 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7339 /* Low level DIE reading support. */
7341 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7344 init_cu_die_reader (struct die_reader_specs *reader,
7345 struct dwarf2_cu *cu,
7346 struct dwarf2_section_info *section,
7347 struct dwo_file *dwo_file)
7349 gdb_assert (section->readin && section->buffer != NULL);
7350 reader->abfd = get_section_bfd_owner (section);
7352 reader->dwo_file = dwo_file;
7353 reader->die_section = section;
7354 reader->buffer = section->buffer;
7355 reader->buffer_end = section->buffer + section->size;
7356 reader->comp_dir = NULL;
7359 /* Subroutine of init_cutu_and_read_dies to simplify it.
7360 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7361 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7364 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7365 from it to the DIE in the DWO. If NULL we are skipping the stub.
7366 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7367 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7368 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7369 STUB_COMP_DIR may be non-NULL.
7370 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7371 are filled in with the info of the DIE from the DWO file.
7372 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
7373 provided an abbrev table to use.
7374 The result is non-zero if a valid (non-dummy) DIE was found. */
7377 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7378 struct dwo_unit *dwo_unit,
7379 int abbrev_table_provided,
7380 struct die_info *stub_comp_unit_die,
7381 const char *stub_comp_dir,
7382 struct die_reader_specs *result_reader,
7383 const gdb_byte **result_info_ptr,
7384 struct die_info **result_comp_unit_die,
7385 int *result_has_children)
7387 struct objfile *objfile = dwarf2_per_objfile->objfile;
7388 struct dwarf2_cu *cu = this_cu->cu;
7389 struct dwarf2_section_info *section;
7391 const gdb_byte *begin_info_ptr, *info_ptr;
7392 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7393 int i,num_extra_attrs;
7394 struct dwarf2_section_info *dwo_abbrev_section;
7395 struct attribute *attr;
7396 struct die_info *comp_unit_die;
7398 /* At most one of these may be provided. */
7399 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7401 /* These attributes aren't processed until later:
7402 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7403 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7404 referenced later. However, these attributes are found in the stub
7405 which we won't have later. In order to not impose this complication
7406 on the rest of the code, we read them here and copy them to the
7415 if (stub_comp_unit_die != NULL)
7417 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7419 if (! this_cu->is_debug_types)
7420 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7421 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7422 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7423 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7424 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7426 /* There should be a DW_AT_addr_base attribute here (if needed).
7427 We need the value before we can process DW_FORM_GNU_addr_index. */
7429 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7431 cu->addr_base = DW_UNSND (attr);
7433 /* There should be a DW_AT_ranges_base attribute here (if needed).
7434 We need the value before we can process DW_AT_ranges. */
7435 cu->ranges_base = 0;
7436 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7438 cu->ranges_base = DW_UNSND (attr);
7440 else if (stub_comp_dir != NULL)
7442 /* Reconstruct the comp_dir attribute to simplify the code below. */
7443 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7444 comp_dir->name = DW_AT_comp_dir;
7445 comp_dir->form = DW_FORM_string;
7446 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7447 DW_STRING (comp_dir) = stub_comp_dir;
7450 /* Set up for reading the DWO CU/TU. */
7451 cu->dwo_unit = dwo_unit;
7452 section = dwo_unit->section;
7453 dwarf2_read_section (objfile, section);
7454 abfd = get_section_bfd_owner (section);
7455 begin_info_ptr = info_ptr = (section->buffer
7456 + to_underlying (dwo_unit->sect_off));
7457 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7458 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
7460 if (this_cu->is_debug_types)
7462 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7464 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
7466 info_ptr, rcuh_kind::TYPE);
7467 /* This is not an assert because it can be caused by bad debug info. */
7468 if (sig_type->signature != cu->header.signature)
7470 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7471 " TU at offset 0x%x [in module %s]"),
7472 hex_string (sig_type->signature),
7473 hex_string (cu->header.signature),
7474 to_underlying (dwo_unit->sect_off),
7475 bfd_get_filename (abfd));
7477 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7478 /* For DWOs coming from DWP files, we don't know the CU length
7479 nor the type's offset in the TU until now. */
7480 dwo_unit->length = get_cu_length (&cu->header);
7481 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7483 /* Establish the type offset that can be used to lookup the type.
7484 For DWO files, we don't know it until now. */
7485 sig_type->type_offset_in_section
7486 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7490 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
7492 info_ptr, rcuh_kind::COMPILE);
7493 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7494 /* For DWOs coming from DWP files, we don't know the CU length
7496 dwo_unit->length = get_cu_length (&cu->header);
7499 /* Replace the CU's original abbrev table with the DWO's.
7500 Reminder: We can't read the abbrev table until we've read the header. */
7501 if (abbrev_table_provided)
7503 /* Don't free the provided abbrev table, the caller of
7504 init_cutu_and_read_dies owns it. */
7505 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
7506 /* Ensure the DWO abbrev table gets freed. */
7507 make_cleanup (dwarf2_free_abbrev_table, cu);
7511 dwarf2_free_abbrev_table (cu);
7512 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
7513 /* Leave any existing abbrev table cleanup as is. */
7516 /* Read in the die, but leave space to copy over the attributes
7517 from the stub. This has the benefit of simplifying the rest of
7518 the code - all the work to maintain the illusion of a single
7519 DW_TAG_{compile,type}_unit DIE is done here. */
7520 num_extra_attrs = ((stmt_list != NULL)
7524 + (comp_dir != NULL));
7525 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7526 result_has_children, num_extra_attrs);
7528 /* Copy over the attributes from the stub to the DIE we just read in. */
7529 comp_unit_die = *result_comp_unit_die;
7530 i = comp_unit_die->num_attrs;
7531 if (stmt_list != NULL)
7532 comp_unit_die->attrs[i++] = *stmt_list;
7534 comp_unit_die->attrs[i++] = *low_pc;
7535 if (high_pc != NULL)
7536 comp_unit_die->attrs[i++] = *high_pc;
7538 comp_unit_die->attrs[i++] = *ranges;
7539 if (comp_dir != NULL)
7540 comp_unit_die->attrs[i++] = *comp_dir;
7541 comp_unit_die->num_attrs += num_extra_attrs;
7543 if (dwarf_die_debug)
7545 fprintf_unfiltered (gdb_stdlog,
7546 "Read die from %s@0x%x of %s:\n",
7547 get_section_name (section),
7548 (unsigned) (begin_info_ptr - section->buffer),
7549 bfd_get_filename (abfd));
7550 dump_die (comp_unit_die, dwarf_die_debug);
7553 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7554 TUs by skipping the stub and going directly to the entry in the DWO file.
7555 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7556 to get it via circuitous means. Blech. */
7557 if (comp_dir != NULL)
7558 result_reader->comp_dir = DW_STRING (comp_dir);
7560 /* Skip dummy compilation units. */
7561 if (info_ptr >= begin_info_ptr + dwo_unit->length
7562 || peek_abbrev_code (abfd, info_ptr) == 0)
7565 *result_info_ptr = info_ptr;
7569 /* Subroutine of init_cutu_and_read_dies to simplify it.
7570 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7571 Returns NULL if the specified DWO unit cannot be found. */
7573 static struct dwo_unit *
7574 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7575 struct die_info *comp_unit_die)
7577 struct dwarf2_cu *cu = this_cu->cu;
7579 struct dwo_unit *dwo_unit;
7580 const char *comp_dir, *dwo_name;
7582 gdb_assert (cu != NULL);
7584 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7585 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7586 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7588 if (this_cu->is_debug_types)
7590 struct signatured_type *sig_type;
7592 /* Since this_cu is the first member of struct signatured_type,
7593 we can go from a pointer to one to a pointer to the other. */
7594 sig_type = (struct signatured_type *) this_cu;
7595 signature = sig_type->signature;
7596 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7600 struct attribute *attr;
7602 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7604 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7606 dwo_name, objfile_name (this_cu->objfile));
7607 signature = DW_UNSND (attr);
7608 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7615 /* Subroutine of init_cutu_and_read_dies to simplify it.
7616 See it for a description of the parameters.
7617 Read a TU directly from a DWO file, bypassing the stub.
7619 Note: This function could be a little bit simpler if we shared cleanups
7620 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
7621 to do, so we keep this function self-contained. Or we could move this
7622 into our caller, but it's complex enough already. */
7625 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7626 int use_existing_cu, int keep,
7627 die_reader_func_ftype *die_reader_func,
7630 struct dwarf2_cu *cu;
7631 struct signatured_type *sig_type;
7632 struct cleanup *cleanups, *free_cu_cleanup = NULL;
7633 struct die_reader_specs reader;
7634 const gdb_byte *info_ptr;
7635 struct die_info *comp_unit_die;
7638 /* Verify we can do the following downcast, and that we have the
7640 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7641 sig_type = (struct signatured_type *) this_cu;
7642 gdb_assert (sig_type->dwo_unit != NULL);
7644 cleanups = make_cleanup (null_cleanup, NULL);
7646 if (use_existing_cu && this_cu->cu != NULL)
7648 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7650 /* There's no need to do the rereading_dwo_cu handling that
7651 init_cutu_and_read_dies does since we don't read the stub. */
7655 /* If !use_existing_cu, this_cu->cu must be NULL. */
7656 gdb_assert (this_cu->cu == NULL);
7657 cu = XNEW (struct dwarf2_cu);
7658 init_one_comp_unit (cu, this_cu);
7659 /* If an error occurs while loading, release our storage. */
7660 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
7663 /* A future optimization, if needed, would be to use an existing
7664 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7665 could share abbrev tables. */
7667 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7668 0 /* abbrev_table_provided */,
7669 NULL /* stub_comp_unit_die */,
7670 sig_type->dwo_unit->dwo_file->comp_dir,
7672 &comp_unit_die, &has_children) == 0)
7675 do_cleanups (cleanups);
7679 /* All the "real" work is done here. */
7680 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7682 /* This duplicates the code in init_cutu_and_read_dies,
7683 but the alternative is making the latter more complex.
7684 This function is only for the special case of using DWO files directly:
7685 no point in overly complicating the general case just to handle this. */
7686 if (free_cu_cleanup != NULL)
7690 /* We've successfully allocated this compilation unit. Let our
7691 caller clean it up when finished with it. */
7692 discard_cleanups (free_cu_cleanup);
7694 /* We can only discard free_cu_cleanup and all subsequent cleanups.
7695 So we have to manually free the abbrev table. */
7696 dwarf2_free_abbrev_table (cu);
7698 /* Link this CU into read_in_chain. */
7699 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7700 dwarf2_per_objfile->read_in_chain = this_cu;
7703 do_cleanups (free_cu_cleanup);
7706 do_cleanups (cleanups);
7709 /* Initialize a CU (or TU) and read its DIEs.
7710 If the CU defers to a DWO file, read the DWO file as well.
7712 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7713 Otherwise the table specified in the comp unit header is read in and used.
7714 This is an optimization for when we already have the abbrev table.
7716 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7717 Otherwise, a new CU is allocated with xmalloc.
7719 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7720 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7722 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7723 linker) then DIE_READER_FUNC will not get called. */
7726 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7727 struct abbrev_table *abbrev_table,
7728 int use_existing_cu, int keep,
7729 die_reader_func_ftype *die_reader_func,
7732 struct objfile *objfile = dwarf2_per_objfile->objfile;
7733 struct dwarf2_section_info *section = this_cu->section;
7734 bfd *abfd = get_section_bfd_owner (section);
7735 struct dwarf2_cu *cu;
7736 const gdb_byte *begin_info_ptr, *info_ptr;
7737 struct die_reader_specs reader;
7738 struct die_info *comp_unit_die;
7740 struct attribute *attr;
7741 struct cleanup *cleanups, *free_cu_cleanup = NULL;
7742 struct signatured_type *sig_type = NULL;
7743 struct dwarf2_section_info *abbrev_section;
7744 /* Non-zero if CU currently points to a DWO file and we need to
7745 reread it. When this happens we need to reread the skeleton die
7746 before we can reread the DWO file (this only applies to CUs, not TUs). */
7747 int rereading_dwo_cu = 0;
7749 if (dwarf_die_debug)
7750 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
7751 this_cu->is_debug_types ? "type" : "comp",
7752 to_underlying (this_cu->sect_off));
7754 if (use_existing_cu)
7757 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7758 file (instead of going through the stub), short-circuit all of this. */
7759 if (this_cu->reading_dwo_directly)
7761 /* Narrow down the scope of possibilities to have to understand. */
7762 gdb_assert (this_cu->is_debug_types);
7763 gdb_assert (abbrev_table == NULL);
7764 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7765 die_reader_func, data);
7769 cleanups = make_cleanup (null_cleanup, NULL);
7771 /* This is cheap if the section is already read in. */
7772 dwarf2_read_section (objfile, section);
7774 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7776 abbrev_section = get_abbrev_section_for_cu (this_cu);
7778 if (use_existing_cu && this_cu->cu != NULL)
7781 /* If this CU is from a DWO file we need to start over, we need to
7782 refetch the attributes from the skeleton CU.
7783 This could be optimized by retrieving those attributes from when we
7784 were here the first time: the previous comp_unit_die was stored in
7785 comp_unit_obstack. But there's no data yet that we need this
7787 if (cu->dwo_unit != NULL)
7788 rereading_dwo_cu = 1;
7792 /* If !use_existing_cu, this_cu->cu must be NULL. */
7793 gdb_assert (this_cu->cu == NULL);
7794 cu = XNEW (struct dwarf2_cu);
7795 init_one_comp_unit (cu, this_cu);
7796 /* If an error occurs while loading, release our storage. */
7797 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
7800 /* Get the header. */
7801 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7803 /* We already have the header, there's no need to read it in again. */
7804 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7808 if (this_cu->is_debug_types)
7810 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
7811 abbrev_section, info_ptr,
7814 /* Since per_cu is the first member of struct signatured_type,
7815 we can go from a pointer to one to a pointer to the other. */
7816 sig_type = (struct signatured_type *) this_cu;
7817 gdb_assert (sig_type->signature == cu->header.signature);
7818 gdb_assert (sig_type->type_offset_in_tu
7819 == cu->header.type_cu_offset_in_tu);
7820 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7822 /* LENGTH has not been set yet for type units if we're
7823 using .gdb_index. */
7824 this_cu->length = get_cu_length (&cu->header);
7826 /* Establish the type offset that can be used to lookup the type. */
7827 sig_type->type_offset_in_section =
7828 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7830 this_cu->dwarf_version = cu->header.version;
7834 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
7837 rcuh_kind::COMPILE);
7839 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7840 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7841 this_cu->dwarf_version = cu->header.version;
7845 /* Skip dummy compilation units. */
7846 if (info_ptr >= begin_info_ptr + this_cu->length
7847 || peek_abbrev_code (abfd, info_ptr) == 0)
7849 do_cleanups (cleanups);
7853 /* If we don't have them yet, read the abbrevs for this compilation unit.
7854 And if we need to read them now, make sure they're freed when we're
7855 done. Note that it's important that if the CU had an abbrev table
7856 on entry we don't free it when we're done: Somewhere up the call stack
7857 it may be in use. */
7858 if (abbrev_table != NULL)
7860 gdb_assert (cu->abbrev_table == NULL);
7861 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7862 cu->abbrev_table = abbrev_table;
7864 else if (cu->abbrev_table == NULL)
7866 dwarf2_read_abbrevs (cu, abbrev_section);
7867 make_cleanup (dwarf2_free_abbrev_table, cu);
7869 else if (rereading_dwo_cu)
7871 dwarf2_free_abbrev_table (cu);
7872 dwarf2_read_abbrevs (cu, abbrev_section);
7875 /* Read the top level CU/TU die. */
7876 init_cu_die_reader (&reader, cu, section, NULL);
7877 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7879 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7881 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7882 DWO CU, that this test will fail (the attribute will not be present). */
7883 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7886 struct dwo_unit *dwo_unit;
7887 struct die_info *dwo_comp_unit_die;
7891 complaint (&symfile_complaints,
7892 _("compilation unit with DW_AT_GNU_dwo_name"
7893 " has children (offset 0x%x) [in module %s]"),
7894 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
7896 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7897 if (dwo_unit != NULL)
7899 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7900 abbrev_table != NULL,
7901 comp_unit_die, NULL,
7903 &dwo_comp_unit_die, &has_children) == 0)
7906 do_cleanups (cleanups);
7909 comp_unit_die = dwo_comp_unit_die;
7913 /* Yikes, we couldn't find the rest of the DIE, we only have
7914 the stub. A complaint has already been logged. There's
7915 not much more we can do except pass on the stub DIE to
7916 die_reader_func. We don't want to throw an error on bad
7921 /* All of the above is setup for this call. Yikes. */
7922 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7924 /* Done, clean up. */
7925 if (free_cu_cleanup != NULL)
7929 /* We've successfully allocated this compilation unit. Let our
7930 caller clean it up when finished with it. */
7931 discard_cleanups (free_cu_cleanup);
7933 /* We can only discard free_cu_cleanup and all subsequent cleanups.
7934 So we have to manually free the abbrev table. */
7935 dwarf2_free_abbrev_table (cu);
7937 /* Link this CU into read_in_chain. */
7938 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7939 dwarf2_per_objfile->read_in_chain = this_cu;
7942 do_cleanups (free_cu_cleanup);
7945 do_cleanups (cleanups);
7948 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7949 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7950 to have already done the lookup to find the DWO file).
7952 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7953 THIS_CU->is_debug_types, but nothing else.
7955 We fill in THIS_CU->length.
7957 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7958 linker) then DIE_READER_FUNC will not get called.
7960 THIS_CU->cu is always freed when done.
7961 This is done in order to not leave THIS_CU->cu in a state where we have
7962 to care whether it refers to the "main" CU or the DWO CU. */
7965 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7966 struct dwo_file *dwo_file,
7967 die_reader_func_ftype *die_reader_func,
7970 struct objfile *objfile = dwarf2_per_objfile->objfile;
7971 struct dwarf2_section_info *section = this_cu->section;
7972 bfd *abfd = get_section_bfd_owner (section);
7973 struct dwarf2_section_info *abbrev_section;
7974 struct dwarf2_cu cu;
7975 const gdb_byte *begin_info_ptr, *info_ptr;
7976 struct die_reader_specs reader;
7977 struct cleanup *cleanups;
7978 struct die_info *comp_unit_die;
7981 if (dwarf_die_debug)
7982 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
7983 this_cu->is_debug_types ? "type" : "comp",
7984 to_underlying (this_cu->sect_off));
7986 gdb_assert (this_cu->cu == NULL);
7988 abbrev_section = (dwo_file != NULL
7989 ? &dwo_file->sections.abbrev
7990 : get_abbrev_section_for_cu (this_cu));
7992 /* This is cheap if the section is already read in. */
7993 dwarf2_read_section (objfile, section);
7995 init_one_comp_unit (&cu, this_cu);
7997 cleanups = make_cleanup (free_stack_comp_unit, &cu);
7999 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
8000 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
8001 abbrev_section, info_ptr,
8002 (this_cu->is_debug_types
8004 : rcuh_kind::COMPILE));
8006 this_cu->length = get_cu_length (&cu.header);
8008 /* Skip dummy compilation units. */
8009 if (info_ptr >= begin_info_ptr + this_cu->length
8010 || peek_abbrev_code (abfd, info_ptr) == 0)
8012 do_cleanups (cleanups);
8016 dwarf2_read_abbrevs (&cu, abbrev_section);
8017 make_cleanup (dwarf2_free_abbrev_table, &cu);
8019 init_cu_die_reader (&reader, &cu, section, dwo_file);
8020 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8022 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8024 do_cleanups (cleanups);
8027 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
8028 does not lookup the specified DWO file.
8029 This cannot be used to read DWO files.
8031 THIS_CU->cu is always freed when done.
8032 This is done in order to not leave THIS_CU->cu in a state where we have
8033 to care whether it refers to the "main" CU or the DWO CU.
8034 We can revisit this if the data shows there's a performance issue. */
8037 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
8038 die_reader_func_ftype *die_reader_func,
8041 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
8044 /* Type Unit Groups.
8046 Type Unit Groups are a way to collapse the set of all TUs (type units) into
8047 a more manageable set. The grouping is done by DW_AT_stmt_list entry
8048 so that all types coming from the same compilation (.o file) are grouped
8049 together. A future step could be to put the types in the same symtab as
8050 the CU the types ultimately came from. */
8053 hash_type_unit_group (const void *item)
8055 const struct type_unit_group *tu_group
8056 = (const struct type_unit_group *) item;
8058 return hash_stmt_list_entry (&tu_group->hash);
8062 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
8064 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
8065 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
8067 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
8070 /* Allocate a hash table for type unit groups. */
8073 allocate_type_unit_groups_table (void)
8075 return htab_create_alloc_ex (3,
8076 hash_type_unit_group,
8079 &dwarf2_per_objfile->objfile->objfile_obstack,
8080 hashtab_obstack_allocate,
8081 dummy_obstack_deallocate);
8084 /* Type units that don't have DW_AT_stmt_list are grouped into their own
8085 partial symtabs. We combine several TUs per psymtab to not let the size
8086 of any one psymtab grow too big. */
8087 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
8088 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
8090 /* Helper routine for get_type_unit_group.
8091 Create the type_unit_group object used to hold one or more TUs. */
8093 static struct type_unit_group *
8094 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
8096 struct objfile *objfile = dwarf2_per_objfile->objfile;
8097 struct dwarf2_per_cu_data *per_cu;
8098 struct type_unit_group *tu_group;
8100 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8101 struct type_unit_group);
8102 per_cu = &tu_group->per_cu;
8103 per_cu->objfile = objfile;
8105 if (dwarf2_per_objfile->using_index)
8107 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8108 struct dwarf2_per_cu_quick_data);
8112 unsigned int line_offset = to_underlying (line_offset_struct);
8113 struct partial_symtab *pst;
8116 /* Give the symtab a useful name for debug purposes. */
8117 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
8118 name = xstrprintf ("<type_units_%d>",
8119 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
8121 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
8123 pst = create_partial_symtab (per_cu, name);
8129 tu_group->hash.dwo_unit = cu->dwo_unit;
8130 tu_group->hash.line_sect_off = line_offset_struct;
8135 /* Look up the type_unit_group for type unit CU, and create it if necessary.
8136 STMT_LIST is a DW_AT_stmt_list attribute. */
8138 static struct type_unit_group *
8139 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
8141 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8142 struct type_unit_group *tu_group;
8144 unsigned int line_offset;
8145 struct type_unit_group type_unit_group_for_lookup;
8147 if (dwarf2_per_objfile->type_unit_groups == NULL)
8149 dwarf2_per_objfile->type_unit_groups =
8150 allocate_type_unit_groups_table ();
8153 /* Do we need to create a new group, or can we use an existing one? */
8157 line_offset = DW_UNSND (stmt_list);
8158 ++tu_stats->nr_symtab_sharers;
8162 /* Ugh, no stmt_list. Rare, but we have to handle it.
8163 We can do various things here like create one group per TU or
8164 spread them over multiple groups to split up the expansion work.
8165 To avoid worst case scenarios (too many groups or too large groups)
8166 we, umm, group them in bunches. */
8167 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
8168 | (tu_stats->nr_stmt_less_type_units
8169 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
8170 ++tu_stats->nr_stmt_less_type_units;
8173 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
8174 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
8175 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8176 &type_unit_group_for_lookup, INSERT);
8179 tu_group = (struct type_unit_group *) *slot;
8180 gdb_assert (tu_group != NULL);
8184 sect_offset line_offset_struct = (sect_offset) line_offset;
8185 tu_group = create_type_unit_group (cu, line_offset_struct);
8187 ++tu_stats->nr_symtabs;
8193 /* Partial symbol tables. */
8195 /* Create a psymtab named NAME and assign it to PER_CU.
8197 The caller must fill in the following details:
8198 dirname, textlow, texthigh. */
8200 static struct partial_symtab *
8201 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8203 struct objfile *objfile = per_cu->objfile;
8204 struct partial_symtab *pst;
8206 pst = start_psymtab_common (objfile, name, 0,
8207 objfile->global_psymbols,
8208 objfile->static_psymbols);
8210 pst->psymtabs_addrmap_supported = 1;
8212 /* This is the glue that links PST into GDB's symbol API. */
8213 pst->read_symtab_private = per_cu;
8214 pst->read_symtab = dwarf2_read_symtab;
8215 per_cu->v.psymtab = pst;
8220 /* The DATA object passed to process_psymtab_comp_unit_reader has this
8223 struct process_psymtab_comp_unit_data
8225 /* True if we are reading a DW_TAG_partial_unit. */
8227 int want_partial_unit;
8229 /* The "pretend" language that is used if the CU doesn't declare a
8232 enum language pretend_language;
8235 /* die_reader_func for process_psymtab_comp_unit. */
8238 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8239 const gdb_byte *info_ptr,
8240 struct die_info *comp_unit_die,
8244 struct dwarf2_cu *cu = reader->cu;
8245 struct objfile *objfile = cu->objfile;
8246 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8247 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8249 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8250 struct partial_symtab *pst;
8251 enum pc_bounds_kind cu_bounds_kind;
8252 const char *filename;
8253 struct process_psymtab_comp_unit_data *info
8254 = (struct process_psymtab_comp_unit_data *) data;
8256 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8259 gdb_assert (! per_cu->is_debug_types);
8261 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8263 cu->list_in_scope = &file_symbols;
8265 /* Allocate a new partial symbol table structure. */
8266 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8267 if (filename == NULL)
8270 pst = create_partial_symtab (per_cu, filename);
8272 /* This must be done before calling dwarf2_build_include_psymtabs. */
8273 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8275 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8277 dwarf2_find_base_address (comp_unit_die, cu);
8279 /* Possibly set the default values of LOWPC and HIGHPC from
8281 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8282 &best_highpc, cu, pst);
8283 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8284 /* Store the contiguous range if it is not empty; it can be empty for
8285 CUs with no code. */
8286 addrmap_set_empty (objfile->psymtabs_addrmap,
8287 gdbarch_adjust_dwarf2_addr (gdbarch,
8288 best_lowpc + baseaddr),
8289 gdbarch_adjust_dwarf2_addr (gdbarch,
8290 best_highpc + baseaddr) - 1,
8293 /* Check if comp unit has_children.
8294 If so, read the rest of the partial symbols from this comp unit.
8295 If not, there's no more debug_info for this comp unit. */
8298 struct partial_die_info *first_die;
8299 CORE_ADDR lowpc, highpc;
8301 lowpc = ((CORE_ADDR) -1);
8302 highpc = ((CORE_ADDR) 0);
8304 first_die = load_partial_dies (reader, info_ptr, 1);
8306 scan_partial_symbols (first_die, &lowpc, &highpc,
8307 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8309 /* If we didn't find a lowpc, set it to highpc to avoid
8310 complaints from `maint check'. */
8311 if (lowpc == ((CORE_ADDR) -1))
8314 /* If the compilation unit didn't have an explicit address range,
8315 then use the information extracted from its child dies. */
8316 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8319 best_highpc = highpc;
8322 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8323 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
8325 end_psymtab_common (objfile, pst);
8327 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8330 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8331 struct dwarf2_per_cu_data *iter;
8333 /* Fill in 'dependencies' here; we fill in 'users' in a
8335 pst->number_of_dependencies = len;
8337 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8339 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8342 pst->dependencies[i] = iter->v.psymtab;
8344 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8347 /* Get the list of files included in the current compilation unit,
8348 and build a psymtab for each of them. */
8349 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8351 if (dwarf_read_debug)
8353 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8355 fprintf_unfiltered (gdb_stdlog,
8356 "Psymtab for %s unit @0x%x: %s - %s"
8357 ", %d global, %d static syms\n",
8358 per_cu->is_debug_types ? "type" : "comp",
8359 to_underlying (per_cu->sect_off),
8360 paddress (gdbarch, pst->textlow),
8361 paddress (gdbarch, pst->texthigh),
8362 pst->n_global_syms, pst->n_static_syms);
8366 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8367 Process compilation unit THIS_CU for a psymtab. */
8370 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8371 int want_partial_unit,
8372 enum language pretend_language)
8374 /* If this compilation unit was already read in, free the
8375 cached copy in order to read it in again. This is
8376 necessary because we skipped some symbols when we first
8377 read in the compilation unit (see load_partial_dies).
8378 This problem could be avoided, but the benefit is unclear. */
8379 if (this_cu->cu != NULL)
8380 free_one_cached_comp_unit (this_cu);
8382 if (this_cu->is_debug_types)
8383 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8387 process_psymtab_comp_unit_data info;
8388 info.want_partial_unit = want_partial_unit;
8389 info.pretend_language = pretend_language;
8390 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8391 process_psymtab_comp_unit_reader, &info);
8394 /* Age out any secondary CUs. */
8395 age_cached_comp_units ();
8398 /* Reader function for build_type_psymtabs. */
8401 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8402 const gdb_byte *info_ptr,
8403 struct die_info *type_unit_die,
8407 struct objfile *objfile = dwarf2_per_objfile->objfile;
8408 struct dwarf2_cu *cu = reader->cu;
8409 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8410 struct signatured_type *sig_type;
8411 struct type_unit_group *tu_group;
8412 struct attribute *attr;
8413 struct partial_die_info *first_die;
8414 CORE_ADDR lowpc, highpc;
8415 struct partial_symtab *pst;
8417 gdb_assert (data == NULL);
8418 gdb_assert (per_cu->is_debug_types);
8419 sig_type = (struct signatured_type *) per_cu;
8424 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8425 tu_group = get_type_unit_group (cu, attr);
8427 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8429 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8430 cu->list_in_scope = &file_symbols;
8431 pst = create_partial_symtab (per_cu, "");
8434 first_die = load_partial_dies (reader, info_ptr, 1);
8436 lowpc = (CORE_ADDR) -1;
8437 highpc = (CORE_ADDR) 0;
8438 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8440 end_psymtab_common (objfile, pst);
8443 /* Struct used to sort TUs by their abbreviation table offset. */
8445 struct tu_abbrev_offset
8447 struct signatured_type *sig_type;
8448 sect_offset abbrev_offset;
8451 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
8454 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
8456 const struct tu_abbrev_offset * const *a
8457 = (const struct tu_abbrev_offset * const*) ap;
8458 const struct tu_abbrev_offset * const *b
8459 = (const struct tu_abbrev_offset * const*) bp;
8460 sect_offset aoff = (*a)->abbrev_offset;
8461 sect_offset boff = (*b)->abbrev_offset;
8463 return (aoff > boff) - (aoff < boff);
8466 /* Efficiently read all the type units.
8467 This does the bulk of the work for build_type_psymtabs.
8469 The efficiency is because we sort TUs by the abbrev table they use and
8470 only read each abbrev table once. In one program there are 200K TUs
8471 sharing 8K abbrev tables.
8473 The main purpose of this function is to support building the
8474 dwarf2_per_objfile->type_unit_groups table.
8475 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8476 can collapse the search space by grouping them by stmt_list.
8477 The savings can be significant, in the same program from above the 200K TUs
8478 share 8K stmt_list tables.
8480 FUNC is expected to call get_type_unit_group, which will create the
8481 struct type_unit_group if necessary and add it to
8482 dwarf2_per_objfile->type_unit_groups. */
8485 build_type_psymtabs_1 (void)
8487 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8488 struct cleanup *cleanups;
8489 struct abbrev_table *abbrev_table;
8490 sect_offset abbrev_offset;
8491 struct tu_abbrev_offset *sorted_by_abbrev;
8494 /* It's up to the caller to not call us multiple times. */
8495 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8497 if (dwarf2_per_objfile->n_type_units == 0)
8500 /* TUs typically share abbrev tables, and there can be way more TUs than
8501 abbrev tables. Sort by abbrev table to reduce the number of times we
8502 read each abbrev table in.
8503 Alternatives are to punt or to maintain a cache of abbrev tables.
8504 This is simpler and efficient enough for now.
8506 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8507 symtab to use). Typically TUs with the same abbrev offset have the same
8508 stmt_list value too so in practice this should work well.
8510 The basic algorithm here is:
8512 sort TUs by abbrev table
8513 for each TU with same abbrev table:
8514 read abbrev table if first user
8515 read TU top level DIE
8516 [IWBN if DWO skeletons had DW_AT_stmt_list]
8519 if (dwarf_read_debug)
8520 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8522 /* Sort in a separate table to maintain the order of all_type_units
8523 for .gdb_index: TU indices directly index all_type_units. */
8524 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
8525 dwarf2_per_objfile->n_type_units);
8526 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8528 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8530 sorted_by_abbrev[i].sig_type = sig_type;
8531 sorted_by_abbrev[i].abbrev_offset =
8532 read_abbrev_offset (sig_type->per_cu.section,
8533 sig_type->per_cu.sect_off);
8535 cleanups = make_cleanup (xfree, sorted_by_abbrev);
8536 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
8537 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
8539 abbrev_offset = (sect_offset) ~(unsigned) 0;
8540 abbrev_table = NULL;
8541 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
8543 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8545 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8547 /* Switch to the next abbrev table if necessary. */
8548 if (abbrev_table == NULL
8549 || tu->abbrev_offset != abbrev_offset)
8551 if (abbrev_table != NULL)
8553 abbrev_table_free (abbrev_table);
8554 /* Reset to NULL in case abbrev_table_read_table throws
8555 an error: abbrev_table_free_cleanup will get called. */
8556 abbrev_table = NULL;
8558 abbrev_offset = tu->abbrev_offset;
8560 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
8562 ++tu_stats->nr_uniq_abbrev_tables;
8565 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
8566 build_type_psymtabs_reader, NULL);
8569 do_cleanups (cleanups);
8572 /* Print collected type unit statistics. */
8575 print_tu_stats (void)
8577 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8579 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8580 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8581 dwarf2_per_objfile->n_type_units);
8582 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8583 tu_stats->nr_uniq_abbrev_tables);
8584 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8585 tu_stats->nr_symtabs);
8586 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8587 tu_stats->nr_symtab_sharers);
8588 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8589 tu_stats->nr_stmt_less_type_units);
8590 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8591 tu_stats->nr_all_type_units_reallocs);
8594 /* Traversal function for build_type_psymtabs. */
8597 build_type_psymtab_dependencies (void **slot, void *info)
8599 struct objfile *objfile = dwarf2_per_objfile->objfile;
8600 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8601 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8602 struct partial_symtab *pst = per_cu->v.psymtab;
8603 int len = VEC_length (sig_type_ptr, tu_group->tus);
8604 struct signatured_type *iter;
8607 gdb_assert (len > 0);
8608 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8610 pst->number_of_dependencies = len;
8612 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8614 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8617 gdb_assert (iter->per_cu.is_debug_types);
8618 pst->dependencies[i] = iter->per_cu.v.psymtab;
8619 iter->type_unit_group = tu_group;
8622 VEC_free (sig_type_ptr, tu_group->tus);
8627 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8628 Build partial symbol tables for the .debug_types comp-units. */
8631 build_type_psymtabs (struct objfile *objfile)
8633 if (! create_all_type_units (objfile))
8636 build_type_psymtabs_1 ();
8639 /* Traversal function for process_skeletonless_type_unit.
8640 Read a TU in a DWO file and build partial symbols for it. */
8643 process_skeletonless_type_unit (void **slot, void *info)
8645 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8646 struct objfile *objfile = (struct objfile *) info;
8647 struct signatured_type find_entry, *entry;
8649 /* If this TU doesn't exist in the global table, add it and read it in. */
8651 if (dwarf2_per_objfile->signatured_types == NULL)
8653 dwarf2_per_objfile->signatured_types
8654 = allocate_signatured_type_table (objfile);
8657 find_entry.signature = dwo_unit->signature;
8658 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8660 /* If we've already seen this type there's nothing to do. What's happening
8661 is we're doing our own version of comdat-folding here. */
8665 /* This does the job that create_all_type_units would have done for
8667 entry = add_type_unit (dwo_unit->signature, slot);
8668 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
8671 /* This does the job that build_type_psymtabs_1 would have done. */
8672 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8673 build_type_psymtabs_reader, NULL);
8678 /* Traversal function for process_skeletonless_type_units. */
8681 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8683 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8685 if (dwo_file->tus != NULL)
8687 htab_traverse_noresize (dwo_file->tus,
8688 process_skeletonless_type_unit, info);
8694 /* Scan all TUs of DWO files, verifying we've processed them.
8695 This is needed in case a TU was emitted without its skeleton.
8696 Note: This can't be done until we know what all the DWO files are. */
8699 process_skeletonless_type_units (struct objfile *objfile)
8701 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8702 if (get_dwp_file () == NULL
8703 && dwarf2_per_objfile->dwo_files != NULL)
8705 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8706 process_dwo_file_for_skeletonless_type_units,
8711 /* Compute the 'user' field for each psymtab in OBJFILE. */
8714 set_partial_user (struct objfile *objfile)
8718 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8720 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
8721 struct partial_symtab *pst = per_cu->v.psymtab;
8727 for (j = 0; j < pst->number_of_dependencies; ++j)
8729 /* Set the 'user' field only if it is not already set. */
8730 if (pst->dependencies[j]->user == NULL)
8731 pst->dependencies[j]->user = pst;
8736 /* Build the partial symbol table by doing a quick pass through the
8737 .debug_info and .debug_abbrev sections. */
8740 dwarf2_build_psymtabs_hard (struct objfile *objfile)
8742 struct cleanup *back_to;
8745 if (dwarf_read_debug)
8747 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8748 objfile_name (objfile));
8751 dwarf2_per_objfile->reading_partial_symbols = 1;
8753 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8755 /* Any cached compilation units will be linked by the per-objfile
8756 read_in_chain. Make sure to free them when we're done. */
8757 back_to = make_cleanup (free_cached_comp_units, NULL);
8759 build_type_psymtabs (objfile);
8761 create_all_comp_units (objfile);
8763 /* Create a temporary address map on a temporary obstack. We later
8764 copy this to the final obstack. */
8765 auto_obstack temp_obstack;
8767 scoped_restore save_psymtabs_addrmap
8768 = make_scoped_restore (&objfile->psymtabs_addrmap,
8769 addrmap_create_mutable (&temp_obstack));
8771 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8773 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
8775 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8778 /* This has to wait until we read the CUs, we need the list of DWOs. */
8779 process_skeletonless_type_units (objfile);
8781 /* Now that all TUs have been processed we can fill in the dependencies. */
8782 if (dwarf2_per_objfile->type_unit_groups != NULL)
8784 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8785 build_type_psymtab_dependencies, NULL);
8788 if (dwarf_read_debug)
8791 set_partial_user (objfile);
8793 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8794 &objfile->objfile_obstack);
8795 /* At this point we want to keep the address map. */
8796 save_psymtabs_addrmap.release ();
8798 do_cleanups (back_to);
8800 if (dwarf_read_debug)
8801 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8802 objfile_name (objfile));
8805 /* die_reader_func for load_partial_comp_unit. */
8808 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8809 const gdb_byte *info_ptr,
8810 struct die_info *comp_unit_die,
8814 struct dwarf2_cu *cu = reader->cu;
8816 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8818 /* Check if comp unit has_children.
8819 If so, read the rest of the partial symbols from this comp unit.
8820 If not, there's no more debug_info for this comp unit. */
8822 load_partial_dies (reader, info_ptr, 0);
8825 /* Load the partial DIEs for a secondary CU into memory.
8826 This is also used when rereading a primary CU with load_all_dies. */
8829 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8831 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8832 load_partial_comp_unit_reader, NULL);
8836 read_comp_units_from_section (struct objfile *objfile,
8837 struct dwarf2_section_info *section,
8838 struct dwarf2_section_info *abbrev_section,
8839 unsigned int is_dwz,
8842 struct dwarf2_per_cu_data ***all_comp_units)
8844 const gdb_byte *info_ptr;
8846 if (dwarf_read_debug)
8847 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8848 get_section_name (section),
8849 get_section_file_name (section));
8851 dwarf2_read_section (objfile, section);
8853 info_ptr = section->buffer;
8855 while (info_ptr < section->buffer + section->size)
8857 struct dwarf2_per_cu_data *this_cu;
8859 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8861 comp_unit_head cu_header;
8862 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
8863 info_ptr, rcuh_kind::COMPILE);
8865 /* Save the compilation unit for later lookup. */
8866 if (cu_header.unit_type != DW_UT_type)
8868 this_cu = XOBNEW (&objfile->objfile_obstack,
8869 struct dwarf2_per_cu_data);
8870 memset (this_cu, 0, sizeof (*this_cu));
8874 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8875 struct signatured_type);
8876 memset (sig_type, 0, sizeof (*sig_type));
8877 sig_type->signature = cu_header.signature;
8878 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8879 this_cu = &sig_type->per_cu;
8881 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8882 this_cu->sect_off = sect_off;
8883 this_cu->length = cu_header.length + cu_header.initial_length_size;
8884 this_cu->is_dwz = is_dwz;
8885 this_cu->objfile = objfile;
8886 this_cu->section = section;
8888 if (*n_comp_units == *n_allocated)
8891 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
8892 *all_comp_units, *n_allocated);
8894 (*all_comp_units)[*n_comp_units] = this_cu;
8897 info_ptr = info_ptr + this_cu->length;
8901 /* Create a list of all compilation units in OBJFILE.
8902 This is only done for -readnow and building partial symtabs. */
8905 create_all_comp_units (struct objfile *objfile)
8909 struct dwarf2_per_cu_data **all_comp_units;
8910 struct dwz_file *dwz;
8914 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
8916 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info,
8917 &dwarf2_per_objfile->abbrev, 0,
8918 &n_allocated, &n_comp_units, &all_comp_units);
8920 dwz = dwarf2_get_dwz_file ();
8922 read_comp_units_from_section (objfile, &dwz->info, &dwz->abbrev, 1,
8923 &n_allocated, &n_comp_units,
8926 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
8927 struct dwarf2_per_cu_data *,
8929 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
8930 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
8931 xfree (all_comp_units);
8932 dwarf2_per_objfile->n_comp_units = n_comp_units;
8935 /* Process all loaded DIEs for compilation unit CU, starting at
8936 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8937 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8938 DW_AT_ranges). See the comments of add_partial_subprogram on how
8939 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8942 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8943 CORE_ADDR *highpc, int set_addrmap,
8944 struct dwarf2_cu *cu)
8946 struct partial_die_info *pdi;
8948 /* Now, march along the PDI's, descending into ones which have
8949 interesting children but skipping the children of the other ones,
8950 until we reach the end of the compilation unit. */
8956 fixup_partial_die (pdi, cu);
8958 /* Anonymous namespaces or modules have no name but have interesting
8959 children, so we need to look at them. Ditto for anonymous
8962 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8963 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8964 || pdi->tag == DW_TAG_imported_unit)
8968 case DW_TAG_subprogram:
8969 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8971 case DW_TAG_constant:
8972 case DW_TAG_variable:
8973 case DW_TAG_typedef:
8974 case DW_TAG_union_type:
8975 if (!pdi->is_declaration)
8977 add_partial_symbol (pdi, cu);
8980 case DW_TAG_class_type:
8981 case DW_TAG_interface_type:
8982 case DW_TAG_structure_type:
8983 if (!pdi->is_declaration)
8985 add_partial_symbol (pdi, cu);
8987 if (cu->language == language_rust && pdi->has_children)
8988 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8991 case DW_TAG_enumeration_type:
8992 if (!pdi->is_declaration)
8993 add_partial_enumeration (pdi, cu);
8995 case DW_TAG_base_type:
8996 case DW_TAG_subrange_type:
8997 /* File scope base type definitions are added to the partial
8999 add_partial_symbol (pdi, cu);
9001 case DW_TAG_namespace:
9002 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
9005 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
9007 case DW_TAG_imported_unit:
9009 struct dwarf2_per_cu_data *per_cu;
9011 /* For now we don't handle imported units in type units. */
9012 if (cu->per_cu->is_debug_types)
9014 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9015 " supported in type units [in module %s]"),
9016 objfile_name (cu->objfile));
9019 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
9023 /* Go read the partial unit, if needed. */
9024 if (per_cu->v.psymtab == NULL)
9025 process_psymtab_comp_unit (per_cu, 1, cu->language);
9027 VEC_safe_push (dwarf2_per_cu_ptr,
9028 cu->per_cu->imported_symtabs, per_cu);
9031 case DW_TAG_imported_declaration:
9032 add_partial_symbol (pdi, cu);
9039 /* If the die has a sibling, skip to the sibling. */
9041 pdi = pdi->die_sibling;
9045 /* Functions used to compute the fully scoped name of a partial DIE.
9047 Normally, this is simple. For C++, the parent DIE's fully scoped
9048 name is concatenated with "::" and the partial DIE's name.
9049 Enumerators are an exception; they use the scope of their parent
9050 enumeration type, i.e. the name of the enumeration type is not
9051 prepended to the enumerator.
9053 There are two complexities. One is DW_AT_specification; in this
9054 case "parent" means the parent of the target of the specification,
9055 instead of the direct parent of the DIE. The other is compilers
9056 which do not emit DW_TAG_namespace; in this case we try to guess
9057 the fully qualified name of structure types from their members'
9058 linkage names. This must be done using the DIE's children rather
9059 than the children of any DW_AT_specification target. We only need
9060 to do this for structures at the top level, i.e. if the target of
9061 any DW_AT_specification (if any; otherwise the DIE itself) does not
9064 /* Compute the scope prefix associated with PDI's parent, in
9065 compilation unit CU. The result will be allocated on CU's
9066 comp_unit_obstack, or a copy of the already allocated PDI->NAME
9067 field. NULL is returned if no prefix is necessary. */
9069 partial_die_parent_scope (struct partial_die_info *pdi,
9070 struct dwarf2_cu *cu)
9072 const char *grandparent_scope;
9073 struct partial_die_info *parent, *real_pdi;
9075 /* We need to look at our parent DIE; if we have a DW_AT_specification,
9076 then this means the parent of the specification DIE. */
9079 while (real_pdi->has_specification)
9080 real_pdi = find_partial_die (real_pdi->spec_offset,
9081 real_pdi->spec_is_dwz, cu);
9083 parent = real_pdi->die_parent;
9087 if (parent->scope_set)
9088 return parent->scope;
9090 fixup_partial_die (parent, cu);
9092 grandparent_scope = partial_die_parent_scope (parent, cu);
9094 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9095 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9096 Work around this problem here. */
9097 if (cu->language == language_cplus
9098 && parent->tag == DW_TAG_namespace
9099 && strcmp (parent->name, "::") == 0
9100 && grandparent_scope == NULL)
9102 parent->scope = NULL;
9103 parent->scope_set = 1;
9107 if (pdi->tag == DW_TAG_enumerator)
9108 /* Enumerators should not get the name of the enumeration as a prefix. */
9109 parent->scope = grandparent_scope;
9110 else if (parent->tag == DW_TAG_namespace
9111 || parent->tag == DW_TAG_module
9112 || parent->tag == DW_TAG_structure_type
9113 || parent->tag == DW_TAG_class_type
9114 || parent->tag == DW_TAG_interface_type
9115 || parent->tag == DW_TAG_union_type
9116 || parent->tag == DW_TAG_enumeration_type)
9118 if (grandparent_scope == NULL)
9119 parent->scope = parent->name;
9121 parent->scope = typename_concat (&cu->comp_unit_obstack,
9123 parent->name, 0, cu);
9127 /* FIXME drow/2004-04-01: What should we be doing with
9128 function-local names? For partial symbols, we should probably be
9130 complaint (&symfile_complaints,
9131 _("unhandled containing DIE tag %d for DIE at %d"),
9132 parent->tag, to_underlying (pdi->sect_off));
9133 parent->scope = grandparent_scope;
9136 parent->scope_set = 1;
9137 return parent->scope;
9140 /* Return the fully scoped name associated with PDI, from compilation unit
9141 CU. The result will be allocated with malloc. */
9144 partial_die_full_name (struct partial_die_info *pdi,
9145 struct dwarf2_cu *cu)
9147 const char *parent_scope;
9149 /* If this is a template instantiation, we can not work out the
9150 template arguments from partial DIEs. So, unfortunately, we have
9151 to go through the full DIEs. At least any work we do building
9152 types here will be reused if full symbols are loaded later. */
9153 if (pdi->has_template_arguments)
9155 fixup_partial_die (pdi, cu);
9157 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
9159 struct die_info *die;
9160 struct attribute attr;
9161 struct dwarf2_cu *ref_cu = cu;
9163 /* DW_FORM_ref_addr is using section offset. */
9164 attr.name = (enum dwarf_attribute) 0;
9165 attr.form = DW_FORM_ref_addr;
9166 attr.u.unsnd = to_underlying (pdi->sect_off);
9167 die = follow_die_ref (NULL, &attr, &ref_cu);
9169 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
9173 parent_scope = partial_die_parent_scope (pdi, cu);
9174 if (parent_scope == NULL)
9177 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
9181 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
9183 struct objfile *objfile = cu->objfile;
9184 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9186 const char *actual_name = NULL;
9188 char *built_actual_name;
9190 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9192 built_actual_name = partial_die_full_name (pdi, cu);
9193 if (built_actual_name != NULL)
9194 actual_name = built_actual_name;
9196 if (actual_name == NULL)
9197 actual_name = pdi->name;
9201 case DW_TAG_subprogram:
9202 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
9203 if (pdi->is_external || cu->language == language_ada)
9205 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
9206 of the global scope. But in Ada, we want to be able to access
9207 nested procedures globally. So all Ada subprograms are stored
9208 in the global scope. */
9209 add_psymbol_to_list (actual_name, strlen (actual_name),
9210 built_actual_name != NULL,
9211 VAR_DOMAIN, LOC_BLOCK,
9212 &objfile->global_psymbols,
9213 addr, cu->language, objfile);
9217 add_psymbol_to_list (actual_name, strlen (actual_name),
9218 built_actual_name != NULL,
9219 VAR_DOMAIN, LOC_BLOCK,
9220 &objfile->static_psymbols,
9221 addr, cu->language, objfile);
9224 if (pdi->main_subprogram && actual_name != NULL)
9225 set_objfile_main_name (objfile, actual_name, cu->language);
9227 case DW_TAG_constant:
9229 std::vector<partial_symbol *> *list;
9231 if (pdi->is_external)
9232 list = &objfile->global_psymbols;
9234 list = &objfile->static_psymbols;
9235 add_psymbol_to_list (actual_name, strlen (actual_name),
9236 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9237 list, 0, cu->language, objfile);
9240 case DW_TAG_variable:
9242 addr = decode_locdesc (pdi->d.locdesc, cu);
9246 && !dwarf2_per_objfile->has_section_at_zero)
9248 /* A global or static variable may also have been stripped
9249 out by the linker if unused, in which case its address
9250 will be nullified; do not add such variables into partial
9251 symbol table then. */
9253 else if (pdi->is_external)
9256 Don't enter into the minimal symbol tables as there is
9257 a minimal symbol table entry from the ELF symbols already.
9258 Enter into partial symbol table if it has a location
9259 descriptor or a type.
9260 If the location descriptor is missing, new_symbol will create
9261 a LOC_UNRESOLVED symbol, the address of the variable will then
9262 be determined from the minimal symbol table whenever the variable
9264 The address for the partial symbol table entry is not
9265 used by GDB, but it comes in handy for debugging partial symbol
9268 if (pdi->d.locdesc || pdi->has_type)
9269 add_psymbol_to_list (actual_name, strlen (actual_name),
9270 built_actual_name != NULL,
9271 VAR_DOMAIN, LOC_STATIC,
9272 &objfile->global_psymbols,
9274 cu->language, objfile);
9278 int has_loc = pdi->d.locdesc != NULL;
9280 /* Static Variable. Skip symbols whose value we cannot know (those
9281 without location descriptors or constant values). */
9282 if (!has_loc && !pdi->has_const_value)
9284 xfree (built_actual_name);
9288 add_psymbol_to_list (actual_name, strlen (actual_name),
9289 built_actual_name != NULL,
9290 VAR_DOMAIN, LOC_STATIC,
9291 &objfile->static_psymbols,
9292 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
9293 cu->language, objfile);
9296 case DW_TAG_typedef:
9297 case DW_TAG_base_type:
9298 case DW_TAG_subrange_type:
9299 add_psymbol_to_list (actual_name, strlen (actual_name),
9300 built_actual_name != NULL,
9301 VAR_DOMAIN, LOC_TYPEDEF,
9302 &objfile->static_psymbols,
9303 0, cu->language, objfile);
9305 case DW_TAG_imported_declaration:
9306 case DW_TAG_namespace:
9307 add_psymbol_to_list (actual_name, strlen (actual_name),
9308 built_actual_name != NULL,
9309 VAR_DOMAIN, LOC_TYPEDEF,
9310 &objfile->global_psymbols,
9311 0, cu->language, objfile);
9314 add_psymbol_to_list (actual_name, strlen (actual_name),
9315 built_actual_name != NULL,
9316 MODULE_DOMAIN, LOC_TYPEDEF,
9317 &objfile->global_psymbols,
9318 0, cu->language, objfile);
9320 case DW_TAG_class_type:
9321 case DW_TAG_interface_type:
9322 case DW_TAG_structure_type:
9323 case DW_TAG_union_type:
9324 case DW_TAG_enumeration_type:
9325 /* Skip external references. The DWARF standard says in the section
9326 about "Structure, Union, and Class Type Entries": "An incomplete
9327 structure, union or class type is represented by a structure,
9328 union or class entry that does not have a byte size attribute
9329 and that has a DW_AT_declaration attribute." */
9330 if (!pdi->has_byte_size && pdi->is_declaration)
9332 xfree (built_actual_name);
9336 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9337 static vs. global. */
9338 add_psymbol_to_list (actual_name, strlen (actual_name),
9339 built_actual_name != NULL,
9340 STRUCT_DOMAIN, LOC_TYPEDEF,
9341 cu->language == language_cplus
9342 ? &objfile->global_psymbols
9343 : &objfile->static_psymbols,
9344 0, cu->language, objfile);
9347 case DW_TAG_enumerator:
9348 add_psymbol_to_list (actual_name, strlen (actual_name),
9349 built_actual_name != NULL,
9350 VAR_DOMAIN, LOC_CONST,
9351 cu->language == language_cplus
9352 ? &objfile->global_psymbols
9353 : &objfile->static_psymbols,
9354 0, cu->language, objfile);
9360 xfree (built_actual_name);
9363 /* Read a partial die corresponding to a namespace; also, add a symbol
9364 corresponding to that namespace to the symbol table. NAMESPACE is
9365 the name of the enclosing namespace. */
9368 add_partial_namespace (struct partial_die_info *pdi,
9369 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9370 int set_addrmap, struct dwarf2_cu *cu)
9372 /* Add a symbol for the namespace. */
9374 add_partial_symbol (pdi, cu);
9376 /* Now scan partial symbols in that namespace. */
9378 if (pdi->has_children)
9379 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9382 /* Read a partial die corresponding to a Fortran module. */
9385 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9386 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9388 /* Add a symbol for the namespace. */
9390 add_partial_symbol (pdi, cu);
9392 /* Now scan partial symbols in that module. */
9394 if (pdi->has_children)
9395 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9398 /* Read a partial die corresponding to a subprogram and create a partial
9399 symbol for that subprogram. When the CU language allows it, this
9400 routine also defines a partial symbol for each nested subprogram
9401 that this subprogram contains. If SET_ADDRMAP is true, record the
9402 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
9403 and highest PC values found in PDI.
9405 PDI may also be a lexical block, in which case we simply search
9406 recursively for subprograms defined inside that lexical block.
9407 Again, this is only performed when the CU language allows this
9408 type of definitions. */
9411 add_partial_subprogram (struct partial_die_info *pdi,
9412 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9413 int set_addrmap, struct dwarf2_cu *cu)
9415 if (pdi->tag == DW_TAG_subprogram)
9417 if (pdi->has_pc_info)
9419 if (pdi->lowpc < *lowpc)
9420 *lowpc = pdi->lowpc;
9421 if (pdi->highpc > *highpc)
9422 *highpc = pdi->highpc;
9425 struct objfile *objfile = cu->objfile;
9426 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9431 baseaddr = ANOFFSET (objfile->section_offsets,
9432 SECT_OFF_TEXT (objfile));
9433 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9434 pdi->lowpc + baseaddr);
9435 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9436 pdi->highpc + baseaddr);
9437 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9438 cu->per_cu->v.psymtab);
9442 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9444 if (!pdi->is_declaration)
9445 /* Ignore subprogram DIEs that do not have a name, they are
9446 illegal. Do not emit a complaint at this point, we will
9447 do so when we convert this psymtab into a symtab. */
9449 add_partial_symbol (pdi, cu);
9453 if (! pdi->has_children)
9456 if (cu->language == language_ada)
9458 pdi = pdi->die_child;
9461 fixup_partial_die (pdi, cu);
9462 if (pdi->tag == DW_TAG_subprogram
9463 || pdi->tag == DW_TAG_lexical_block)
9464 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9465 pdi = pdi->die_sibling;
9470 /* Read a partial die corresponding to an enumeration type. */
9473 add_partial_enumeration (struct partial_die_info *enum_pdi,
9474 struct dwarf2_cu *cu)
9476 struct partial_die_info *pdi;
9478 if (enum_pdi->name != NULL)
9479 add_partial_symbol (enum_pdi, cu);
9481 pdi = enum_pdi->die_child;
9484 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9485 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
9487 add_partial_symbol (pdi, cu);
9488 pdi = pdi->die_sibling;
9492 /* Return the initial uleb128 in the die at INFO_PTR. */
9495 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9497 unsigned int bytes_read;
9499 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9502 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
9503 Return the corresponding abbrev, or NULL if the number is zero (indicating
9504 an empty DIE). In either case *BYTES_READ will be set to the length of
9505 the initial number. */
9507 static struct abbrev_info *
9508 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
9509 struct dwarf2_cu *cu)
9511 bfd *abfd = cu->objfile->obfd;
9512 unsigned int abbrev_number;
9513 struct abbrev_info *abbrev;
9515 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9517 if (abbrev_number == 0)
9520 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
9523 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9524 " at offset 0x%x [in module %s]"),
9525 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9526 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
9532 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9533 Returns a pointer to the end of a series of DIEs, terminated by an empty
9534 DIE. Any children of the skipped DIEs will also be skipped. */
9536 static const gdb_byte *
9537 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9539 struct dwarf2_cu *cu = reader->cu;
9540 struct abbrev_info *abbrev;
9541 unsigned int bytes_read;
9545 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9547 return info_ptr + bytes_read;
9549 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9553 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9554 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9555 abbrev corresponding to that skipped uleb128 should be passed in
9556 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9559 static const gdb_byte *
9560 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9561 struct abbrev_info *abbrev)
9563 unsigned int bytes_read;
9564 struct attribute attr;
9565 bfd *abfd = reader->abfd;
9566 struct dwarf2_cu *cu = reader->cu;
9567 const gdb_byte *buffer = reader->buffer;
9568 const gdb_byte *buffer_end = reader->buffer_end;
9569 unsigned int form, i;
9571 for (i = 0; i < abbrev->num_attrs; i++)
9573 /* The only abbrev we care about is DW_AT_sibling. */
9574 if (abbrev->attrs[i].name == DW_AT_sibling)
9576 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9577 if (attr.form == DW_FORM_ref_addr)
9578 complaint (&symfile_complaints,
9579 _("ignoring absolute DW_AT_sibling"));
9582 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9583 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9585 if (sibling_ptr < info_ptr)
9586 complaint (&symfile_complaints,
9587 _("DW_AT_sibling points backwards"));
9588 else if (sibling_ptr > reader->buffer_end)
9589 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9595 /* If it isn't DW_AT_sibling, skip this attribute. */
9596 form = abbrev->attrs[i].form;
9600 case DW_FORM_ref_addr:
9601 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9602 and later it is offset sized. */
9603 if (cu->header.version == 2)
9604 info_ptr += cu->header.addr_size;
9606 info_ptr += cu->header.offset_size;
9608 case DW_FORM_GNU_ref_alt:
9609 info_ptr += cu->header.offset_size;
9612 info_ptr += cu->header.addr_size;
9619 case DW_FORM_flag_present:
9620 case DW_FORM_implicit_const:
9632 case DW_FORM_ref_sig8:
9635 case DW_FORM_data16:
9638 case DW_FORM_string:
9639 read_direct_string (abfd, info_ptr, &bytes_read);
9640 info_ptr += bytes_read;
9642 case DW_FORM_sec_offset:
9644 case DW_FORM_GNU_strp_alt:
9645 info_ptr += cu->header.offset_size;
9647 case DW_FORM_exprloc:
9649 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9650 info_ptr += bytes_read;
9652 case DW_FORM_block1:
9653 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9655 case DW_FORM_block2:
9656 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9658 case DW_FORM_block4:
9659 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9663 case DW_FORM_ref_udata:
9664 case DW_FORM_GNU_addr_index:
9665 case DW_FORM_GNU_str_index:
9666 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9668 case DW_FORM_indirect:
9669 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9670 info_ptr += bytes_read;
9671 /* We need to continue parsing from here, so just go back to
9673 goto skip_attribute;
9676 error (_("Dwarf Error: Cannot handle %s "
9677 "in DWARF reader [in module %s]"),
9678 dwarf_form_name (form),
9679 bfd_get_filename (abfd));
9683 if (abbrev->has_children)
9684 return skip_children (reader, info_ptr);
9689 /* Locate ORIG_PDI's sibling.
9690 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9692 static const gdb_byte *
9693 locate_pdi_sibling (const struct die_reader_specs *reader,
9694 struct partial_die_info *orig_pdi,
9695 const gdb_byte *info_ptr)
9697 /* Do we know the sibling already? */
9699 if (orig_pdi->sibling)
9700 return orig_pdi->sibling;
9702 /* Are there any children to deal with? */
9704 if (!orig_pdi->has_children)
9707 /* Skip the children the long way. */
9709 return skip_children (reader, info_ptr);
9712 /* Expand this partial symbol table into a full symbol table. SELF is
9716 dwarf2_read_symtab (struct partial_symtab *self,
9717 struct objfile *objfile)
9721 warning (_("bug: psymtab for %s is already read in."),
9728 printf_filtered (_("Reading in symbols for %s..."),
9730 gdb_flush (gdb_stdout);
9733 /* Restore our global data. */
9735 = (struct dwarf2_per_objfile *) objfile_data (objfile,
9736 dwarf2_objfile_data_key);
9738 /* If this psymtab is constructed from a debug-only objfile, the
9739 has_section_at_zero flag will not necessarily be correct. We
9740 can get the correct value for this flag by looking at the data
9741 associated with the (presumably stripped) associated objfile. */
9742 if (objfile->separate_debug_objfile_backlink)
9744 struct dwarf2_per_objfile *dpo_backlink
9745 = ((struct dwarf2_per_objfile *)
9746 objfile_data (objfile->separate_debug_objfile_backlink,
9747 dwarf2_objfile_data_key));
9749 dwarf2_per_objfile->has_section_at_zero
9750 = dpo_backlink->has_section_at_zero;
9753 dwarf2_per_objfile->reading_partial_symbols = 0;
9755 psymtab_to_symtab_1 (self);
9757 /* Finish up the debug error message. */
9759 printf_filtered (_("done.\n"));
9762 process_cu_includes ();
9765 /* Reading in full CUs. */
9767 /* Add PER_CU to the queue. */
9770 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9771 enum language pretend_language)
9773 struct dwarf2_queue_item *item;
9776 item = XNEW (struct dwarf2_queue_item);
9777 item->per_cu = per_cu;
9778 item->pretend_language = pretend_language;
9781 if (dwarf2_queue == NULL)
9782 dwarf2_queue = item;
9784 dwarf2_queue_tail->next = item;
9786 dwarf2_queue_tail = item;
9789 /* If PER_CU is not yet queued, add it to the queue.
9790 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9792 The result is non-zero if PER_CU was queued, otherwise the result is zero
9793 meaning either PER_CU is already queued or it is already loaded.
9795 N.B. There is an invariant here that if a CU is queued then it is loaded.
9796 The caller is required to load PER_CU if we return non-zero. */
9799 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9800 struct dwarf2_per_cu_data *per_cu,
9801 enum language pretend_language)
9803 /* We may arrive here during partial symbol reading, if we need full
9804 DIEs to process an unusual case (e.g. template arguments). Do
9805 not queue PER_CU, just tell our caller to load its DIEs. */
9806 if (dwarf2_per_objfile->reading_partial_symbols)
9808 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9813 /* Mark the dependence relation so that we don't flush PER_CU
9815 if (dependent_cu != NULL)
9816 dwarf2_add_dependence (dependent_cu, per_cu);
9818 /* If it's already on the queue, we have nothing to do. */
9822 /* If the compilation unit is already loaded, just mark it as
9824 if (per_cu->cu != NULL)
9826 per_cu->cu->last_used = 0;
9830 /* Add it to the queue. */
9831 queue_comp_unit (per_cu, pretend_language);
9836 /* Process the queue. */
9839 process_queue (void)
9841 struct dwarf2_queue_item *item, *next_item;
9843 if (dwarf_read_debug)
9845 fprintf_unfiltered (gdb_stdlog,
9846 "Expanding one or more symtabs of objfile %s ...\n",
9847 objfile_name (dwarf2_per_objfile->objfile));
9850 /* The queue starts out with one item, but following a DIE reference
9851 may load a new CU, adding it to the end of the queue. */
9852 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9854 if ((dwarf2_per_objfile->using_index
9855 ? !item->per_cu->v.quick->compunit_symtab
9856 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9857 /* Skip dummy CUs. */
9858 && item->per_cu->cu != NULL)
9860 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9861 unsigned int debug_print_threshold;
9864 if (per_cu->is_debug_types)
9866 struct signatured_type *sig_type =
9867 (struct signatured_type *) per_cu;
9869 sprintf (buf, "TU %s at offset 0x%x",
9870 hex_string (sig_type->signature),
9871 to_underlying (per_cu->sect_off));
9872 /* There can be 100s of TUs.
9873 Only print them in verbose mode. */
9874 debug_print_threshold = 2;
9878 sprintf (buf, "CU at offset 0x%x",
9879 to_underlying (per_cu->sect_off));
9880 debug_print_threshold = 1;
9883 if (dwarf_read_debug >= debug_print_threshold)
9884 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9886 if (per_cu->is_debug_types)
9887 process_full_type_unit (per_cu, item->pretend_language);
9889 process_full_comp_unit (per_cu, item->pretend_language);
9891 if (dwarf_read_debug >= debug_print_threshold)
9892 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9895 item->per_cu->queued = 0;
9896 next_item = item->next;
9900 dwarf2_queue_tail = NULL;
9902 if (dwarf_read_debug)
9904 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9905 objfile_name (dwarf2_per_objfile->objfile));
9909 /* Free all allocated queue entries. This function only releases anything if
9910 an error was thrown; if the queue was processed then it would have been
9911 freed as we went along. */
9914 dwarf2_release_queue (void *dummy)
9916 struct dwarf2_queue_item *item, *last;
9918 item = dwarf2_queue;
9921 /* Anything still marked queued is likely to be in an
9922 inconsistent state, so discard it. */
9923 if (item->per_cu->queued)
9925 if (item->per_cu->cu != NULL)
9926 free_one_cached_comp_unit (item->per_cu);
9927 item->per_cu->queued = 0;
9935 dwarf2_queue = dwarf2_queue_tail = NULL;
9938 /* Read in full symbols for PST, and anything it depends on. */
9941 psymtab_to_symtab_1 (struct partial_symtab *pst)
9943 struct dwarf2_per_cu_data *per_cu;
9949 for (i = 0; i < pst->number_of_dependencies; i++)
9950 if (!pst->dependencies[i]->readin
9951 && pst->dependencies[i]->user == NULL)
9953 /* Inform about additional files that need to be read in. */
9956 /* FIXME: i18n: Need to make this a single string. */
9957 fputs_filtered (" ", gdb_stdout);
9959 fputs_filtered ("and ", gdb_stdout);
9961 printf_filtered ("%s...", pst->dependencies[i]->filename);
9962 wrap_here (""); /* Flush output. */
9963 gdb_flush (gdb_stdout);
9965 psymtab_to_symtab_1 (pst->dependencies[i]);
9968 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9972 /* It's an include file, no symbols to read for it.
9973 Everything is in the parent symtab. */
9978 dw2_do_instantiate_symtab (per_cu);
9981 /* Trivial hash function for die_info: the hash value of a DIE
9982 is its offset in .debug_info for this objfile. */
9985 die_hash (const void *item)
9987 const struct die_info *die = (const struct die_info *) item;
9989 return to_underlying (die->sect_off);
9992 /* Trivial comparison function for die_info structures: two DIEs
9993 are equal if they have the same offset. */
9996 die_eq (const void *item_lhs, const void *item_rhs)
9998 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9999 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
10001 return die_lhs->sect_off == die_rhs->sect_off;
10004 /* die_reader_func for load_full_comp_unit.
10005 This is identical to read_signatured_type_reader,
10006 but is kept separate for now. */
10009 load_full_comp_unit_reader (const struct die_reader_specs *reader,
10010 const gdb_byte *info_ptr,
10011 struct die_info *comp_unit_die,
10015 struct dwarf2_cu *cu = reader->cu;
10016 enum language *language_ptr = (enum language *) data;
10018 gdb_assert (cu->die_hash == NULL);
10020 htab_create_alloc_ex (cu->header.length / 12,
10024 &cu->comp_unit_obstack,
10025 hashtab_obstack_allocate,
10026 dummy_obstack_deallocate);
10029 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
10030 &info_ptr, comp_unit_die);
10031 cu->dies = comp_unit_die;
10032 /* comp_unit_die is not stored in die_hash, no need. */
10034 /* We try not to read any attributes in this function, because not
10035 all CUs needed for references have been loaded yet, and symbol
10036 table processing isn't initialized. But we have to set the CU language,
10037 or we won't be able to build types correctly.
10038 Similarly, if we do not read the producer, we can not apply
10039 producer-specific interpretation. */
10040 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
10043 /* Load the DIEs associated with PER_CU into memory. */
10046 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
10047 enum language pretend_language)
10049 gdb_assert (! this_cu->is_debug_types);
10051 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
10052 load_full_comp_unit_reader, &pretend_language);
10055 /* Add a DIE to the delayed physname list. */
10058 add_to_method_list (struct type *type, int fnfield_index, int index,
10059 const char *name, struct die_info *die,
10060 struct dwarf2_cu *cu)
10062 struct delayed_method_info mi;
10064 mi.fnfield_index = fnfield_index;
10068 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
10071 /* A cleanup for freeing the delayed method list. */
10074 free_delayed_list (void *ptr)
10076 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
10077 if (cu->method_list != NULL)
10079 VEC_free (delayed_method_info, cu->method_list);
10080 cu->method_list = NULL;
10084 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
10085 "const" / "volatile". If so, decrements LEN by the length of the
10086 modifier and return true. Otherwise return false. */
10090 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
10092 size_t mod_len = sizeof (mod) - 1;
10093 if (len > mod_len && startswith (physname + (len - mod_len), mod))
10101 /* Compute the physnames of any methods on the CU's method list.
10103 The computation of method physnames is delayed in order to avoid the
10104 (bad) condition that one of the method's formal parameters is of an as yet
10105 incomplete type. */
10108 compute_delayed_physnames (struct dwarf2_cu *cu)
10111 struct delayed_method_info *mi;
10113 /* Only C++ delays computing physnames. */
10114 if (VEC_empty (delayed_method_info, cu->method_list))
10116 gdb_assert (cu->language == language_cplus);
10118 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
10120 const char *physname;
10121 struct fn_fieldlist *fn_flp
10122 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
10123 physname = dwarf2_physname (mi->name, mi->die, cu);
10124 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
10125 = physname ? physname : "";
10127 /* Since there's no tag to indicate whether a method is a
10128 const/volatile overload, extract that information out of the
10130 if (physname != NULL)
10132 size_t len = strlen (physname);
10136 if (physname[len] == ')') /* shortcut */
10138 else if (check_modifier (physname, len, " const"))
10139 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi->index) = 1;
10140 else if (check_modifier (physname, len, " volatile"))
10141 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi->index) = 1;
10149 /* Go objects should be embedded in a DW_TAG_module DIE,
10150 and it's not clear if/how imported objects will appear.
10151 To keep Go support simple until that's worked out,
10152 go back through what we've read and create something usable.
10153 We could do this while processing each DIE, and feels kinda cleaner,
10154 but that way is more invasive.
10155 This is to, for example, allow the user to type "p var" or "b main"
10156 without having to specify the package name, and allow lookups
10157 of module.object to work in contexts that use the expression
10161 fixup_go_packaging (struct dwarf2_cu *cu)
10163 char *package_name = NULL;
10164 struct pending *list;
10167 for (list = global_symbols; list != NULL; list = list->next)
10169 for (i = 0; i < list->nsyms; ++i)
10171 struct symbol *sym = list->symbol[i];
10173 if (SYMBOL_LANGUAGE (sym) == language_go
10174 && SYMBOL_CLASS (sym) == LOC_BLOCK)
10176 char *this_package_name = go_symbol_package_name (sym);
10178 if (this_package_name == NULL)
10180 if (package_name == NULL)
10181 package_name = this_package_name;
10184 if (strcmp (package_name, this_package_name) != 0)
10185 complaint (&symfile_complaints,
10186 _("Symtab %s has objects from two different Go packages: %s and %s"),
10187 (symbol_symtab (sym) != NULL
10188 ? symtab_to_filename_for_display
10189 (symbol_symtab (sym))
10190 : objfile_name (cu->objfile)),
10191 this_package_name, package_name);
10192 xfree (this_package_name);
10198 if (package_name != NULL)
10200 struct objfile *objfile = cu->objfile;
10201 const char *saved_package_name
10202 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
10204 strlen (package_name));
10205 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
10206 saved_package_name);
10207 struct symbol *sym;
10209 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10211 sym = allocate_symbol (objfile);
10212 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
10213 SYMBOL_SET_NAMES (sym, saved_package_name,
10214 strlen (saved_package_name), 0, objfile);
10215 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
10216 e.g., "main" finds the "main" module and not C's main(). */
10217 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10218 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
10219 SYMBOL_TYPE (sym) = type;
10221 add_symbol_to_list (sym, &global_symbols);
10223 xfree (package_name);
10227 /* Return the symtab for PER_CU. This works properly regardless of
10228 whether we're using the index or psymtabs. */
10230 static struct compunit_symtab *
10231 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10233 return (dwarf2_per_objfile->using_index
10234 ? per_cu->v.quick->compunit_symtab
10235 : per_cu->v.psymtab->compunit_symtab);
10238 /* A helper function for computing the list of all symbol tables
10239 included by PER_CU. */
10242 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
10243 htab_t all_children, htab_t all_type_symtabs,
10244 struct dwarf2_per_cu_data *per_cu,
10245 struct compunit_symtab *immediate_parent)
10249 struct compunit_symtab *cust;
10250 struct dwarf2_per_cu_data *iter;
10252 slot = htab_find_slot (all_children, per_cu, INSERT);
10255 /* This inclusion and its children have been processed. */
10260 /* Only add a CU if it has a symbol table. */
10261 cust = get_compunit_symtab (per_cu);
10264 /* If this is a type unit only add its symbol table if we haven't
10265 seen it yet (type unit per_cu's can share symtabs). */
10266 if (per_cu->is_debug_types)
10268 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10272 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10273 if (cust->user == NULL)
10274 cust->user = immediate_parent;
10279 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10280 if (cust->user == NULL)
10281 cust->user = immediate_parent;
10286 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10289 recursively_compute_inclusions (result, all_children,
10290 all_type_symtabs, iter, cust);
10294 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10298 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10300 gdb_assert (! per_cu->is_debug_types);
10302 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10305 struct dwarf2_per_cu_data *per_cu_iter;
10306 struct compunit_symtab *compunit_symtab_iter;
10307 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
10308 htab_t all_children, all_type_symtabs;
10309 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10311 /* If we don't have a symtab, we can just skip this case. */
10315 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10316 NULL, xcalloc, xfree);
10317 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10318 NULL, xcalloc, xfree);
10321 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10325 recursively_compute_inclusions (&result_symtabs, all_children,
10326 all_type_symtabs, per_cu_iter,
10330 /* Now we have a transitive closure of all the included symtabs. */
10331 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10333 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
10334 struct compunit_symtab *, len + 1);
10336 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10337 compunit_symtab_iter);
10339 cust->includes[ix] = compunit_symtab_iter;
10340 cust->includes[len] = NULL;
10342 VEC_free (compunit_symtab_ptr, result_symtabs);
10343 htab_delete (all_children);
10344 htab_delete (all_type_symtabs);
10348 /* Compute the 'includes' field for the symtabs of all the CUs we just
10352 process_cu_includes (void)
10355 struct dwarf2_per_cu_data *iter;
10358 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10362 if (! iter->is_debug_types)
10363 compute_compunit_symtab_includes (iter);
10366 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10369 /* Generate full symbol information for PER_CU, whose DIEs have
10370 already been loaded into memory. */
10373 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10374 enum language pretend_language)
10376 struct dwarf2_cu *cu = per_cu->cu;
10377 struct objfile *objfile = per_cu->objfile;
10378 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10379 CORE_ADDR lowpc, highpc;
10380 struct compunit_symtab *cust;
10381 struct cleanup *delayed_list_cleanup;
10382 CORE_ADDR baseaddr;
10383 struct block *static_block;
10386 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10389 scoped_free_pendings free_pending;
10390 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10392 cu->list_in_scope = &file_symbols;
10394 cu->language = pretend_language;
10395 cu->language_defn = language_def (cu->language);
10397 /* Do line number decoding in read_file_scope () */
10398 process_die (cu->dies, cu);
10400 /* For now fudge the Go package. */
10401 if (cu->language == language_go)
10402 fixup_go_packaging (cu);
10404 /* Now that we have processed all the DIEs in the CU, all the types
10405 should be complete, and it should now be safe to compute all of the
10407 compute_delayed_physnames (cu);
10408 do_cleanups (delayed_list_cleanup);
10410 /* Some compilers don't define a DW_AT_high_pc attribute for the
10411 compilation unit. If the DW_AT_high_pc is missing, synthesize
10412 it, by scanning the DIE's below the compilation unit. */
10413 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10415 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10416 static_block = end_symtab_get_static_block (addr, 0, 1);
10418 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10419 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10420 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10421 addrmap to help ensure it has an accurate map of pc values belonging to
10423 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10425 cust = end_symtab_from_static_block (static_block,
10426 SECT_OFF_TEXT (objfile), 0);
10430 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10432 /* Set symtab language to language from DW_AT_language. If the
10433 compilation is from a C file generated by language preprocessors, do
10434 not set the language if it was already deduced by start_subfile. */
10435 if (!(cu->language == language_c
10436 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10437 COMPUNIT_FILETABS (cust)->language = cu->language;
10439 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10440 produce DW_AT_location with location lists but it can be possibly
10441 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10442 there were bugs in prologue debug info, fixed later in GCC-4.5
10443 by "unwind info for epilogues" patch (which is not directly related).
10445 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10446 needed, it would be wrong due to missing DW_AT_producer there.
10448 Still one can confuse GDB by using non-standard GCC compilation
10449 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10451 if (cu->has_loclist && gcc_4_minor >= 5)
10452 cust->locations_valid = 1;
10454 if (gcc_4_minor >= 5)
10455 cust->epilogue_unwind_valid = 1;
10457 cust->call_site_htab = cu->call_site_htab;
10460 if (dwarf2_per_objfile->using_index)
10461 per_cu->v.quick->compunit_symtab = cust;
10464 struct partial_symtab *pst = per_cu->v.psymtab;
10465 pst->compunit_symtab = cust;
10469 /* Push it for inclusion processing later. */
10470 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
10473 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10474 already been loaded into memory. */
10477 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10478 enum language pretend_language)
10480 struct dwarf2_cu *cu = per_cu->cu;
10481 struct objfile *objfile = per_cu->objfile;
10482 struct compunit_symtab *cust;
10483 struct cleanup *delayed_list_cleanup;
10484 struct signatured_type *sig_type;
10486 gdb_assert (per_cu->is_debug_types);
10487 sig_type = (struct signatured_type *) per_cu;
10490 scoped_free_pendings free_pending;
10491 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10493 cu->list_in_scope = &file_symbols;
10495 cu->language = pretend_language;
10496 cu->language_defn = language_def (cu->language);
10498 /* The symbol tables are set up in read_type_unit_scope. */
10499 process_die (cu->dies, cu);
10501 /* For now fudge the Go package. */
10502 if (cu->language == language_go)
10503 fixup_go_packaging (cu);
10505 /* Now that we have processed all the DIEs in the CU, all the types
10506 should be complete, and it should now be safe to compute all of the
10508 compute_delayed_physnames (cu);
10509 do_cleanups (delayed_list_cleanup);
10511 /* TUs share symbol tables.
10512 If this is the first TU to use this symtab, complete the construction
10513 of it with end_expandable_symtab. Otherwise, complete the addition of
10514 this TU's symbols to the existing symtab. */
10515 if (sig_type->type_unit_group->compunit_symtab == NULL)
10517 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10518 sig_type->type_unit_group->compunit_symtab = cust;
10522 /* Set symtab language to language from DW_AT_language. If the
10523 compilation is from a C file generated by language preprocessors,
10524 do not set the language if it was already deduced by
10526 if (!(cu->language == language_c
10527 && COMPUNIT_FILETABS (cust)->language != language_c))
10528 COMPUNIT_FILETABS (cust)->language = cu->language;
10533 augment_type_symtab ();
10534 cust = sig_type->type_unit_group->compunit_symtab;
10537 if (dwarf2_per_objfile->using_index)
10538 per_cu->v.quick->compunit_symtab = cust;
10541 struct partial_symtab *pst = per_cu->v.psymtab;
10542 pst->compunit_symtab = cust;
10547 /* Process an imported unit DIE. */
10550 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10552 struct attribute *attr;
10554 /* For now we don't handle imported units in type units. */
10555 if (cu->per_cu->is_debug_types)
10557 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10558 " supported in type units [in module %s]"),
10559 objfile_name (cu->objfile));
10562 attr = dwarf2_attr (die, DW_AT_import, cu);
10565 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10566 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10567 dwarf2_per_cu_data *per_cu
10568 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
10570 /* If necessary, add it to the queue and load its DIEs. */
10571 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10572 load_full_comp_unit (per_cu, cu->language);
10574 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10579 /* RAII object that represents a process_die scope: i.e.,
10580 starts/finishes processing a DIE. */
10581 class process_die_scope
10584 process_die_scope (die_info *die, dwarf2_cu *cu)
10585 : m_die (die), m_cu (cu)
10587 /* We should only be processing DIEs not already in process. */
10588 gdb_assert (!m_die->in_process);
10589 m_die->in_process = true;
10592 ~process_die_scope ()
10594 m_die->in_process = false;
10596 /* If we're done processing the DIE for the CU that owns the line
10597 header, we don't need the line header anymore. */
10598 if (m_cu->line_header_die_owner == m_die)
10600 delete m_cu->line_header;
10601 m_cu->line_header = NULL;
10602 m_cu->line_header_die_owner = NULL;
10611 /* Process a die and its children. */
10614 process_die (struct die_info *die, struct dwarf2_cu *cu)
10616 process_die_scope scope (die, cu);
10620 case DW_TAG_padding:
10622 case DW_TAG_compile_unit:
10623 case DW_TAG_partial_unit:
10624 read_file_scope (die, cu);
10626 case DW_TAG_type_unit:
10627 read_type_unit_scope (die, cu);
10629 case DW_TAG_subprogram:
10630 case DW_TAG_inlined_subroutine:
10631 read_func_scope (die, cu);
10633 case DW_TAG_lexical_block:
10634 case DW_TAG_try_block:
10635 case DW_TAG_catch_block:
10636 read_lexical_block_scope (die, cu);
10638 case DW_TAG_call_site:
10639 case DW_TAG_GNU_call_site:
10640 read_call_site_scope (die, cu);
10642 case DW_TAG_class_type:
10643 case DW_TAG_interface_type:
10644 case DW_TAG_structure_type:
10645 case DW_TAG_union_type:
10646 process_structure_scope (die, cu);
10648 case DW_TAG_enumeration_type:
10649 process_enumeration_scope (die, cu);
10652 /* These dies have a type, but processing them does not create
10653 a symbol or recurse to process the children. Therefore we can
10654 read them on-demand through read_type_die. */
10655 case DW_TAG_subroutine_type:
10656 case DW_TAG_set_type:
10657 case DW_TAG_array_type:
10658 case DW_TAG_pointer_type:
10659 case DW_TAG_ptr_to_member_type:
10660 case DW_TAG_reference_type:
10661 case DW_TAG_rvalue_reference_type:
10662 case DW_TAG_string_type:
10665 case DW_TAG_base_type:
10666 case DW_TAG_subrange_type:
10667 case DW_TAG_typedef:
10668 /* Add a typedef symbol for the type definition, if it has a
10670 new_symbol (die, read_type_die (die, cu), cu);
10672 case DW_TAG_common_block:
10673 read_common_block (die, cu);
10675 case DW_TAG_common_inclusion:
10677 case DW_TAG_namespace:
10678 cu->processing_has_namespace_info = 1;
10679 read_namespace (die, cu);
10681 case DW_TAG_module:
10682 cu->processing_has_namespace_info = 1;
10683 read_module (die, cu);
10685 case DW_TAG_imported_declaration:
10686 cu->processing_has_namespace_info = 1;
10687 if (read_namespace_alias (die, cu))
10689 /* The declaration is not a global namespace alias: fall through. */
10690 case DW_TAG_imported_module:
10691 cu->processing_has_namespace_info = 1;
10692 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10693 || cu->language != language_fortran))
10694 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10695 dwarf_tag_name (die->tag));
10696 read_import_statement (die, cu);
10699 case DW_TAG_imported_unit:
10700 process_imported_unit_die (die, cu);
10703 case DW_TAG_variable:
10704 read_variable (die, cu);
10708 new_symbol (die, NULL, cu);
10713 /* DWARF name computation. */
10715 /* A helper function for dwarf2_compute_name which determines whether DIE
10716 needs to have the name of the scope prepended to the name listed in the
10720 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10722 struct attribute *attr;
10726 case DW_TAG_namespace:
10727 case DW_TAG_typedef:
10728 case DW_TAG_class_type:
10729 case DW_TAG_interface_type:
10730 case DW_TAG_structure_type:
10731 case DW_TAG_union_type:
10732 case DW_TAG_enumeration_type:
10733 case DW_TAG_enumerator:
10734 case DW_TAG_subprogram:
10735 case DW_TAG_inlined_subroutine:
10736 case DW_TAG_member:
10737 case DW_TAG_imported_declaration:
10740 case DW_TAG_variable:
10741 case DW_TAG_constant:
10742 /* We only need to prefix "globally" visible variables. These include
10743 any variable marked with DW_AT_external or any variable that
10744 lives in a namespace. [Variables in anonymous namespaces
10745 require prefixing, but they are not DW_AT_external.] */
10747 if (dwarf2_attr (die, DW_AT_specification, cu))
10749 struct dwarf2_cu *spec_cu = cu;
10751 return die_needs_namespace (die_specification (die, &spec_cu),
10755 attr = dwarf2_attr (die, DW_AT_external, cu);
10756 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10757 && die->parent->tag != DW_TAG_module)
10759 /* A variable in a lexical block of some kind does not need a
10760 namespace, even though in C++ such variables may be external
10761 and have a mangled name. */
10762 if (die->parent->tag == DW_TAG_lexical_block
10763 || die->parent->tag == DW_TAG_try_block
10764 || die->parent->tag == DW_TAG_catch_block
10765 || die->parent->tag == DW_TAG_subprogram)
10774 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10775 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10776 defined for the given DIE. */
10778 static struct attribute *
10779 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10781 struct attribute *attr;
10783 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10785 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10790 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10791 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10792 defined for the given DIE. */
10794 static const char *
10795 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10797 const char *linkage_name;
10799 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10800 if (linkage_name == NULL)
10801 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10803 return linkage_name;
10806 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10807 compute the physname for the object, which include a method's:
10808 - formal parameters (C++),
10809 - receiver type (Go),
10811 The term "physname" is a bit confusing.
10812 For C++, for example, it is the demangled name.
10813 For Go, for example, it's the mangled name.
10815 For Ada, return the DIE's linkage name rather than the fully qualified
10816 name. PHYSNAME is ignored..
10818 The result is allocated on the objfile_obstack and canonicalized. */
10820 static const char *
10821 dwarf2_compute_name (const char *name,
10822 struct die_info *die, struct dwarf2_cu *cu,
10825 struct objfile *objfile = cu->objfile;
10828 name = dwarf2_name (die, cu);
10830 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10831 but otherwise compute it by typename_concat inside GDB.
10832 FIXME: Actually this is not really true, or at least not always true.
10833 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10834 Fortran names because there is no mangling standard. So new_symbol_full
10835 will set the demangled name to the result of dwarf2_full_name, and it is
10836 the demangled name that GDB uses if it exists. */
10837 if (cu->language == language_ada
10838 || (cu->language == language_fortran && physname))
10840 /* For Ada unit, we prefer the linkage name over the name, as
10841 the former contains the exported name, which the user expects
10842 to be able to reference. Ideally, we want the user to be able
10843 to reference this entity using either natural or linkage name,
10844 but we haven't started looking at this enhancement yet. */
10845 const char *linkage_name = dw2_linkage_name (die, cu);
10847 if (linkage_name != NULL)
10848 return linkage_name;
10851 /* These are the only languages we know how to qualify names in. */
10853 && (cu->language == language_cplus
10854 || cu->language == language_fortran || cu->language == language_d
10855 || cu->language == language_rust))
10857 if (die_needs_namespace (die, cu))
10859 const char *prefix;
10860 const char *canonical_name = NULL;
10864 prefix = determine_prefix (die, cu);
10865 if (*prefix != '\0')
10867 char *prefixed_name = typename_concat (NULL, prefix, name,
10870 buf.puts (prefixed_name);
10871 xfree (prefixed_name);
10876 /* Template parameters may be specified in the DIE's DW_AT_name, or
10877 as children with DW_TAG_template_type_param or
10878 DW_TAG_value_type_param. If the latter, add them to the name
10879 here. If the name already has template parameters, then
10880 skip this step; some versions of GCC emit both, and
10881 it is more efficient to use the pre-computed name.
10883 Something to keep in mind about this process: it is very
10884 unlikely, or in some cases downright impossible, to produce
10885 something that will match the mangled name of a function.
10886 If the definition of the function has the same debug info,
10887 we should be able to match up with it anyway. But fallbacks
10888 using the minimal symbol, for instance to find a method
10889 implemented in a stripped copy of libstdc++, will not work.
10890 If we do not have debug info for the definition, we will have to
10891 match them up some other way.
10893 When we do name matching there is a related problem with function
10894 templates; two instantiated function templates are allowed to
10895 differ only by their return types, which we do not add here. */
10897 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10899 struct attribute *attr;
10900 struct die_info *child;
10903 die->building_fullname = 1;
10905 for (child = die->child; child != NULL; child = child->sibling)
10909 const gdb_byte *bytes;
10910 struct dwarf2_locexpr_baton *baton;
10913 if (child->tag != DW_TAG_template_type_param
10914 && child->tag != DW_TAG_template_value_param)
10925 attr = dwarf2_attr (child, DW_AT_type, cu);
10928 complaint (&symfile_complaints,
10929 _("template parameter missing DW_AT_type"));
10930 buf.puts ("UNKNOWN_TYPE");
10933 type = die_type (child, cu);
10935 if (child->tag == DW_TAG_template_type_param)
10937 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
10941 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10944 complaint (&symfile_complaints,
10945 _("template parameter missing "
10946 "DW_AT_const_value"));
10947 buf.puts ("UNKNOWN_VALUE");
10951 dwarf2_const_value_attr (attr, type, name,
10952 &cu->comp_unit_obstack, cu,
10953 &value, &bytes, &baton);
10955 if (TYPE_NOSIGN (type))
10956 /* GDB prints characters as NUMBER 'CHAR'. If that's
10957 changed, this can use value_print instead. */
10958 c_printchar (value, type, &buf);
10961 struct value_print_options opts;
10964 v = dwarf2_evaluate_loc_desc (type, NULL,
10968 else if (bytes != NULL)
10970 v = allocate_value (type);
10971 memcpy (value_contents_writeable (v), bytes,
10972 TYPE_LENGTH (type));
10975 v = value_from_longest (type, value);
10977 /* Specify decimal so that we do not depend on
10979 get_formatted_print_options (&opts, 'd');
10981 value_print (v, &buf, &opts);
10987 die->building_fullname = 0;
10991 /* Close the argument list, with a space if necessary
10992 (nested templates). */
10993 if (!buf.empty () && buf.string ().back () == '>')
11000 /* For C++ methods, append formal parameter type
11001 information, if PHYSNAME. */
11003 if (physname && die->tag == DW_TAG_subprogram
11004 && cu->language == language_cplus)
11006 struct type *type = read_type_die (die, cu);
11008 c_type_print_args (type, &buf, 1, cu->language,
11009 &type_print_raw_options);
11011 if (cu->language == language_cplus)
11013 /* Assume that an artificial first parameter is
11014 "this", but do not crash if it is not. RealView
11015 marks unnamed (and thus unused) parameters as
11016 artificial; there is no way to differentiate
11018 if (TYPE_NFIELDS (type) > 0
11019 && TYPE_FIELD_ARTIFICIAL (type, 0)
11020 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11021 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11023 buf.puts (" const");
11027 const std::string &intermediate_name = buf.string ();
11029 if (cu->language == language_cplus)
11031 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11032 &objfile->per_bfd->storage_obstack);
11034 /* If we only computed INTERMEDIATE_NAME, or if
11035 INTERMEDIATE_NAME is already canonical, then we need to
11036 copy it to the appropriate obstack. */
11037 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11038 name = ((const char *)
11039 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11040 intermediate_name.c_str (),
11041 intermediate_name.length ()));
11043 name = canonical_name;
11050 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11051 If scope qualifiers are appropriate they will be added. The result
11052 will be allocated on the storage_obstack, or NULL if the DIE does
11053 not have a name. NAME may either be from a previous call to
11054 dwarf2_name or NULL.
11056 The output string will be canonicalized (if C++). */
11058 static const char *
11059 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11061 return dwarf2_compute_name (name, die, cu, 0);
11064 /* Construct a physname for the given DIE in CU. NAME may either be
11065 from a previous call to dwarf2_name or NULL. The result will be
11066 allocated on the objfile_objstack or NULL if the DIE does not have a
11069 The output string will be canonicalized (if C++). */
11071 static const char *
11072 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11074 struct objfile *objfile = cu->objfile;
11075 const char *retval, *mangled = NULL, *canon = NULL;
11078 /* In this case dwarf2_compute_name is just a shortcut not building anything
11080 if (!die_needs_namespace (die, cu))
11081 return dwarf2_compute_name (name, die, cu, 1);
11083 mangled = dw2_linkage_name (die, cu);
11085 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11086 See https://github.com/rust-lang/rust/issues/32925. */
11087 if (cu->language == language_rust && mangled != NULL
11088 && strchr (mangled, '{') != NULL)
11091 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11093 gdb::unique_xmalloc_ptr<char> demangled;
11094 if (mangled != NULL)
11096 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
11097 type. It is easier for GDB users to search for such functions as
11098 `name(params)' than `long name(params)'. In such case the minimal
11099 symbol names do not match the full symbol names but for template
11100 functions there is never a need to look up their definition from their
11101 declaration so the only disadvantage remains the minimal symbol
11102 variant `long name(params)' does not have the proper inferior type.
11105 if (cu->language == language_go)
11107 /* This is a lie, but we already lie to the caller new_symbol_full.
11108 new_symbol_full assumes we return the mangled name.
11109 This just undoes that lie until things are cleaned up. */
11113 demangled.reset (gdb_demangle (mangled,
11114 (DMGL_PARAMS | DMGL_ANSI
11115 | DMGL_RET_DROP)));
11118 canon = demangled.get ();
11126 if (canon == NULL || check_physname)
11128 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11130 if (canon != NULL && strcmp (physname, canon) != 0)
11132 /* It may not mean a bug in GDB. The compiler could also
11133 compute DW_AT_linkage_name incorrectly. But in such case
11134 GDB would need to be bug-to-bug compatible. */
11136 complaint (&symfile_complaints,
11137 _("Computed physname <%s> does not match demangled <%s> "
11138 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
11139 physname, canon, mangled, to_underlying (die->sect_off),
11140 objfile_name (objfile));
11142 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11143 is available here - over computed PHYSNAME. It is safer
11144 against both buggy GDB and buggy compilers. */
11158 retval = ((const char *)
11159 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11160 retval, strlen (retval)));
11165 /* Inspect DIE in CU for a namespace alias. If one exists, record
11166 a new symbol for it.
11168 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11171 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11173 struct attribute *attr;
11175 /* If the die does not have a name, this is not a namespace
11177 attr = dwarf2_attr (die, DW_AT_name, cu);
11181 struct die_info *d = die;
11182 struct dwarf2_cu *imported_cu = cu;
11184 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11185 keep inspecting DIEs until we hit the underlying import. */
11186 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11187 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11189 attr = dwarf2_attr (d, DW_AT_import, cu);
11193 d = follow_die_ref (d, attr, &imported_cu);
11194 if (d->tag != DW_TAG_imported_declaration)
11198 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11200 complaint (&symfile_complaints,
11201 _("DIE at 0x%x has too many recursively imported "
11202 "declarations"), to_underlying (d->sect_off));
11209 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11211 type = get_die_type_at_offset (sect_off, cu->per_cu);
11212 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11214 /* This declaration is a global namespace alias. Add
11215 a symbol for it whose type is the aliased namespace. */
11216 new_symbol (die, type, cu);
11225 /* Return the using directives repository (global or local?) to use in the
11226 current context for LANGUAGE.
11228 For Ada, imported declarations can materialize renamings, which *may* be
11229 global. However it is impossible (for now?) in DWARF to distinguish
11230 "external" imported declarations and "static" ones. As all imported
11231 declarations seem to be static in all other languages, make them all CU-wide
11232 global only in Ada. */
11234 static struct using_direct **
11235 using_directives (enum language language)
11237 if (language == language_ada && context_stack_depth == 0)
11238 return &global_using_directives;
11240 return &local_using_directives;
11243 /* Read the import statement specified by the given die and record it. */
11246 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11248 struct objfile *objfile = cu->objfile;
11249 struct attribute *import_attr;
11250 struct die_info *imported_die, *child_die;
11251 struct dwarf2_cu *imported_cu;
11252 const char *imported_name;
11253 const char *imported_name_prefix;
11254 const char *canonical_name;
11255 const char *import_alias;
11256 const char *imported_declaration = NULL;
11257 const char *import_prefix;
11258 std::vector<const char *> excludes;
11260 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11261 if (import_attr == NULL)
11263 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11264 dwarf_tag_name (die->tag));
11269 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11270 imported_name = dwarf2_name (imported_die, imported_cu);
11271 if (imported_name == NULL)
11273 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11275 The import in the following code:
11289 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11290 <52> DW_AT_decl_file : 1
11291 <53> DW_AT_decl_line : 6
11292 <54> DW_AT_import : <0x75>
11293 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11294 <59> DW_AT_name : B
11295 <5b> DW_AT_decl_file : 1
11296 <5c> DW_AT_decl_line : 2
11297 <5d> DW_AT_type : <0x6e>
11299 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11300 <76> DW_AT_byte_size : 4
11301 <77> DW_AT_encoding : 5 (signed)
11303 imports the wrong die ( 0x75 instead of 0x58 ).
11304 This case will be ignored until the gcc bug is fixed. */
11308 /* Figure out the local name after import. */
11309 import_alias = dwarf2_name (die, cu);
11311 /* Figure out where the statement is being imported to. */
11312 import_prefix = determine_prefix (die, cu);
11314 /* Figure out what the scope of the imported die is and prepend it
11315 to the name of the imported die. */
11316 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11318 if (imported_die->tag != DW_TAG_namespace
11319 && imported_die->tag != DW_TAG_module)
11321 imported_declaration = imported_name;
11322 canonical_name = imported_name_prefix;
11324 else if (strlen (imported_name_prefix) > 0)
11325 canonical_name = obconcat (&objfile->objfile_obstack,
11326 imported_name_prefix,
11327 (cu->language == language_d ? "." : "::"),
11328 imported_name, (char *) NULL);
11330 canonical_name = imported_name;
11332 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11333 for (child_die = die->child; child_die && child_die->tag;
11334 child_die = sibling_die (child_die))
11336 /* DWARF-4: A Fortran use statement with a “rename list” may be
11337 represented by an imported module entry with an import attribute
11338 referring to the module and owned entries corresponding to those
11339 entities that are renamed as part of being imported. */
11341 if (child_die->tag != DW_TAG_imported_declaration)
11343 complaint (&symfile_complaints,
11344 _("child DW_TAG_imported_declaration expected "
11345 "- DIE at 0x%x [in module %s]"),
11346 to_underlying (child_die->sect_off), objfile_name (objfile));
11350 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11351 if (import_attr == NULL)
11353 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11354 dwarf_tag_name (child_die->tag));
11359 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11361 imported_name = dwarf2_name (imported_die, imported_cu);
11362 if (imported_name == NULL)
11364 complaint (&symfile_complaints,
11365 _("child DW_TAG_imported_declaration has unknown "
11366 "imported name - DIE at 0x%x [in module %s]"),
11367 to_underlying (child_die->sect_off), objfile_name (objfile));
11371 excludes.push_back (imported_name);
11373 process_die (child_die, cu);
11376 add_using_directive (using_directives (cu->language),
11380 imported_declaration,
11383 &objfile->objfile_obstack);
11386 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11387 types, but gives them a size of zero. Starting with version 14,
11388 ICC is compatible with GCC. */
11391 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11393 if (!cu->checked_producer)
11394 check_producer (cu);
11396 return cu->producer_is_icc_lt_14;
11399 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11400 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11401 this, it was first present in GCC release 4.3.0. */
11404 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11406 if (!cu->checked_producer)
11407 check_producer (cu);
11409 return cu->producer_is_gcc_lt_4_3;
11412 static file_and_directory
11413 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11415 file_and_directory res;
11417 /* Find the filename. Do not use dwarf2_name here, since the filename
11418 is not a source language identifier. */
11419 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11420 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11422 if (res.comp_dir == NULL
11423 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11424 && IS_ABSOLUTE_PATH (res.name))
11426 res.comp_dir_storage = ldirname (res.name);
11427 if (!res.comp_dir_storage.empty ())
11428 res.comp_dir = res.comp_dir_storage.c_str ();
11430 if (res.comp_dir != NULL)
11432 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11433 directory, get rid of it. */
11434 const char *cp = strchr (res.comp_dir, ':');
11436 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11437 res.comp_dir = cp + 1;
11440 if (res.name == NULL)
11441 res.name = "<unknown>";
11446 /* Handle DW_AT_stmt_list for a compilation unit.
11447 DIE is the DW_TAG_compile_unit die for CU.
11448 COMP_DIR is the compilation directory. LOWPC is passed to
11449 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11452 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11453 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11455 struct objfile *objfile = dwarf2_per_objfile->objfile;
11456 struct attribute *attr;
11457 struct line_header line_header_local;
11458 hashval_t line_header_local_hash;
11460 int decode_mapping;
11462 gdb_assert (! cu->per_cu->is_debug_types);
11464 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11468 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11470 /* The line header hash table is only created if needed (it exists to
11471 prevent redundant reading of the line table for partial_units).
11472 If we're given a partial_unit, we'll need it. If we're given a
11473 compile_unit, then use the line header hash table if it's already
11474 created, but don't create one just yet. */
11476 if (dwarf2_per_objfile->line_header_hash == NULL
11477 && die->tag == DW_TAG_partial_unit)
11479 dwarf2_per_objfile->line_header_hash
11480 = htab_create_alloc_ex (127, line_header_hash_voidp,
11481 line_header_eq_voidp,
11482 free_line_header_voidp,
11483 &objfile->objfile_obstack,
11484 hashtab_obstack_allocate,
11485 dummy_obstack_deallocate);
11488 line_header_local.sect_off = line_offset;
11489 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11490 line_header_local_hash = line_header_hash (&line_header_local);
11491 if (dwarf2_per_objfile->line_header_hash != NULL)
11493 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11494 &line_header_local,
11495 line_header_local_hash, NO_INSERT);
11497 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11498 is not present in *SLOT (since if there is something in *SLOT then
11499 it will be for a partial_unit). */
11500 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11502 gdb_assert (*slot != NULL);
11503 cu->line_header = (struct line_header *) *slot;
11508 /* dwarf_decode_line_header does not yet provide sufficient information.
11509 We always have to call also dwarf_decode_lines for it. */
11510 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11514 cu->line_header = lh.release ();
11515 cu->line_header_die_owner = die;
11517 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, INSERT);
11524 gdb_assert (slot != NULL);
11526 if (slot != NULL && *slot == NULL)
11528 /* This newly decoded line number information unit will be owned
11529 by line_header_hash hash table. */
11530 *slot = cu->line_header;
11531 cu->line_header_die_owner = NULL;
11535 /* We cannot free any current entry in (*slot) as that struct line_header
11536 may be already used by multiple CUs. Create only temporary decoded
11537 line_header for this CU - it may happen at most once for each line
11538 number information unit. And if we're not using line_header_hash
11539 then this is what we want as well. */
11540 gdb_assert (die->tag != DW_TAG_partial_unit);
11542 decode_mapping = (die->tag != DW_TAG_partial_unit);
11543 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11548 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11551 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11553 struct objfile *objfile = dwarf2_per_objfile->objfile;
11554 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11555 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11556 CORE_ADDR highpc = ((CORE_ADDR) 0);
11557 struct attribute *attr;
11558 struct die_info *child_die;
11559 CORE_ADDR baseaddr;
11561 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11563 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11565 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11566 from finish_block. */
11567 if (lowpc == ((CORE_ADDR) -1))
11569 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11571 file_and_directory fnd = find_file_and_directory (die, cu);
11573 prepare_one_comp_unit (cu, die, cu->language);
11575 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11576 standardised yet. As a workaround for the language detection we fall
11577 back to the DW_AT_producer string. */
11578 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11579 cu->language = language_opencl;
11581 /* Similar hack for Go. */
11582 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11583 set_cu_language (DW_LANG_Go, cu);
11585 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
11587 /* Decode line number information if present. We do this before
11588 processing child DIEs, so that the line header table is available
11589 for DW_AT_decl_file. */
11590 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11592 /* Process all dies in compilation unit. */
11593 if (die->child != NULL)
11595 child_die = die->child;
11596 while (child_die && child_die->tag)
11598 process_die (child_die, cu);
11599 child_die = sibling_die (child_die);
11603 /* Decode macro information, if present. Dwarf 2 macro information
11604 refers to information in the line number info statement program
11605 header, so we can only read it if we've read the header
11607 attr = dwarf2_attr (die, DW_AT_macros, cu);
11609 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11610 if (attr && cu->line_header)
11612 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11613 complaint (&symfile_complaints,
11614 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11616 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11620 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11621 if (attr && cu->line_header)
11623 unsigned int macro_offset = DW_UNSND (attr);
11625 dwarf_decode_macros (cu, macro_offset, 0);
11630 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11631 Create the set of symtabs used by this TU, or if this TU is sharing
11632 symtabs with another TU and the symtabs have already been created
11633 then restore those symtabs in the line header.
11634 We don't need the pc/line-number mapping for type units. */
11637 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
11639 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11640 struct type_unit_group *tu_group;
11642 struct attribute *attr;
11644 struct signatured_type *sig_type;
11646 gdb_assert (per_cu->is_debug_types);
11647 sig_type = (struct signatured_type *) per_cu;
11649 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11651 /* If we're using .gdb_index (includes -readnow) then
11652 per_cu->type_unit_group may not have been set up yet. */
11653 if (sig_type->type_unit_group == NULL)
11654 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11655 tu_group = sig_type->type_unit_group;
11657 /* If we've already processed this stmt_list there's no real need to
11658 do it again, we could fake it and just recreate the part we need
11659 (file name,index -> symtab mapping). If data shows this optimization
11660 is useful we can do it then. */
11661 first_time = tu_group->compunit_symtab == NULL;
11663 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11668 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11669 lh = dwarf_decode_line_header (line_offset, cu);
11674 dwarf2_start_symtab (cu, "", NULL, 0);
11677 gdb_assert (tu_group->symtabs == NULL);
11678 restart_symtab (tu_group->compunit_symtab, "", 0);
11683 cu->line_header = lh.release ();
11684 cu->line_header_die_owner = die;
11688 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
11690 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11691 still initializing it, and our caller (a few levels up)
11692 process_full_type_unit still needs to know if this is the first
11695 tu_group->num_symtabs = cu->line_header->file_names.size ();
11696 tu_group->symtabs = XNEWVEC (struct symtab *,
11697 cu->line_header->file_names.size ());
11699 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11701 file_entry &fe = cu->line_header->file_names[i];
11703 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
11705 if (current_subfile->symtab == NULL)
11707 /* NOTE: start_subfile will recognize when it's been
11708 passed a file it has already seen. So we can't
11709 assume there's a simple mapping from
11710 cu->line_header->file_names to subfiles, plus
11711 cu->line_header->file_names may contain dups. */
11712 current_subfile->symtab
11713 = allocate_symtab (cust, current_subfile->name);
11716 fe.symtab = current_subfile->symtab;
11717 tu_group->symtabs[i] = fe.symtab;
11722 restart_symtab (tu_group->compunit_symtab, "", 0);
11724 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11726 file_entry &fe = cu->line_header->file_names[i];
11728 fe.symtab = tu_group->symtabs[i];
11732 /* The main symtab is allocated last. Type units don't have DW_AT_name
11733 so they don't have a "real" (so to speak) symtab anyway.
11734 There is later code that will assign the main symtab to all symbols
11735 that don't have one. We need to handle the case of a symbol with a
11736 missing symtab (DW_AT_decl_file) anyway. */
11739 /* Process DW_TAG_type_unit.
11740 For TUs we want to skip the first top level sibling if it's not the
11741 actual type being defined by this TU. In this case the first top
11742 level sibling is there to provide context only. */
11745 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11747 struct die_info *child_die;
11749 prepare_one_comp_unit (cu, die, language_minimal);
11751 /* Initialize (or reinitialize) the machinery for building symtabs.
11752 We do this before processing child DIEs, so that the line header table
11753 is available for DW_AT_decl_file. */
11754 setup_type_unit_groups (die, cu);
11756 if (die->child != NULL)
11758 child_die = die->child;
11759 while (child_die && child_die->tag)
11761 process_die (child_die, cu);
11762 child_die = sibling_die (child_die);
11769 http://gcc.gnu.org/wiki/DebugFission
11770 http://gcc.gnu.org/wiki/DebugFissionDWP
11772 To simplify handling of both DWO files ("object" files with the DWARF info)
11773 and DWP files (a file with the DWOs packaged up into one file), we treat
11774 DWP files as having a collection of virtual DWO files. */
11777 hash_dwo_file (const void *item)
11779 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11782 hash = htab_hash_string (dwo_file->dwo_name);
11783 if (dwo_file->comp_dir != NULL)
11784 hash += htab_hash_string (dwo_file->comp_dir);
11789 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11791 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11792 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11794 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11796 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11797 return lhs->comp_dir == rhs->comp_dir;
11798 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11801 /* Allocate a hash table for DWO files. */
11804 allocate_dwo_file_hash_table (void)
11806 struct objfile *objfile = dwarf2_per_objfile->objfile;
11808 return htab_create_alloc_ex (41,
11812 &objfile->objfile_obstack,
11813 hashtab_obstack_allocate,
11814 dummy_obstack_deallocate);
11817 /* Lookup DWO file DWO_NAME. */
11820 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
11822 struct dwo_file find_entry;
11825 if (dwarf2_per_objfile->dwo_files == NULL)
11826 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
11828 memset (&find_entry, 0, sizeof (find_entry));
11829 find_entry.dwo_name = dwo_name;
11830 find_entry.comp_dir = comp_dir;
11831 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11837 hash_dwo_unit (const void *item)
11839 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11841 /* This drops the top 32 bits of the id, but is ok for a hash. */
11842 return dwo_unit->signature;
11846 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11848 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11849 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11851 /* The signature is assumed to be unique within the DWO file.
11852 So while object file CU dwo_id's always have the value zero,
11853 that's OK, assuming each object file DWO file has only one CU,
11854 and that's the rule for now. */
11855 return lhs->signature == rhs->signature;
11858 /* Allocate a hash table for DWO CUs,TUs.
11859 There is one of these tables for each of CUs,TUs for each DWO file. */
11862 allocate_dwo_unit_table (struct objfile *objfile)
11864 /* Start out with a pretty small number.
11865 Generally DWO files contain only one CU and maybe some TUs. */
11866 return htab_create_alloc_ex (3,
11870 &objfile->objfile_obstack,
11871 hashtab_obstack_allocate,
11872 dummy_obstack_deallocate);
11875 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11877 struct create_dwo_cu_data
11879 struct dwo_file *dwo_file;
11880 struct dwo_unit dwo_unit;
11883 /* die_reader_func for create_dwo_cu. */
11886 create_dwo_cu_reader (const struct die_reader_specs *reader,
11887 const gdb_byte *info_ptr,
11888 struct die_info *comp_unit_die,
11892 struct dwarf2_cu *cu = reader->cu;
11893 sect_offset sect_off = cu->per_cu->sect_off;
11894 struct dwarf2_section_info *section = cu->per_cu->section;
11895 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11896 struct dwo_file *dwo_file = data->dwo_file;
11897 struct dwo_unit *dwo_unit = &data->dwo_unit;
11898 struct attribute *attr;
11900 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11903 complaint (&symfile_complaints,
11904 _("Dwarf Error: debug entry at offset 0x%x is missing"
11905 " its dwo_id [in module %s]"),
11906 to_underlying (sect_off), dwo_file->dwo_name);
11910 dwo_unit->dwo_file = dwo_file;
11911 dwo_unit->signature = DW_UNSND (attr);
11912 dwo_unit->section = section;
11913 dwo_unit->sect_off = sect_off;
11914 dwo_unit->length = cu->per_cu->length;
11916 if (dwarf_read_debug)
11917 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
11918 to_underlying (sect_off),
11919 hex_string (dwo_unit->signature));
11922 /* Create the dwo_units for the CUs in a DWO_FILE.
11923 Note: This function processes DWO files only, not DWP files. */
11926 create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info §ion,
11929 struct objfile *objfile = dwarf2_per_objfile->objfile;
11930 const gdb_byte *info_ptr, *end_ptr;
11932 dwarf2_read_section (objfile, §ion);
11933 info_ptr = section.buffer;
11935 if (info_ptr == NULL)
11938 if (dwarf_read_debug)
11940 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11941 get_section_name (§ion),
11942 get_section_file_name (§ion));
11945 end_ptr = info_ptr + section.size;
11946 while (info_ptr < end_ptr)
11948 struct dwarf2_per_cu_data per_cu;
11949 struct create_dwo_cu_data create_dwo_cu_data;
11950 struct dwo_unit *dwo_unit;
11952 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11954 memset (&create_dwo_cu_data.dwo_unit, 0,
11955 sizeof (create_dwo_cu_data.dwo_unit));
11956 memset (&per_cu, 0, sizeof (per_cu));
11957 per_cu.objfile = objfile;
11958 per_cu.is_debug_types = 0;
11959 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11960 per_cu.section = §ion;
11961 create_dwo_cu_data.dwo_file = &dwo_file;
11963 init_cutu_and_read_dies_no_follow (
11964 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11965 info_ptr += per_cu.length;
11967 // If the unit could not be parsed, skip it.
11968 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11971 if (cus_htab == NULL)
11972 cus_htab = allocate_dwo_unit_table (objfile);
11974 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11975 *dwo_unit = create_dwo_cu_data.dwo_unit;
11976 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11977 gdb_assert (slot != NULL);
11980 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11981 sect_offset dup_sect_off = dup_cu->sect_off;
11983 complaint (&symfile_complaints,
11984 _("debug cu entry at offset 0x%x is duplicate to"
11985 " the entry at offset 0x%x, signature %s"),
11986 to_underlying (sect_off), to_underlying (dup_sect_off),
11987 hex_string (dwo_unit->signature));
11989 *slot = (void *)dwo_unit;
11993 /* DWP file .debug_{cu,tu}_index section format:
11994 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11998 Both index sections have the same format, and serve to map a 64-bit
11999 signature to a set of section numbers. Each section begins with a header,
12000 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12001 indexes, and a pool of 32-bit section numbers. The index sections will be
12002 aligned at 8-byte boundaries in the file.
12004 The index section header consists of:
12006 V, 32 bit version number
12008 N, 32 bit number of compilation units or type units in the index
12009 M, 32 bit number of slots in the hash table
12011 Numbers are recorded using the byte order of the application binary.
12013 The hash table begins at offset 16 in the section, and consists of an array
12014 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12015 order of the application binary). Unused slots in the hash table are 0.
12016 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12018 The parallel table begins immediately after the hash table
12019 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12020 array of 32-bit indexes (using the byte order of the application binary),
12021 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12022 table contains a 32-bit index into the pool of section numbers. For unused
12023 hash table slots, the corresponding entry in the parallel table will be 0.
12025 The pool of section numbers begins immediately following the hash table
12026 (at offset 16 + 12 * M from the beginning of the section). The pool of
12027 section numbers consists of an array of 32-bit words (using the byte order
12028 of the application binary). Each item in the array is indexed starting
12029 from 0. The hash table entry provides the index of the first section
12030 number in the set. Additional section numbers in the set follow, and the
12031 set is terminated by a 0 entry (section number 0 is not used in ELF).
12033 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12034 section must be the first entry in the set, and the .debug_abbrev.dwo must
12035 be the second entry. Other members of the set may follow in any order.
12041 DWP Version 2 combines all the .debug_info, etc. sections into one,
12042 and the entries in the index tables are now offsets into these sections.
12043 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12046 Index Section Contents:
12048 Hash Table of Signatures dwp_hash_table.hash_table
12049 Parallel Table of Indices dwp_hash_table.unit_table
12050 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12051 Table of Section Sizes dwp_hash_table.v2.sizes
12053 The index section header consists of:
12055 V, 32 bit version number
12056 L, 32 bit number of columns in the table of section offsets
12057 N, 32 bit number of compilation units or type units in the index
12058 M, 32 bit number of slots in the hash table
12060 Numbers are recorded using the byte order of the application binary.
12062 The hash table has the same format as version 1.
12063 The parallel table of indices has the same format as version 1,
12064 except that the entries are origin-1 indices into the table of sections
12065 offsets and the table of section sizes.
12067 The table of offsets begins immediately following the parallel table
12068 (at offset 16 + 12 * M from the beginning of the section). The table is
12069 a two-dimensional array of 32-bit words (using the byte order of the
12070 application binary), with L columns and N+1 rows, in row-major order.
12071 Each row in the array is indexed starting from 0. The first row provides
12072 a key to the remaining rows: each column in this row provides an identifier
12073 for a debug section, and the offsets in the same column of subsequent rows
12074 refer to that section. The section identifiers are:
12076 DW_SECT_INFO 1 .debug_info.dwo
12077 DW_SECT_TYPES 2 .debug_types.dwo
12078 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12079 DW_SECT_LINE 4 .debug_line.dwo
12080 DW_SECT_LOC 5 .debug_loc.dwo
12081 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12082 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12083 DW_SECT_MACRO 8 .debug_macro.dwo
12085 The offsets provided by the CU and TU index sections are the base offsets
12086 for the contributions made by each CU or TU to the corresponding section
12087 in the package file. Each CU and TU header contains an abbrev_offset
12088 field, used to find the abbreviations table for that CU or TU within the
12089 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12090 be interpreted as relative to the base offset given in the index section.
12091 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12092 should be interpreted as relative to the base offset for .debug_line.dwo,
12093 and offsets into other debug sections obtained from DWARF attributes should
12094 also be interpreted as relative to the corresponding base offset.
12096 The table of sizes begins immediately following the table of offsets.
12097 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12098 with L columns and N rows, in row-major order. Each row in the array is
12099 indexed starting from 1 (row 0 is shared by the two tables).
12103 Hash table lookup is handled the same in version 1 and 2:
12105 We assume that N and M will not exceed 2^32 - 1.
12106 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12108 Given a 64-bit compilation unit signature or a type signature S, an entry
12109 in the hash table is located as follows:
12111 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12112 the low-order k bits all set to 1.
12114 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12116 3) If the hash table entry at index H matches the signature, use that
12117 entry. If the hash table entry at index H is unused (all zeroes),
12118 terminate the search: the signature is not present in the table.
12120 4) Let H = (H + H') modulo M. Repeat at Step 3.
12122 Because M > N and H' and M are relatively prime, the search is guaranteed
12123 to stop at an unused slot or find the match. */
12125 /* Create a hash table to map DWO IDs to their CU/TU entry in
12126 .debug_{info,types}.dwo in DWP_FILE.
12127 Returns NULL if there isn't one.
12128 Note: This function processes DWP files only, not DWO files. */
12130 static struct dwp_hash_table *
12131 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
12133 struct objfile *objfile = dwarf2_per_objfile->objfile;
12134 bfd *dbfd = dwp_file->dbfd;
12135 const gdb_byte *index_ptr, *index_end;
12136 struct dwarf2_section_info *index;
12137 uint32_t version, nr_columns, nr_units, nr_slots;
12138 struct dwp_hash_table *htab;
12140 if (is_debug_types)
12141 index = &dwp_file->sections.tu_index;
12143 index = &dwp_file->sections.cu_index;
12145 if (dwarf2_section_empty_p (index))
12147 dwarf2_read_section (objfile, index);
12149 index_ptr = index->buffer;
12150 index_end = index_ptr + index->size;
12152 version = read_4_bytes (dbfd, index_ptr);
12155 nr_columns = read_4_bytes (dbfd, index_ptr);
12159 nr_units = read_4_bytes (dbfd, index_ptr);
12161 nr_slots = read_4_bytes (dbfd, index_ptr);
12164 if (version != 1 && version != 2)
12166 error (_("Dwarf Error: unsupported DWP file version (%s)"
12167 " [in module %s]"),
12168 pulongest (version), dwp_file->name);
12170 if (nr_slots != (nr_slots & -nr_slots))
12172 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12173 " is not power of 2 [in module %s]"),
12174 pulongest (nr_slots), dwp_file->name);
12177 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12178 htab->version = version;
12179 htab->nr_columns = nr_columns;
12180 htab->nr_units = nr_units;
12181 htab->nr_slots = nr_slots;
12182 htab->hash_table = index_ptr;
12183 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12185 /* Exit early if the table is empty. */
12186 if (nr_slots == 0 || nr_units == 0
12187 || (version == 2 && nr_columns == 0))
12189 /* All must be zero. */
12190 if (nr_slots != 0 || nr_units != 0
12191 || (version == 2 && nr_columns != 0))
12193 complaint (&symfile_complaints,
12194 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12195 " all zero [in modules %s]"),
12203 htab->section_pool.v1.indices =
12204 htab->unit_table + sizeof (uint32_t) * nr_slots;
12205 /* It's harder to decide whether the section is too small in v1.
12206 V1 is deprecated anyway so we punt. */
12210 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12211 int *ids = htab->section_pool.v2.section_ids;
12212 /* Reverse map for error checking. */
12213 int ids_seen[DW_SECT_MAX + 1];
12216 if (nr_columns < 2)
12218 error (_("Dwarf Error: bad DWP hash table, too few columns"
12219 " in section table [in module %s]"),
12222 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12224 error (_("Dwarf Error: bad DWP hash table, too many columns"
12225 " in section table [in module %s]"),
12228 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12229 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12230 for (i = 0; i < nr_columns; ++i)
12232 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12234 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12236 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12237 " in section table [in module %s]"),
12238 id, dwp_file->name);
12240 if (ids_seen[id] != -1)
12242 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12243 " id %d in section table [in module %s]"),
12244 id, dwp_file->name);
12249 /* Must have exactly one info or types section. */
12250 if (((ids_seen[DW_SECT_INFO] != -1)
12251 + (ids_seen[DW_SECT_TYPES] != -1))
12254 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12255 " DWO info/types section [in module %s]"),
12258 /* Must have an abbrev section. */
12259 if (ids_seen[DW_SECT_ABBREV] == -1)
12261 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12262 " section [in module %s]"),
12265 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12266 htab->section_pool.v2.sizes =
12267 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12268 * nr_units * nr_columns);
12269 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12270 * nr_units * nr_columns))
12273 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12274 " [in module %s]"),
12282 /* Update SECTIONS with the data from SECTP.
12284 This function is like the other "locate" section routines that are
12285 passed to bfd_map_over_sections, but in this context the sections to
12286 read comes from the DWP V1 hash table, not the full ELF section table.
12288 The result is non-zero for success, or zero if an error was found. */
12291 locate_v1_virtual_dwo_sections (asection *sectp,
12292 struct virtual_v1_dwo_sections *sections)
12294 const struct dwop_section_names *names = &dwop_section_names;
12296 if (section_is_p (sectp->name, &names->abbrev_dwo))
12298 /* There can be only one. */
12299 if (sections->abbrev.s.section != NULL)
12301 sections->abbrev.s.section = sectp;
12302 sections->abbrev.size = bfd_get_section_size (sectp);
12304 else if (section_is_p (sectp->name, &names->info_dwo)
12305 || section_is_p (sectp->name, &names->types_dwo))
12307 /* There can be only one. */
12308 if (sections->info_or_types.s.section != NULL)
12310 sections->info_or_types.s.section = sectp;
12311 sections->info_or_types.size = bfd_get_section_size (sectp);
12313 else if (section_is_p (sectp->name, &names->line_dwo))
12315 /* There can be only one. */
12316 if (sections->line.s.section != NULL)
12318 sections->line.s.section = sectp;
12319 sections->line.size = bfd_get_section_size (sectp);
12321 else if (section_is_p (sectp->name, &names->loc_dwo))
12323 /* There can be only one. */
12324 if (sections->loc.s.section != NULL)
12326 sections->loc.s.section = sectp;
12327 sections->loc.size = bfd_get_section_size (sectp);
12329 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12331 /* There can be only one. */
12332 if (sections->macinfo.s.section != NULL)
12334 sections->macinfo.s.section = sectp;
12335 sections->macinfo.size = bfd_get_section_size (sectp);
12337 else if (section_is_p (sectp->name, &names->macro_dwo))
12339 /* There can be only one. */
12340 if (sections->macro.s.section != NULL)
12342 sections->macro.s.section = sectp;
12343 sections->macro.size = bfd_get_section_size (sectp);
12345 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12347 /* There can be only one. */
12348 if (sections->str_offsets.s.section != NULL)
12350 sections->str_offsets.s.section = sectp;
12351 sections->str_offsets.size = bfd_get_section_size (sectp);
12355 /* No other kind of section is valid. */
12362 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12363 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12364 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12365 This is for DWP version 1 files. */
12367 static struct dwo_unit *
12368 create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
12369 uint32_t unit_index,
12370 const char *comp_dir,
12371 ULONGEST signature, int is_debug_types)
12373 struct objfile *objfile = dwarf2_per_objfile->objfile;
12374 const struct dwp_hash_table *dwp_htab =
12375 is_debug_types ? dwp_file->tus : dwp_file->cus;
12376 bfd *dbfd = dwp_file->dbfd;
12377 const char *kind = is_debug_types ? "TU" : "CU";
12378 struct dwo_file *dwo_file;
12379 struct dwo_unit *dwo_unit;
12380 struct virtual_v1_dwo_sections sections;
12381 void **dwo_file_slot;
12384 gdb_assert (dwp_file->version == 1);
12386 if (dwarf_read_debug)
12388 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12390 pulongest (unit_index), hex_string (signature),
12394 /* Fetch the sections of this DWO unit.
12395 Put a limit on the number of sections we look for so that bad data
12396 doesn't cause us to loop forever. */
12398 #define MAX_NR_V1_DWO_SECTIONS \
12399 (1 /* .debug_info or .debug_types */ \
12400 + 1 /* .debug_abbrev */ \
12401 + 1 /* .debug_line */ \
12402 + 1 /* .debug_loc */ \
12403 + 1 /* .debug_str_offsets */ \
12404 + 1 /* .debug_macro or .debug_macinfo */ \
12405 + 1 /* trailing zero */)
12407 memset (§ions, 0, sizeof (sections));
12409 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12412 uint32_t section_nr =
12413 read_4_bytes (dbfd,
12414 dwp_htab->section_pool.v1.indices
12415 + (unit_index + i) * sizeof (uint32_t));
12417 if (section_nr == 0)
12419 if (section_nr >= dwp_file->num_sections)
12421 error (_("Dwarf Error: bad DWP hash table, section number too large"
12422 " [in module %s]"),
12426 sectp = dwp_file->elf_sections[section_nr];
12427 if (! locate_v1_virtual_dwo_sections (sectp, §ions))
12429 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12430 " [in module %s]"),
12436 || dwarf2_section_empty_p (§ions.info_or_types)
12437 || dwarf2_section_empty_p (§ions.abbrev))
12439 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12440 " [in module %s]"),
12443 if (i == MAX_NR_V1_DWO_SECTIONS)
12445 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12446 " [in module %s]"),
12450 /* It's easier for the rest of the code if we fake a struct dwo_file and
12451 have dwo_unit "live" in that. At least for now.
12453 The DWP file can be made up of a random collection of CUs and TUs.
12454 However, for each CU + set of TUs that came from the same original DWO
12455 file, we can combine them back into a virtual DWO file to save space
12456 (fewer struct dwo_file objects to allocate). Remember that for really
12457 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12459 std::string virtual_dwo_name =
12460 string_printf ("virtual-dwo/%d-%d-%d-%d",
12461 get_section_id (§ions.abbrev),
12462 get_section_id (§ions.line),
12463 get_section_id (§ions.loc),
12464 get_section_id (§ions.str_offsets));
12465 /* Can we use an existing virtual DWO file? */
12466 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
12467 /* Create one if necessary. */
12468 if (*dwo_file_slot == NULL)
12470 if (dwarf_read_debug)
12472 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12473 virtual_dwo_name.c_str ());
12475 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12477 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12478 virtual_dwo_name.c_str (),
12479 virtual_dwo_name.size ());
12480 dwo_file->comp_dir = comp_dir;
12481 dwo_file->sections.abbrev = sections.abbrev;
12482 dwo_file->sections.line = sections.line;
12483 dwo_file->sections.loc = sections.loc;
12484 dwo_file->sections.macinfo = sections.macinfo;
12485 dwo_file->sections.macro = sections.macro;
12486 dwo_file->sections.str_offsets = sections.str_offsets;
12487 /* The "str" section is global to the entire DWP file. */
12488 dwo_file->sections.str = dwp_file->sections.str;
12489 /* The info or types section is assigned below to dwo_unit,
12490 there's no need to record it in dwo_file.
12491 Also, we can't simply record type sections in dwo_file because
12492 we record a pointer into the vector in dwo_unit. As we collect more
12493 types we'll grow the vector and eventually have to reallocate space
12494 for it, invalidating all copies of pointers into the previous
12496 *dwo_file_slot = dwo_file;
12500 if (dwarf_read_debug)
12502 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12503 virtual_dwo_name.c_str ());
12505 dwo_file = (struct dwo_file *) *dwo_file_slot;
12508 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12509 dwo_unit->dwo_file = dwo_file;
12510 dwo_unit->signature = signature;
12511 dwo_unit->section =
12512 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12513 *dwo_unit->section = sections.info_or_types;
12514 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12519 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12520 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12521 piece within that section used by a TU/CU, return a virtual section
12522 of just that piece. */
12524 static struct dwarf2_section_info
12525 create_dwp_v2_section (struct dwarf2_section_info *section,
12526 bfd_size_type offset, bfd_size_type size)
12528 struct dwarf2_section_info result;
12531 gdb_assert (section != NULL);
12532 gdb_assert (!section->is_virtual);
12534 memset (&result, 0, sizeof (result));
12535 result.s.containing_section = section;
12536 result.is_virtual = 1;
12541 sectp = get_section_bfd_section (section);
12543 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12544 bounds of the real section. This is a pretty-rare event, so just
12545 flag an error (easier) instead of a warning and trying to cope. */
12547 || offset + size > bfd_get_section_size (sectp))
12549 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12550 " in section %s [in module %s]"),
12551 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12552 objfile_name (dwarf2_per_objfile->objfile));
12555 result.virtual_offset = offset;
12556 result.size = size;
12560 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12561 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12562 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12563 This is for DWP version 2 files. */
12565 static struct dwo_unit *
12566 create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
12567 uint32_t unit_index,
12568 const char *comp_dir,
12569 ULONGEST signature, int is_debug_types)
12571 struct objfile *objfile = dwarf2_per_objfile->objfile;
12572 const struct dwp_hash_table *dwp_htab =
12573 is_debug_types ? dwp_file->tus : dwp_file->cus;
12574 bfd *dbfd = dwp_file->dbfd;
12575 const char *kind = is_debug_types ? "TU" : "CU";
12576 struct dwo_file *dwo_file;
12577 struct dwo_unit *dwo_unit;
12578 struct virtual_v2_dwo_sections sections;
12579 void **dwo_file_slot;
12582 gdb_assert (dwp_file->version == 2);
12584 if (dwarf_read_debug)
12586 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12588 pulongest (unit_index), hex_string (signature),
12592 /* Fetch the section offsets of this DWO unit. */
12594 memset (§ions, 0, sizeof (sections));
12596 for (i = 0; i < dwp_htab->nr_columns; ++i)
12598 uint32_t offset = read_4_bytes (dbfd,
12599 dwp_htab->section_pool.v2.offsets
12600 + (((unit_index - 1) * dwp_htab->nr_columns
12602 * sizeof (uint32_t)));
12603 uint32_t size = read_4_bytes (dbfd,
12604 dwp_htab->section_pool.v2.sizes
12605 + (((unit_index - 1) * dwp_htab->nr_columns
12607 * sizeof (uint32_t)));
12609 switch (dwp_htab->section_pool.v2.section_ids[i])
12612 case DW_SECT_TYPES:
12613 sections.info_or_types_offset = offset;
12614 sections.info_or_types_size = size;
12616 case DW_SECT_ABBREV:
12617 sections.abbrev_offset = offset;
12618 sections.abbrev_size = size;
12621 sections.line_offset = offset;
12622 sections.line_size = size;
12625 sections.loc_offset = offset;
12626 sections.loc_size = size;
12628 case DW_SECT_STR_OFFSETS:
12629 sections.str_offsets_offset = offset;
12630 sections.str_offsets_size = size;
12632 case DW_SECT_MACINFO:
12633 sections.macinfo_offset = offset;
12634 sections.macinfo_size = size;
12636 case DW_SECT_MACRO:
12637 sections.macro_offset = offset;
12638 sections.macro_size = size;
12643 /* It's easier for the rest of the code if we fake a struct dwo_file and
12644 have dwo_unit "live" in that. At least for now.
12646 The DWP file can be made up of a random collection of CUs and TUs.
12647 However, for each CU + set of TUs that came from the same original DWO
12648 file, we can combine them back into a virtual DWO file to save space
12649 (fewer struct dwo_file objects to allocate). Remember that for really
12650 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12652 std::string virtual_dwo_name =
12653 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12654 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12655 (long) (sections.line_size ? sections.line_offset : 0),
12656 (long) (sections.loc_size ? sections.loc_offset : 0),
12657 (long) (sections.str_offsets_size
12658 ? sections.str_offsets_offset : 0));
12659 /* Can we use an existing virtual DWO file? */
12660 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
12661 /* Create one if necessary. */
12662 if (*dwo_file_slot == NULL)
12664 if (dwarf_read_debug)
12666 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12667 virtual_dwo_name.c_str ());
12669 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12671 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12672 virtual_dwo_name.c_str (),
12673 virtual_dwo_name.size ());
12674 dwo_file->comp_dir = comp_dir;
12675 dwo_file->sections.abbrev =
12676 create_dwp_v2_section (&dwp_file->sections.abbrev,
12677 sections.abbrev_offset, sections.abbrev_size);
12678 dwo_file->sections.line =
12679 create_dwp_v2_section (&dwp_file->sections.line,
12680 sections.line_offset, sections.line_size);
12681 dwo_file->sections.loc =
12682 create_dwp_v2_section (&dwp_file->sections.loc,
12683 sections.loc_offset, sections.loc_size);
12684 dwo_file->sections.macinfo =
12685 create_dwp_v2_section (&dwp_file->sections.macinfo,
12686 sections.macinfo_offset, sections.macinfo_size);
12687 dwo_file->sections.macro =
12688 create_dwp_v2_section (&dwp_file->sections.macro,
12689 sections.macro_offset, sections.macro_size);
12690 dwo_file->sections.str_offsets =
12691 create_dwp_v2_section (&dwp_file->sections.str_offsets,
12692 sections.str_offsets_offset,
12693 sections.str_offsets_size);
12694 /* The "str" section is global to the entire DWP file. */
12695 dwo_file->sections.str = dwp_file->sections.str;
12696 /* The info or types section is assigned below to dwo_unit,
12697 there's no need to record it in dwo_file.
12698 Also, we can't simply record type sections in dwo_file because
12699 we record a pointer into the vector in dwo_unit. As we collect more
12700 types we'll grow the vector and eventually have to reallocate space
12701 for it, invalidating all copies of pointers into the previous
12703 *dwo_file_slot = dwo_file;
12707 if (dwarf_read_debug)
12709 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12710 virtual_dwo_name.c_str ());
12712 dwo_file = (struct dwo_file *) *dwo_file_slot;
12715 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12716 dwo_unit->dwo_file = dwo_file;
12717 dwo_unit->signature = signature;
12718 dwo_unit->section =
12719 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12720 *dwo_unit->section = create_dwp_v2_section (is_debug_types
12721 ? &dwp_file->sections.types
12722 : &dwp_file->sections.info,
12723 sections.info_or_types_offset,
12724 sections.info_or_types_size);
12725 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12730 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12731 Returns NULL if the signature isn't found. */
12733 static struct dwo_unit *
12734 lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
12735 ULONGEST signature, int is_debug_types)
12737 const struct dwp_hash_table *dwp_htab =
12738 is_debug_types ? dwp_file->tus : dwp_file->cus;
12739 bfd *dbfd = dwp_file->dbfd;
12740 uint32_t mask = dwp_htab->nr_slots - 1;
12741 uint32_t hash = signature & mask;
12742 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12745 struct dwo_unit find_dwo_cu;
12747 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12748 find_dwo_cu.signature = signature;
12749 slot = htab_find_slot (is_debug_types
12750 ? dwp_file->loaded_tus
12751 : dwp_file->loaded_cus,
12752 &find_dwo_cu, INSERT);
12755 return (struct dwo_unit *) *slot;
12757 /* Use a for loop so that we don't loop forever on bad debug info. */
12758 for (i = 0; i < dwp_htab->nr_slots; ++i)
12760 ULONGEST signature_in_table;
12762 signature_in_table =
12763 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12764 if (signature_in_table == signature)
12766 uint32_t unit_index =
12767 read_4_bytes (dbfd,
12768 dwp_htab->unit_table + hash * sizeof (uint32_t));
12770 if (dwp_file->version == 1)
12772 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
12773 comp_dir, signature,
12778 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
12779 comp_dir, signature,
12782 return (struct dwo_unit *) *slot;
12784 if (signature_in_table == 0)
12786 hash = (hash + hash2) & mask;
12789 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12790 " [in module %s]"),
12794 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12795 Open the file specified by FILE_NAME and hand it off to BFD for
12796 preliminary analysis. Return a newly initialized bfd *, which
12797 includes a canonicalized copy of FILE_NAME.
12798 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12799 SEARCH_CWD is true if the current directory is to be searched.
12800 It will be searched before debug-file-directory.
12801 If successful, the file is added to the bfd include table of the
12802 objfile's bfd (see gdb_bfd_record_inclusion).
12803 If unable to find/open the file, return NULL.
12804 NOTE: This function is derived from symfile_bfd_open. */
12806 static gdb_bfd_ref_ptr
12807 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
12810 char *absolute_name;
12811 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12812 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12813 to debug_file_directory. */
12815 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12819 if (*debug_file_directory != '\0')
12820 search_path = concat (".", dirname_separator_string,
12821 debug_file_directory, (char *) NULL);
12823 search_path = xstrdup (".");
12826 search_path = xstrdup (debug_file_directory);
12828 flags = OPF_RETURN_REALPATH;
12830 flags |= OPF_SEARCH_IN_PATH;
12831 desc = openp (search_path, flags, file_name,
12832 O_RDONLY | O_BINARY, &absolute_name);
12833 xfree (search_path);
12837 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
12838 xfree (absolute_name);
12839 if (sym_bfd == NULL)
12841 bfd_set_cacheable (sym_bfd.get (), 1);
12843 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12846 /* Success. Record the bfd as having been included by the objfile's bfd.
12847 This is important because things like demangled_names_hash lives in the
12848 objfile's per_bfd space and may have references to things like symbol
12849 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12850 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12855 /* Try to open DWO file FILE_NAME.
12856 COMP_DIR is the DW_AT_comp_dir attribute.
12857 The result is the bfd handle of the file.
12858 If there is a problem finding or opening the file, return NULL.
12859 Upon success, the canonicalized path of the file is stored in the bfd,
12860 same as symfile_bfd_open. */
12862 static gdb_bfd_ref_ptr
12863 open_dwo_file (const char *file_name, const char *comp_dir)
12865 if (IS_ABSOLUTE_PATH (file_name))
12866 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
12868 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12870 if (comp_dir != NULL)
12872 char *path_to_try = concat (comp_dir, SLASH_STRING,
12873 file_name, (char *) NULL);
12875 /* NOTE: If comp_dir is a relative path, this will also try the
12876 search path, which seems useful. */
12877 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
12878 1 /*search_cwd*/));
12879 xfree (path_to_try);
12884 /* That didn't work, try debug-file-directory, which, despite its name,
12885 is a list of paths. */
12887 if (*debug_file_directory == '\0')
12890 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
12893 /* This function is mapped across the sections and remembers the offset and
12894 size of each of the DWO debugging sections we are interested in. */
12897 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12899 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12900 const struct dwop_section_names *names = &dwop_section_names;
12902 if (section_is_p (sectp->name, &names->abbrev_dwo))
12904 dwo_sections->abbrev.s.section = sectp;
12905 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12907 else if (section_is_p (sectp->name, &names->info_dwo))
12909 dwo_sections->info.s.section = sectp;
12910 dwo_sections->info.size = bfd_get_section_size (sectp);
12912 else if (section_is_p (sectp->name, &names->line_dwo))
12914 dwo_sections->line.s.section = sectp;
12915 dwo_sections->line.size = bfd_get_section_size (sectp);
12917 else if (section_is_p (sectp->name, &names->loc_dwo))
12919 dwo_sections->loc.s.section = sectp;
12920 dwo_sections->loc.size = bfd_get_section_size (sectp);
12922 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12924 dwo_sections->macinfo.s.section = sectp;
12925 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12927 else if (section_is_p (sectp->name, &names->macro_dwo))
12929 dwo_sections->macro.s.section = sectp;
12930 dwo_sections->macro.size = bfd_get_section_size (sectp);
12932 else if (section_is_p (sectp->name, &names->str_dwo))
12934 dwo_sections->str.s.section = sectp;
12935 dwo_sections->str.size = bfd_get_section_size (sectp);
12937 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12939 dwo_sections->str_offsets.s.section = sectp;
12940 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12942 else if (section_is_p (sectp->name, &names->types_dwo))
12944 struct dwarf2_section_info type_section;
12946 memset (&type_section, 0, sizeof (type_section));
12947 type_section.s.section = sectp;
12948 type_section.size = bfd_get_section_size (sectp);
12949 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
12954 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12955 by PER_CU. This is for the non-DWP case.
12956 The result is NULL if DWO_NAME can't be found. */
12958 static struct dwo_file *
12959 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12960 const char *dwo_name, const char *comp_dir)
12962 struct objfile *objfile = dwarf2_per_objfile->objfile;
12963 struct dwo_file *dwo_file;
12964 struct cleanup *cleanups;
12966 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
12969 if (dwarf_read_debug)
12970 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12973 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12974 dwo_file->dwo_name = dwo_name;
12975 dwo_file->comp_dir = comp_dir;
12976 dwo_file->dbfd = dbfd.release ();
12978 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
12980 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
12981 &dwo_file->sections);
12983 create_cus_hash_table (*dwo_file, dwo_file->sections.info, dwo_file->cus);
12985 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
12988 discard_cleanups (cleanups);
12990 if (dwarf_read_debug)
12991 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12996 /* This function is mapped across the sections and remembers the offset and
12997 size of each of the DWP debugging sections common to version 1 and 2 that
12998 we are interested in. */
13001 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13002 void *dwp_file_ptr)
13004 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13005 const struct dwop_section_names *names = &dwop_section_names;
13006 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13008 /* Record the ELF section number for later lookup: this is what the
13009 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13010 gdb_assert (elf_section_nr < dwp_file->num_sections);
13011 dwp_file->elf_sections[elf_section_nr] = sectp;
13013 /* Look for specific sections that we need. */
13014 if (section_is_p (sectp->name, &names->str_dwo))
13016 dwp_file->sections.str.s.section = sectp;
13017 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13019 else if (section_is_p (sectp->name, &names->cu_index))
13021 dwp_file->sections.cu_index.s.section = sectp;
13022 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13024 else if (section_is_p (sectp->name, &names->tu_index))
13026 dwp_file->sections.tu_index.s.section = sectp;
13027 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13031 /* This function is mapped across the sections and remembers the offset and
13032 size of each of the DWP version 2 debugging sections that we are interested
13033 in. This is split into a separate function because we don't know if we
13034 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13037 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13039 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13040 const struct dwop_section_names *names = &dwop_section_names;
13041 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13043 /* Record the ELF section number for later lookup: this is what the
13044 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13045 gdb_assert (elf_section_nr < dwp_file->num_sections);
13046 dwp_file->elf_sections[elf_section_nr] = sectp;
13048 /* Look for specific sections that we need. */
13049 if (section_is_p (sectp->name, &names->abbrev_dwo))
13051 dwp_file->sections.abbrev.s.section = sectp;
13052 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13054 else if (section_is_p (sectp->name, &names->info_dwo))
13056 dwp_file->sections.info.s.section = sectp;
13057 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13059 else if (section_is_p (sectp->name, &names->line_dwo))
13061 dwp_file->sections.line.s.section = sectp;
13062 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13064 else if (section_is_p (sectp->name, &names->loc_dwo))
13066 dwp_file->sections.loc.s.section = sectp;
13067 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13069 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13071 dwp_file->sections.macinfo.s.section = sectp;
13072 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13074 else if (section_is_p (sectp->name, &names->macro_dwo))
13076 dwp_file->sections.macro.s.section = sectp;
13077 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13079 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13081 dwp_file->sections.str_offsets.s.section = sectp;
13082 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13084 else if (section_is_p (sectp->name, &names->types_dwo))
13086 dwp_file->sections.types.s.section = sectp;
13087 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13091 /* Hash function for dwp_file loaded CUs/TUs. */
13094 hash_dwp_loaded_cutus (const void *item)
13096 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13098 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13099 return dwo_unit->signature;
13102 /* Equality function for dwp_file loaded CUs/TUs. */
13105 eq_dwp_loaded_cutus (const void *a, const void *b)
13107 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13108 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13110 return dua->signature == dub->signature;
13113 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13116 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13118 return htab_create_alloc_ex (3,
13119 hash_dwp_loaded_cutus,
13120 eq_dwp_loaded_cutus,
13122 &objfile->objfile_obstack,
13123 hashtab_obstack_allocate,
13124 dummy_obstack_deallocate);
13127 /* Try to open DWP file FILE_NAME.
13128 The result is the bfd handle of the file.
13129 If there is a problem finding or opening the file, return NULL.
13130 Upon success, the canonicalized path of the file is stored in the bfd,
13131 same as symfile_bfd_open. */
13133 static gdb_bfd_ref_ptr
13134 open_dwp_file (const char *file_name)
13136 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
13137 1 /*search_cwd*/));
13141 /* Work around upstream bug 15652.
13142 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13143 [Whether that's a "bug" is debatable, but it is getting in our way.]
13144 We have no real idea where the dwp file is, because gdb's realpath-ing
13145 of the executable's path may have discarded the needed info.
13146 [IWBN if the dwp file name was recorded in the executable, akin to
13147 .gnu_debuglink, but that doesn't exist yet.]
13148 Strip the directory from FILE_NAME and search again. */
13149 if (*debug_file_directory != '\0')
13151 /* Don't implicitly search the current directory here.
13152 If the user wants to search "." to handle this case,
13153 it must be added to debug-file-directory. */
13154 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
13161 /* Initialize the use of the DWP file for the current objfile.
13162 By convention the name of the DWP file is ${objfile}.dwp.
13163 The result is NULL if it can't be found. */
13165 static struct dwp_file *
13166 open_and_init_dwp_file (void)
13168 struct objfile *objfile = dwarf2_per_objfile->objfile;
13169 struct dwp_file *dwp_file;
13171 /* Try to find first .dwp for the binary file before any symbolic links
13174 /* If the objfile is a debug file, find the name of the real binary
13175 file and get the name of dwp file from there. */
13176 std::string dwp_name;
13177 if (objfile->separate_debug_objfile_backlink != NULL)
13179 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13180 const char *backlink_basename = lbasename (backlink->original_name);
13182 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13185 dwp_name = objfile->original_name;
13187 dwp_name += ".dwp";
13189 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
13191 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13193 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13194 dwp_name = objfile_name (objfile);
13195 dwp_name += ".dwp";
13196 dbfd = open_dwp_file (dwp_name.c_str ());
13201 if (dwarf_read_debug)
13202 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13205 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
13206 dwp_file->name = bfd_get_filename (dbfd.get ());
13207 dwp_file->dbfd = dbfd.release ();
13209 /* +1: section 0 is unused */
13210 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
13211 dwp_file->elf_sections =
13212 OBSTACK_CALLOC (&objfile->objfile_obstack,
13213 dwp_file->num_sections, asection *);
13215 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13218 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
13220 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
13222 /* The DWP file version is stored in the hash table. Oh well. */
13223 if (dwp_file->cus && dwp_file->tus
13224 && dwp_file->cus->version != dwp_file->tus->version)
13226 /* Technically speaking, we should try to limp along, but this is
13227 pretty bizarre. We use pulongest here because that's the established
13228 portability solution (e.g, we cannot use %u for uint32_t). */
13229 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13230 " TU version %s [in DWP file %s]"),
13231 pulongest (dwp_file->cus->version),
13232 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13236 dwp_file->version = dwp_file->cus->version;
13237 else if (dwp_file->tus)
13238 dwp_file->version = dwp_file->tus->version;
13240 dwp_file->version = 2;
13242 if (dwp_file->version == 2)
13243 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13246 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13247 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13249 if (dwarf_read_debug)
13251 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13252 fprintf_unfiltered (gdb_stdlog,
13253 " %s CUs, %s TUs\n",
13254 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13255 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13261 /* Wrapper around open_and_init_dwp_file, only open it once. */
13263 static struct dwp_file *
13264 get_dwp_file (void)
13266 if (! dwarf2_per_objfile->dwp_checked)
13268 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
13269 dwarf2_per_objfile->dwp_checked = 1;
13271 return dwarf2_per_objfile->dwp_file;
13274 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13275 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13276 or in the DWP file for the objfile, referenced by THIS_UNIT.
13277 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13278 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13280 This is called, for example, when wanting to read a variable with a
13281 complex location. Therefore we don't want to do file i/o for every call.
13282 Therefore we don't want to look for a DWO file on every call.
13283 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13284 then we check if we've already seen DWO_NAME, and only THEN do we check
13287 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13288 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13290 static struct dwo_unit *
13291 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13292 const char *dwo_name, const char *comp_dir,
13293 ULONGEST signature, int is_debug_types)
13295 struct objfile *objfile = dwarf2_per_objfile->objfile;
13296 const char *kind = is_debug_types ? "TU" : "CU";
13297 void **dwo_file_slot;
13298 struct dwo_file *dwo_file;
13299 struct dwp_file *dwp_file;
13301 /* First see if there's a DWP file.
13302 If we have a DWP file but didn't find the DWO inside it, don't
13303 look for the original DWO file. It makes gdb behave differently
13304 depending on whether one is debugging in the build tree. */
13306 dwp_file = get_dwp_file ();
13307 if (dwp_file != NULL)
13309 const struct dwp_hash_table *dwp_htab =
13310 is_debug_types ? dwp_file->tus : dwp_file->cus;
13312 if (dwp_htab != NULL)
13314 struct dwo_unit *dwo_cutu =
13315 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
13316 signature, is_debug_types);
13318 if (dwo_cutu != NULL)
13320 if (dwarf_read_debug)
13322 fprintf_unfiltered (gdb_stdlog,
13323 "Virtual DWO %s %s found: @%s\n",
13324 kind, hex_string (signature),
13325 host_address_to_string (dwo_cutu));
13333 /* No DWP file, look for the DWO file. */
13335 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
13336 if (*dwo_file_slot == NULL)
13338 /* Read in the file and build a table of the CUs/TUs it contains. */
13339 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13341 /* NOTE: This will be NULL if unable to open the file. */
13342 dwo_file = (struct dwo_file *) *dwo_file_slot;
13344 if (dwo_file != NULL)
13346 struct dwo_unit *dwo_cutu = NULL;
13348 if (is_debug_types && dwo_file->tus)
13350 struct dwo_unit find_dwo_cutu;
13352 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13353 find_dwo_cutu.signature = signature;
13355 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13357 else if (!is_debug_types && dwo_file->cus)
13359 struct dwo_unit find_dwo_cutu;
13361 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13362 find_dwo_cutu.signature = signature;
13363 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13367 if (dwo_cutu != NULL)
13369 if (dwarf_read_debug)
13371 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13372 kind, dwo_name, hex_string (signature),
13373 host_address_to_string (dwo_cutu));
13380 /* We didn't find it. This could mean a dwo_id mismatch, or
13381 someone deleted the DWO/DWP file, or the search path isn't set up
13382 correctly to find the file. */
13384 if (dwarf_read_debug)
13386 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13387 kind, dwo_name, hex_string (signature));
13390 /* This is a warning and not a complaint because it can be caused by
13391 pilot error (e.g., user accidentally deleting the DWO). */
13393 /* Print the name of the DWP file if we looked there, helps the user
13394 better diagnose the problem. */
13395 std::string dwp_text;
13397 if (dwp_file != NULL)
13398 dwp_text = string_printf (" [in DWP file %s]",
13399 lbasename (dwp_file->name));
13401 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
13402 " [in module %s]"),
13403 kind, dwo_name, hex_string (signature),
13405 this_unit->is_debug_types ? "TU" : "CU",
13406 to_underlying (this_unit->sect_off), objfile_name (objfile));
13411 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13412 See lookup_dwo_cutu_unit for details. */
13414 static struct dwo_unit *
13415 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13416 const char *dwo_name, const char *comp_dir,
13417 ULONGEST signature)
13419 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13422 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13423 See lookup_dwo_cutu_unit for details. */
13425 static struct dwo_unit *
13426 lookup_dwo_type_unit (struct signatured_type *this_tu,
13427 const char *dwo_name, const char *comp_dir)
13429 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13432 /* Traversal function for queue_and_load_all_dwo_tus. */
13435 queue_and_load_dwo_tu (void **slot, void *info)
13437 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13438 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13439 ULONGEST signature = dwo_unit->signature;
13440 struct signatured_type *sig_type =
13441 lookup_dwo_signatured_type (per_cu->cu, signature);
13443 if (sig_type != NULL)
13445 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13447 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13448 a real dependency of PER_CU on SIG_TYPE. That is detected later
13449 while processing PER_CU. */
13450 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13451 load_full_type_unit (sig_cu);
13452 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13458 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13459 The DWO may have the only definition of the type, though it may not be
13460 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13461 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13464 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13466 struct dwo_unit *dwo_unit;
13467 struct dwo_file *dwo_file;
13469 gdb_assert (!per_cu->is_debug_types);
13470 gdb_assert (get_dwp_file () == NULL);
13471 gdb_assert (per_cu->cu != NULL);
13473 dwo_unit = per_cu->cu->dwo_unit;
13474 gdb_assert (dwo_unit != NULL);
13476 dwo_file = dwo_unit->dwo_file;
13477 if (dwo_file->tus != NULL)
13478 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13481 /* Free all resources associated with DWO_FILE.
13482 Close the DWO file and munmap the sections.
13483 All memory should be on the objfile obstack. */
13486 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
13489 /* Note: dbfd is NULL for virtual DWO files. */
13490 gdb_bfd_unref (dwo_file->dbfd);
13492 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13495 /* Wrapper for free_dwo_file for use in cleanups. */
13498 free_dwo_file_cleanup (void *arg)
13500 struct dwo_file *dwo_file = (struct dwo_file *) arg;
13501 struct objfile *objfile = dwarf2_per_objfile->objfile;
13503 free_dwo_file (dwo_file, objfile);
13506 /* Traversal function for free_dwo_files. */
13509 free_dwo_file_from_slot (void **slot, void *info)
13511 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13512 struct objfile *objfile = (struct objfile *) info;
13514 free_dwo_file (dwo_file, objfile);
13519 /* Free all resources associated with DWO_FILES. */
13522 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13524 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
13527 /* Read in various DIEs. */
13529 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13530 Inherit only the children of the DW_AT_abstract_origin DIE not being
13531 already referenced by DW_AT_abstract_origin from the children of the
13535 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13537 struct die_info *child_die;
13538 sect_offset *offsetp;
13539 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13540 struct die_info *origin_die;
13541 /* Iterator of the ORIGIN_DIE children. */
13542 struct die_info *origin_child_die;
13543 struct attribute *attr;
13544 struct dwarf2_cu *origin_cu;
13545 struct pending **origin_previous_list_in_scope;
13547 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13551 /* Note that following die references may follow to a die in a
13555 origin_die = follow_die_ref (die, attr, &origin_cu);
13557 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13559 origin_previous_list_in_scope = origin_cu->list_in_scope;
13560 origin_cu->list_in_scope = cu->list_in_scope;
13562 if (die->tag != origin_die->tag
13563 && !(die->tag == DW_TAG_inlined_subroutine
13564 && origin_die->tag == DW_TAG_subprogram))
13565 complaint (&symfile_complaints,
13566 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
13567 to_underlying (die->sect_off),
13568 to_underlying (origin_die->sect_off));
13570 std::vector<sect_offset> offsets;
13572 for (child_die = die->child;
13573 child_die && child_die->tag;
13574 child_die = sibling_die (child_die))
13576 struct die_info *child_origin_die;
13577 struct dwarf2_cu *child_origin_cu;
13579 /* We are trying to process concrete instance entries:
13580 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13581 it's not relevant to our analysis here. i.e. detecting DIEs that are
13582 present in the abstract instance but not referenced in the concrete
13584 if (child_die->tag == DW_TAG_call_site
13585 || child_die->tag == DW_TAG_GNU_call_site)
13588 /* For each CHILD_DIE, find the corresponding child of
13589 ORIGIN_DIE. If there is more than one layer of
13590 DW_AT_abstract_origin, follow them all; there shouldn't be,
13591 but GCC versions at least through 4.4 generate this (GCC PR
13593 child_origin_die = child_die;
13594 child_origin_cu = cu;
13597 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13601 child_origin_die = follow_die_ref (child_origin_die, attr,
13605 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13606 counterpart may exist. */
13607 if (child_origin_die != child_die)
13609 if (child_die->tag != child_origin_die->tag
13610 && !(child_die->tag == DW_TAG_inlined_subroutine
13611 && child_origin_die->tag == DW_TAG_subprogram))
13612 complaint (&symfile_complaints,
13613 _("Child DIE 0x%x and its abstract origin 0x%x have "
13615 to_underlying (child_die->sect_off),
13616 to_underlying (child_origin_die->sect_off));
13617 if (child_origin_die->parent != origin_die)
13618 complaint (&symfile_complaints,
13619 _("Child DIE 0x%x and its abstract origin 0x%x have "
13620 "different parents"),
13621 to_underlying (child_die->sect_off),
13622 to_underlying (child_origin_die->sect_off));
13624 offsets.push_back (child_origin_die->sect_off);
13627 std::sort (offsets.begin (), offsets.end ());
13628 sect_offset *offsets_end = offsets.data () + offsets.size ();
13629 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13630 if (offsetp[-1] == *offsetp)
13631 complaint (&symfile_complaints,
13632 _("Multiple children of DIE 0x%x refer "
13633 "to DIE 0x%x as their abstract origin"),
13634 to_underlying (die->sect_off), to_underlying (*offsetp));
13636 offsetp = offsets.data ();
13637 origin_child_die = origin_die->child;
13638 while (origin_child_die && origin_child_die->tag)
13640 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13641 while (offsetp < offsets_end
13642 && *offsetp < origin_child_die->sect_off)
13644 if (offsetp >= offsets_end
13645 || *offsetp > origin_child_die->sect_off)
13647 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13648 Check whether we're already processing ORIGIN_CHILD_DIE.
13649 This can happen with mutually referenced abstract_origins.
13651 if (!origin_child_die->in_process)
13652 process_die (origin_child_die, origin_cu);
13654 origin_child_die = sibling_die (origin_child_die);
13656 origin_cu->list_in_scope = origin_previous_list_in_scope;
13660 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13662 struct objfile *objfile = cu->objfile;
13663 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13664 struct context_stack *newobj;
13667 struct die_info *child_die;
13668 struct attribute *attr, *call_line, *call_file;
13670 CORE_ADDR baseaddr;
13671 struct block *block;
13672 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13673 std::vector<struct symbol *> template_args;
13674 struct template_symbol *templ_func = NULL;
13678 /* If we do not have call site information, we can't show the
13679 caller of this inlined function. That's too confusing, so
13680 only use the scope for local variables. */
13681 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13682 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13683 if (call_line == NULL || call_file == NULL)
13685 read_lexical_block_scope (die, cu);
13690 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13692 name = dwarf2_name (die, cu);
13694 /* Ignore functions with missing or empty names. These are actually
13695 illegal according to the DWARF standard. */
13698 complaint (&symfile_complaints,
13699 _("missing name for subprogram DIE at %d"),
13700 to_underlying (die->sect_off));
13704 /* Ignore functions with missing or invalid low and high pc attributes. */
13705 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13706 <= PC_BOUNDS_INVALID)
13708 attr = dwarf2_attr (die, DW_AT_external, cu);
13709 if (!attr || !DW_UNSND (attr))
13710 complaint (&symfile_complaints,
13711 _("cannot get low and high bounds "
13712 "for subprogram DIE at %d"),
13713 to_underlying (die->sect_off));
13717 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13718 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13720 /* If we have any template arguments, then we must allocate a
13721 different sort of symbol. */
13722 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13724 if (child_die->tag == DW_TAG_template_type_param
13725 || child_die->tag == DW_TAG_template_value_param)
13727 templ_func = allocate_template_symbol (objfile);
13728 templ_func->subclass = SYMBOL_TEMPLATE;
13733 newobj = push_context (0, lowpc);
13734 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
13735 (struct symbol *) templ_func);
13737 /* If there is a location expression for DW_AT_frame_base, record
13739 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13741 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13743 /* If there is a location for the static link, record it. */
13744 newobj->static_link = NULL;
13745 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13748 newobj->static_link
13749 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13750 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13753 cu->list_in_scope = &local_symbols;
13755 if (die->child != NULL)
13757 child_die = die->child;
13758 while (child_die && child_die->tag)
13760 if (child_die->tag == DW_TAG_template_type_param
13761 || child_die->tag == DW_TAG_template_value_param)
13763 struct symbol *arg = new_symbol (child_die, NULL, cu);
13766 template_args.push_back (arg);
13769 process_die (child_die, cu);
13770 child_die = sibling_die (child_die);
13774 inherit_abstract_dies (die, cu);
13776 /* If we have a DW_AT_specification, we might need to import using
13777 directives from the context of the specification DIE. See the
13778 comment in determine_prefix. */
13779 if (cu->language == language_cplus
13780 && dwarf2_attr (die, DW_AT_specification, cu))
13782 struct dwarf2_cu *spec_cu = cu;
13783 struct die_info *spec_die = die_specification (die, &spec_cu);
13787 child_die = spec_die->child;
13788 while (child_die && child_die->tag)
13790 if (child_die->tag == DW_TAG_imported_module)
13791 process_die (child_die, spec_cu);
13792 child_die = sibling_die (child_die);
13795 /* In some cases, GCC generates specification DIEs that
13796 themselves contain DW_AT_specification attributes. */
13797 spec_die = die_specification (spec_die, &spec_cu);
13801 newobj = pop_context ();
13802 /* Make a block for the local symbols within. */
13803 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
13804 newobj->static_link, lowpc, highpc);
13806 /* For C++, set the block's scope. */
13807 if ((cu->language == language_cplus
13808 || cu->language == language_fortran
13809 || cu->language == language_d
13810 || cu->language == language_rust)
13811 && cu->processing_has_namespace_info)
13812 block_set_scope (block, determine_prefix (die, cu),
13813 &objfile->objfile_obstack);
13815 /* If we have address ranges, record them. */
13816 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13818 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
13820 /* Attach template arguments to function. */
13821 if (!template_args.empty ())
13823 gdb_assert (templ_func != NULL);
13825 templ_func->n_template_arguments = template_args.size ();
13826 templ_func->template_arguments
13827 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13828 templ_func->n_template_arguments);
13829 memcpy (templ_func->template_arguments,
13830 template_args.data (),
13831 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13834 /* In C++, we can have functions nested inside functions (e.g., when
13835 a function declares a class that has methods). This means that
13836 when we finish processing a function scope, we may need to go
13837 back to building a containing block's symbol lists. */
13838 local_symbols = newobj->locals;
13839 local_using_directives = newobj->local_using_directives;
13841 /* If we've finished processing a top-level function, subsequent
13842 symbols go in the file symbol list. */
13843 if (outermost_context_p ())
13844 cu->list_in_scope = &file_symbols;
13847 /* Process all the DIES contained within a lexical block scope. Start
13848 a new scope, process the dies, and then close the scope. */
13851 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13853 struct objfile *objfile = cu->objfile;
13854 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13855 struct context_stack *newobj;
13856 CORE_ADDR lowpc, highpc;
13857 struct die_info *child_die;
13858 CORE_ADDR baseaddr;
13860 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13862 /* Ignore blocks with missing or invalid low and high pc attributes. */
13863 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13864 as multiple lexical blocks? Handling children in a sane way would
13865 be nasty. Might be easier to properly extend generic blocks to
13866 describe ranges. */
13867 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13869 case PC_BOUNDS_NOT_PRESENT:
13870 /* DW_TAG_lexical_block has no attributes, process its children as if
13871 there was no wrapping by that DW_TAG_lexical_block.
13872 GCC does no longer produces such DWARF since GCC r224161. */
13873 for (child_die = die->child;
13874 child_die != NULL && child_die->tag;
13875 child_die = sibling_die (child_die))
13876 process_die (child_die, cu);
13878 case PC_BOUNDS_INVALID:
13881 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13882 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13884 push_context (0, lowpc);
13885 if (die->child != NULL)
13887 child_die = die->child;
13888 while (child_die && child_die->tag)
13890 process_die (child_die, cu);
13891 child_die = sibling_die (child_die);
13894 inherit_abstract_dies (die, cu);
13895 newobj = pop_context ();
13897 if (local_symbols != NULL || local_using_directives != NULL)
13899 struct block *block
13900 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
13901 newobj->start_addr, highpc);
13903 /* Note that recording ranges after traversing children, as we
13904 do here, means that recording a parent's ranges entails
13905 walking across all its children's ranges as they appear in
13906 the address map, which is quadratic behavior.
13908 It would be nicer to record the parent's ranges before
13909 traversing its children, simply overriding whatever you find
13910 there. But since we don't even decide whether to create a
13911 block until after we've traversed its children, that's hard
13913 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13915 local_symbols = newobj->locals;
13916 local_using_directives = newobj->local_using_directives;
13919 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13922 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13924 struct objfile *objfile = cu->objfile;
13925 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13926 CORE_ADDR pc, baseaddr;
13927 struct attribute *attr;
13928 struct call_site *call_site, call_site_local;
13931 struct die_info *child_die;
13933 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13935 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13938 /* This was a pre-DWARF-5 GNU extension alias
13939 for DW_AT_call_return_pc. */
13940 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13944 complaint (&symfile_complaints,
13945 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
13946 "DIE 0x%x [in module %s]"),
13947 to_underlying (die->sect_off), objfile_name (objfile));
13950 pc = attr_value_as_address (attr) + baseaddr;
13951 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13953 if (cu->call_site_htab == NULL)
13954 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13955 NULL, &objfile->objfile_obstack,
13956 hashtab_obstack_allocate, NULL);
13957 call_site_local.pc = pc;
13958 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13961 complaint (&symfile_complaints,
13962 _("Duplicate PC %s for DW_TAG_call_site "
13963 "DIE 0x%x [in module %s]"),
13964 paddress (gdbarch, pc), to_underlying (die->sect_off),
13965 objfile_name (objfile));
13969 /* Count parameters at the caller. */
13972 for (child_die = die->child; child_die && child_die->tag;
13973 child_die = sibling_die (child_die))
13975 if (child_die->tag != DW_TAG_call_site_parameter
13976 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13978 complaint (&symfile_complaints,
13979 _("Tag %d is not DW_TAG_call_site_parameter in "
13980 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
13981 child_die->tag, to_underlying (child_die->sect_off),
13982 objfile_name (objfile));
13990 = ((struct call_site *)
13991 obstack_alloc (&objfile->objfile_obstack,
13992 sizeof (*call_site)
13993 + (sizeof (*call_site->parameter) * (nparams - 1))));
13995 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13996 call_site->pc = pc;
13998 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13999 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14001 struct die_info *func_die;
14003 /* Skip also over DW_TAG_inlined_subroutine. */
14004 for (func_die = die->parent;
14005 func_die && func_die->tag != DW_TAG_subprogram
14006 && func_die->tag != DW_TAG_subroutine_type;
14007 func_die = func_die->parent);
14009 /* DW_AT_call_all_calls is a superset
14010 of DW_AT_call_all_tail_calls. */
14012 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14013 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14014 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14015 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14017 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14018 not complete. But keep CALL_SITE for look ups via call_site_htab,
14019 both the initial caller containing the real return address PC and
14020 the final callee containing the current PC of a chain of tail
14021 calls do not need to have the tail call list complete. But any
14022 function candidate for a virtual tail call frame searched via
14023 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14024 determined unambiguously. */
14028 struct type *func_type = NULL;
14031 func_type = get_die_type (func_die, cu);
14032 if (func_type != NULL)
14034 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14036 /* Enlist this call site to the function. */
14037 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14038 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14041 complaint (&symfile_complaints,
14042 _("Cannot find function owning DW_TAG_call_site "
14043 "DIE 0x%x [in module %s]"),
14044 to_underlying (die->sect_off), objfile_name (objfile));
14048 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14050 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14052 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14055 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14056 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14058 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14059 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14060 /* Keep NULL DWARF_BLOCK. */;
14061 else if (attr_form_is_block (attr))
14063 struct dwarf2_locexpr_baton *dlbaton;
14065 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14066 dlbaton->data = DW_BLOCK (attr)->data;
14067 dlbaton->size = DW_BLOCK (attr)->size;
14068 dlbaton->per_cu = cu->per_cu;
14070 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14072 else if (attr_form_is_ref (attr))
14074 struct dwarf2_cu *target_cu = cu;
14075 struct die_info *target_die;
14077 target_die = follow_die_ref (die, attr, &target_cu);
14078 gdb_assert (target_cu->objfile == objfile);
14079 if (die_is_declaration (target_die, target_cu))
14081 const char *target_physname;
14083 /* Prefer the mangled name; otherwise compute the demangled one. */
14084 target_physname = dw2_linkage_name (target_die, target_cu);
14085 if (target_physname == NULL)
14086 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14087 if (target_physname == NULL)
14088 complaint (&symfile_complaints,
14089 _("DW_AT_call_target target DIE has invalid "
14090 "physname, for referencing DIE 0x%x [in module %s]"),
14091 to_underlying (die->sect_off), objfile_name (objfile));
14093 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14099 /* DW_AT_entry_pc should be preferred. */
14100 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14101 <= PC_BOUNDS_INVALID)
14102 complaint (&symfile_complaints,
14103 _("DW_AT_call_target target DIE has invalid "
14104 "low pc, for referencing DIE 0x%x [in module %s]"),
14105 to_underlying (die->sect_off), objfile_name (objfile));
14108 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14109 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14114 complaint (&symfile_complaints,
14115 _("DW_TAG_call_site DW_AT_call_target is neither "
14116 "block nor reference, for DIE 0x%x [in module %s]"),
14117 to_underlying (die->sect_off), objfile_name (objfile));
14119 call_site->per_cu = cu->per_cu;
14121 for (child_die = die->child;
14122 child_die && child_die->tag;
14123 child_die = sibling_die (child_die))
14125 struct call_site_parameter *parameter;
14126 struct attribute *loc, *origin;
14128 if (child_die->tag != DW_TAG_call_site_parameter
14129 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14131 /* Already printed the complaint above. */
14135 gdb_assert (call_site->parameter_count < nparams);
14136 parameter = &call_site->parameter[call_site->parameter_count];
14138 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14139 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14140 register is contained in DW_AT_call_value. */
14142 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14143 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14144 if (origin == NULL)
14146 /* This was a pre-DWARF-5 GNU extension alias
14147 for DW_AT_call_parameter. */
14148 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14150 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14152 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14154 sect_offset sect_off
14155 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14156 if (!offset_in_cu_p (&cu->header, sect_off))
14158 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14159 binding can be done only inside one CU. Such referenced DIE
14160 therefore cannot be even moved to DW_TAG_partial_unit. */
14161 complaint (&symfile_complaints,
14162 _("DW_AT_call_parameter offset is not in CU for "
14163 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14164 to_underlying (child_die->sect_off),
14165 objfile_name (objfile));
14168 parameter->u.param_cu_off
14169 = (cu_offset) (sect_off - cu->header.sect_off);
14171 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14173 complaint (&symfile_complaints,
14174 _("No DW_FORM_block* DW_AT_location for "
14175 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14176 to_underlying (child_die->sect_off), objfile_name (objfile));
14181 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14182 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14183 if (parameter->u.dwarf_reg != -1)
14184 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14185 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14186 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14187 ¶meter->u.fb_offset))
14188 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14191 complaint (&symfile_complaints,
14192 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14193 "for DW_FORM_block* DW_AT_location is supported for "
14194 "DW_TAG_call_site child DIE 0x%x "
14196 to_underlying (child_die->sect_off),
14197 objfile_name (objfile));
14202 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14204 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14205 if (!attr_form_is_block (attr))
14207 complaint (&symfile_complaints,
14208 _("No DW_FORM_block* DW_AT_call_value for "
14209 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14210 to_underlying (child_die->sect_off),
14211 objfile_name (objfile));
14214 parameter->value = DW_BLOCK (attr)->data;
14215 parameter->value_size = DW_BLOCK (attr)->size;
14217 /* Parameters are not pre-cleared by memset above. */
14218 parameter->data_value = NULL;
14219 parameter->data_value_size = 0;
14220 call_site->parameter_count++;
14222 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14224 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14227 if (!attr_form_is_block (attr))
14228 complaint (&symfile_complaints,
14229 _("No DW_FORM_block* DW_AT_call_data_value for "
14230 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14231 to_underlying (child_die->sect_off),
14232 objfile_name (objfile));
14235 parameter->data_value = DW_BLOCK (attr)->data;
14236 parameter->data_value_size = DW_BLOCK (attr)->size;
14242 /* Helper function for read_variable. If DIE represents a virtual
14243 table, then return the type of the concrete object that is
14244 associated with the virtual table. Otherwise, return NULL. */
14246 static struct type *
14247 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14249 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14253 /* Find the type DIE. */
14254 struct die_info *type_die = NULL;
14255 struct dwarf2_cu *type_cu = cu;
14257 if (attr_form_is_ref (attr))
14258 type_die = follow_die_ref (die, attr, &type_cu);
14259 if (type_die == NULL)
14262 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14264 return die_containing_type (type_die, type_cu);
14267 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14270 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14272 struct rust_vtable_symbol *storage = NULL;
14274 if (cu->language == language_rust)
14276 struct type *containing_type = rust_containing_type (die, cu);
14278 if (containing_type != NULL)
14280 struct objfile *objfile = cu->objfile;
14282 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14283 struct rust_vtable_symbol);
14284 initialize_objfile_symbol (storage);
14285 storage->concrete_type = containing_type;
14286 storage->subclass = SYMBOL_RUST_VTABLE;
14290 new_symbol_full (die, NULL, cu, storage);
14293 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14294 reading .debug_rnglists.
14295 Callback's type should be:
14296 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14297 Return true if the attributes are present and valid, otherwise,
14300 template <typename Callback>
14302 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14303 Callback &&callback)
14305 struct objfile *objfile = cu->objfile;
14306 bfd *obfd = objfile->obfd;
14307 /* Base address selection entry. */
14310 const gdb_byte *buffer;
14311 CORE_ADDR baseaddr;
14312 bool overflow = false;
14314 found_base = cu->base_known;
14315 base = cu->base_address;
14317 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14318 if (offset >= dwarf2_per_objfile->rnglists.size)
14320 complaint (&symfile_complaints,
14321 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14325 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14327 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14331 /* Initialize it due to a false compiler warning. */
14332 CORE_ADDR range_beginning = 0, range_end = 0;
14333 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14334 + dwarf2_per_objfile->rnglists.size);
14335 unsigned int bytes_read;
14337 if (buffer == buf_end)
14342 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14345 case DW_RLE_end_of_list:
14347 case DW_RLE_base_address:
14348 if (buffer + cu->header.addr_size > buf_end)
14353 base = read_address (obfd, buffer, cu, &bytes_read);
14355 buffer += bytes_read;
14357 case DW_RLE_start_length:
14358 if (buffer + cu->header.addr_size > buf_end)
14363 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14364 buffer += bytes_read;
14365 range_end = (range_beginning
14366 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14367 buffer += bytes_read;
14368 if (buffer > buf_end)
14374 case DW_RLE_offset_pair:
14375 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14376 buffer += bytes_read;
14377 if (buffer > buf_end)
14382 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14383 buffer += bytes_read;
14384 if (buffer > buf_end)
14390 case DW_RLE_start_end:
14391 if (buffer + 2 * cu->header.addr_size > buf_end)
14396 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14397 buffer += bytes_read;
14398 range_end = read_address (obfd, buffer, cu, &bytes_read);
14399 buffer += bytes_read;
14402 complaint (&symfile_complaints,
14403 _("Invalid .debug_rnglists data (no base address)"));
14406 if (rlet == DW_RLE_end_of_list || overflow)
14408 if (rlet == DW_RLE_base_address)
14413 /* We have no valid base address for the ranges
14415 complaint (&symfile_complaints,
14416 _("Invalid .debug_rnglists data (no base address)"));
14420 if (range_beginning > range_end)
14422 /* Inverted range entries are invalid. */
14423 complaint (&symfile_complaints,
14424 _("Invalid .debug_rnglists data (inverted range)"));
14428 /* Empty range entries have no effect. */
14429 if (range_beginning == range_end)
14432 range_beginning += base;
14435 /* A not-uncommon case of bad debug info.
14436 Don't pollute the addrmap with bad data. */
14437 if (range_beginning + baseaddr == 0
14438 && !dwarf2_per_objfile->has_section_at_zero)
14440 complaint (&symfile_complaints,
14441 _(".debug_rnglists entry has start address of zero"
14442 " [in module %s]"), objfile_name (objfile));
14446 callback (range_beginning, range_end);
14451 complaint (&symfile_complaints,
14452 _("Offset %d is not terminated "
14453 "for DW_AT_ranges attribute"),
14461 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14462 Callback's type should be:
14463 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14464 Return 1 if the attributes are present and valid, otherwise, return 0. */
14466 template <typename Callback>
14468 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14469 Callback &&callback)
14471 struct objfile *objfile = cu->objfile;
14472 struct comp_unit_head *cu_header = &cu->header;
14473 bfd *obfd = objfile->obfd;
14474 unsigned int addr_size = cu_header->addr_size;
14475 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14476 /* Base address selection entry. */
14479 unsigned int dummy;
14480 const gdb_byte *buffer;
14481 CORE_ADDR baseaddr;
14483 if (cu_header->version >= 5)
14484 return dwarf2_rnglists_process (offset, cu, callback);
14486 found_base = cu->base_known;
14487 base = cu->base_address;
14489 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14490 if (offset >= dwarf2_per_objfile->ranges.size)
14492 complaint (&symfile_complaints,
14493 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14497 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14499 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14503 CORE_ADDR range_beginning, range_end;
14505 range_beginning = read_address (obfd, buffer, cu, &dummy);
14506 buffer += addr_size;
14507 range_end = read_address (obfd, buffer, cu, &dummy);
14508 buffer += addr_size;
14509 offset += 2 * addr_size;
14511 /* An end of list marker is a pair of zero addresses. */
14512 if (range_beginning == 0 && range_end == 0)
14513 /* Found the end of list entry. */
14516 /* Each base address selection entry is a pair of 2 values.
14517 The first is the largest possible address, the second is
14518 the base address. Check for a base address here. */
14519 if ((range_beginning & mask) == mask)
14521 /* If we found the largest possible address, then we already
14522 have the base address in range_end. */
14530 /* We have no valid base address for the ranges
14532 complaint (&symfile_complaints,
14533 _("Invalid .debug_ranges data (no base address)"));
14537 if (range_beginning > range_end)
14539 /* Inverted range entries are invalid. */
14540 complaint (&symfile_complaints,
14541 _("Invalid .debug_ranges data (inverted range)"));
14545 /* Empty range entries have no effect. */
14546 if (range_beginning == range_end)
14549 range_beginning += base;
14552 /* A not-uncommon case of bad debug info.
14553 Don't pollute the addrmap with bad data. */
14554 if (range_beginning + baseaddr == 0
14555 && !dwarf2_per_objfile->has_section_at_zero)
14557 complaint (&symfile_complaints,
14558 _(".debug_ranges entry has start address of zero"
14559 " [in module %s]"), objfile_name (objfile));
14563 callback (range_beginning, range_end);
14569 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14570 Return 1 if the attributes are present and valid, otherwise, return 0.
14571 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14574 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14575 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14576 struct partial_symtab *ranges_pst)
14578 struct objfile *objfile = cu->objfile;
14579 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14580 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14581 SECT_OFF_TEXT (objfile));
14584 CORE_ADDR high = 0;
14587 retval = dwarf2_ranges_process (offset, cu,
14588 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14590 if (ranges_pst != NULL)
14595 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14596 range_beginning + baseaddr);
14597 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14598 range_end + baseaddr);
14599 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14603 /* FIXME: This is recording everything as a low-high
14604 segment of consecutive addresses. We should have a
14605 data structure for discontiguous block ranges
14609 low = range_beginning;
14615 if (range_beginning < low)
14616 low = range_beginning;
14617 if (range_end > high)
14625 /* If the first entry is an end-of-list marker, the range
14626 describes an empty scope, i.e. no instructions. */
14632 *high_return = high;
14636 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14637 definition for the return value. *LOWPC and *HIGHPC are set iff
14638 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14640 static enum pc_bounds_kind
14641 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14642 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14643 struct partial_symtab *pst)
14645 struct attribute *attr;
14646 struct attribute *attr_high;
14648 CORE_ADDR high = 0;
14649 enum pc_bounds_kind ret;
14651 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14654 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14657 low = attr_value_as_address (attr);
14658 high = attr_value_as_address (attr_high);
14659 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14663 /* Found high w/o low attribute. */
14664 return PC_BOUNDS_INVALID;
14666 /* Found consecutive range of addresses. */
14667 ret = PC_BOUNDS_HIGH_LOW;
14671 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14674 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14675 We take advantage of the fact that DW_AT_ranges does not appear
14676 in DW_TAG_compile_unit of DWO files. */
14677 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14678 unsigned int ranges_offset = (DW_UNSND (attr)
14679 + (need_ranges_base
14683 /* Value of the DW_AT_ranges attribute is the offset in the
14684 .debug_ranges section. */
14685 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14686 return PC_BOUNDS_INVALID;
14687 /* Found discontinuous range of addresses. */
14688 ret = PC_BOUNDS_RANGES;
14691 return PC_BOUNDS_NOT_PRESENT;
14694 /* read_partial_die has also the strict LOW < HIGH requirement. */
14696 return PC_BOUNDS_INVALID;
14698 /* When using the GNU linker, .gnu.linkonce. sections are used to
14699 eliminate duplicate copies of functions and vtables and such.
14700 The linker will arbitrarily choose one and discard the others.
14701 The AT_*_pc values for such functions refer to local labels in
14702 these sections. If the section from that file was discarded, the
14703 labels are not in the output, so the relocs get a value of 0.
14704 If this is a discarded function, mark the pc bounds as invalid,
14705 so that GDB will ignore it. */
14706 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14707 return PC_BOUNDS_INVALID;
14715 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14716 its low and high PC addresses. Do nothing if these addresses could not
14717 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14718 and HIGHPC to the high address if greater than HIGHPC. */
14721 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14722 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14723 struct dwarf2_cu *cu)
14725 CORE_ADDR low, high;
14726 struct die_info *child = die->child;
14728 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14730 *lowpc = std::min (*lowpc, low);
14731 *highpc = std::max (*highpc, high);
14734 /* If the language does not allow nested subprograms (either inside
14735 subprograms or lexical blocks), we're done. */
14736 if (cu->language != language_ada)
14739 /* Check all the children of the given DIE. If it contains nested
14740 subprograms, then check their pc bounds. Likewise, we need to
14741 check lexical blocks as well, as they may also contain subprogram
14743 while (child && child->tag)
14745 if (child->tag == DW_TAG_subprogram
14746 || child->tag == DW_TAG_lexical_block)
14747 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14748 child = sibling_die (child);
14752 /* Get the low and high pc's represented by the scope DIE, and store
14753 them in *LOWPC and *HIGHPC. If the correct values can't be
14754 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14757 get_scope_pc_bounds (struct die_info *die,
14758 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14759 struct dwarf2_cu *cu)
14761 CORE_ADDR best_low = (CORE_ADDR) -1;
14762 CORE_ADDR best_high = (CORE_ADDR) 0;
14763 CORE_ADDR current_low, current_high;
14765 if (dwarf2_get_pc_bounds (die, ¤t_low, ¤t_high, cu, NULL)
14766 >= PC_BOUNDS_RANGES)
14768 best_low = current_low;
14769 best_high = current_high;
14773 struct die_info *child = die->child;
14775 while (child && child->tag)
14777 switch (child->tag) {
14778 case DW_TAG_subprogram:
14779 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14781 case DW_TAG_namespace:
14782 case DW_TAG_module:
14783 /* FIXME: carlton/2004-01-16: Should we do this for
14784 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14785 that current GCC's always emit the DIEs corresponding
14786 to definitions of methods of classes as children of a
14787 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14788 the DIEs giving the declarations, which could be
14789 anywhere). But I don't see any reason why the
14790 standards says that they have to be there. */
14791 get_scope_pc_bounds (child, ¤t_low, ¤t_high, cu);
14793 if (current_low != ((CORE_ADDR) -1))
14795 best_low = std::min (best_low, current_low);
14796 best_high = std::max (best_high, current_high);
14804 child = sibling_die (child);
14809 *highpc = best_high;
14812 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14816 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14817 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14819 struct objfile *objfile = cu->objfile;
14820 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14821 struct attribute *attr;
14822 struct attribute *attr_high;
14824 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14827 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14830 CORE_ADDR low = attr_value_as_address (attr);
14831 CORE_ADDR high = attr_value_as_address (attr_high);
14833 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14836 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14837 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14838 record_block_range (block, low, high - 1);
14842 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14845 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14846 We take advantage of the fact that DW_AT_ranges does not appear
14847 in DW_TAG_compile_unit of DWO files. */
14848 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14850 /* The value of the DW_AT_ranges attribute is the offset of the
14851 address range list in the .debug_ranges section. */
14852 unsigned long offset = (DW_UNSND (attr)
14853 + (need_ranges_base ? cu->ranges_base : 0));
14854 const gdb_byte *buffer;
14856 /* For some target architectures, but not others, the
14857 read_address function sign-extends the addresses it returns.
14858 To recognize base address selection entries, we need a
14860 unsigned int addr_size = cu->header.addr_size;
14861 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14863 /* The base address, to which the next pair is relative. Note
14864 that this 'base' is a DWARF concept: most entries in a range
14865 list are relative, to reduce the number of relocs against the
14866 debugging information. This is separate from this function's
14867 'baseaddr' argument, which GDB uses to relocate debugging
14868 information from a shared library based on the address at
14869 which the library was loaded. */
14870 CORE_ADDR base = cu->base_address;
14871 int base_known = cu->base_known;
14873 dwarf2_ranges_process (offset, cu,
14874 [&] (CORE_ADDR start, CORE_ADDR end)
14878 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14879 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14880 record_block_range (block, start, end - 1);
14885 /* Check whether the producer field indicates either of GCC < 4.6, or the
14886 Intel C/C++ compiler, and cache the result in CU. */
14889 check_producer (struct dwarf2_cu *cu)
14893 if (cu->producer == NULL)
14895 /* For unknown compilers expect their behavior is DWARF version
14898 GCC started to support .debug_types sections by -gdwarf-4 since
14899 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14900 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14901 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14902 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14904 else if (producer_is_gcc (cu->producer, &major, &minor))
14906 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14907 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14909 else if (producer_is_icc (cu->producer, &major, &minor))
14910 cu->producer_is_icc_lt_14 = major < 14;
14913 /* For other non-GCC compilers, expect their behavior is DWARF version
14917 cu->checked_producer = 1;
14920 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14921 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14922 during 4.6.0 experimental. */
14925 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14927 if (!cu->checked_producer)
14928 check_producer (cu);
14930 return cu->producer_is_gxx_lt_4_6;
14933 /* Return the default accessibility type if it is not overriden by
14934 DW_AT_accessibility. */
14936 static enum dwarf_access_attribute
14937 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14939 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14941 /* The default DWARF 2 accessibility for members is public, the default
14942 accessibility for inheritance is private. */
14944 if (die->tag != DW_TAG_inheritance)
14945 return DW_ACCESS_public;
14947 return DW_ACCESS_private;
14951 /* DWARF 3+ defines the default accessibility a different way. The same
14952 rules apply now for DW_TAG_inheritance as for the members and it only
14953 depends on the container kind. */
14955 if (die->parent->tag == DW_TAG_class_type)
14956 return DW_ACCESS_private;
14958 return DW_ACCESS_public;
14962 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14963 offset. If the attribute was not found return 0, otherwise return
14964 1. If it was found but could not properly be handled, set *OFFSET
14968 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14971 struct attribute *attr;
14973 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14978 /* Note that we do not check for a section offset first here.
14979 This is because DW_AT_data_member_location is new in DWARF 4,
14980 so if we see it, we can assume that a constant form is really
14981 a constant and not a section offset. */
14982 if (attr_form_is_constant (attr))
14983 *offset = dwarf2_get_attr_constant_value (attr, 0);
14984 else if (attr_form_is_section_offset (attr))
14985 dwarf2_complex_location_expr_complaint ();
14986 else if (attr_form_is_block (attr))
14987 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14989 dwarf2_complex_location_expr_complaint ();
14997 /* Add an aggregate field to the field list. */
15000 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15001 struct dwarf2_cu *cu)
15003 struct objfile *objfile = cu->objfile;
15004 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15005 struct nextfield *new_field;
15006 struct attribute *attr;
15008 const char *fieldname = "";
15010 /* Allocate a new field list entry and link it in. */
15011 new_field = XNEW (struct nextfield);
15012 make_cleanup (xfree, new_field);
15013 memset (new_field, 0, sizeof (struct nextfield));
15015 if (die->tag == DW_TAG_inheritance)
15017 new_field->next = fip->baseclasses;
15018 fip->baseclasses = new_field;
15022 new_field->next = fip->fields;
15023 fip->fields = new_field;
15027 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15029 new_field->accessibility = DW_UNSND (attr);
15031 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15032 if (new_field->accessibility != DW_ACCESS_public)
15033 fip->non_public_fields = 1;
15035 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15037 new_field->virtuality = DW_UNSND (attr);
15039 new_field->virtuality = DW_VIRTUALITY_none;
15041 fp = &new_field->field;
15043 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15047 /* Data member other than a C++ static data member. */
15049 /* Get type of field. */
15050 fp->type = die_type (die, cu);
15052 SET_FIELD_BITPOS (*fp, 0);
15054 /* Get bit size of field (zero if none). */
15055 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15058 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15062 FIELD_BITSIZE (*fp) = 0;
15065 /* Get bit offset of field. */
15066 if (handle_data_member_location (die, cu, &offset))
15067 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15068 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15071 if (gdbarch_bits_big_endian (gdbarch))
15073 /* For big endian bits, the DW_AT_bit_offset gives the
15074 additional bit offset from the MSB of the containing
15075 anonymous object to the MSB of the field. We don't
15076 have to do anything special since we don't need to
15077 know the size of the anonymous object. */
15078 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15082 /* For little endian bits, compute the bit offset to the
15083 MSB of the anonymous object, subtract off the number of
15084 bits from the MSB of the field to the MSB of the
15085 object, and then subtract off the number of bits of
15086 the field itself. The result is the bit offset of
15087 the LSB of the field. */
15088 int anonymous_size;
15089 int bit_offset = DW_UNSND (attr);
15091 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15094 /* The size of the anonymous object containing
15095 the bit field is explicit, so use the
15096 indicated size (in bytes). */
15097 anonymous_size = DW_UNSND (attr);
15101 /* The size of the anonymous object containing
15102 the bit field must be inferred from the type
15103 attribute of the data member containing the
15105 anonymous_size = TYPE_LENGTH (fp->type);
15107 SET_FIELD_BITPOS (*fp,
15108 (FIELD_BITPOS (*fp)
15109 + anonymous_size * bits_per_byte
15110 - bit_offset - FIELD_BITSIZE (*fp)));
15113 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15115 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15116 + dwarf2_get_attr_constant_value (attr, 0)));
15118 /* Get name of field. */
15119 fieldname = dwarf2_name (die, cu);
15120 if (fieldname == NULL)
15123 /* The name is already allocated along with this objfile, so we don't
15124 need to duplicate it for the type. */
15125 fp->name = fieldname;
15127 /* Change accessibility for artificial fields (e.g. virtual table
15128 pointer or virtual base class pointer) to private. */
15129 if (dwarf2_attr (die, DW_AT_artificial, cu))
15131 FIELD_ARTIFICIAL (*fp) = 1;
15132 new_field->accessibility = DW_ACCESS_private;
15133 fip->non_public_fields = 1;
15136 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15138 /* C++ static member. */
15140 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15141 is a declaration, but all versions of G++ as of this writing
15142 (so through at least 3.2.1) incorrectly generate
15143 DW_TAG_variable tags. */
15145 const char *physname;
15147 /* Get name of field. */
15148 fieldname = dwarf2_name (die, cu);
15149 if (fieldname == NULL)
15152 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15154 /* Only create a symbol if this is an external value.
15155 new_symbol checks this and puts the value in the global symbol
15156 table, which we want. If it is not external, new_symbol
15157 will try to put the value in cu->list_in_scope which is wrong. */
15158 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15160 /* A static const member, not much different than an enum as far as
15161 we're concerned, except that we can support more types. */
15162 new_symbol (die, NULL, cu);
15165 /* Get physical name. */
15166 physname = dwarf2_physname (fieldname, die, cu);
15168 /* The name is already allocated along with this objfile, so we don't
15169 need to duplicate it for the type. */
15170 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15171 FIELD_TYPE (*fp) = die_type (die, cu);
15172 FIELD_NAME (*fp) = fieldname;
15174 else if (die->tag == DW_TAG_inheritance)
15178 /* C++ base class field. */
15179 if (handle_data_member_location (die, cu, &offset))
15180 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15181 FIELD_BITSIZE (*fp) = 0;
15182 FIELD_TYPE (*fp) = die_type (die, cu);
15183 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15184 fip->nbaseclasses++;
15188 /* Can the type given by DIE define another type? */
15191 type_can_define_types (const struct die_info *die)
15195 case DW_TAG_typedef:
15196 case DW_TAG_class_type:
15197 case DW_TAG_structure_type:
15198 case DW_TAG_union_type:
15199 case DW_TAG_enumeration_type:
15207 /* Add a type definition defined in the scope of the FIP's class. */
15210 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15211 struct dwarf2_cu *cu)
15213 struct decl_field_list *new_field;
15214 struct decl_field *fp;
15216 /* Allocate a new field list entry and link it in. */
15217 new_field = XCNEW (struct decl_field_list);
15218 make_cleanup (xfree, new_field);
15220 gdb_assert (type_can_define_types (die));
15222 fp = &new_field->field;
15224 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15225 fp->name = dwarf2_name (die, cu);
15226 fp->type = read_type_die (die, cu);
15228 /* Save accessibility. */
15229 enum dwarf_access_attribute accessibility;
15230 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15232 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15234 accessibility = dwarf2_default_access_attribute (die, cu);
15235 switch (accessibility)
15237 case DW_ACCESS_public:
15238 /* The assumed value if neither private nor protected. */
15240 case DW_ACCESS_private:
15241 fp->is_private = 1;
15243 case DW_ACCESS_protected:
15244 fp->is_protected = 1;
15247 complaint (&symfile_complaints,
15248 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15251 if (die->tag == DW_TAG_typedef)
15253 new_field->next = fip->typedef_field_list;
15254 fip->typedef_field_list = new_field;
15255 fip->typedef_field_list_count++;
15259 new_field->next = fip->nested_types_list;
15260 fip->nested_types_list = new_field;
15261 fip->nested_types_list_count++;
15265 /* Create the vector of fields, and attach it to the type. */
15268 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15269 struct dwarf2_cu *cu)
15271 int nfields = fip->nfields;
15273 /* Record the field count, allocate space for the array of fields,
15274 and create blank accessibility bitfields if necessary. */
15275 TYPE_NFIELDS (type) = nfields;
15276 TYPE_FIELDS (type) = (struct field *)
15277 TYPE_ALLOC (type, sizeof (struct field) * nfields);
15278 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
15280 if (fip->non_public_fields && cu->language != language_ada)
15282 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15284 TYPE_FIELD_PRIVATE_BITS (type) =
15285 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15286 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15288 TYPE_FIELD_PROTECTED_BITS (type) =
15289 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15290 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15292 TYPE_FIELD_IGNORE_BITS (type) =
15293 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15294 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15297 /* If the type has baseclasses, allocate and clear a bit vector for
15298 TYPE_FIELD_VIRTUAL_BITS. */
15299 if (fip->nbaseclasses && cu->language != language_ada)
15301 int num_bytes = B_BYTES (fip->nbaseclasses);
15302 unsigned char *pointer;
15304 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15305 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15306 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15307 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
15308 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
15311 /* Copy the saved-up fields into the field vector. Start from the head of
15312 the list, adding to the tail of the field array, so that they end up in
15313 the same order in the array in which they were added to the list. */
15314 while (nfields-- > 0)
15316 struct nextfield *fieldp;
15320 fieldp = fip->fields;
15321 fip->fields = fieldp->next;
15325 fieldp = fip->baseclasses;
15326 fip->baseclasses = fieldp->next;
15329 TYPE_FIELD (type, nfields) = fieldp->field;
15330 switch (fieldp->accessibility)
15332 case DW_ACCESS_private:
15333 if (cu->language != language_ada)
15334 SET_TYPE_FIELD_PRIVATE (type, nfields);
15337 case DW_ACCESS_protected:
15338 if (cu->language != language_ada)
15339 SET_TYPE_FIELD_PROTECTED (type, nfields);
15342 case DW_ACCESS_public:
15346 /* Unknown accessibility. Complain and treat it as public. */
15348 complaint (&symfile_complaints, _("unsupported accessibility %d"),
15349 fieldp->accessibility);
15353 if (nfields < fip->nbaseclasses)
15355 switch (fieldp->virtuality)
15357 case DW_VIRTUALITY_virtual:
15358 case DW_VIRTUALITY_pure_virtual:
15359 if (cu->language == language_ada)
15360 error (_("unexpected virtuality in component of Ada type"));
15361 SET_TYPE_FIELD_VIRTUAL (type, nfields);
15368 /* Return true if this member function is a constructor, false
15372 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15374 const char *fieldname;
15375 const char *type_name;
15378 if (die->parent == NULL)
15381 if (die->parent->tag != DW_TAG_structure_type
15382 && die->parent->tag != DW_TAG_union_type
15383 && die->parent->tag != DW_TAG_class_type)
15386 fieldname = dwarf2_name (die, cu);
15387 type_name = dwarf2_name (die->parent, cu);
15388 if (fieldname == NULL || type_name == NULL)
15391 len = strlen (fieldname);
15392 return (strncmp (fieldname, type_name, len) == 0
15393 && (type_name[len] == '\0' || type_name[len] == '<'));
15396 /* Add a member function to the proper fieldlist. */
15399 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15400 struct type *type, struct dwarf2_cu *cu)
15402 struct objfile *objfile = cu->objfile;
15403 struct attribute *attr;
15404 struct fnfieldlist *flp;
15406 struct fn_field *fnp;
15407 const char *fieldname;
15408 struct nextfnfield *new_fnfield;
15409 struct type *this_type;
15410 enum dwarf_access_attribute accessibility;
15412 if (cu->language == language_ada)
15413 error (_("unexpected member function in Ada type"));
15415 /* Get name of member function. */
15416 fieldname = dwarf2_name (die, cu);
15417 if (fieldname == NULL)
15420 /* Look up member function name in fieldlist. */
15421 for (i = 0; i < fip->nfnfields; i++)
15423 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15427 /* Create new list element if necessary. */
15428 if (i < fip->nfnfields)
15429 flp = &fip->fnfieldlists[i];
15432 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
15434 fip->fnfieldlists = (struct fnfieldlist *)
15435 xrealloc (fip->fnfieldlists,
15436 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
15437 * sizeof (struct fnfieldlist));
15438 if (fip->nfnfields == 0)
15439 make_cleanup (free_current_contents, &fip->fnfieldlists);
15441 flp = &fip->fnfieldlists[fip->nfnfields];
15442 flp->name = fieldname;
15445 i = fip->nfnfields++;
15448 /* Create a new member function field and chain it to the field list
15450 new_fnfield = XNEW (struct nextfnfield);
15451 make_cleanup (xfree, new_fnfield);
15452 memset (new_fnfield, 0, sizeof (struct nextfnfield));
15453 new_fnfield->next = flp->head;
15454 flp->head = new_fnfield;
15457 /* Fill in the member function field info. */
15458 fnp = &new_fnfield->fnfield;
15460 /* Delay processing of the physname until later. */
15461 if (cu->language == language_cplus)
15463 add_to_method_list (type, i, flp->length - 1, fieldname,
15468 const char *physname = dwarf2_physname (fieldname, die, cu);
15469 fnp->physname = physname ? physname : "";
15472 fnp->type = alloc_type (objfile);
15473 this_type = read_type_die (die, cu);
15474 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15476 int nparams = TYPE_NFIELDS (this_type);
15478 /* TYPE is the domain of this method, and THIS_TYPE is the type
15479 of the method itself (TYPE_CODE_METHOD). */
15480 smash_to_method_type (fnp->type, type,
15481 TYPE_TARGET_TYPE (this_type),
15482 TYPE_FIELDS (this_type),
15483 TYPE_NFIELDS (this_type),
15484 TYPE_VARARGS (this_type));
15486 /* Handle static member functions.
15487 Dwarf2 has no clean way to discern C++ static and non-static
15488 member functions. G++ helps GDB by marking the first
15489 parameter for non-static member functions (which is the this
15490 pointer) as artificial. We obtain this information from
15491 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15492 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15493 fnp->voffset = VOFFSET_STATIC;
15496 complaint (&symfile_complaints, _("member function type missing for '%s'"),
15497 dwarf2_full_name (fieldname, die, cu));
15499 /* Get fcontext from DW_AT_containing_type if present. */
15500 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15501 fnp->fcontext = die_containing_type (die, cu);
15503 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15504 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15506 /* Get accessibility. */
15507 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15509 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15511 accessibility = dwarf2_default_access_attribute (die, cu);
15512 switch (accessibility)
15514 case DW_ACCESS_private:
15515 fnp->is_private = 1;
15517 case DW_ACCESS_protected:
15518 fnp->is_protected = 1;
15522 /* Check for artificial methods. */
15523 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15524 if (attr && DW_UNSND (attr) != 0)
15525 fnp->is_artificial = 1;
15527 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15529 /* Get index in virtual function table if it is a virtual member
15530 function. For older versions of GCC, this is an offset in the
15531 appropriate virtual table, as specified by DW_AT_containing_type.
15532 For everyone else, it is an expression to be evaluated relative
15533 to the object address. */
15535 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15538 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15540 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15542 /* Old-style GCC. */
15543 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15545 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15546 || (DW_BLOCK (attr)->size > 1
15547 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15548 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15550 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15551 if ((fnp->voffset % cu->header.addr_size) != 0)
15552 dwarf2_complex_location_expr_complaint ();
15554 fnp->voffset /= cu->header.addr_size;
15558 dwarf2_complex_location_expr_complaint ();
15560 if (!fnp->fcontext)
15562 /* If there is no `this' field and no DW_AT_containing_type,
15563 we cannot actually find a base class context for the
15565 if (TYPE_NFIELDS (this_type) == 0
15566 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15568 complaint (&symfile_complaints,
15569 _("cannot determine context for virtual member "
15570 "function \"%s\" (offset %d)"),
15571 fieldname, to_underlying (die->sect_off));
15576 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15580 else if (attr_form_is_section_offset (attr))
15582 dwarf2_complex_location_expr_complaint ();
15586 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15592 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15593 if (attr && DW_UNSND (attr))
15595 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15596 complaint (&symfile_complaints,
15597 _("Member function \"%s\" (offset %d) is virtual "
15598 "but the vtable offset is not specified"),
15599 fieldname, to_underlying (die->sect_off));
15600 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15601 TYPE_CPLUS_DYNAMIC (type) = 1;
15606 /* Create the vector of member function fields, and attach it to the type. */
15609 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15610 struct dwarf2_cu *cu)
15612 struct fnfieldlist *flp;
15615 if (cu->language == language_ada)
15616 error (_("unexpected member functions in Ada type"));
15618 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15619 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15620 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
15622 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
15624 struct nextfnfield *nfp = flp->head;
15625 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15628 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
15629 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
15630 fn_flp->fn_fields = (struct fn_field *)
15631 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
15632 for (k = flp->length; (k--, nfp); nfp = nfp->next)
15633 fn_flp->fn_fields[k] = nfp->fnfield;
15636 TYPE_NFN_FIELDS (type) = fip->nfnfields;
15639 /* Returns non-zero if NAME is the name of a vtable member in CU's
15640 language, zero otherwise. */
15642 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15644 static const char vptr[] = "_vptr";
15646 /* Look for the C++ form of the vtable. */
15647 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15653 /* GCC outputs unnamed structures that are really pointers to member
15654 functions, with the ABI-specified layout. If TYPE describes
15655 such a structure, smash it into a member function type.
15657 GCC shouldn't do this; it should just output pointer to member DIEs.
15658 This is GCC PR debug/28767. */
15661 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15663 struct type *pfn_type, *self_type, *new_type;
15665 /* Check for a structure with no name and two children. */
15666 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15669 /* Check for __pfn and __delta members. */
15670 if (TYPE_FIELD_NAME (type, 0) == NULL
15671 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15672 || TYPE_FIELD_NAME (type, 1) == NULL
15673 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15676 /* Find the type of the method. */
15677 pfn_type = TYPE_FIELD_TYPE (type, 0);
15678 if (pfn_type == NULL
15679 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15680 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15683 /* Look for the "this" argument. */
15684 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15685 if (TYPE_NFIELDS (pfn_type) == 0
15686 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15687 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15690 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15691 new_type = alloc_type (objfile);
15692 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15693 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15694 TYPE_VARARGS (pfn_type));
15695 smash_to_methodptr_type (type, new_type);
15699 /* Called when we find the DIE that starts a structure or union scope
15700 (definition) to create a type for the structure or union. Fill in
15701 the type's name and general properties; the members will not be
15702 processed until process_structure_scope. A symbol table entry for
15703 the type will also not be done until process_structure_scope (assuming
15704 the type has a name).
15706 NOTE: we need to call these functions regardless of whether or not the
15707 DIE has a DW_AT_name attribute, since it might be an anonymous
15708 structure or union. This gets the type entered into our set of
15709 user defined types. */
15711 static struct type *
15712 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15714 struct objfile *objfile = cu->objfile;
15716 struct attribute *attr;
15719 /* If the definition of this type lives in .debug_types, read that type.
15720 Don't follow DW_AT_specification though, that will take us back up
15721 the chain and we want to go down. */
15722 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15725 type = get_DW_AT_signature_type (die, attr, cu);
15727 /* The type's CU may not be the same as CU.
15728 Ensure TYPE is recorded with CU in die_type_hash. */
15729 return set_die_type (die, type, cu);
15732 type = alloc_type (objfile);
15733 INIT_CPLUS_SPECIFIC (type);
15735 name = dwarf2_name (die, cu);
15738 if (cu->language == language_cplus
15739 || cu->language == language_d
15740 || cu->language == language_rust)
15742 const char *full_name = dwarf2_full_name (name, die, cu);
15744 /* dwarf2_full_name might have already finished building the DIE's
15745 type. If so, there is no need to continue. */
15746 if (get_die_type (die, cu) != NULL)
15747 return get_die_type (die, cu);
15749 TYPE_TAG_NAME (type) = full_name;
15750 if (die->tag == DW_TAG_structure_type
15751 || die->tag == DW_TAG_class_type)
15752 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15756 /* The name is already allocated along with this objfile, so
15757 we don't need to duplicate it for the type. */
15758 TYPE_TAG_NAME (type) = name;
15759 if (die->tag == DW_TAG_class_type)
15760 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15764 if (die->tag == DW_TAG_structure_type)
15766 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15768 else if (die->tag == DW_TAG_union_type)
15770 TYPE_CODE (type) = TYPE_CODE_UNION;
15774 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15777 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15778 TYPE_DECLARED_CLASS (type) = 1;
15780 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15783 if (attr_form_is_constant (attr))
15784 TYPE_LENGTH (type) = DW_UNSND (attr);
15787 /* For the moment, dynamic type sizes are not supported
15788 by GDB's struct type. The actual size is determined
15789 on-demand when resolving the type of a given object,
15790 so set the type's length to zero for now. Otherwise,
15791 we record an expression as the length, and that expression
15792 could lead to a very large value, which could eventually
15793 lead to us trying to allocate that much memory when creating
15794 a value of that type. */
15795 TYPE_LENGTH (type) = 0;
15800 TYPE_LENGTH (type) = 0;
15803 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15805 /* ICC<14 does not output the required DW_AT_declaration on
15806 incomplete types, but gives them a size of zero. */
15807 TYPE_STUB (type) = 1;
15810 TYPE_STUB_SUPPORTED (type) = 1;
15812 if (die_is_declaration (die, cu))
15813 TYPE_STUB (type) = 1;
15814 else if (attr == NULL && die->child == NULL
15815 && producer_is_realview (cu->producer))
15816 /* RealView does not output the required DW_AT_declaration
15817 on incomplete types. */
15818 TYPE_STUB (type) = 1;
15820 /* We need to add the type field to the die immediately so we don't
15821 infinitely recurse when dealing with pointers to the structure
15822 type within the structure itself. */
15823 set_die_type (die, type, cu);
15825 /* set_die_type should be already done. */
15826 set_descriptive_type (type, die, cu);
15831 /* Finish creating a structure or union type, including filling in
15832 its members and creating a symbol for it. */
15835 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15837 struct objfile *objfile = cu->objfile;
15838 struct die_info *child_die;
15841 type = get_die_type (die, cu);
15843 type = read_structure_type (die, cu);
15845 if (die->child != NULL && ! die_is_declaration (die, cu))
15847 struct field_info fi;
15848 std::vector<struct symbol *> template_args;
15849 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
15851 memset (&fi, 0, sizeof (struct field_info));
15853 child_die = die->child;
15855 while (child_die && child_die->tag)
15857 if (child_die->tag == DW_TAG_member
15858 || child_die->tag == DW_TAG_variable)
15860 /* NOTE: carlton/2002-11-05: A C++ static data member
15861 should be a DW_TAG_member that is a declaration, but
15862 all versions of G++ as of this writing (so through at
15863 least 3.2.1) incorrectly generate DW_TAG_variable
15864 tags for them instead. */
15865 dwarf2_add_field (&fi, child_die, cu);
15867 else if (child_die->tag == DW_TAG_subprogram)
15869 /* Rust doesn't have member functions in the C++ sense.
15870 However, it does emit ordinary functions as children
15871 of a struct DIE. */
15872 if (cu->language == language_rust)
15873 read_func_scope (child_die, cu);
15876 /* C++ member function. */
15877 dwarf2_add_member_fn (&fi, child_die, type, cu);
15880 else if (child_die->tag == DW_TAG_inheritance)
15882 /* C++ base class field. */
15883 dwarf2_add_field (&fi, child_die, cu);
15885 else if (type_can_define_types (child_die))
15886 dwarf2_add_type_defn (&fi, child_die, cu);
15887 else if (child_die->tag == DW_TAG_template_type_param
15888 || child_die->tag == DW_TAG_template_value_param)
15890 struct symbol *arg = new_symbol (child_die, NULL, cu);
15893 template_args.push_back (arg);
15896 child_die = sibling_die (child_die);
15899 /* Attach template arguments to type. */
15900 if (!template_args.empty ())
15902 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15903 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15904 TYPE_TEMPLATE_ARGUMENTS (type)
15905 = XOBNEWVEC (&objfile->objfile_obstack,
15907 TYPE_N_TEMPLATE_ARGUMENTS (type));
15908 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15909 template_args.data (),
15910 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15911 * sizeof (struct symbol *)));
15914 /* Attach fields and member functions to the type. */
15916 dwarf2_attach_fields_to_type (&fi, type, cu);
15919 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15921 /* Get the type which refers to the base class (possibly this
15922 class itself) which contains the vtable pointer for the current
15923 class from the DW_AT_containing_type attribute. This use of
15924 DW_AT_containing_type is a GNU extension. */
15926 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15928 struct type *t = die_containing_type (die, cu);
15930 set_type_vptr_basetype (type, t);
15935 /* Our own class provides vtbl ptr. */
15936 for (i = TYPE_NFIELDS (t) - 1;
15937 i >= TYPE_N_BASECLASSES (t);
15940 const char *fieldname = TYPE_FIELD_NAME (t, i);
15942 if (is_vtable_name (fieldname, cu))
15944 set_type_vptr_fieldno (type, i);
15949 /* Complain if virtual function table field not found. */
15950 if (i < TYPE_N_BASECLASSES (t))
15951 complaint (&symfile_complaints,
15952 _("virtual function table pointer "
15953 "not found when defining class '%s'"),
15954 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
15959 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15962 else if (cu->producer
15963 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15965 /* The IBM XLC compiler does not provide direct indication
15966 of the containing type, but the vtable pointer is
15967 always named __vfp. */
15971 for (i = TYPE_NFIELDS (type) - 1;
15972 i >= TYPE_N_BASECLASSES (type);
15975 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15977 set_type_vptr_fieldno (type, i);
15978 set_type_vptr_basetype (type, type);
15985 /* Copy fi.typedef_field_list linked list elements content into the
15986 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15987 if (fi.typedef_field_list)
15989 int i = fi.typedef_field_list_count;
15991 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15992 TYPE_TYPEDEF_FIELD_ARRAY (type)
15993 = ((struct decl_field *)
15994 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
15995 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
15997 /* Reverse the list order to keep the debug info elements order. */
16000 struct decl_field *dest, *src;
16002 dest = &TYPE_TYPEDEF_FIELD (type, i);
16003 src = &fi.typedef_field_list->field;
16004 fi.typedef_field_list = fi.typedef_field_list->next;
16009 /* Copy fi.nested_types_list linked list elements content into the
16010 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16011 if (fi.nested_types_list != NULL && cu->language != language_ada)
16013 int i = fi.nested_types_list_count;
16015 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16016 TYPE_NESTED_TYPES_ARRAY (type)
16017 = ((struct decl_field *)
16018 TYPE_ALLOC (type, sizeof (struct decl_field) * i));
16019 TYPE_NESTED_TYPES_COUNT (type) = i;
16021 /* Reverse the list order to keep the debug info elements order. */
16024 struct decl_field *dest, *src;
16026 dest = &TYPE_NESTED_TYPES_FIELD (type, i);
16027 src = &fi.nested_types_list->field;
16028 fi.nested_types_list = fi.nested_types_list->next;
16033 do_cleanups (back_to);
16036 quirk_gcc_member_function_pointer (type, objfile);
16038 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16039 snapshots) has been known to create a die giving a declaration
16040 for a class that has, as a child, a die giving a definition for a
16041 nested class. So we have to process our children even if the
16042 current die is a declaration. Normally, of course, a declaration
16043 won't have any children at all. */
16045 child_die = die->child;
16047 while (child_die != NULL && child_die->tag)
16049 if (child_die->tag == DW_TAG_member
16050 || child_die->tag == DW_TAG_variable
16051 || child_die->tag == DW_TAG_inheritance
16052 || child_die->tag == DW_TAG_template_value_param
16053 || child_die->tag == DW_TAG_template_type_param)
16058 process_die (child_die, cu);
16060 child_die = sibling_die (child_die);
16063 /* Do not consider external references. According to the DWARF standard,
16064 these DIEs are identified by the fact that they have no byte_size
16065 attribute, and a declaration attribute. */
16066 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16067 || !die_is_declaration (die, cu))
16068 new_symbol (die, type, cu);
16071 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16072 update TYPE using some information only available in DIE's children. */
16075 update_enumeration_type_from_children (struct die_info *die,
16077 struct dwarf2_cu *cu)
16079 struct die_info *child_die;
16080 int unsigned_enum = 1;
16084 auto_obstack obstack;
16086 for (child_die = die->child;
16087 child_die != NULL && child_die->tag;
16088 child_die = sibling_die (child_die))
16090 struct attribute *attr;
16092 const gdb_byte *bytes;
16093 struct dwarf2_locexpr_baton *baton;
16096 if (child_die->tag != DW_TAG_enumerator)
16099 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16103 name = dwarf2_name (child_die, cu);
16105 name = "<anonymous enumerator>";
16107 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16108 &value, &bytes, &baton);
16114 else if ((mask & value) != 0)
16119 /* If we already know that the enum type is neither unsigned, nor
16120 a flag type, no need to look at the rest of the enumerates. */
16121 if (!unsigned_enum && !flag_enum)
16126 TYPE_UNSIGNED (type) = 1;
16128 TYPE_FLAG_ENUM (type) = 1;
16131 /* Given a DW_AT_enumeration_type die, set its type. We do not
16132 complete the type's fields yet, or create any symbols. */
16134 static struct type *
16135 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16137 struct objfile *objfile = cu->objfile;
16139 struct attribute *attr;
16142 /* If the definition of this type lives in .debug_types, read that type.
16143 Don't follow DW_AT_specification though, that will take us back up
16144 the chain and we want to go down. */
16145 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16148 type = get_DW_AT_signature_type (die, attr, cu);
16150 /* The type's CU may not be the same as CU.
16151 Ensure TYPE is recorded with CU in die_type_hash. */
16152 return set_die_type (die, type, cu);
16155 type = alloc_type (objfile);
16157 TYPE_CODE (type) = TYPE_CODE_ENUM;
16158 name = dwarf2_full_name (NULL, die, cu);
16160 TYPE_TAG_NAME (type) = name;
16162 attr = dwarf2_attr (die, DW_AT_type, cu);
16165 struct type *underlying_type = die_type (die, cu);
16167 TYPE_TARGET_TYPE (type) = underlying_type;
16170 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16173 TYPE_LENGTH (type) = DW_UNSND (attr);
16177 TYPE_LENGTH (type) = 0;
16180 /* The enumeration DIE can be incomplete. In Ada, any type can be
16181 declared as private in the package spec, and then defined only
16182 inside the package body. Such types are known as Taft Amendment
16183 Types. When another package uses such a type, an incomplete DIE
16184 may be generated by the compiler. */
16185 if (die_is_declaration (die, cu))
16186 TYPE_STUB (type) = 1;
16188 /* Finish the creation of this type by using the enum's children.
16189 We must call this even when the underlying type has been provided
16190 so that we can determine if we're looking at a "flag" enum. */
16191 update_enumeration_type_from_children (die, type, cu);
16193 /* If this type has an underlying type that is not a stub, then we
16194 may use its attributes. We always use the "unsigned" attribute
16195 in this situation, because ordinarily we guess whether the type
16196 is unsigned -- but the guess can be wrong and the underlying type
16197 can tell us the reality. However, we defer to a local size
16198 attribute if one exists, because this lets the compiler override
16199 the underlying type if needed. */
16200 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16202 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16203 if (TYPE_LENGTH (type) == 0)
16204 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16207 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16209 return set_die_type (die, type, cu);
16212 /* Given a pointer to a die which begins an enumeration, process all
16213 the dies that define the members of the enumeration, and create the
16214 symbol for the enumeration type.
16216 NOTE: We reverse the order of the element list. */
16219 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16221 struct type *this_type;
16223 this_type = get_die_type (die, cu);
16224 if (this_type == NULL)
16225 this_type = read_enumeration_type (die, cu);
16227 if (die->child != NULL)
16229 struct die_info *child_die;
16230 struct symbol *sym;
16231 struct field *fields = NULL;
16232 int num_fields = 0;
16235 child_die = die->child;
16236 while (child_die && child_die->tag)
16238 if (child_die->tag != DW_TAG_enumerator)
16240 process_die (child_die, cu);
16244 name = dwarf2_name (child_die, cu);
16247 sym = new_symbol (child_die, this_type, cu);
16249 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16251 fields = (struct field *)
16253 (num_fields + DW_FIELD_ALLOC_CHUNK)
16254 * sizeof (struct field));
16257 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16258 FIELD_TYPE (fields[num_fields]) = NULL;
16259 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16260 FIELD_BITSIZE (fields[num_fields]) = 0;
16266 child_die = sibling_die (child_die);
16271 TYPE_NFIELDS (this_type) = num_fields;
16272 TYPE_FIELDS (this_type) = (struct field *)
16273 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16274 memcpy (TYPE_FIELDS (this_type), fields,
16275 sizeof (struct field) * num_fields);
16280 /* If we are reading an enum from a .debug_types unit, and the enum
16281 is a declaration, and the enum is not the signatured type in the
16282 unit, then we do not want to add a symbol for it. Adding a
16283 symbol would in some cases obscure the true definition of the
16284 enum, giving users an incomplete type when the definition is
16285 actually available. Note that we do not want to do this for all
16286 enums which are just declarations, because C++0x allows forward
16287 enum declarations. */
16288 if (cu->per_cu->is_debug_types
16289 && die_is_declaration (die, cu))
16291 struct signatured_type *sig_type;
16293 sig_type = (struct signatured_type *) cu->per_cu;
16294 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16295 if (sig_type->type_offset_in_section != die->sect_off)
16299 new_symbol (die, this_type, cu);
16302 /* Extract all information from a DW_TAG_array_type DIE and put it in
16303 the DIE's type field. For now, this only handles one dimensional
16306 static struct type *
16307 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16309 struct objfile *objfile = cu->objfile;
16310 struct die_info *child_die;
16312 struct type *element_type, *range_type, *index_type;
16313 struct attribute *attr;
16315 struct dynamic_prop *byte_stride_prop = NULL;
16316 unsigned int bit_stride = 0;
16318 element_type = die_type (die, cu);
16320 /* The die_type call above may have already set the type for this DIE. */
16321 type = get_die_type (die, cu);
16325 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16331 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16332 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16335 complaint (&symfile_complaints,
16336 _("unable to read array DW_AT_byte_stride "
16337 " - DIE at 0x%x [in module %s]"),
16338 to_underlying (die->sect_off),
16339 objfile_name (cu->objfile));
16340 /* Ignore this attribute. We will likely not be able to print
16341 arrays of this type correctly, but there is little we can do
16342 to help if we cannot read the attribute's value. */
16343 byte_stride_prop = NULL;
16347 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16349 bit_stride = DW_UNSND (attr);
16351 /* Irix 6.2 native cc creates array types without children for
16352 arrays with unspecified length. */
16353 if (die->child == NULL)
16355 index_type = objfile_type (objfile)->builtin_int;
16356 range_type = create_static_range_type (NULL, index_type, 0, -1);
16357 type = create_array_type_with_stride (NULL, element_type, range_type,
16358 byte_stride_prop, bit_stride);
16359 return set_die_type (die, type, cu);
16362 std::vector<struct type *> range_types;
16363 child_die = die->child;
16364 while (child_die && child_die->tag)
16366 if (child_die->tag == DW_TAG_subrange_type)
16368 struct type *child_type = read_type_die (child_die, cu);
16370 if (child_type != NULL)
16372 /* The range type was succesfully read. Save it for the
16373 array type creation. */
16374 range_types.push_back (child_type);
16377 child_die = sibling_die (child_die);
16380 /* Dwarf2 dimensions are output from left to right, create the
16381 necessary array types in backwards order. */
16383 type = element_type;
16385 if (read_array_order (die, cu) == DW_ORD_col_major)
16389 while (i < range_types.size ())
16390 type = create_array_type_with_stride (NULL, type, range_types[i++],
16391 byte_stride_prop, bit_stride);
16395 size_t ndim = range_types.size ();
16397 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16398 byte_stride_prop, bit_stride);
16401 /* Understand Dwarf2 support for vector types (like they occur on
16402 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16403 array type. This is not part of the Dwarf2/3 standard yet, but a
16404 custom vendor extension. The main difference between a regular
16405 array and the vector variant is that vectors are passed by value
16407 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16409 make_vector_type (type);
16411 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16412 implementation may choose to implement triple vectors using this
16414 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16417 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16418 TYPE_LENGTH (type) = DW_UNSND (attr);
16420 complaint (&symfile_complaints,
16421 _("DW_AT_byte_size for array type smaller "
16422 "than the total size of elements"));
16425 name = dwarf2_name (die, cu);
16427 TYPE_NAME (type) = name;
16429 /* Install the type in the die. */
16430 set_die_type (die, type, cu);
16432 /* set_die_type should be already done. */
16433 set_descriptive_type (type, die, cu);
16438 static enum dwarf_array_dim_ordering
16439 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16441 struct attribute *attr;
16443 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16446 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16448 /* GNU F77 is a special case, as at 08/2004 array type info is the
16449 opposite order to the dwarf2 specification, but data is still
16450 laid out as per normal fortran.
16452 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16453 version checking. */
16455 if (cu->language == language_fortran
16456 && cu->producer && strstr (cu->producer, "GNU F77"))
16458 return DW_ORD_row_major;
16461 switch (cu->language_defn->la_array_ordering)
16463 case array_column_major:
16464 return DW_ORD_col_major;
16465 case array_row_major:
16467 return DW_ORD_row_major;
16471 /* Extract all information from a DW_TAG_set_type DIE and put it in
16472 the DIE's type field. */
16474 static struct type *
16475 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16477 struct type *domain_type, *set_type;
16478 struct attribute *attr;
16480 domain_type = die_type (die, cu);
16482 /* The die_type call above may have already set the type for this DIE. */
16483 set_type = get_die_type (die, cu);
16487 set_type = create_set_type (NULL, domain_type);
16489 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16491 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16493 return set_die_type (die, set_type, cu);
16496 /* A helper for read_common_block that creates a locexpr baton.
16497 SYM is the symbol which we are marking as computed.
16498 COMMON_DIE is the DIE for the common block.
16499 COMMON_LOC is the location expression attribute for the common
16501 MEMBER_LOC is the location expression attribute for the particular
16502 member of the common block that we are processing.
16503 CU is the CU from which the above come. */
16506 mark_common_block_symbol_computed (struct symbol *sym,
16507 struct die_info *common_die,
16508 struct attribute *common_loc,
16509 struct attribute *member_loc,
16510 struct dwarf2_cu *cu)
16512 struct objfile *objfile = dwarf2_per_objfile->objfile;
16513 struct dwarf2_locexpr_baton *baton;
16515 unsigned int cu_off;
16516 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16517 LONGEST offset = 0;
16519 gdb_assert (common_loc && member_loc);
16520 gdb_assert (attr_form_is_block (common_loc));
16521 gdb_assert (attr_form_is_block (member_loc)
16522 || attr_form_is_constant (member_loc));
16524 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16525 baton->per_cu = cu->per_cu;
16526 gdb_assert (baton->per_cu);
16528 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16530 if (attr_form_is_constant (member_loc))
16532 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16533 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16536 baton->size += DW_BLOCK (member_loc)->size;
16538 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16541 *ptr++ = DW_OP_call4;
16542 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16543 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16546 if (attr_form_is_constant (member_loc))
16548 *ptr++ = DW_OP_addr;
16549 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16550 ptr += cu->header.addr_size;
16554 /* We have to copy the data here, because DW_OP_call4 will only
16555 use a DW_AT_location attribute. */
16556 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16557 ptr += DW_BLOCK (member_loc)->size;
16560 *ptr++ = DW_OP_plus;
16561 gdb_assert (ptr - baton->data == baton->size);
16563 SYMBOL_LOCATION_BATON (sym) = baton;
16564 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16567 /* Create appropriate locally-scoped variables for all the
16568 DW_TAG_common_block entries. Also create a struct common_block
16569 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16570 is used to sepate the common blocks name namespace from regular
16574 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16576 struct attribute *attr;
16578 attr = dwarf2_attr (die, DW_AT_location, cu);
16581 /* Support the .debug_loc offsets. */
16582 if (attr_form_is_block (attr))
16586 else if (attr_form_is_section_offset (attr))
16588 dwarf2_complex_location_expr_complaint ();
16593 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16594 "common block member");
16599 if (die->child != NULL)
16601 struct objfile *objfile = cu->objfile;
16602 struct die_info *child_die;
16603 size_t n_entries = 0, size;
16604 struct common_block *common_block;
16605 struct symbol *sym;
16607 for (child_die = die->child;
16608 child_die && child_die->tag;
16609 child_die = sibling_die (child_die))
16612 size = (sizeof (struct common_block)
16613 + (n_entries - 1) * sizeof (struct symbol *));
16615 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16617 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16618 common_block->n_entries = 0;
16620 for (child_die = die->child;
16621 child_die && child_die->tag;
16622 child_die = sibling_die (child_die))
16624 /* Create the symbol in the DW_TAG_common_block block in the current
16626 sym = new_symbol (child_die, NULL, cu);
16629 struct attribute *member_loc;
16631 common_block->contents[common_block->n_entries++] = sym;
16633 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16637 /* GDB has handled this for a long time, but it is
16638 not specified by DWARF. It seems to have been
16639 emitted by gfortran at least as recently as:
16640 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16641 complaint (&symfile_complaints,
16642 _("Variable in common block has "
16643 "DW_AT_data_member_location "
16644 "- DIE at 0x%x [in module %s]"),
16645 to_underlying (child_die->sect_off),
16646 objfile_name (cu->objfile));
16648 if (attr_form_is_section_offset (member_loc))
16649 dwarf2_complex_location_expr_complaint ();
16650 else if (attr_form_is_constant (member_loc)
16651 || attr_form_is_block (member_loc))
16654 mark_common_block_symbol_computed (sym, die, attr,
16658 dwarf2_complex_location_expr_complaint ();
16663 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16664 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16668 /* Create a type for a C++ namespace. */
16670 static struct type *
16671 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16673 struct objfile *objfile = cu->objfile;
16674 const char *previous_prefix, *name;
16678 /* For extensions, reuse the type of the original namespace. */
16679 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16681 struct die_info *ext_die;
16682 struct dwarf2_cu *ext_cu = cu;
16684 ext_die = dwarf2_extension (die, &ext_cu);
16685 type = read_type_die (ext_die, ext_cu);
16687 /* EXT_CU may not be the same as CU.
16688 Ensure TYPE is recorded with CU in die_type_hash. */
16689 return set_die_type (die, type, cu);
16692 name = namespace_name (die, &is_anonymous, cu);
16694 /* Now build the name of the current namespace. */
16696 previous_prefix = determine_prefix (die, cu);
16697 if (previous_prefix[0] != '\0')
16698 name = typename_concat (&objfile->objfile_obstack,
16699 previous_prefix, name, 0, cu);
16701 /* Create the type. */
16702 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16703 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16705 return set_die_type (die, type, cu);
16708 /* Read a namespace scope. */
16711 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16713 struct objfile *objfile = cu->objfile;
16716 /* Add a symbol associated to this if we haven't seen the namespace
16717 before. Also, add a using directive if it's an anonymous
16720 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16724 type = read_type_die (die, cu);
16725 new_symbol (die, type, cu);
16727 namespace_name (die, &is_anonymous, cu);
16730 const char *previous_prefix = determine_prefix (die, cu);
16732 std::vector<const char *> excludes;
16733 add_using_directive (using_directives (cu->language),
16734 previous_prefix, TYPE_NAME (type), NULL,
16735 NULL, excludes, 0, &objfile->objfile_obstack);
16739 if (die->child != NULL)
16741 struct die_info *child_die = die->child;
16743 while (child_die && child_die->tag)
16745 process_die (child_die, cu);
16746 child_die = sibling_die (child_die);
16751 /* Read a Fortran module as type. This DIE can be only a declaration used for
16752 imported module. Still we need that type as local Fortran "use ... only"
16753 declaration imports depend on the created type in determine_prefix. */
16755 static struct type *
16756 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16758 struct objfile *objfile = cu->objfile;
16759 const char *module_name;
16762 module_name = dwarf2_name (die, cu);
16764 complaint (&symfile_complaints,
16765 _("DW_TAG_module has no name, offset 0x%x"),
16766 to_underlying (die->sect_off));
16767 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16769 /* determine_prefix uses TYPE_TAG_NAME. */
16770 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16772 return set_die_type (die, type, cu);
16775 /* Read a Fortran module. */
16778 read_module (struct die_info *die, struct dwarf2_cu *cu)
16780 struct die_info *child_die = die->child;
16783 type = read_type_die (die, cu);
16784 new_symbol (die, type, cu);
16786 while (child_die && child_die->tag)
16788 process_die (child_die, cu);
16789 child_die = sibling_die (child_die);
16793 /* Return the name of the namespace represented by DIE. Set
16794 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16797 static const char *
16798 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16800 struct die_info *current_die;
16801 const char *name = NULL;
16803 /* Loop through the extensions until we find a name. */
16805 for (current_die = die;
16806 current_die != NULL;
16807 current_die = dwarf2_extension (die, &cu))
16809 /* We don't use dwarf2_name here so that we can detect the absence
16810 of a name -> anonymous namespace. */
16811 name = dwarf2_string_attr (die, DW_AT_name, cu);
16817 /* Is it an anonymous namespace? */
16819 *is_anonymous = (name == NULL);
16821 name = CP_ANONYMOUS_NAMESPACE_STR;
16826 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16827 the user defined type vector. */
16829 static struct type *
16830 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16832 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
16833 struct comp_unit_head *cu_header = &cu->header;
16835 struct attribute *attr_byte_size;
16836 struct attribute *attr_address_class;
16837 int byte_size, addr_class;
16838 struct type *target_type;
16840 target_type = die_type (die, cu);
16842 /* The die_type call above may have already set the type for this DIE. */
16843 type = get_die_type (die, cu);
16847 type = lookup_pointer_type (target_type);
16849 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16850 if (attr_byte_size)
16851 byte_size = DW_UNSND (attr_byte_size);
16853 byte_size = cu_header->addr_size;
16855 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16856 if (attr_address_class)
16857 addr_class = DW_UNSND (attr_address_class);
16859 addr_class = DW_ADDR_none;
16861 /* If the pointer size or address class is different than the
16862 default, create a type variant marked as such and set the
16863 length accordingly. */
16864 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
16866 if (gdbarch_address_class_type_flags_p (gdbarch))
16870 type_flags = gdbarch_address_class_type_flags
16871 (gdbarch, byte_size, addr_class);
16872 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16874 type = make_type_with_address_space (type, type_flags);
16876 else if (TYPE_LENGTH (type) != byte_size)
16878 complaint (&symfile_complaints,
16879 _("invalid pointer size %d"), byte_size);
16883 /* Should we also complain about unhandled address classes? */
16887 TYPE_LENGTH (type) = byte_size;
16888 return set_die_type (die, type, cu);
16891 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16892 the user defined type vector. */
16894 static struct type *
16895 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16898 struct type *to_type;
16899 struct type *domain;
16901 to_type = die_type (die, cu);
16902 domain = die_containing_type (die, cu);
16904 /* The calls above may have already set the type for this DIE. */
16905 type = get_die_type (die, cu);
16909 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16910 type = lookup_methodptr_type (to_type);
16911 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16913 struct type *new_type = alloc_type (cu->objfile);
16915 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16916 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16917 TYPE_VARARGS (to_type));
16918 type = lookup_methodptr_type (new_type);
16921 type = lookup_memberptr_type (to_type, domain);
16923 return set_die_type (die, type, cu);
16926 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16927 the user defined type vector. */
16929 static struct type *
16930 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16931 enum type_code refcode)
16933 struct comp_unit_head *cu_header = &cu->header;
16934 struct type *type, *target_type;
16935 struct attribute *attr;
16937 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16939 target_type = die_type (die, cu);
16941 /* The die_type call above may have already set the type for this DIE. */
16942 type = get_die_type (die, cu);
16946 type = lookup_reference_type (target_type, refcode);
16947 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16950 TYPE_LENGTH (type) = DW_UNSND (attr);
16954 TYPE_LENGTH (type) = cu_header->addr_size;
16956 return set_die_type (die, type, cu);
16959 /* Add the given cv-qualifiers to the element type of the array. GCC
16960 outputs DWARF type qualifiers that apply to an array, not the
16961 element type. But GDB relies on the array element type to carry
16962 the cv-qualifiers. This mimics section 6.7.3 of the C99
16965 static struct type *
16966 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16967 struct type *base_type, int cnst, int voltl)
16969 struct type *el_type, *inner_array;
16971 base_type = copy_type (base_type);
16972 inner_array = base_type;
16974 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16976 TYPE_TARGET_TYPE (inner_array) =
16977 copy_type (TYPE_TARGET_TYPE (inner_array));
16978 inner_array = TYPE_TARGET_TYPE (inner_array);
16981 el_type = TYPE_TARGET_TYPE (inner_array);
16982 cnst |= TYPE_CONST (el_type);
16983 voltl |= TYPE_VOLATILE (el_type);
16984 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16986 return set_die_type (die, base_type, cu);
16989 static struct type *
16990 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16992 struct type *base_type, *cv_type;
16994 base_type = die_type (die, cu);
16996 /* The die_type call above may have already set the type for this DIE. */
16997 cv_type = get_die_type (die, cu);
17001 /* In case the const qualifier is applied to an array type, the element type
17002 is so qualified, not the array type (section 6.7.3 of C99). */
17003 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17004 return add_array_cv_type (die, cu, base_type, 1, 0);
17006 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17007 return set_die_type (die, cv_type, cu);
17010 static struct type *
17011 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17013 struct type *base_type, *cv_type;
17015 base_type = die_type (die, cu);
17017 /* The die_type call above may have already set the type for this DIE. */
17018 cv_type = get_die_type (die, cu);
17022 /* In case the volatile qualifier is applied to an array type, the
17023 element type is so qualified, not the array type (section 6.7.3
17025 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17026 return add_array_cv_type (die, cu, base_type, 0, 1);
17028 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17029 return set_die_type (die, cv_type, cu);
17032 /* Handle DW_TAG_restrict_type. */
17034 static struct type *
17035 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17037 struct type *base_type, *cv_type;
17039 base_type = die_type (die, cu);
17041 /* The die_type call above may have already set the type for this DIE. */
17042 cv_type = get_die_type (die, cu);
17046 cv_type = make_restrict_type (base_type);
17047 return set_die_type (die, cv_type, cu);
17050 /* Handle DW_TAG_atomic_type. */
17052 static struct type *
17053 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17055 struct type *base_type, *cv_type;
17057 base_type = die_type (die, cu);
17059 /* The die_type call above may have already set the type for this DIE. */
17060 cv_type = get_die_type (die, cu);
17064 cv_type = make_atomic_type (base_type);
17065 return set_die_type (die, cv_type, cu);
17068 /* Extract all information from a DW_TAG_string_type DIE and add to
17069 the user defined type vector. It isn't really a user defined type,
17070 but it behaves like one, with other DIE's using an AT_user_def_type
17071 attribute to reference it. */
17073 static struct type *
17074 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17076 struct objfile *objfile = cu->objfile;
17077 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17078 struct type *type, *range_type, *index_type, *char_type;
17079 struct attribute *attr;
17080 unsigned int length;
17082 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17085 length = DW_UNSND (attr);
17089 /* Check for the DW_AT_byte_size attribute. */
17090 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17093 length = DW_UNSND (attr);
17101 index_type = objfile_type (objfile)->builtin_int;
17102 range_type = create_static_range_type (NULL, index_type, 1, length);
17103 char_type = language_string_char_type (cu->language_defn, gdbarch);
17104 type = create_string_type (NULL, char_type, range_type);
17106 return set_die_type (die, type, cu);
17109 /* Assuming that DIE corresponds to a function, returns nonzero
17110 if the function is prototyped. */
17113 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17115 struct attribute *attr;
17117 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17118 if (attr && (DW_UNSND (attr) != 0))
17121 /* The DWARF standard implies that the DW_AT_prototyped attribute
17122 is only meaninful for C, but the concept also extends to other
17123 languages that allow unprototyped functions (Eg: Objective C).
17124 For all other languages, assume that functions are always
17126 if (cu->language != language_c
17127 && cu->language != language_objc
17128 && cu->language != language_opencl)
17131 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17132 prototyped and unprototyped functions; default to prototyped,
17133 since that is more common in modern code (and RealView warns
17134 about unprototyped functions). */
17135 if (producer_is_realview (cu->producer))
17141 /* Handle DIES due to C code like:
17145 int (*funcp)(int a, long l);
17149 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17151 static struct type *
17152 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17154 struct objfile *objfile = cu->objfile;
17155 struct type *type; /* Type that this function returns. */
17156 struct type *ftype; /* Function that returns above type. */
17157 struct attribute *attr;
17159 type = die_type (die, cu);
17161 /* The die_type call above may have already set the type for this DIE. */
17162 ftype = get_die_type (die, cu);
17166 ftype = lookup_function_type (type);
17168 if (prototyped_function_p (die, cu))
17169 TYPE_PROTOTYPED (ftype) = 1;
17171 /* Store the calling convention in the type if it's available in
17172 the subroutine die. Otherwise set the calling convention to
17173 the default value DW_CC_normal. */
17174 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17176 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17177 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17178 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17180 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17182 /* Record whether the function returns normally to its caller or not
17183 if the DWARF producer set that information. */
17184 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17185 if (attr && (DW_UNSND (attr) != 0))
17186 TYPE_NO_RETURN (ftype) = 1;
17188 /* We need to add the subroutine type to the die immediately so
17189 we don't infinitely recurse when dealing with parameters
17190 declared as the same subroutine type. */
17191 set_die_type (die, ftype, cu);
17193 if (die->child != NULL)
17195 struct type *void_type = objfile_type (objfile)->builtin_void;
17196 struct die_info *child_die;
17197 int nparams, iparams;
17199 /* Count the number of parameters.
17200 FIXME: GDB currently ignores vararg functions, but knows about
17201 vararg member functions. */
17203 child_die = die->child;
17204 while (child_die && child_die->tag)
17206 if (child_die->tag == DW_TAG_formal_parameter)
17208 else if (child_die->tag == DW_TAG_unspecified_parameters)
17209 TYPE_VARARGS (ftype) = 1;
17210 child_die = sibling_die (child_die);
17213 /* Allocate storage for parameters and fill them in. */
17214 TYPE_NFIELDS (ftype) = nparams;
17215 TYPE_FIELDS (ftype) = (struct field *)
17216 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17218 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17219 even if we error out during the parameters reading below. */
17220 for (iparams = 0; iparams < nparams; iparams++)
17221 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17224 child_die = die->child;
17225 while (child_die && child_die->tag)
17227 if (child_die->tag == DW_TAG_formal_parameter)
17229 struct type *arg_type;
17231 /* DWARF version 2 has no clean way to discern C++
17232 static and non-static member functions. G++ helps
17233 GDB by marking the first parameter for non-static
17234 member functions (which is the this pointer) as
17235 artificial. We pass this information to
17236 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17238 DWARF version 3 added DW_AT_object_pointer, which GCC
17239 4.5 does not yet generate. */
17240 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17242 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17244 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17245 arg_type = die_type (child_die, cu);
17247 /* RealView does not mark THIS as const, which the testsuite
17248 expects. GCC marks THIS as const in method definitions,
17249 but not in the class specifications (GCC PR 43053). */
17250 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17251 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17254 struct dwarf2_cu *arg_cu = cu;
17255 const char *name = dwarf2_name (child_die, cu);
17257 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17260 /* If the compiler emits this, use it. */
17261 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17264 else if (name && strcmp (name, "this") == 0)
17265 /* Function definitions will have the argument names. */
17267 else if (name == NULL && iparams == 0)
17268 /* Declarations may not have the names, so like
17269 elsewhere in GDB, assume an artificial first
17270 argument is "this". */
17274 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17278 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17281 child_die = sibling_die (child_die);
17288 static struct type *
17289 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17291 struct objfile *objfile = cu->objfile;
17292 const char *name = NULL;
17293 struct type *this_type, *target_type;
17295 name = dwarf2_full_name (NULL, die, cu);
17296 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17297 TYPE_TARGET_STUB (this_type) = 1;
17298 set_die_type (die, this_type, cu);
17299 target_type = die_type (die, cu);
17300 if (target_type != this_type)
17301 TYPE_TARGET_TYPE (this_type) = target_type;
17304 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17305 spec and cause infinite loops in GDB. */
17306 complaint (&symfile_complaints,
17307 _("Self-referential DW_TAG_typedef "
17308 "- DIE at 0x%x [in module %s]"),
17309 to_underlying (die->sect_off), objfile_name (objfile));
17310 TYPE_TARGET_TYPE (this_type) = NULL;
17315 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17316 (which may be different from NAME) to the architecture back-end to allow
17317 it to guess the correct format if necessary. */
17319 static struct type *
17320 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17321 const char *name_hint)
17323 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17324 const struct floatformat **format;
17327 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17329 type = init_float_type (objfile, bits, name, format);
17331 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17336 /* Find a representation of a given base type and install
17337 it in the TYPE field of the die. */
17339 static struct type *
17340 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17342 struct objfile *objfile = cu->objfile;
17344 struct attribute *attr;
17345 int encoding = 0, bits = 0;
17348 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17351 encoding = DW_UNSND (attr);
17353 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17356 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17358 name = dwarf2_name (die, cu);
17361 complaint (&symfile_complaints,
17362 _("DW_AT_name missing from DW_TAG_base_type"));
17367 case DW_ATE_address:
17368 /* Turn DW_ATE_address into a void * pointer. */
17369 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17370 type = init_pointer_type (objfile, bits, name, type);
17372 case DW_ATE_boolean:
17373 type = init_boolean_type (objfile, bits, 1, name);
17375 case DW_ATE_complex_float:
17376 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
17377 type = init_complex_type (objfile, name, type);
17379 case DW_ATE_decimal_float:
17380 type = init_decfloat_type (objfile, bits, name);
17383 type = dwarf2_init_float_type (objfile, bits, name, name);
17385 case DW_ATE_signed:
17386 type = init_integer_type (objfile, bits, 0, name);
17388 case DW_ATE_unsigned:
17389 if (cu->language == language_fortran
17391 && startswith (name, "character("))
17392 type = init_character_type (objfile, bits, 1, name);
17394 type = init_integer_type (objfile, bits, 1, name);
17396 case DW_ATE_signed_char:
17397 if (cu->language == language_ada || cu->language == language_m2
17398 || cu->language == language_pascal
17399 || cu->language == language_fortran)
17400 type = init_character_type (objfile, bits, 0, name);
17402 type = init_integer_type (objfile, bits, 0, name);
17404 case DW_ATE_unsigned_char:
17405 if (cu->language == language_ada || cu->language == language_m2
17406 || cu->language == language_pascal
17407 || cu->language == language_fortran
17408 || cu->language == language_rust)
17409 type = init_character_type (objfile, bits, 1, name);
17411 type = init_integer_type (objfile, bits, 1, name);
17415 gdbarch *arch = get_objfile_arch (objfile);
17418 type = builtin_type (arch)->builtin_char16;
17419 else if (bits == 32)
17420 type = builtin_type (arch)->builtin_char32;
17423 complaint (&symfile_complaints,
17424 _("unsupported DW_ATE_UTF bit size: '%d'"),
17426 type = init_integer_type (objfile, bits, 1, name);
17428 return set_die_type (die, type, cu);
17433 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17434 dwarf_type_encoding_name (encoding));
17435 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17439 if (name && strcmp (name, "char") == 0)
17440 TYPE_NOSIGN (type) = 1;
17442 return set_die_type (die, type, cu);
17445 /* Parse dwarf attribute if it's a block, reference or constant and put the
17446 resulting value of the attribute into struct bound_prop.
17447 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17450 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17451 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17453 struct dwarf2_property_baton *baton;
17454 struct obstack *obstack = &cu->objfile->objfile_obstack;
17456 if (attr == NULL || prop == NULL)
17459 if (attr_form_is_block (attr))
17461 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17462 baton->referenced_type = NULL;
17463 baton->locexpr.per_cu = cu->per_cu;
17464 baton->locexpr.size = DW_BLOCK (attr)->size;
17465 baton->locexpr.data = DW_BLOCK (attr)->data;
17466 prop->data.baton = baton;
17467 prop->kind = PROP_LOCEXPR;
17468 gdb_assert (prop->data.baton != NULL);
17470 else if (attr_form_is_ref (attr))
17472 struct dwarf2_cu *target_cu = cu;
17473 struct die_info *target_die;
17474 struct attribute *target_attr;
17476 target_die = follow_die_ref (die, attr, &target_cu);
17477 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17478 if (target_attr == NULL)
17479 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17481 if (target_attr == NULL)
17484 switch (target_attr->name)
17486 case DW_AT_location:
17487 if (attr_form_is_section_offset (target_attr))
17489 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17490 baton->referenced_type = die_type (target_die, target_cu);
17491 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17492 prop->data.baton = baton;
17493 prop->kind = PROP_LOCLIST;
17494 gdb_assert (prop->data.baton != NULL);
17496 else if (attr_form_is_block (target_attr))
17498 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17499 baton->referenced_type = die_type (target_die, target_cu);
17500 baton->locexpr.per_cu = cu->per_cu;
17501 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17502 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17503 prop->data.baton = baton;
17504 prop->kind = PROP_LOCEXPR;
17505 gdb_assert (prop->data.baton != NULL);
17509 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17510 "dynamic property");
17514 case DW_AT_data_member_location:
17518 if (!handle_data_member_location (target_die, target_cu,
17522 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17523 baton->referenced_type = read_type_die (target_die->parent,
17525 baton->offset_info.offset = offset;
17526 baton->offset_info.type = die_type (target_die, target_cu);
17527 prop->data.baton = baton;
17528 prop->kind = PROP_ADDR_OFFSET;
17533 else if (attr_form_is_constant (attr))
17535 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17536 prop->kind = PROP_CONST;
17540 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17541 dwarf2_name (die, cu));
17548 /* Read the given DW_AT_subrange DIE. */
17550 static struct type *
17551 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17553 struct type *base_type, *orig_base_type;
17554 struct type *range_type;
17555 struct attribute *attr;
17556 struct dynamic_prop low, high;
17557 int low_default_is_valid;
17558 int high_bound_is_count = 0;
17560 LONGEST negative_mask;
17562 orig_base_type = die_type (die, cu);
17563 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17564 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17565 creating the range type, but we use the result of check_typedef
17566 when examining properties of the type. */
17567 base_type = check_typedef (orig_base_type);
17569 /* The die_type call above may have already set the type for this DIE. */
17570 range_type = get_die_type (die, cu);
17574 low.kind = PROP_CONST;
17575 high.kind = PROP_CONST;
17576 high.data.const_val = 0;
17578 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17579 omitting DW_AT_lower_bound. */
17580 switch (cu->language)
17583 case language_cplus:
17584 low.data.const_val = 0;
17585 low_default_is_valid = 1;
17587 case language_fortran:
17588 low.data.const_val = 1;
17589 low_default_is_valid = 1;
17592 case language_objc:
17593 case language_rust:
17594 low.data.const_val = 0;
17595 low_default_is_valid = (cu->header.version >= 4);
17599 case language_pascal:
17600 low.data.const_val = 1;
17601 low_default_is_valid = (cu->header.version >= 4);
17604 low.data.const_val = 0;
17605 low_default_is_valid = 0;
17609 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17611 attr_to_dynamic_prop (attr, die, cu, &low);
17612 else if (!low_default_is_valid)
17613 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
17614 "- DIE at 0x%x [in module %s]"),
17615 to_underlying (die->sect_off), objfile_name (cu->objfile));
17617 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
17618 if (!attr_to_dynamic_prop (attr, die, cu, &high))
17620 attr = dwarf2_attr (die, DW_AT_count, cu);
17621 if (attr_to_dynamic_prop (attr, die, cu, &high))
17623 /* If bounds are constant do the final calculation here. */
17624 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17625 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17627 high_bound_is_count = 1;
17631 /* Dwarf-2 specifications explicitly allows to create subrange types
17632 without specifying a base type.
17633 In that case, the base type must be set to the type of
17634 the lower bound, upper bound or count, in that order, if any of these
17635 three attributes references an object that has a type.
17636 If no base type is found, the Dwarf-2 specifications say that
17637 a signed integer type of size equal to the size of an address should
17639 For the following C code: `extern char gdb_int [];'
17640 GCC produces an empty range DIE.
17641 FIXME: muller/2010-05-28: Possible references to object for low bound,
17642 high bound or count are not yet handled by this code. */
17643 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17645 struct objfile *objfile = cu->objfile;
17646 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17647 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17648 struct type *int_type = objfile_type (objfile)->builtin_int;
17650 /* Test "int", "long int", and "long long int" objfile types,
17651 and select the first one having a size above or equal to the
17652 architecture address size. */
17653 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17654 base_type = int_type;
17657 int_type = objfile_type (objfile)->builtin_long;
17658 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17659 base_type = int_type;
17662 int_type = objfile_type (objfile)->builtin_long_long;
17663 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17664 base_type = int_type;
17669 /* Normally, the DWARF producers are expected to use a signed
17670 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17671 But this is unfortunately not always the case, as witnessed
17672 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17673 is used instead. To work around that ambiguity, we treat
17674 the bounds as signed, and thus sign-extend their values, when
17675 the base type is signed. */
17677 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17678 if (low.kind == PROP_CONST
17679 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17680 low.data.const_val |= negative_mask;
17681 if (high.kind == PROP_CONST
17682 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17683 high.data.const_val |= negative_mask;
17685 range_type = create_range_type (NULL, orig_base_type, &low, &high);
17687 if (high_bound_is_count)
17688 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17690 /* Ada expects an empty array on no boundary attributes. */
17691 if (attr == NULL && cu->language != language_ada)
17692 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17694 name = dwarf2_name (die, cu);
17696 TYPE_NAME (range_type) = name;
17698 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17700 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17702 set_die_type (die, range_type, cu);
17704 /* set_die_type should be already done. */
17705 set_descriptive_type (range_type, die, cu);
17710 static struct type *
17711 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17715 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
17716 TYPE_NAME (type) = dwarf2_name (die, cu);
17718 /* In Ada, an unspecified type is typically used when the description
17719 of the type is defered to a different unit. When encountering
17720 such a type, we treat it as a stub, and try to resolve it later on,
17722 if (cu->language == language_ada)
17723 TYPE_STUB (type) = 1;
17725 return set_die_type (die, type, cu);
17728 /* Read a single die and all its descendents. Set the die's sibling
17729 field to NULL; set other fields in the die correctly, and set all
17730 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17731 location of the info_ptr after reading all of those dies. PARENT
17732 is the parent of the die in question. */
17734 static struct die_info *
17735 read_die_and_children (const struct die_reader_specs *reader,
17736 const gdb_byte *info_ptr,
17737 const gdb_byte **new_info_ptr,
17738 struct die_info *parent)
17740 struct die_info *die;
17741 const gdb_byte *cur_ptr;
17744 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
17747 *new_info_ptr = cur_ptr;
17750 store_in_ref_table (die, reader->cu);
17753 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17757 *new_info_ptr = cur_ptr;
17760 die->sibling = NULL;
17761 die->parent = parent;
17765 /* Read a die, all of its descendents, and all of its siblings; set
17766 all of the fields of all of the dies correctly. Arguments are as
17767 in read_die_and_children. */
17769 static struct die_info *
17770 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17771 const gdb_byte *info_ptr,
17772 const gdb_byte **new_info_ptr,
17773 struct die_info *parent)
17775 struct die_info *first_die, *last_sibling;
17776 const gdb_byte *cur_ptr;
17778 cur_ptr = info_ptr;
17779 first_die = last_sibling = NULL;
17783 struct die_info *die
17784 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17788 *new_info_ptr = cur_ptr;
17795 last_sibling->sibling = die;
17797 last_sibling = die;
17801 /* Read a die, all of its descendents, and all of its siblings; set
17802 all of the fields of all of the dies correctly. Arguments are as
17803 in read_die_and_children.
17804 This the main entry point for reading a DIE and all its children. */
17806 static struct die_info *
17807 read_die_and_siblings (const struct die_reader_specs *reader,
17808 const gdb_byte *info_ptr,
17809 const gdb_byte **new_info_ptr,
17810 struct die_info *parent)
17812 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17813 new_info_ptr, parent);
17815 if (dwarf_die_debug)
17817 fprintf_unfiltered (gdb_stdlog,
17818 "Read die from %s@0x%x of %s:\n",
17819 get_section_name (reader->die_section),
17820 (unsigned) (info_ptr - reader->die_section->buffer),
17821 bfd_get_filename (reader->abfd));
17822 dump_die (die, dwarf_die_debug);
17828 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17830 The caller is responsible for filling in the extra attributes
17831 and updating (*DIEP)->num_attrs.
17832 Set DIEP to point to a newly allocated die with its information,
17833 except for its child, sibling, and parent fields.
17834 Set HAS_CHILDREN to tell whether the die has children or not. */
17836 static const gdb_byte *
17837 read_full_die_1 (const struct die_reader_specs *reader,
17838 struct die_info **diep, const gdb_byte *info_ptr,
17839 int *has_children, int num_extra_attrs)
17841 unsigned int abbrev_number, bytes_read, i;
17842 struct abbrev_info *abbrev;
17843 struct die_info *die;
17844 struct dwarf2_cu *cu = reader->cu;
17845 bfd *abfd = reader->abfd;
17847 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17848 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17849 info_ptr += bytes_read;
17850 if (!abbrev_number)
17857 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
17859 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17861 bfd_get_filename (abfd));
17863 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17864 die->sect_off = sect_off;
17865 die->tag = abbrev->tag;
17866 die->abbrev = abbrev_number;
17868 /* Make the result usable.
17869 The caller needs to update num_attrs after adding the extra
17871 die->num_attrs = abbrev->num_attrs;
17873 for (i = 0; i < abbrev->num_attrs; ++i)
17874 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17878 *has_children = abbrev->has_children;
17882 /* Read a die and all its attributes.
17883 Set DIEP to point to a newly allocated die with its information,
17884 except for its child, sibling, and parent fields.
17885 Set HAS_CHILDREN to tell whether the die has children or not. */
17887 static const gdb_byte *
17888 read_full_die (const struct die_reader_specs *reader,
17889 struct die_info **diep, const gdb_byte *info_ptr,
17892 const gdb_byte *result;
17894 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
17896 if (dwarf_die_debug)
17898 fprintf_unfiltered (gdb_stdlog,
17899 "Read die from %s@0x%x of %s:\n",
17900 get_section_name (reader->die_section),
17901 (unsigned) (info_ptr - reader->die_section->buffer),
17902 bfd_get_filename (reader->abfd));
17903 dump_die (*diep, dwarf_die_debug);
17909 /* Abbreviation tables.
17911 In DWARF version 2, the description of the debugging information is
17912 stored in a separate .debug_abbrev section. Before we read any
17913 dies from a section we read in all abbreviations and install them
17914 in a hash table. */
17916 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
17918 static struct abbrev_info *
17919 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
17921 struct abbrev_info *abbrev;
17923 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
17924 memset (abbrev, 0, sizeof (struct abbrev_info));
17929 /* Add an abbreviation to the table. */
17932 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
17933 unsigned int abbrev_number,
17934 struct abbrev_info *abbrev)
17936 unsigned int hash_number;
17938 hash_number = abbrev_number % ABBREV_HASH_SIZE;
17939 abbrev->next = abbrev_table->abbrevs[hash_number];
17940 abbrev_table->abbrevs[hash_number] = abbrev;
17943 /* Look up an abbrev in the table.
17944 Returns NULL if the abbrev is not found. */
17946 static struct abbrev_info *
17947 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
17948 unsigned int abbrev_number)
17950 unsigned int hash_number;
17951 struct abbrev_info *abbrev;
17953 hash_number = abbrev_number % ABBREV_HASH_SIZE;
17954 abbrev = abbrev_table->abbrevs[hash_number];
17958 if (abbrev->number == abbrev_number)
17960 abbrev = abbrev->next;
17965 /* Read in an abbrev table. */
17967 static struct abbrev_table *
17968 abbrev_table_read_table (struct dwarf2_section_info *section,
17969 sect_offset sect_off)
17971 struct objfile *objfile = dwarf2_per_objfile->objfile;
17972 bfd *abfd = get_section_bfd_owner (section);
17973 struct abbrev_table *abbrev_table;
17974 const gdb_byte *abbrev_ptr;
17975 struct abbrev_info *cur_abbrev;
17976 unsigned int abbrev_number, bytes_read, abbrev_name;
17977 unsigned int abbrev_form;
17978 struct attr_abbrev *cur_attrs;
17979 unsigned int allocated_attrs;
17981 abbrev_table = XNEW (struct abbrev_table);
17982 abbrev_table->sect_off = sect_off;
17983 obstack_init (&abbrev_table->abbrev_obstack);
17984 abbrev_table->abbrevs =
17985 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
17987 memset (abbrev_table->abbrevs, 0,
17988 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
17990 dwarf2_read_section (objfile, section);
17991 abbrev_ptr = section->buffer + to_underlying (sect_off);
17992 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17993 abbrev_ptr += bytes_read;
17995 allocated_attrs = ATTR_ALLOC_CHUNK;
17996 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
17998 /* Loop until we reach an abbrev number of 0. */
17999 while (abbrev_number)
18001 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
18003 /* read in abbrev header */
18004 cur_abbrev->number = abbrev_number;
18006 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18007 abbrev_ptr += bytes_read;
18008 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18011 /* now read in declarations */
18014 LONGEST implicit_const;
18016 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18017 abbrev_ptr += bytes_read;
18018 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18019 abbrev_ptr += bytes_read;
18020 if (abbrev_form == DW_FORM_implicit_const)
18022 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18024 abbrev_ptr += bytes_read;
18028 /* Initialize it due to a false compiler warning. */
18029 implicit_const = -1;
18032 if (abbrev_name == 0)
18035 if (cur_abbrev->num_attrs == allocated_attrs)
18037 allocated_attrs += ATTR_ALLOC_CHUNK;
18039 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18042 cur_attrs[cur_abbrev->num_attrs].name
18043 = (enum dwarf_attribute) abbrev_name;
18044 cur_attrs[cur_abbrev->num_attrs].form
18045 = (enum dwarf_form) abbrev_form;
18046 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18047 ++cur_abbrev->num_attrs;
18050 cur_abbrev->attrs =
18051 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18052 cur_abbrev->num_attrs);
18053 memcpy (cur_abbrev->attrs, cur_attrs,
18054 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18056 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
18058 /* Get next abbreviation.
18059 Under Irix6 the abbreviations for a compilation unit are not
18060 always properly terminated with an abbrev number of 0.
18061 Exit loop if we encounter an abbreviation which we have
18062 already read (which means we are about to read the abbreviations
18063 for the next compile unit) or if the end of the abbreviation
18064 table is reached. */
18065 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18067 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18068 abbrev_ptr += bytes_read;
18069 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
18074 return abbrev_table;
18077 /* Free the resources held by ABBREV_TABLE. */
18080 abbrev_table_free (struct abbrev_table *abbrev_table)
18082 obstack_free (&abbrev_table->abbrev_obstack, NULL);
18083 xfree (abbrev_table);
18086 /* Same as abbrev_table_free but as a cleanup.
18087 We pass in a pointer to the pointer to the table so that we can
18088 set the pointer to NULL when we're done. It also simplifies
18089 build_type_psymtabs_1. */
18092 abbrev_table_free_cleanup (void *table_ptr)
18094 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
18096 if (*abbrev_table_ptr != NULL)
18097 abbrev_table_free (*abbrev_table_ptr);
18098 *abbrev_table_ptr = NULL;
18101 /* Read the abbrev table for CU from ABBREV_SECTION. */
18104 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
18105 struct dwarf2_section_info *abbrev_section)
18108 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
18111 /* Release the memory used by the abbrev table for a compilation unit. */
18114 dwarf2_free_abbrev_table (void *ptr_to_cu)
18116 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
18118 if (cu->abbrev_table != NULL)
18119 abbrev_table_free (cu->abbrev_table);
18120 /* Set this to NULL so that we SEGV if we try to read it later,
18121 and also because free_comp_unit verifies this is NULL. */
18122 cu->abbrev_table = NULL;
18125 /* Returns nonzero if TAG represents a type that we might generate a partial
18129 is_type_tag_for_partial (int tag)
18134 /* Some types that would be reasonable to generate partial symbols for,
18135 that we don't at present. */
18136 case DW_TAG_array_type:
18137 case DW_TAG_file_type:
18138 case DW_TAG_ptr_to_member_type:
18139 case DW_TAG_set_type:
18140 case DW_TAG_string_type:
18141 case DW_TAG_subroutine_type:
18143 case DW_TAG_base_type:
18144 case DW_TAG_class_type:
18145 case DW_TAG_interface_type:
18146 case DW_TAG_enumeration_type:
18147 case DW_TAG_structure_type:
18148 case DW_TAG_subrange_type:
18149 case DW_TAG_typedef:
18150 case DW_TAG_union_type:
18157 /* Load all DIEs that are interesting for partial symbols into memory. */
18159 static struct partial_die_info *
18160 load_partial_dies (const struct die_reader_specs *reader,
18161 const gdb_byte *info_ptr, int building_psymtab)
18163 struct dwarf2_cu *cu = reader->cu;
18164 struct objfile *objfile = cu->objfile;
18165 struct partial_die_info *part_die;
18166 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18167 struct abbrev_info *abbrev;
18168 unsigned int bytes_read;
18169 unsigned int load_all = 0;
18170 int nesting_level = 1;
18175 gdb_assert (cu->per_cu != NULL);
18176 if (cu->per_cu->load_all_dies)
18180 = htab_create_alloc_ex (cu->header.length / 12,
18184 &cu->comp_unit_obstack,
18185 hashtab_obstack_allocate,
18186 dummy_obstack_deallocate);
18188 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
18192 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
18194 /* A NULL abbrev means the end of a series of children. */
18195 if (abbrev == NULL)
18197 if (--nesting_level == 0)
18199 /* PART_DIE was probably the last thing allocated on the
18200 comp_unit_obstack, so we could call obstack_free
18201 here. We don't do that because the waste is small,
18202 and will be cleaned up when we're done with this
18203 compilation unit. This way, we're also more robust
18204 against other users of the comp_unit_obstack. */
18207 info_ptr += bytes_read;
18208 last_die = parent_die;
18209 parent_die = parent_die->die_parent;
18213 /* Check for template arguments. We never save these; if
18214 they're seen, we just mark the parent, and go on our way. */
18215 if (parent_die != NULL
18216 && cu->language == language_cplus
18217 && (abbrev->tag == DW_TAG_template_type_param
18218 || abbrev->tag == DW_TAG_template_value_param))
18220 parent_die->has_template_arguments = 1;
18224 /* We don't need a partial DIE for the template argument. */
18225 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18230 /* We only recurse into c++ subprograms looking for template arguments.
18231 Skip their other children. */
18233 && cu->language == language_cplus
18234 && parent_die != NULL
18235 && parent_die->tag == DW_TAG_subprogram)
18237 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18241 /* Check whether this DIE is interesting enough to save. Normally
18242 we would not be interested in members here, but there may be
18243 later variables referencing them via DW_AT_specification (for
18244 static members). */
18246 && !is_type_tag_for_partial (abbrev->tag)
18247 && abbrev->tag != DW_TAG_constant
18248 && abbrev->tag != DW_TAG_enumerator
18249 && abbrev->tag != DW_TAG_subprogram
18250 && abbrev->tag != DW_TAG_lexical_block
18251 && abbrev->tag != DW_TAG_variable
18252 && abbrev->tag != DW_TAG_namespace
18253 && abbrev->tag != DW_TAG_module
18254 && abbrev->tag != DW_TAG_member
18255 && abbrev->tag != DW_TAG_imported_unit
18256 && abbrev->tag != DW_TAG_imported_declaration)
18258 /* Otherwise we skip to the next sibling, if any. */
18259 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18263 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
18266 /* This two-pass algorithm for processing partial symbols has a
18267 high cost in cache pressure. Thus, handle some simple cases
18268 here which cover the majority of C partial symbols. DIEs
18269 which neither have specification tags in them, nor could have
18270 specification tags elsewhere pointing at them, can simply be
18271 processed and discarded.
18273 This segment is also optional; scan_partial_symbols and
18274 add_partial_symbol will handle these DIEs if we chain
18275 them in normally. When compilers which do not emit large
18276 quantities of duplicate debug information are more common,
18277 this code can probably be removed. */
18279 /* Any complete simple types at the top level (pretty much all
18280 of them, for a language without namespaces), can be processed
18282 if (parent_die == NULL
18283 && part_die->has_specification == 0
18284 && part_die->is_declaration == 0
18285 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
18286 || part_die->tag == DW_TAG_base_type
18287 || part_die->tag == DW_TAG_subrange_type))
18289 if (building_psymtab && part_die->name != NULL)
18290 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
18291 VAR_DOMAIN, LOC_TYPEDEF,
18292 &objfile->static_psymbols,
18293 0, cu->language, objfile);
18294 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
18298 /* The exception for DW_TAG_typedef with has_children above is
18299 a workaround of GCC PR debug/47510. In the case of this complaint
18300 type_name_no_tag_or_error will error on such types later.
18302 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18303 it could not find the child DIEs referenced later, this is checked
18304 above. In correct DWARF DW_TAG_typedef should have no children. */
18306 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
18307 complaint (&symfile_complaints,
18308 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18309 "- DIE at 0x%x [in module %s]"),
18310 to_underlying (part_die->sect_off), objfile_name (objfile));
18312 /* If we're at the second level, and we're an enumerator, and
18313 our parent has no specification (meaning possibly lives in a
18314 namespace elsewhere), then we can add the partial symbol now
18315 instead of queueing it. */
18316 if (part_die->tag == DW_TAG_enumerator
18317 && parent_die != NULL
18318 && parent_die->die_parent == NULL
18319 && parent_die->tag == DW_TAG_enumeration_type
18320 && parent_die->has_specification == 0)
18322 if (part_die->name == NULL)
18323 complaint (&symfile_complaints,
18324 _("malformed enumerator DIE ignored"));
18325 else if (building_psymtab)
18326 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
18327 VAR_DOMAIN, LOC_CONST,
18328 cu->language == language_cplus
18329 ? &objfile->global_psymbols
18330 : &objfile->static_psymbols,
18331 0, cu->language, objfile);
18333 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
18337 /* We'll save this DIE so link it in. */
18338 part_die->die_parent = parent_die;
18339 part_die->die_sibling = NULL;
18340 part_die->die_child = NULL;
18342 if (last_die && last_die == parent_die)
18343 last_die->die_child = part_die;
18345 last_die->die_sibling = part_die;
18347 last_die = part_die;
18349 if (first_die == NULL)
18350 first_die = part_die;
18352 /* Maybe add the DIE to the hash table. Not all DIEs that we
18353 find interesting need to be in the hash table, because we
18354 also have the parent/sibling/child chains; only those that we
18355 might refer to by offset later during partial symbol reading.
18357 For now this means things that might have be the target of a
18358 DW_AT_specification, DW_AT_abstract_origin, or
18359 DW_AT_extension. DW_AT_extension will refer only to
18360 namespaces; DW_AT_abstract_origin refers to functions (and
18361 many things under the function DIE, but we do not recurse
18362 into function DIEs during partial symbol reading) and
18363 possibly variables as well; DW_AT_specification refers to
18364 declarations. Declarations ought to have the DW_AT_declaration
18365 flag. It happens that GCC forgets to put it in sometimes, but
18366 only for functions, not for types.
18368 Adding more things than necessary to the hash table is harmless
18369 except for the performance cost. Adding too few will result in
18370 wasted time in find_partial_die, when we reread the compilation
18371 unit with load_all_dies set. */
18374 || abbrev->tag == DW_TAG_constant
18375 || abbrev->tag == DW_TAG_subprogram
18376 || abbrev->tag == DW_TAG_variable
18377 || abbrev->tag == DW_TAG_namespace
18378 || part_die->is_declaration)
18382 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18383 to_underlying (part_die->sect_off),
18388 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
18390 /* For some DIEs we want to follow their children (if any). For C
18391 we have no reason to follow the children of structures; for other
18392 languages we have to, so that we can get at method physnames
18393 to infer fully qualified class names, for DW_AT_specification,
18394 and for C++ template arguments. For C++, we also look one level
18395 inside functions to find template arguments (if the name of the
18396 function does not already contain the template arguments).
18398 For Ada, we need to scan the children of subprograms and lexical
18399 blocks as well because Ada allows the definition of nested
18400 entities that could be interesting for the debugger, such as
18401 nested subprograms for instance. */
18402 if (last_die->has_children
18404 || last_die->tag == DW_TAG_namespace
18405 || last_die->tag == DW_TAG_module
18406 || last_die->tag == DW_TAG_enumeration_type
18407 || (cu->language == language_cplus
18408 && last_die->tag == DW_TAG_subprogram
18409 && (last_die->name == NULL
18410 || strchr (last_die->name, '<') == NULL))
18411 || (cu->language != language_c
18412 && (last_die->tag == DW_TAG_class_type
18413 || last_die->tag == DW_TAG_interface_type
18414 || last_die->tag == DW_TAG_structure_type
18415 || last_die->tag == DW_TAG_union_type))
18416 || (cu->language == language_ada
18417 && (last_die->tag == DW_TAG_subprogram
18418 || last_die->tag == DW_TAG_lexical_block))))
18421 parent_die = last_die;
18425 /* Otherwise we skip to the next sibling, if any. */
18426 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18428 /* Back to the top, do it again. */
18432 /* Read a minimal amount of information into the minimal die structure. */
18434 static const gdb_byte *
18435 read_partial_die (const struct die_reader_specs *reader,
18436 struct partial_die_info *part_die,
18437 struct abbrev_info *abbrev, unsigned int abbrev_len,
18438 const gdb_byte *info_ptr)
18440 struct dwarf2_cu *cu = reader->cu;
18441 struct objfile *objfile = cu->objfile;
18442 const gdb_byte *buffer = reader->buffer;
18444 struct attribute attr;
18445 int has_low_pc_attr = 0;
18446 int has_high_pc_attr = 0;
18447 int high_pc_relative = 0;
18449 memset (part_die, 0, sizeof (struct partial_die_info));
18451 part_die->sect_off = (sect_offset) (info_ptr - buffer);
18453 info_ptr += abbrev_len;
18455 if (abbrev == NULL)
18458 part_die->tag = abbrev->tag;
18459 part_die->has_children = abbrev->has_children;
18461 for (i = 0; i < abbrev->num_attrs; ++i)
18463 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
18465 /* Store the data if it is of an attribute we want to keep in a
18466 partial symbol table. */
18470 switch (part_die->tag)
18472 case DW_TAG_compile_unit:
18473 case DW_TAG_partial_unit:
18474 case DW_TAG_type_unit:
18475 /* Compilation units have a DW_AT_name that is a filename, not
18476 a source language identifier. */
18477 case DW_TAG_enumeration_type:
18478 case DW_TAG_enumerator:
18479 /* These tags always have simple identifiers already; no need
18480 to canonicalize them. */
18481 part_die->name = DW_STRING (&attr);
18485 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18486 &objfile->per_bfd->storage_obstack);
18490 case DW_AT_linkage_name:
18491 case DW_AT_MIPS_linkage_name:
18492 /* Note that both forms of linkage name might appear. We
18493 assume they will be the same, and we only store the last
18495 if (cu->language == language_ada)
18496 part_die->name = DW_STRING (&attr);
18497 part_die->linkage_name = DW_STRING (&attr);
18500 has_low_pc_attr = 1;
18501 part_die->lowpc = attr_value_as_address (&attr);
18503 case DW_AT_high_pc:
18504 has_high_pc_attr = 1;
18505 part_die->highpc = attr_value_as_address (&attr);
18506 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18507 high_pc_relative = 1;
18509 case DW_AT_location:
18510 /* Support the .debug_loc offsets. */
18511 if (attr_form_is_block (&attr))
18513 part_die->d.locdesc = DW_BLOCK (&attr);
18515 else if (attr_form_is_section_offset (&attr))
18517 dwarf2_complex_location_expr_complaint ();
18521 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18522 "partial symbol information");
18525 case DW_AT_external:
18526 part_die->is_external = DW_UNSND (&attr);
18528 case DW_AT_declaration:
18529 part_die->is_declaration = DW_UNSND (&attr);
18532 part_die->has_type = 1;
18534 case DW_AT_abstract_origin:
18535 case DW_AT_specification:
18536 case DW_AT_extension:
18537 part_die->has_specification = 1;
18538 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
18539 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18540 || cu->per_cu->is_dwz);
18542 case DW_AT_sibling:
18543 /* Ignore absolute siblings, they might point outside of
18544 the current compile unit. */
18545 if (attr.form == DW_FORM_ref_addr)
18546 complaint (&symfile_complaints,
18547 _("ignoring absolute DW_AT_sibling"));
18550 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18551 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18553 if (sibling_ptr < info_ptr)
18554 complaint (&symfile_complaints,
18555 _("DW_AT_sibling points backwards"));
18556 else if (sibling_ptr > reader->buffer_end)
18557 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18559 part_die->sibling = sibling_ptr;
18562 case DW_AT_byte_size:
18563 part_die->has_byte_size = 1;
18565 case DW_AT_const_value:
18566 part_die->has_const_value = 1;
18568 case DW_AT_calling_convention:
18569 /* DWARF doesn't provide a way to identify a program's source-level
18570 entry point. DW_AT_calling_convention attributes are only meant
18571 to describe functions' calling conventions.
18573 However, because it's a necessary piece of information in
18574 Fortran, and before DWARF 4 DW_CC_program was the only
18575 piece of debugging information whose definition refers to
18576 a 'main program' at all, several compilers marked Fortran
18577 main programs with DW_CC_program --- even when those
18578 functions use the standard calling conventions.
18580 Although DWARF now specifies a way to provide this
18581 information, we support this practice for backward
18583 if (DW_UNSND (&attr) == DW_CC_program
18584 && cu->language == language_fortran)
18585 part_die->main_subprogram = 1;
18588 if (DW_UNSND (&attr) == DW_INL_inlined
18589 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18590 part_die->may_be_inlined = 1;
18594 if (part_die->tag == DW_TAG_imported_unit)
18596 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
18597 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18598 || cu->per_cu->is_dwz);
18602 case DW_AT_main_subprogram:
18603 part_die->main_subprogram = DW_UNSND (&attr);
18611 if (high_pc_relative)
18612 part_die->highpc += part_die->lowpc;
18614 if (has_low_pc_attr && has_high_pc_attr)
18616 /* When using the GNU linker, .gnu.linkonce. sections are used to
18617 eliminate duplicate copies of functions and vtables and such.
18618 The linker will arbitrarily choose one and discard the others.
18619 The AT_*_pc values for such functions refer to local labels in
18620 these sections. If the section from that file was discarded, the
18621 labels are not in the output, so the relocs get a value of 0.
18622 If this is a discarded function, mark the pc bounds as invalid,
18623 so that GDB will ignore it. */
18624 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18626 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18628 complaint (&symfile_complaints,
18629 _("DW_AT_low_pc %s is zero "
18630 "for DIE at 0x%x [in module %s]"),
18631 paddress (gdbarch, part_die->lowpc),
18632 to_underlying (part_die->sect_off), objfile_name (objfile));
18634 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18635 else if (part_die->lowpc >= part_die->highpc)
18637 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18639 complaint (&symfile_complaints,
18640 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18641 "for DIE at 0x%x [in module %s]"),
18642 paddress (gdbarch, part_die->lowpc),
18643 paddress (gdbarch, part_die->highpc),
18644 to_underlying (part_die->sect_off),
18645 objfile_name (objfile));
18648 part_die->has_pc_info = 1;
18654 /* Find a cached partial DIE at OFFSET in CU. */
18656 static struct partial_die_info *
18657 find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
18659 struct partial_die_info *lookup_die = NULL;
18660 struct partial_die_info part_die;
18662 part_die.sect_off = sect_off;
18663 lookup_die = ((struct partial_die_info *)
18664 htab_find_with_hash (cu->partial_dies, &part_die,
18665 to_underlying (sect_off)));
18670 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18671 except in the case of .debug_types DIEs which do not reference
18672 outside their CU (they do however referencing other types via
18673 DW_FORM_ref_sig8). */
18675 static struct partial_die_info *
18676 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18678 struct objfile *objfile = cu->objfile;
18679 struct dwarf2_per_cu_data *per_cu = NULL;
18680 struct partial_die_info *pd = NULL;
18682 if (offset_in_dwz == cu->per_cu->is_dwz
18683 && offset_in_cu_p (&cu->header, sect_off))
18685 pd = find_partial_die_in_comp_unit (sect_off, cu);
18688 /* We missed recording what we needed.
18689 Load all dies and try again. */
18690 per_cu = cu->per_cu;
18694 /* TUs don't reference other CUs/TUs (except via type signatures). */
18695 if (cu->per_cu->is_debug_types)
18697 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
18698 " external reference to offset 0x%x [in module %s].\n"),
18699 to_underlying (cu->header.sect_off), to_underlying (sect_off),
18700 bfd_get_filename (objfile->obfd));
18702 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18705 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18706 load_partial_comp_unit (per_cu);
18708 per_cu->cu->last_used = 0;
18709 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
18712 /* If we didn't find it, and not all dies have been loaded,
18713 load them all and try again. */
18715 if (pd == NULL && per_cu->load_all_dies == 0)
18717 per_cu->load_all_dies = 1;
18719 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18720 THIS_CU->cu may already be in use. So we can't just free it and
18721 replace its DIEs with the ones we read in. Instead, we leave those
18722 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18723 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18725 load_partial_comp_unit (per_cu);
18727 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
18731 internal_error (__FILE__, __LINE__,
18732 _("could not find partial DIE 0x%x "
18733 "in cache [from module %s]\n"),
18734 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
18738 /* See if we can figure out if the class lives in a namespace. We do
18739 this by looking for a member function; its demangled name will
18740 contain namespace info, if there is any. */
18743 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18744 struct dwarf2_cu *cu)
18746 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18747 what template types look like, because the demangler
18748 frequently doesn't give the same name as the debug info. We
18749 could fix this by only using the demangled name to get the
18750 prefix (but see comment in read_structure_type). */
18752 struct partial_die_info *real_pdi;
18753 struct partial_die_info *child_pdi;
18755 /* If this DIE (this DIE's specification, if any) has a parent, then
18756 we should not do this. We'll prepend the parent's fully qualified
18757 name when we create the partial symbol. */
18759 real_pdi = struct_pdi;
18760 while (real_pdi->has_specification)
18761 real_pdi = find_partial_die (real_pdi->spec_offset,
18762 real_pdi->spec_is_dwz, cu);
18764 if (real_pdi->die_parent != NULL)
18767 for (child_pdi = struct_pdi->die_child;
18769 child_pdi = child_pdi->die_sibling)
18771 if (child_pdi->tag == DW_TAG_subprogram
18772 && child_pdi->linkage_name != NULL)
18774 char *actual_class_name
18775 = language_class_name_from_physname (cu->language_defn,
18776 child_pdi->linkage_name);
18777 if (actual_class_name != NULL)
18781 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
18783 strlen (actual_class_name)));
18784 xfree (actual_class_name);
18791 /* Adjust PART_DIE before generating a symbol for it. This function
18792 may set the is_external flag or change the DIE's name. */
18795 fixup_partial_die (struct partial_die_info *part_die,
18796 struct dwarf2_cu *cu)
18798 /* Once we've fixed up a die, there's no point in doing so again.
18799 This also avoids a memory leak if we were to call
18800 guess_partial_die_structure_name multiple times. */
18801 if (part_die->fixup_called)
18804 /* If we found a reference attribute and the DIE has no name, try
18805 to find a name in the referred to DIE. */
18807 if (part_die->name == NULL && part_die->has_specification)
18809 struct partial_die_info *spec_die;
18811 spec_die = find_partial_die (part_die->spec_offset,
18812 part_die->spec_is_dwz, cu);
18814 fixup_partial_die (spec_die, cu);
18816 if (spec_die->name)
18818 part_die->name = spec_die->name;
18820 /* Copy DW_AT_external attribute if it is set. */
18821 if (spec_die->is_external)
18822 part_die->is_external = spec_die->is_external;
18826 /* Set default names for some unnamed DIEs. */
18828 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
18829 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
18831 /* If there is no parent die to provide a namespace, and there are
18832 children, see if we can determine the namespace from their linkage
18834 if (cu->language == language_cplus
18835 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
18836 && part_die->die_parent == NULL
18837 && part_die->has_children
18838 && (part_die->tag == DW_TAG_class_type
18839 || part_die->tag == DW_TAG_structure_type
18840 || part_die->tag == DW_TAG_union_type))
18841 guess_partial_die_structure_name (part_die, cu);
18843 /* GCC might emit a nameless struct or union that has a linkage
18844 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18845 if (part_die->name == NULL
18846 && (part_die->tag == DW_TAG_class_type
18847 || part_die->tag == DW_TAG_interface_type
18848 || part_die->tag == DW_TAG_structure_type
18849 || part_die->tag == DW_TAG_union_type)
18850 && part_die->linkage_name != NULL)
18854 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
18859 /* Strip any leading namespaces/classes, keep only the base name.
18860 DW_AT_name for named DIEs does not contain the prefixes. */
18861 base = strrchr (demangled, ':');
18862 if (base && base > demangled && base[-1] == ':')
18869 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
18870 base, strlen (base)));
18875 part_die->fixup_called = 1;
18878 /* Read an attribute value described by an attribute form. */
18880 static const gdb_byte *
18881 read_attribute_value (const struct die_reader_specs *reader,
18882 struct attribute *attr, unsigned form,
18883 LONGEST implicit_const, const gdb_byte *info_ptr)
18885 struct dwarf2_cu *cu = reader->cu;
18886 struct objfile *objfile = cu->objfile;
18887 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18888 bfd *abfd = reader->abfd;
18889 struct comp_unit_head *cu_header = &cu->header;
18890 unsigned int bytes_read;
18891 struct dwarf_block *blk;
18893 attr->form = (enum dwarf_form) form;
18896 case DW_FORM_ref_addr:
18897 if (cu->header.version == 2)
18898 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18900 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18901 &cu->header, &bytes_read);
18902 info_ptr += bytes_read;
18904 case DW_FORM_GNU_ref_alt:
18905 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18906 info_ptr += bytes_read;
18909 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18910 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18911 info_ptr += bytes_read;
18913 case DW_FORM_block2:
18914 blk = dwarf_alloc_block (cu);
18915 blk->size = read_2_bytes (abfd, info_ptr);
18917 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18918 info_ptr += blk->size;
18919 DW_BLOCK (attr) = blk;
18921 case DW_FORM_block4:
18922 blk = dwarf_alloc_block (cu);
18923 blk->size = read_4_bytes (abfd, info_ptr);
18925 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18926 info_ptr += blk->size;
18927 DW_BLOCK (attr) = blk;
18929 case DW_FORM_data2:
18930 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18933 case DW_FORM_data4:
18934 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18937 case DW_FORM_data8:
18938 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18941 case DW_FORM_data16:
18942 blk = dwarf_alloc_block (cu);
18944 blk->data = read_n_bytes (abfd, info_ptr, 16);
18946 DW_BLOCK (attr) = blk;
18948 case DW_FORM_sec_offset:
18949 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18950 info_ptr += bytes_read;
18952 case DW_FORM_string:
18953 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18954 DW_STRING_IS_CANONICAL (attr) = 0;
18955 info_ptr += bytes_read;
18958 if (!cu->per_cu->is_dwz)
18960 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
18962 DW_STRING_IS_CANONICAL (attr) = 0;
18963 info_ptr += bytes_read;
18967 case DW_FORM_line_strp:
18968 if (!cu->per_cu->is_dwz)
18970 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
18971 cu_header, &bytes_read);
18972 DW_STRING_IS_CANONICAL (attr) = 0;
18973 info_ptr += bytes_read;
18977 case DW_FORM_GNU_strp_alt:
18979 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18980 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
18983 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
18984 DW_STRING_IS_CANONICAL (attr) = 0;
18985 info_ptr += bytes_read;
18988 case DW_FORM_exprloc:
18989 case DW_FORM_block:
18990 blk = dwarf_alloc_block (cu);
18991 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18992 info_ptr += bytes_read;
18993 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18994 info_ptr += blk->size;
18995 DW_BLOCK (attr) = blk;
18997 case DW_FORM_block1:
18998 blk = dwarf_alloc_block (cu);
18999 blk->size = read_1_byte (abfd, info_ptr);
19001 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19002 info_ptr += blk->size;
19003 DW_BLOCK (attr) = blk;
19005 case DW_FORM_data1:
19006 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19010 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19013 case DW_FORM_flag_present:
19014 DW_UNSND (attr) = 1;
19016 case DW_FORM_sdata:
19017 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19018 info_ptr += bytes_read;
19020 case DW_FORM_udata:
19021 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19022 info_ptr += bytes_read;
19025 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19026 + read_1_byte (abfd, info_ptr));
19030 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19031 + read_2_bytes (abfd, info_ptr));
19035 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19036 + read_4_bytes (abfd, info_ptr));
19040 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19041 + read_8_bytes (abfd, info_ptr));
19044 case DW_FORM_ref_sig8:
19045 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19048 case DW_FORM_ref_udata:
19049 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19050 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19051 info_ptr += bytes_read;
19053 case DW_FORM_indirect:
19054 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19055 info_ptr += bytes_read;
19056 if (form == DW_FORM_implicit_const)
19058 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19059 info_ptr += bytes_read;
19061 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19064 case DW_FORM_implicit_const:
19065 DW_SND (attr) = implicit_const;
19067 case DW_FORM_GNU_addr_index:
19068 if (reader->dwo_file == NULL)
19070 /* For now flag a hard error.
19071 Later we can turn this into a complaint. */
19072 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19073 dwarf_form_name (form),
19074 bfd_get_filename (abfd));
19076 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19077 info_ptr += bytes_read;
19079 case DW_FORM_GNU_str_index:
19080 if (reader->dwo_file == NULL)
19082 /* For now flag a hard error.
19083 Later we can turn this into a complaint if warranted. */
19084 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19085 dwarf_form_name (form),
19086 bfd_get_filename (abfd));
19089 ULONGEST str_index =
19090 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19092 DW_STRING (attr) = read_str_index (reader, str_index);
19093 DW_STRING_IS_CANONICAL (attr) = 0;
19094 info_ptr += bytes_read;
19098 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19099 dwarf_form_name (form),
19100 bfd_get_filename (abfd));
19104 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19105 attr->form = DW_FORM_GNU_ref_alt;
19107 /* We have seen instances where the compiler tried to emit a byte
19108 size attribute of -1 which ended up being encoded as an unsigned
19109 0xffffffff. Although 0xffffffff is technically a valid size value,
19110 an object of this size seems pretty unlikely so we can relatively
19111 safely treat these cases as if the size attribute was invalid and
19112 treat them as zero by default. */
19113 if (attr->name == DW_AT_byte_size
19114 && form == DW_FORM_data4
19115 && DW_UNSND (attr) >= 0xffffffff)
19118 (&symfile_complaints,
19119 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19120 hex_string (DW_UNSND (attr)));
19121 DW_UNSND (attr) = 0;
19127 /* Read an attribute described by an abbreviated attribute. */
19129 static const gdb_byte *
19130 read_attribute (const struct die_reader_specs *reader,
19131 struct attribute *attr, struct attr_abbrev *abbrev,
19132 const gdb_byte *info_ptr)
19134 attr->name = abbrev->name;
19135 return read_attribute_value (reader, attr, abbrev->form,
19136 abbrev->implicit_const, info_ptr);
19139 /* Read dwarf information from a buffer. */
19141 static unsigned int
19142 read_1_byte (bfd *abfd, const gdb_byte *buf)
19144 return bfd_get_8 (abfd, buf);
19148 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19150 return bfd_get_signed_8 (abfd, buf);
19153 static unsigned int
19154 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19156 return bfd_get_16 (abfd, buf);
19160 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19162 return bfd_get_signed_16 (abfd, buf);
19165 static unsigned int
19166 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19168 return bfd_get_32 (abfd, buf);
19172 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19174 return bfd_get_signed_32 (abfd, buf);
19178 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19180 return bfd_get_64 (abfd, buf);
19184 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19185 unsigned int *bytes_read)
19187 struct comp_unit_head *cu_header = &cu->header;
19188 CORE_ADDR retval = 0;
19190 if (cu_header->signed_addr_p)
19192 switch (cu_header->addr_size)
19195 retval = bfd_get_signed_16 (abfd, buf);
19198 retval = bfd_get_signed_32 (abfd, buf);
19201 retval = bfd_get_signed_64 (abfd, buf);
19204 internal_error (__FILE__, __LINE__,
19205 _("read_address: bad switch, signed [in module %s]"),
19206 bfd_get_filename (abfd));
19211 switch (cu_header->addr_size)
19214 retval = bfd_get_16 (abfd, buf);
19217 retval = bfd_get_32 (abfd, buf);
19220 retval = bfd_get_64 (abfd, buf);
19223 internal_error (__FILE__, __LINE__,
19224 _("read_address: bad switch, "
19225 "unsigned [in module %s]"),
19226 bfd_get_filename (abfd));
19230 *bytes_read = cu_header->addr_size;
19234 /* Read the initial length from a section. The (draft) DWARF 3
19235 specification allows the initial length to take up either 4 bytes
19236 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19237 bytes describe the length and all offsets will be 8 bytes in length
19240 An older, non-standard 64-bit format is also handled by this
19241 function. The older format in question stores the initial length
19242 as an 8-byte quantity without an escape value. Lengths greater
19243 than 2^32 aren't very common which means that the initial 4 bytes
19244 is almost always zero. Since a length value of zero doesn't make
19245 sense for the 32-bit format, this initial zero can be considered to
19246 be an escape value which indicates the presence of the older 64-bit
19247 format. As written, the code can't detect (old format) lengths
19248 greater than 4GB. If it becomes necessary to handle lengths
19249 somewhat larger than 4GB, we could allow other small values (such
19250 as the non-sensical values of 1, 2, and 3) to also be used as
19251 escape values indicating the presence of the old format.
19253 The value returned via bytes_read should be used to increment the
19254 relevant pointer after calling read_initial_length().
19256 [ Note: read_initial_length() and read_offset() are based on the
19257 document entitled "DWARF Debugging Information Format", revision
19258 3, draft 8, dated November 19, 2001. This document was obtained
19261 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19263 This document is only a draft and is subject to change. (So beware.)
19265 Details regarding the older, non-standard 64-bit format were
19266 determined empirically by examining 64-bit ELF files produced by
19267 the SGI toolchain on an IRIX 6.5 machine.
19269 - Kevin, July 16, 2002
19273 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19275 LONGEST length = bfd_get_32 (abfd, buf);
19277 if (length == 0xffffffff)
19279 length = bfd_get_64 (abfd, buf + 4);
19282 else if (length == 0)
19284 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19285 length = bfd_get_64 (abfd, buf);
19296 /* Cover function for read_initial_length.
19297 Returns the length of the object at BUF, and stores the size of the
19298 initial length in *BYTES_READ and stores the size that offsets will be in
19300 If the initial length size is not equivalent to that specified in
19301 CU_HEADER then issue a complaint.
19302 This is useful when reading non-comp-unit headers. */
19305 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19306 const struct comp_unit_head *cu_header,
19307 unsigned int *bytes_read,
19308 unsigned int *offset_size)
19310 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19312 gdb_assert (cu_header->initial_length_size == 4
19313 || cu_header->initial_length_size == 8
19314 || cu_header->initial_length_size == 12);
19316 if (cu_header->initial_length_size != *bytes_read)
19317 complaint (&symfile_complaints,
19318 _("intermixed 32-bit and 64-bit DWARF sections"));
19320 *offset_size = (*bytes_read == 4) ? 4 : 8;
19324 /* Read an offset from the data stream. The size of the offset is
19325 given by cu_header->offset_size. */
19328 read_offset (bfd *abfd, const gdb_byte *buf,
19329 const struct comp_unit_head *cu_header,
19330 unsigned int *bytes_read)
19332 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19334 *bytes_read = cu_header->offset_size;
19338 /* Read an offset from the data stream. */
19341 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19343 LONGEST retval = 0;
19345 switch (offset_size)
19348 retval = bfd_get_32 (abfd, buf);
19351 retval = bfd_get_64 (abfd, buf);
19354 internal_error (__FILE__, __LINE__,
19355 _("read_offset_1: bad switch [in module %s]"),
19356 bfd_get_filename (abfd));
19362 static const gdb_byte *
19363 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19365 /* If the size of a host char is 8 bits, we can return a pointer
19366 to the buffer, otherwise we have to copy the data to a buffer
19367 allocated on the temporary obstack. */
19368 gdb_assert (HOST_CHAR_BIT == 8);
19372 static const char *
19373 read_direct_string (bfd *abfd, const gdb_byte *buf,
19374 unsigned int *bytes_read_ptr)
19376 /* If the size of a host char is 8 bits, we can return a pointer
19377 to the string, otherwise we have to copy the string to a buffer
19378 allocated on the temporary obstack. */
19379 gdb_assert (HOST_CHAR_BIT == 8);
19382 *bytes_read_ptr = 1;
19385 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19386 return (const char *) buf;
19389 /* Return pointer to string at section SECT offset STR_OFFSET with error
19390 reporting strings FORM_NAME and SECT_NAME. */
19392 static const char *
19393 read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
19394 struct dwarf2_section_info *sect,
19395 const char *form_name,
19396 const char *sect_name)
19398 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
19399 if (sect->buffer == NULL)
19400 error (_("%s used without %s section [in module %s]"),
19401 form_name, sect_name, bfd_get_filename (abfd));
19402 if (str_offset >= sect->size)
19403 error (_("%s pointing outside of %s section [in module %s]"),
19404 form_name, sect_name, bfd_get_filename (abfd));
19405 gdb_assert (HOST_CHAR_BIT == 8);
19406 if (sect->buffer[str_offset] == '\0')
19408 return (const char *) (sect->buffer + str_offset);
19411 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19413 static const char *
19414 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
19416 return read_indirect_string_at_offset_from (abfd, str_offset,
19417 &dwarf2_per_objfile->str,
19418 "DW_FORM_strp", ".debug_str");
19421 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19423 static const char *
19424 read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
19426 return read_indirect_string_at_offset_from (abfd, str_offset,
19427 &dwarf2_per_objfile->line_str,
19428 "DW_FORM_line_strp",
19429 ".debug_line_str");
19432 /* Read a string at offset STR_OFFSET in the .debug_str section from
19433 the .dwz file DWZ. Throw an error if the offset is too large. If
19434 the string consists of a single NUL byte, return NULL; otherwise
19435 return a pointer to the string. */
19437 static const char *
19438 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
19440 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
19442 if (dwz->str.buffer == NULL)
19443 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19444 "section [in module %s]"),
19445 bfd_get_filename (dwz->dwz_bfd));
19446 if (str_offset >= dwz->str.size)
19447 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19448 ".debug_str section [in module %s]"),
19449 bfd_get_filename (dwz->dwz_bfd));
19450 gdb_assert (HOST_CHAR_BIT == 8);
19451 if (dwz->str.buffer[str_offset] == '\0')
19453 return (const char *) (dwz->str.buffer + str_offset);
19456 /* Return pointer to string at .debug_str offset as read from BUF.
19457 BUF is assumed to be in a compilation unit described by CU_HEADER.
19458 Return *BYTES_READ_PTR count of bytes read from BUF. */
19460 static const char *
19461 read_indirect_string (bfd *abfd, const gdb_byte *buf,
19462 const struct comp_unit_head *cu_header,
19463 unsigned int *bytes_read_ptr)
19465 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19467 return read_indirect_string_at_offset (abfd, str_offset);
19470 /* Return pointer to string at .debug_line_str offset as read from BUF.
19471 BUF is assumed to be in a compilation unit described by CU_HEADER.
19472 Return *BYTES_READ_PTR count of bytes read from BUF. */
19474 static const char *
19475 read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
19476 const struct comp_unit_head *cu_header,
19477 unsigned int *bytes_read_ptr)
19479 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19481 return read_indirect_line_string_at_offset (abfd, str_offset);
19485 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19486 unsigned int *bytes_read_ptr)
19489 unsigned int num_read;
19491 unsigned char byte;
19498 byte = bfd_get_8 (abfd, buf);
19501 result |= ((ULONGEST) (byte & 127) << shift);
19502 if ((byte & 128) == 0)
19508 *bytes_read_ptr = num_read;
19513 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19514 unsigned int *bytes_read_ptr)
19517 int shift, num_read;
19518 unsigned char byte;
19525 byte = bfd_get_8 (abfd, buf);
19528 result |= ((LONGEST) (byte & 127) << shift);
19530 if ((byte & 128) == 0)
19535 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19536 result |= -(((LONGEST) 1) << shift);
19537 *bytes_read_ptr = num_read;
19541 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19542 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19543 ADDR_SIZE is the size of addresses from the CU header. */
19546 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
19548 struct objfile *objfile = dwarf2_per_objfile->objfile;
19549 bfd *abfd = objfile->obfd;
19550 const gdb_byte *info_ptr;
19552 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19553 if (dwarf2_per_objfile->addr.buffer == NULL)
19554 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19555 objfile_name (objfile));
19556 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19557 error (_("DW_FORM_addr_index pointing outside of "
19558 ".debug_addr section [in module %s]"),
19559 objfile_name (objfile));
19560 info_ptr = (dwarf2_per_objfile->addr.buffer
19561 + addr_base + addr_index * addr_size);
19562 if (addr_size == 4)
19563 return bfd_get_32 (abfd, info_ptr);
19565 return bfd_get_64 (abfd, info_ptr);
19568 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19571 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19573 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
19576 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19579 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19580 unsigned int *bytes_read)
19582 bfd *abfd = cu->objfile->obfd;
19583 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19585 return read_addr_index (cu, addr_index);
19588 /* Data structure to pass results from dwarf2_read_addr_index_reader
19589 back to dwarf2_read_addr_index. */
19591 struct dwarf2_read_addr_index_data
19593 ULONGEST addr_base;
19597 /* die_reader_func for dwarf2_read_addr_index. */
19600 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19601 const gdb_byte *info_ptr,
19602 struct die_info *comp_unit_die,
19606 struct dwarf2_cu *cu = reader->cu;
19607 struct dwarf2_read_addr_index_data *aidata =
19608 (struct dwarf2_read_addr_index_data *) data;
19610 aidata->addr_base = cu->addr_base;
19611 aidata->addr_size = cu->header.addr_size;
19614 /* Given an index in .debug_addr, fetch the value.
19615 NOTE: This can be called during dwarf expression evaluation,
19616 long after the debug information has been read, and thus per_cu->cu
19617 may no longer exist. */
19620 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19621 unsigned int addr_index)
19623 struct objfile *objfile = per_cu->objfile;
19624 struct dwarf2_cu *cu = per_cu->cu;
19625 ULONGEST addr_base;
19628 /* This is intended to be called from outside this file. */
19629 dw2_setup (objfile);
19631 /* We need addr_base and addr_size.
19632 If we don't have PER_CU->cu, we have to get it.
19633 Nasty, but the alternative is storing the needed info in PER_CU,
19634 which at this point doesn't seem justified: it's not clear how frequently
19635 it would get used and it would increase the size of every PER_CU.
19636 Entry points like dwarf2_per_cu_addr_size do a similar thing
19637 so we're not in uncharted territory here.
19638 Alas we need to be a bit more complicated as addr_base is contained
19641 We don't need to read the entire CU(/TU).
19642 We just need the header and top level die.
19644 IWBN to use the aging mechanism to let us lazily later discard the CU.
19645 For now we skip this optimization. */
19649 addr_base = cu->addr_base;
19650 addr_size = cu->header.addr_size;
19654 struct dwarf2_read_addr_index_data aidata;
19656 /* Note: We can't use init_cutu_and_read_dies_simple here,
19657 we need addr_base. */
19658 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
19659 dwarf2_read_addr_index_reader, &aidata);
19660 addr_base = aidata.addr_base;
19661 addr_size = aidata.addr_size;
19664 return read_addr_index_1 (addr_index, addr_base, addr_size);
19667 /* Given a DW_FORM_GNU_str_index, fetch the string.
19668 This is only used by the Fission support. */
19670 static const char *
19671 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19673 struct objfile *objfile = dwarf2_per_objfile->objfile;
19674 const char *objf_name = objfile_name (objfile);
19675 bfd *abfd = objfile->obfd;
19676 struct dwarf2_cu *cu = reader->cu;
19677 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19678 struct dwarf2_section_info *str_offsets_section =
19679 &reader->dwo_file->sections.str_offsets;
19680 const gdb_byte *info_ptr;
19681 ULONGEST str_offset;
19682 static const char form_name[] = "DW_FORM_GNU_str_index";
19684 dwarf2_read_section (objfile, str_section);
19685 dwarf2_read_section (objfile, str_offsets_section);
19686 if (str_section->buffer == NULL)
19687 error (_("%s used without .debug_str.dwo section"
19688 " in CU at offset 0x%x [in module %s]"),
19689 form_name, to_underlying (cu->header.sect_off), objf_name);
19690 if (str_offsets_section->buffer == NULL)
19691 error (_("%s used without .debug_str_offsets.dwo section"
19692 " in CU at offset 0x%x [in module %s]"),
19693 form_name, to_underlying (cu->header.sect_off), objf_name);
19694 if (str_index * cu->header.offset_size >= str_offsets_section->size)
19695 error (_("%s pointing outside of .debug_str_offsets.dwo"
19696 " section in CU at offset 0x%x [in module %s]"),
19697 form_name, to_underlying (cu->header.sect_off), objf_name);
19698 info_ptr = (str_offsets_section->buffer
19699 + str_index * cu->header.offset_size);
19700 if (cu->header.offset_size == 4)
19701 str_offset = bfd_get_32 (abfd, info_ptr);
19703 str_offset = bfd_get_64 (abfd, info_ptr);
19704 if (str_offset >= str_section->size)
19705 error (_("Offset from %s pointing outside of"
19706 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
19707 form_name, to_underlying (cu->header.sect_off), objf_name);
19708 return (const char *) (str_section->buffer + str_offset);
19711 /* Return the length of an LEB128 number in BUF. */
19714 leb128_size (const gdb_byte *buf)
19716 const gdb_byte *begin = buf;
19722 if ((byte & 128) == 0)
19723 return buf - begin;
19728 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19737 cu->language = language_c;
19740 case DW_LANG_C_plus_plus:
19741 case DW_LANG_C_plus_plus_11:
19742 case DW_LANG_C_plus_plus_14:
19743 cu->language = language_cplus;
19746 cu->language = language_d;
19748 case DW_LANG_Fortran77:
19749 case DW_LANG_Fortran90:
19750 case DW_LANG_Fortran95:
19751 case DW_LANG_Fortran03:
19752 case DW_LANG_Fortran08:
19753 cu->language = language_fortran;
19756 cu->language = language_go;
19758 case DW_LANG_Mips_Assembler:
19759 cu->language = language_asm;
19761 case DW_LANG_Ada83:
19762 case DW_LANG_Ada95:
19763 cu->language = language_ada;
19765 case DW_LANG_Modula2:
19766 cu->language = language_m2;
19768 case DW_LANG_Pascal83:
19769 cu->language = language_pascal;
19772 cu->language = language_objc;
19775 case DW_LANG_Rust_old:
19776 cu->language = language_rust;
19778 case DW_LANG_Cobol74:
19779 case DW_LANG_Cobol85:
19781 cu->language = language_minimal;
19784 cu->language_defn = language_def (cu->language);
19787 /* Return the named attribute or NULL if not there. */
19789 static struct attribute *
19790 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19795 struct attribute *spec = NULL;
19797 for (i = 0; i < die->num_attrs; ++i)
19799 if (die->attrs[i].name == name)
19800 return &die->attrs[i];
19801 if (die->attrs[i].name == DW_AT_specification
19802 || die->attrs[i].name == DW_AT_abstract_origin)
19803 spec = &die->attrs[i];
19809 die = follow_die_ref (die, spec, &cu);
19815 /* Return the named attribute or NULL if not there,
19816 but do not follow DW_AT_specification, etc.
19817 This is for use in contexts where we're reading .debug_types dies.
19818 Following DW_AT_specification, DW_AT_abstract_origin will take us
19819 back up the chain, and we want to go down. */
19821 static struct attribute *
19822 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19826 for (i = 0; i < die->num_attrs; ++i)
19827 if (die->attrs[i].name == name)
19828 return &die->attrs[i];
19833 /* Return the string associated with a string-typed attribute, or NULL if it
19834 is either not found or is of an incorrect type. */
19836 static const char *
19837 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19839 struct attribute *attr;
19840 const char *str = NULL;
19842 attr = dwarf2_attr (die, name, cu);
19846 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19847 || attr->form == DW_FORM_string
19848 || attr->form == DW_FORM_GNU_str_index
19849 || attr->form == DW_FORM_GNU_strp_alt)
19850 str = DW_STRING (attr);
19852 complaint (&symfile_complaints,
19853 _("string type expected for attribute %s for "
19854 "DIE at 0x%x in module %s"),
19855 dwarf_attr_name (name), to_underlying (die->sect_off),
19856 objfile_name (cu->objfile));
19862 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19863 and holds a non-zero value. This function should only be used for
19864 DW_FORM_flag or DW_FORM_flag_present attributes. */
19867 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19869 struct attribute *attr = dwarf2_attr (die, name, cu);
19871 return (attr && DW_UNSND (attr));
19875 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19877 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19878 which value is non-zero. However, we have to be careful with
19879 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19880 (via dwarf2_flag_true_p) follows this attribute. So we may
19881 end up accidently finding a declaration attribute that belongs
19882 to a different DIE referenced by the specification attribute,
19883 even though the given DIE does not have a declaration attribute. */
19884 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19885 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19888 /* Return the die giving the specification for DIE, if there is
19889 one. *SPEC_CU is the CU containing DIE on input, and the CU
19890 containing the return value on output. If there is no
19891 specification, but there is an abstract origin, that is
19894 static struct die_info *
19895 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19897 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19900 if (spec_attr == NULL)
19901 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19903 if (spec_attr == NULL)
19906 return follow_die_ref (die, spec_attr, spec_cu);
19909 /* Stub for free_line_header to match void * callback types. */
19912 free_line_header_voidp (void *arg)
19914 struct line_header *lh = (struct line_header *) arg;
19920 line_header::add_include_dir (const char *include_dir)
19922 if (dwarf_line_debug >= 2)
19923 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19924 include_dirs.size () + 1, include_dir);
19926 include_dirs.push_back (include_dir);
19930 line_header::add_file_name (const char *name,
19932 unsigned int mod_time,
19933 unsigned int length)
19935 if (dwarf_line_debug >= 2)
19936 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
19937 (unsigned) file_names.size () + 1, name);
19939 file_names.emplace_back (name, d_index, mod_time, length);
19942 /* A convenience function to find the proper .debug_line section for a CU. */
19944 static struct dwarf2_section_info *
19945 get_debug_line_section (struct dwarf2_cu *cu)
19947 struct dwarf2_section_info *section;
19949 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19951 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19952 section = &cu->dwo_unit->dwo_file->sections.line;
19953 else if (cu->per_cu->is_dwz)
19955 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19957 section = &dwz->line;
19960 section = &dwarf2_per_objfile->line;
19965 /* Read directory or file name entry format, starting with byte of
19966 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19967 entries count and the entries themselves in the described entry
19971 read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
19972 struct line_header *lh,
19973 const struct comp_unit_head *cu_header,
19974 void (*callback) (struct line_header *lh,
19977 unsigned int mod_time,
19978 unsigned int length))
19980 gdb_byte format_count, formati;
19981 ULONGEST data_count, datai;
19982 const gdb_byte *buf = *bufp;
19983 const gdb_byte *format_header_data;
19984 unsigned int bytes_read;
19986 format_count = read_1_byte (abfd, buf);
19988 format_header_data = buf;
19989 for (formati = 0; formati < format_count; formati++)
19991 read_unsigned_leb128 (abfd, buf, &bytes_read);
19993 read_unsigned_leb128 (abfd, buf, &bytes_read);
19997 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19999 for (datai = 0; datai < data_count; datai++)
20001 const gdb_byte *format = format_header_data;
20002 struct file_entry fe;
20004 for (formati = 0; formati < format_count; formati++)
20006 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20007 format += bytes_read;
20009 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20010 format += bytes_read;
20012 gdb::optional<const char *> string;
20013 gdb::optional<unsigned int> uint;
20017 case DW_FORM_string:
20018 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20022 case DW_FORM_line_strp:
20023 string.emplace (read_indirect_line_string (abfd, buf,
20029 case DW_FORM_data1:
20030 uint.emplace (read_1_byte (abfd, buf));
20034 case DW_FORM_data2:
20035 uint.emplace (read_2_bytes (abfd, buf));
20039 case DW_FORM_data4:
20040 uint.emplace (read_4_bytes (abfd, buf));
20044 case DW_FORM_data8:
20045 uint.emplace (read_8_bytes (abfd, buf));
20049 case DW_FORM_udata:
20050 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20054 case DW_FORM_block:
20055 /* It is valid only for DW_LNCT_timestamp which is ignored by
20060 switch (content_type)
20063 if (string.has_value ())
20066 case DW_LNCT_directory_index:
20067 if (uint.has_value ())
20068 fe.d_index = (dir_index) *uint;
20070 case DW_LNCT_timestamp:
20071 if (uint.has_value ())
20072 fe.mod_time = *uint;
20075 if (uint.has_value ())
20081 complaint (&symfile_complaints,
20082 _("Unknown format content type %s"),
20083 pulongest (content_type));
20087 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20093 /* Read the statement program header starting at OFFSET in
20094 .debug_line, or .debug_line.dwo. Return a pointer
20095 to a struct line_header, allocated using xmalloc.
20096 Returns NULL if there is a problem reading the header, e.g., if it
20097 has a version we don't understand.
20099 NOTE: the strings in the include directory and file name tables of
20100 the returned object point into the dwarf line section buffer,
20101 and must not be freed. */
20103 static line_header_up
20104 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20106 const gdb_byte *line_ptr;
20107 unsigned int bytes_read, offset_size;
20109 const char *cur_dir, *cur_file;
20110 struct dwarf2_section_info *section;
20113 section = get_debug_line_section (cu);
20114 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20115 if (section->buffer == NULL)
20117 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20118 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20120 complaint (&symfile_complaints, _("missing .debug_line section"));
20124 /* We can't do this until we know the section is non-empty.
20125 Only then do we know we have such a section. */
20126 abfd = get_section_bfd_owner (section);
20128 /* Make sure that at least there's room for the total_length field.
20129 That could be 12 bytes long, but we're just going to fudge that. */
20130 if (to_underlying (sect_off) + 4 >= section->size)
20132 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20136 line_header_up lh (new line_header ());
20138 lh->sect_off = sect_off;
20139 lh->offset_in_dwz = cu->per_cu->is_dwz;
20141 line_ptr = section->buffer + to_underlying (sect_off);
20143 /* Read in the header. */
20145 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20146 &bytes_read, &offset_size);
20147 line_ptr += bytes_read;
20148 if (line_ptr + lh->total_length > (section->buffer + section->size))
20150 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20153 lh->statement_program_end = line_ptr + lh->total_length;
20154 lh->version = read_2_bytes (abfd, line_ptr);
20156 if (lh->version > 5)
20158 /* This is a version we don't understand. The format could have
20159 changed in ways we don't handle properly so just punt. */
20160 complaint (&symfile_complaints,
20161 _("unsupported version in .debug_line section"));
20164 if (lh->version >= 5)
20166 gdb_byte segment_selector_size;
20168 /* Skip address size. */
20169 read_1_byte (abfd, line_ptr);
20172 segment_selector_size = read_1_byte (abfd, line_ptr);
20174 if (segment_selector_size != 0)
20176 complaint (&symfile_complaints,
20177 _("unsupported segment selector size %u "
20178 "in .debug_line section"),
20179 segment_selector_size);
20183 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20184 line_ptr += offset_size;
20185 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20187 if (lh->version >= 4)
20189 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20193 lh->maximum_ops_per_instruction = 1;
20195 if (lh->maximum_ops_per_instruction == 0)
20197 lh->maximum_ops_per_instruction = 1;
20198 complaint (&symfile_complaints,
20199 _("invalid maximum_ops_per_instruction "
20200 "in `.debug_line' section"));
20203 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20205 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20207 lh->line_range = read_1_byte (abfd, line_ptr);
20209 lh->opcode_base = read_1_byte (abfd, line_ptr);
20211 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20213 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20214 for (i = 1; i < lh->opcode_base; ++i)
20216 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20220 if (lh->version >= 5)
20222 /* Read directory table. */
20223 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
20224 [] (struct line_header *lh, const char *name,
20225 dir_index d_index, unsigned int mod_time,
20226 unsigned int length)
20228 lh->add_include_dir (name);
20231 /* Read file name table. */
20232 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
20233 [] (struct line_header *lh, const char *name,
20234 dir_index d_index, unsigned int mod_time,
20235 unsigned int length)
20237 lh->add_file_name (name, d_index, mod_time, length);
20242 /* Read directory table. */
20243 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20245 line_ptr += bytes_read;
20246 lh->add_include_dir (cur_dir);
20248 line_ptr += bytes_read;
20250 /* Read file name table. */
20251 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20253 unsigned int mod_time, length;
20256 line_ptr += bytes_read;
20257 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20258 line_ptr += bytes_read;
20259 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20260 line_ptr += bytes_read;
20261 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20262 line_ptr += bytes_read;
20264 lh->add_file_name (cur_file, d_index, mod_time, length);
20266 line_ptr += bytes_read;
20268 lh->statement_program_start = line_ptr;
20270 if (line_ptr > (section->buffer + section->size))
20271 complaint (&symfile_complaints,
20272 _("line number info header doesn't "
20273 "fit in `.debug_line' section"));
20278 /* Subroutine of dwarf_decode_lines to simplify it.
20279 Return the file name of the psymtab for included file FILE_INDEX
20280 in line header LH of PST.
20281 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20282 If space for the result is malloc'd, it will be freed by a cleanup.
20283 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
20285 The function creates dangling cleanup registration. */
20287 static const char *
20288 psymtab_include_file_name (const struct line_header *lh, int file_index,
20289 const struct partial_symtab *pst,
20290 const char *comp_dir)
20292 const file_entry &fe = lh->file_names[file_index];
20293 const char *include_name = fe.name;
20294 const char *include_name_to_compare = include_name;
20295 const char *pst_filename;
20296 char *copied_name = NULL;
20299 const char *dir_name = fe.include_dir (lh);
20301 if (!IS_ABSOLUTE_PATH (include_name)
20302 && (dir_name != NULL || comp_dir != NULL))
20304 /* Avoid creating a duplicate psymtab for PST.
20305 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20306 Before we do the comparison, however, we need to account
20307 for DIR_NAME and COMP_DIR.
20308 First prepend dir_name (if non-NULL). If we still don't
20309 have an absolute path prepend comp_dir (if non-NULL).
20310 However, the directory we record in the include-file's
20311 psymtab does not contain COMP_DIR (to match the
20312 corresponding symtab(s)).
20317 bash$ gcc -g ./hello.c
20318 include_name = "hello.c"
20320 DW_AT_comp_dir = comp_dir = "/tmp"
20321 DW_AT_name = "./hello.c"
20325 if (dir_name != NULL)
20327 char *tem = concat (dir_name, SLASH_STRING,
20328 include_name, (char *)NULL);
20330 make_cleanup (xfree, tem);
20331 include_name = tem;
20332 include_name_to_compare = include_name;
20334 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20336 char *tem = concat (comp_dir, SLASH_STRING,
20337 include_name, (char *)NULL);
20339 make_cleanup (xfree, tem);
20340 include_name_to_compare = tem;
20344 pst_filename = pst->filename;
20345 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20347 copied_name = concat (pst->dirname, SLASH_STRING,
20348 pst_filename, (char *)NULL);
20349 pst_filename = copied_name;
20352 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20354 if (copied_name != NULL)
20355 xfree (copied_name);
20359 return include_name;
20362 /* State machine to track the state of the line number program. */
20364 class lnp_state_machine
20367 /* Initialize a machine state for the start of a line number
20369 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20371 file_entry *current_file ()
20373 /* lh->file_names is 0-based, but the file name numbers in the
20374 statement program are 1-based. */
20375 return m_line_header->file_name_at (m_file);
20378 /* Record the line in the state machine. END_SEQUENCE is true if
20379 we're processing the end of a sequence. */
20380 void record_line (bool end_sequence);
20382 /* Check address and if invalid nop-out the rest of the lines in this
20384 void check_line_address (struct dwarf2_cu *cu,
20385 const gdb_byte *line_ptr,
20386 CORE_ADDR lowpc, CORE_ADDR address);
20388 void handle_set_discriminator (unsigned int discriminator)
20390 m_discriminator = discriminator;
20391 m_line_has_non_zero_discriminator |= discriminator != 0;
20394 /* Handle DW_LNE_set_address. */
20395 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20398 address += baseaddr;
20399 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20402 /* Handle DW_LNS_advance_pc. */
20403 void handle_advance_pc (CORE_ADDR adjust);
20405 /* Handle a special opcode. */
20406 void handle_special_opcode (unsigned char op_code);
20408 /* Handle DW_LNS_advance_line. */
20409 void handle_advance_line (int line_delta)
20411 advance_line (line_delta);
20414 /* Handle DW_LNS_set_file. */
20415 void handle_set_file (file_name_index file);
20417 /* Handle DW_LNS_negate_stmt. */
20418 void handle_negate_stmt ()
20420 m_is_stmt = !m_is_stmt;
20423 /* Handle DW_LNS_const_add_pc. */
20424 void handle_const_add_pc ();
20426 /* Handle DW_LNS_fixed_advance_pc. */
20427 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20429 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20433 /* Handle DW_LNS_copy. */
20434 void handle_copy ()
20436 record_line (false);
20437 m_discriminator = 0;
20440 /* Handle DW_LNE_end_sequence. */
20441 void handle_end_sequence ()
20443 m_record_line_callback = ::record_line;
20447 /* Advance the line by LINE_DELTA. */
20448 void advance_line (int line_delta)
20450 m_line += line_delta;
20452 if (line_delta != 0)
20453 m_line_has_non_zero_discriminator = m_discriminator != 0;
20456 gdbarch *m_gdbarch;
20458 /* True if we're recording lines.
20459 Otherwise we're building partial symtabs and are just interested in
20460 finding include files mentioned by the line number program. */
20461 bool m_record_lines_p;
20463 /* The line number header. */
20464 line_header *m_line_header;
20466 /* These are part of the standard DWARF line number state machine,
20467 and initialized according to the DWARF spec. */
20469 unsigned char m_op_index = 0;
20470 /* The line table index (1-based) of the current file. */
20471 file_name_index m_file = (file_name_index) 1;
20472 unsigned int m_line = 1;
20474 /* These are initialized in the constructor. */
20476 CORE_ADDR m_address;
20478 unsigned int m_discriminator;
20480 /* Additional bits of state we need to track. */
20482 /* The last file that we called dwarf2_start_subfile for.
20483 This is only used for TLLs. */
20484 unsigned int m_last_file = 0;
20485 /* The last file a line number was recorded for. */
20486 struct subfile *m_last_subfile = NULL;
20488 /* The function to call to record a line. */
20489 record_line_ftype *m_record_line_callback = NULL;
20491 /* The last line number that was recorded, used to coalesce
20492 consecutive entries for the same line. This can happen, for
20493 example, when discriminators are present. PR 17276. */
20494 unsigned int m_last_line = 0;
20495 bool m_line_has_non_zero_discriminator = false;
20499 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20501 CORE_ADDR addr_adj = (((m_op_index + adjust)
20502 / m_line_header->maximum_ops_per_instruction)
20503 * m_line_header->minimum_instruction_length);
20504 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20505 m_op_index = ((m_op_index + adjust)
20506 % m_line_header->maximum_ops_per_instruction);
20510 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20512 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20513 CORE_ADDR addr_adj = (((m_op_index
20514 + (adj_opcode / m_line_header->line_range))
20515 / m_line_header->maximum_ops_per_instruction)
20516 * m_line_header->minimum_instruction_length);
20517 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20518 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20519 % m_line_header->maximum_ops_per_instruction);
20521 int line_delta = (m_line_header->line_base
20522 + (adj_opcode % m_line_header->line_range));
20523 advance_line (line_delta);
20524 record_line (false);
20525 m_discriminator = 0;
20529 lnp_state_machine::handle_set_file (file_name_index file)
20533 const file_entry *fe = current_file ();
20535 dwarf2_debug_line_missing_file_complaint ();
20536 else if (m_record_lines_p)
20538 const char *dir = fe->include_dir (m_line_header);
20540 m_last_subfile = current_subfile;
20541 m_line_has_non_zero_discriminator = m_discriminator != 0;
20542 dwarf2_start_subfile (fe->name, dir);
20547 lnp_state_machine::handle_const_add_pc ()
20550 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20553 = (((m_op_index + adjust)
20554 / m_line_header->maximum_ops_per_instruction)
20555 * m_line_header->minimum_instruction_length);
20557 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20558 m_op_index = ((m_op_index + adjust)
20559 % m_line_header->maximum_ops_per_instruction);
20562 /* Ignore this record_line request. */
20565 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20570 /* Return non-zero if we should add LINE to the line number table.
20571 LINE is the line to add, LAST_LINE is the last line that was added,
20572 LAST_SUBFILE is the subfile for LAST_LINE.
20573 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20574 had a non-zero discriminator.
20576 We have to be careful in the presence of discriminators.
20577 E.g., for this line:
20579 for (i = 0; i < 100000; i++);
20581 clang can emit four line number entries for that one line,
20582 each with a different discriminator.
20583 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20585 However, we want gdb to coalesce all four entries into one.
20586 Otherwise the user could stepi into the middle of the line and
20587 gdb would get confused about whether the pc really was in the
20588 middle of the line.
20590 Things are further complicated by the fact that two consecutive
20591 line number entries for the same line is a heuristic used by gcc
20592 to denote the end of the prologue. So we can't just discard duplicate
20593 entries, we have to be selective about it. The heuristic we use is
20594 that we only collapse consecutive entries for the same line if at least
20595 one of those entries has a non-zero discriminator. PR 17276.
20597 Note: Addresses in the line number state machine can never go backwards
20598 within one sequence, thus this coalescing is ok. */
20601 dwarf_record_line_p (unsigned int line, unsigned int last_line,
20602 int line_has_non_zero_discriminator,
20603 struct subfile *last_subfile)
20605 if (current_subfile != last_subfile)
20607 if (line != last_line)
20609 /* Same line for the same file that we've seen already.
20610 As a last check, for pr 17276, only record the line if the line
20611 has never had a non-zero discriminator. */
20612 if (!line_has_non_zero_discriminator)
20617 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20618 in the line table of subfile SUBFILE. */
20621 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20622 unsigned int line, CORE_ADDR address,
20623 record_line_ftype p_record_line)
20625 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20627 if (dwarf_line_debug)
20629 fprintf_unfiltered (gdb_stdlog,
20630 "Recording line %u, file %s, address %s\n",
20631 line, lbasename (subfile->name),
20632 paddress (gdbarch, address));
20635 (*p_record_line) (subfile, line, addr);
20638 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20639 Mark the end of a set of line number records.
20640 The arguments are the same as for dwarf_record_line_1.
20641 If SUBFILE is NULL the request is ignored. */
20644 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20645 CORE_ADDR address, record_line_ftype p_record_line)
20647 if (subfile == NULL)
20650 if (dwarf_line_debug)
20652 fprintf_unfiltered (gdb_stdlog,
20653 "Finishing current line, file %s, address %s\n",
20654 lbasename (subfile->name),
20655 paddress (gdbarch, address));
20658 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20662 lnp_state_machine::record_line (bool end_sequence)
20664 if (dwarf_line_debug)
20666 fprintf_unfiltered (gdb_stdlog,
20667 "Processing actual line %u: file %u,"
20668 " address %s, is_stmt %u, discrim %u\n",
20669 m_line, to_underlying (m_file),
20670 paddress (m_gdbarch, m_address),
20671 m_is_stmt, m_discriminator);
20674 file_entry *fe = current_file ();
20677 dwarf2_debug_line_missing_file_complaint ();
20678 /* For now we ignore lines not starting on an instruction boundary.
20679 But not when processing end_sequence for compatibility with the
20680 previous version of the code. */
20681 else if (m_op_index == 0 || end_sequence)
20683 fe->included_p = 1;
20684 if (m_record_lines_p && m_is_stmt)
20686 if (m_last_subfile != current_subfile || end_sequence)
20688 dwarf_finish_line (m_gdbarch, m_last_subfile,
20689 m_address, m_record_line_callback);
20694 if (dwarf_record_line_p (m_line, m_last_line,
20695 m_line_has_non_zero_discriminator,
20698 dwarf_record_line_1 (m_gdbarch, current_subfile,
20700 m_record_line_callback);
20702 m_last_subfile = current_subfile;
20703 m_last_line = m_line;
20709 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20710 bool record_lines_p)
20713 m_record_lines_p = record_lines_p;
20714 m_line_header = lh;
20716 m_record_line_callback = ::record_line;
20718 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20719 was a line entry for it so that the backend has a chance to adjust it
20720 and also record it in case it needs it. This is currently used by MIPS
20721 code, cf. `mips_adjust_dwarf2_line'. */
20722 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20723 m_is_stmt = lh->default_is_stmt;
20724 m_discriminator = 0;
20728 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20729 const gdb_byte *line_ptr,
20730 CORE_ADDR lowpc, CORE_ADDR address)
20732 /* If address < lowpc then it's not a usable value, it's outside the
20733 pc range of the CU. However, we restrict the test to only address
20734 values of zero to preserve GDB's previous behaviour which is to
20735 handle the specific case of a function being GC'd by the linker. */
20737 if (address == 0 && address < lowpc)
20739 /* This line table is for a function which has been
20740 GCd by the linker. Ignore it. PR gdb/12528 */
20742 struct objfile *objfile = cu->objfile;
20743 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20745 complaint (&symfile_complaints,
20746 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20747 line_offset, objfile_name (objfile));
20748 m_record_line_callback = noop_record_line;
20749 /* Note: record_line_callback is left as noop_record_line until
20750 we see DW_LNE_end_sequence. */
20754 /* Subroutine of dwarf_decode_lines to simplify it.
20755 Process the line number information in LH.
20756 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20757 program in order to set included_p for every referenced header. */
20760 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20761 const int decode_for_pst_p, CORE_ADDR lowpc)
20763 const gdb_byte *line_ptr, *extended_end;
20764 const gdb_byte *line_end;
20765 unsigned int bytes_read, extended_len;
20766 unsigned char op_code, extended_op;
20767 CORE_ADDR baseaddr;
20768 struct objfile *objfile = cu->objfile;
20769 bfd *abfd = objfile->obfd;
20770 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20771 /* True if we're recording line info (as opposed to building partial
20772 symtabs and just interested in finding include files mentioned by
20773 the line number program). */
20774 bool record_lines_p = !decode_for_pst_p;
20776 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20778 line_ptr = lh->statement_program_start;
20779 line_end = lh->statement_program_end;
20781 /* Read the statement sequences until there's nothing left. */
20782 while (line_ptr < line_end)
20784 /* The DWARF line number program state machine. Reset the state
20785 machine at the start of each sequence. */
20786 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20787 bool end_sequence = false;
20789 if (record_lines_p)
20791 /* Start a subfile for the current file of the state
20793 const file_entry *fe = state_machine.current_file ();
20796 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
20799 /* Decode the table. */
20800 while (line_ptr < line_end && !end_sequence)
20802 op_code = read_1_byte (abfd, line_ptr);
20805 if (op_code >= lh->opcode_base)
20807 /* Special opcode. */
20808 state_machine.handle_special_opcode (op_code);
20810 else switch (op_code)
20812 case DW_LNS_extended_op:
20813 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20815 line_ptr += bytes_read;
20816 extended_end = line_ptr + extended_len;
20817 extended_op = read_1_byte (abfd, line_ptr);
20819 switch (extended_op)
20821 case DW_LNE_end_sequence:
20822 state_machine.handle_end_sequence ();
20823 end_sequence = true;
20825 case DW_LNE_set_address:
20828 = read_address (abfd, line_ptr, cu, &bytes_read);
20829 line_ptr += bytes_read;
20831 state_machine.check_line_address (cu, line_ptr,
20833 state_machine.handle_set_address (baseaddr, address);
20836 case DW_LNE_define_file:
20838 const char *cur_file;
20839 unsigned int mod_time, length;
20842 cur_file = read_direct_string (abfd, line_ptr,
20844 line_ptr += bytes_read;
20845 dindex = (dir_index)
20846 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20847 line_ptr += bytes_read;
20849 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20850 line_ptr += bytes_read;
20852 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20853 line_ptr += bytes_read;
20854 lh->add_file_name (cur_file, dindex, mod_time, length);
20857 case DW_LNE_set_discriminator:
20859 /* The discriminator is not interesting to the
20860 debugger; just ignore it. We still need to
20861 check its value though:
20862 if there are consecutive entries for the same
20863 (non-prologue) line we want to coalesce them.
20866 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20867 line_ptr += bytes_read;
20869 state_machine.handle_set_discriminator (discr);
20873 complaint (&symfile_complaints,
20874 _("mangled .debug_line section"));
20877 /* Make sure that we parsed the extended op correctly. If e.g.
20878 we expected a different address size than the producer used,
20879 we may have read the wrong number of bytes. */
20880 if (line_ptr != extended_end)
20882 complaint (&symfile_complaints,
20883 _("mangled .debug_line section"));
20888 state_machine.handle_copy ();
20890 case DW_LNS_advance_pc:
20893 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20894 line_ptr += bytes_read;
20896 state_machine.handle_advance_pc (adjust);
20899 case DW_LNS_advance_line:
20902 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20903 line_ptr += bytes_read;
20905 state_machine.handle_advance_line (line_delta);
20908 case DW_LNS_set_file:
20910 file_name_index file
20911 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20913 line_ptr += bytes_read;
20915 state_machine.handle_set_file (file);
20918 case DW_LNS_set_column:
20919 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20920 line_ptr += bytes_read;
20922 case DW_LNS_negate_stmt:
20923 state_machine.handle_negate_stmt ();
20925 case DW_LNS_set_basic_block:
20927 /* Add to the address register of the state machine the
20928 address increment value corresponding to special opcode
20929 255. I.e., this value is scaled by the minimum
20930 instruction length since special opcode 255 would have
20931 scaled the increment. */
20932 case DW_LNS_const_add_pc:
20933 state_machine.handle_const_add_pc ();
20935 case DW_LNS_fixed_advance_pc:
20937 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20940 state_machine.handle_fixed_advance_pc (addr_adj);
20945 /* Unknown standard opcode, ignore it. */
20948 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20950 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20951 line_ptr += bytes_read;
20958 dwarf2_debug_line_missing_end_sequence_complaint ();
20960 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20961 in which case we still finish recording the last line). */
20962 state_machine.record_line (true);
20966 /* Decode the Line Number Program (LNP) for the given line_header
20967 structure and CU. The actual information extracted and the type
20968 of structures created from the LNP depends on the value of PST.
20970 1. If PST is NULL, then this procedure uses the data from the program
20971 to create all necessary symbol tables, and their linetables.
20973 2. If PST is not NULL, this procedure reads the program to determine
20974 the list of files included by the unit represented by PST, and
20975 builds all the associated partial symbol tables.
20977 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20978 It is used for relative paths in the line table.
20979 NOTE: When processing partial symtabs (pst != NULL),
20980 comp_dir == pst->dirname.
20982 NOTE: It is important that psymtabs have the same file name (via strcmp)
20983 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20984 symtab we don't use it in the name of the psymtabs we create.
20985 E.g. expand_line_sal requires this when finding psymtabs to expand.
20986 A good testcase for this is mb-inline.exp.
20988 LOWPC is the lowest address in CU (or 0 if not known).
20990 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20991 for its PC<->lines mapping information. Otherwise only the filename
20992 table is read in. */
20995 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20996 struct dwarf2_cu *cu, struct partial_symtab *pst,
20997 CORE_ADDR lowpc, int decode_mapping)
20999 struct objfile *objfile = cu->objfile;
21000 const int decode_for_pst_p = (pst != NULL);
21002 if (decode_mapping)
21003 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21005 if (decode_for_pst_p)
21009 /* Now that we're done scanning the Line Header Program, we can
21010 create the psymtab of each included file. */
21011 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21012 if (lh->file_names[file_index].included_p == 1)
21014 const char *include_name =
21015 psymtab_include_file_name (lh, file_index, pst, comp_dir);
21016 if (include_name != NULL)
21017 dwarf2_create_include_psymtab (include_name, pst, objfile);
21022 /* Make sure a symtab is created for every file, even files
21023 which contain only variables (i.e. no code with associated
21025 struct compunit_symtab *cust = buildsym_compunit_symtab ();
21028 for (i = 0; i < lh->file_names.size (); i++)
21030 file_entry &fe = lh->file_names[i];
21032 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
21034 if (current_subfile->symtab == NULL)
21036 current_subfile->symtab
21037 = allocate_symtab (cust, current_subfile->name);
21039 fe.symtab = current_subfile->symtab;
21044 /* Start a subfile for DWARF. FILENAME is the name of the file and
21045 DIRNAME the name of the source directory which contains FILENAME
21046 or NULL if not known.
21047 This routine tries to keep line numbers from identical absolute and
21048 relative file names in a common subfile.
21050 Using the `list' example from the GDB testsuite, which resides in
21051 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21052 of /srcdir/list0.c yields the following debugging information for list0.c:
21054 DW_AT_name: /srcdir/list0.c
21055 DW_AT_comp_dir: /compdir
21056 files.files[0].name: list0.h
21057 files.files[0].dir: /srcdir
21058 files.files[1].name: list0.c
21059 files.files[1].dir: /srcdir
21061 The line number information for list0.c has to end up in a single
21062 subfile, so that `break /srcdir/list0.c:1' works as expected.
21063 start_subfile will ensure that this happens provided that we pass the
21064 concatenation of files.files[1].dir and files.files[1].name as the
21068 dwarf2_start_subfile (const char *filename, const char *dirname)
21072 /* In order not to lose the line information directory,
21073 we concatenate it to the filename when it makes sense.
21074 Note that the Dwarf3 standard says (speaking of filenames in line
21075 information): ``The directory index is ignored for file names
21076 that represent full path names''. Thus ignoring dirname in the
21077 `else' branch below isn't an issue. */
21079 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21081 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21085 start_subfile (filename);
21091 /* Start a symtab for DWARF.
21092 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21094 static struct compunit_symtab *
21095 dwarf2_start_symtab (struct dwarf2_cu *cu,
21096 const char *name, const char *comp_dir, CORE_ADDR low_pc)
21098 struct compunit_symtab *cust
21099 = start_symtab (cu->objfile, name, comp_dir, low_pc, cu->language);
21101 record_debugformat ("DWARF 2");
21102 record_producer (cu->producer);
21104 /* We assume that we're processing GCC output. */
21105 processing_gcc_compilation = 2;
21107 cu->processing_has_namespace_info = 0;
21113 var_decode_location (struct attribute *attr, struct symbol *sym,
21114 struct dwarf2_cu *cu)
21116 struct objfile *objfile = cu->objfile;
21117 struct comp_unit_head *cu_header = &cu->header;
21119 /* NOTE drow/2003-01-30: There used to be a comment and some special
21120 code here to turn a symbol with DW_AT_external and a
21121 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21122 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21123 with some versions of binutils) where shared libraries could have
21124 relocations against symbols in their debug information - the
21125 minimal symbol would have the right address, but the debug info
21126 would not. It's no longer necessary, because we will explicitly
21127 apply relocations when we read in the debug information now. */
21129 /* A DW_AT_location attribute with no contents indicates that a
21130 variable has been optimized away. */
21131 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21133 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21137 /* Handle one degenerate form of location expression specially, to
21138 preserve GDB's previous behavior when section offsets are
21139 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21140 then mark this symbol as LOC_STATIC. */
21142 if (attr_form_is_block (attr)
21143 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21144 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21145 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21146 && (DW_BLOCK (attr)->size
21147 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21149 unsigned int dummy;
21151 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21152 SYMBOL_VALUE_ADDRESS (sym) =
21153 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21155 SYMBOL_VALUE_ADDRESS (sym) =
21156 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21157 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21158 fixup_symbol_section (sym, objfile);
21159 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21160 SYMBOL_SECTION (sym));
21164 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21165 expression evaluator, and use LOC_COMPUTED only when necessary
21166 (i.e. when the value of a register or memory location is
21167 referenced, or a thread-local block, etc.). Then again, it might
21168 not be worthwhile. I'm assuming that it isn't unless performance
21169 or memory numbers show me otherwise. */
21171 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21173 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21174 cu->has_loclist = 1;
21177 /* Given a pointer to a DWARF information entry, figure out if we need
21178 to make a symbol table entry for it, and if so, create a new entry
21179 and return a pointer to it.
21180 If TYPE is NULL, determine symbol type from the die, otherwise
21181 used the passed type.
21182 If SPACE is not NULL, use it to hold the new symbol. If it is
21183 NULL, allocate a new symbol on the objfile's obstack. */
21185 static struct symbol *
21186 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21187 struct symbol *space)
21189 struct objfile *objfile = cu->objfile;
21190 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21191 struct symbol *sym = NULL;
21193 struct attribute *attr = NULL;
21194 struct attribute *attr2 = NULL;
21195 CORE_ADDR baseaddr;
21196 struct pending **list_to_add = NULL;
21198 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21200 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21202 name = dwarf2_name (die, cu);
21205 const char *linkagename;
21206 int suppress_add = 0;
21211 sym = allocate_symbol (objfile);
21212 OBJSTAT (objfile, n_syms++);
21214 /* Cache this symbol's name and the name's demangled form (if any). */
21215 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21216 linkagename = dwarf2_physname (name, die, cu);
21217 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21219 /* Fortran does not have mangling standard and the mangling does differ
21220 between gfortran, iFort etc. */
21221 if (cu->language == language_fortran
21222 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21223 symbol_set_demangled_name (&(sym->ginfo),
21224 dwarf2_full_name (name, die, cu),
21227 /* Default assumptions.
21228 Use the passed type or decode it from the die. */
21229 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21230 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21232 SYMBOL_TYPE (sym) = type;
21234 SYMBOL_TYPE (sym) = die_type (die, cu);
21235 attr = dwarf2_attr (die,
21236 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21240 SYMBOL_LINE (sym) = DW_UNSND (attr);
21243 attr = dwarf2_attr (die,
21244 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21248 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21249 struct file_entry *fe;
21251 if (cu->line_header != NULL)
21252 fe = cu->line_header->file_name_at (file_index);
21257 complaint (&symfile_complaints,
21258 _("file index out of range"));
21260 symbol_set_symtab (sym, fe->symtab);
21266 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21271 addr = attr_value_as_address (attr);
21272 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21273 SYMBOL_VALUE_ADDRESS (sym) = addr;
21275 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21276 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21277 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21278 add_symbol_to_list (sym, cu->list_in_scope);
21280 case DW_TAG_subprogram:
21281 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21283 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21284 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21285 if ((attr2 && (DW_UNSND (attr2) != 0))
21286 || cu->language == language_ada)
21288 /* Subprograms marked external are stored as a global symbol.
21289 Ada subprograms, whether marked external or not, are always
21290 stored as a global symbol, because we want to be able to
21291 access them globally. For instance, we want to be able
21292 to break on a nested subprogram without having to
21293 specify the context. */
21294 list_to_add = &global_symbols;
21298 list_to_add = cu->list_in_scope;
21301 case DW_TAG_inlined_subroutine:
21302 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21304 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21305 SYMBOL_INLINED (sym) = 1;
21306 list_to_add = cu->list_in_scope;
21308 case DW_TAG_template_value_param:
21310 /* Fall through. */
21311 case DW_TAG_constant:
21312 case DW_TAG_variable:
21313 case DW_TAG_member:
21314 /* Compilation with minimal debug info may result in
21315 variables with missing type entries. Change the
21316 misleading `void' type to something sensible. */
21317 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21318 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21320 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21321 /* In the case of DW_TAG_member, we should only be called for
21322 static const members. */
21323 if (die->tag == DW_TAG_member)
21325 /* dwarf2_add_field uses die_is_declaration,
21326 so we do the same. */
21327 gdb_assert (die_is_declaration (die, cu));
21332 dwarf2_const_value (attr, sym, cu);
21333 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21336 if (attr2 && (DW_UNSND (attr2) != 0))
21337 list_to_add = &global_symbols;
21339 list_to_add = cu->list_in_scope;
21343 attr = dwarf2_attr (die, DW_AT_location, cu);
21346 var_decode_location (attr, sym, cu);
21347 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21349 /* Fortran explicitly imports any global symbols to the local
21350 scope by DW_TAG_common_block. */
21351 if (cu->language == language_fortran && die->parent
21352 && die->parent->tag == DW_TAG_common_block)
21355 if (SYMBOL_CLASS (sym) == LOC_STATIC
21356 && SYMBOL_VALUE_ADDRESS (sym) == 0
21357 && !dwarf2_per_objfile->has_section_at_zero)
21359 /* When a static variable is eliminated by the linker,
21360 the corresponding debug information is not stripped
21361 out, but the variable address is set to null;
21362 do not add such variables into symbol table. */
21364 else if (attr2 && (DW_UNSND (attr2) != 0))
21366 /* Workaround gfortran PR debug/40040 - it uses
21367 DW_AT_location for variables in -fPIC libraries which may
21368 get overriden by other libraries/executable and get
21369 a different address. Resolve it by the minimal symbol
21370 which may come from inferior's executable using copy
21371 relocation. Make this workaround only for gfortran as for
21372 other compilers GDB cannot guess the minimal symbol
21373 Fortran mangling kind. */
21374 if (cu->language == language_fortran && die->parent
21375 && die->parent->tag == DW_TAG_module
21377 && startswith (cu->producer, "GNU Fortran"))
21378 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21380 /* A variable with DW_AT_external is never static,
21381 but it may be block-scoped. */
21382 list_to_add = (cu->list_in_scope == &file_symbols
21383 ? &global_symbols : cu->list_in_scope);
21386 list_to_add = cu->list_in_scope;
21390 /* We do not know the address of this symbol.
21391 If it is an external symbol and we have type information
21392 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21393 The address of the variable will then be determined from
21394 the minimal symbol table whenever the variable is
21396 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21398 /* Fortran explicitly imports any global symbols to the local
21399 scope by DW_TAG_common_block. */
21400 if (cu->language == language_fortran && die->parent
21401 && die->parent->tag == DW_TAG_common_block)
21403 /* SYMBOL_CLASS doesn't matter here because
21404 read_common_block is going to reset it. */
21406 list_to_add = cu->list_in_scope;
21408 else if (attr2 && (DW_UNSND (attr2) != 0)
21409 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21411 /* A variable with DW_AT_external is never static, but it
21412 may be block-scoped. */
21413 list_to_add = (cu->list_in_scope == &file_symbols
21414 ? &global_symbols : cu->list_in_scope);
21416 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21418 else if (!die_is_declaration (die, cu))
21420 /* Use the default LOC_OPTIMIZED_OUT class. */
21421 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21423 list_to_add = cu->list_in_scope;
21427 case DW_TAG_formal_parameter:
21428 /* If we are inside a function, mark this as an argument. If
21429 not, we might be looking at an argument to an inlined function
21430 when we do not have enough information to show inlined frames;
21431 pretend it's a local variable in that case so that the user can
21433 if (context_stack_depth > 0
21434 && context_stack[context_stack_depth - 1].name != NULL)
21435 SYMBOL_IS_ARGUMENT (sym) = 1;
21436 attr = dwarf2_attr (die, DW_AT_location, cu);
21439 var_decode_location (attr, sym, cu);
21441 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21444 dwarf2_const_value (attr, sym, cu);
21447 list_to_add = cu->list_in_scope;
21449 case DW_TAG_unspecified_parameters:
21450 /* From varargs functions; gdb doesn't seem to have any
21451 interest in this information, so just ignore it for now.
21454 case DW_TAG_template_type_param:
21456 /* Fall through. */
21457 case DW_TAG_class_type:
21458 case DW_TAG_interface_type:
21459 case DW_TAG_structure_type:
21460 case DW_TAG_union_type:
21461 case DW_TAG_set_type:
21462 case DW_TAG_enumeration_type:
21463 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21464 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21467 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21468 really ever be static objects: otherwise, if you try
21469 to, say, break of a class's method and you're in a file
21470 which doesn't mention that class, it won't work unless
21471 the check for all static symbols in lookup_symbol_aux
21472 saves you. See the OtherFileClass tests in
21473 gdb.c++/namespace.exp. */
21477 list_to_add = (cu->list_in_scope == &file_symbols
21478 && cu->language == language_cplus
21479 ? &global_symbols : cu->list_in_scope);
21481 /* The semantics of C++ state that "struct foo {
21482 ... }" also defines a typedef for "foo". */
21483 if (cu->language == language_cplus
21484 || cu->language == language_ada
21485 || cu->language == language_d
21486 || cu->language == language_rust)
21488 /* The symbol's name is already allocated along
21489 with this objfile, so we don't need to
21490 duplicate it for the type. */
21491 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21492 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21497 case DW_TAG_typedef:
21498 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21499 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21500 list_to_add = cu->list_in_scope;
21502 case DW_TAG_base_type:
21503 case DW_TAG_subrange_type:
21504 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21505 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21506 list_to_add = cu->list_in_scope;
21508 case DW_TAG_enumerator:
21509 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21512 dwarf2_const_value (attr, sym, cu);
21515 /* NOTE: carlton/2003-11-10: See comment above in the
21516 DW_TAG_class_type, etc. block. */
21518 list_to_add = (cu->list_in_scope == &file_symbols
21519 && cu->language == language_cplus
21520 ? &global_symbols : cu->list_in_scope);
21523 case DW_TAG_imported_declaration:
21524 case DW_TAG_namespace:
21525 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21526 list_to_add = &global_symbols;
21528 case DW_TAG_module:
21529 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21530 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21531 list_to_add = &global_symbols;
21533 case DW_TAG_common_block:
21534 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21535 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21536 add_symbol_to_list (sym, cu->list_in_scope);
21539 /* Not a tag we recognize. Hopefully we aren't processing
21540 trash data, but since we must specifically ignore things
21541 we don't recognize, there is nothing else we should do at
21543 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
21544 dwarf_tag_name (die->tag));
21550 sym->hash_next = objfile->template_symbols;
21551 objfile->template_symbols = sym;
21552 list_to_add = NULL;
21555 if (list_to_add != NULL)
21556 add_symbol_to_list (sym, list_to_add);
21558 /* For the benefit of old versions of GCC, check for anonymous
21559 namespaces based on the demangled name. */
21560 if (!cu->processing_has_namespace_info
21561 && cu->language == language_cplus)
21562 cp_scan_for_anonymous_namespaces (sym, objfile);
21567 /* A wrapper for new_symbol_full that always allocates a new symbol. */
21569 static struct symbol *
21570 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
21572 return new_symbol_full (die, type, cu, NULL);
21575 /* Given an attr with a DW_FORM_dataN value in host byte order,
21576 zero-extend it as appropriate for the symbol's type. The DWARF
21577 standard (v4) is not entirely clear about the meaning of using
21578 DW_FORM_dataN for a constant with a signed type, where the type is
21579 wider than the data. The conclusion of a discussion on the DWARF
21580 list was that this is unspecified. We choose to always zero-extend
21581 because that is the interpretation long in use by GCC. */
21584 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21585 struct dwarf2_cu *cu, LONGEST *value, int bits)
21587 struct objfile *objfile = cu->objfile;
21588 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21589 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21590 LONGEST l = DW_UNSND (attr);
21592 if (bits < sizeof (*value) * 8)
21594 l &= ((LONGEST) 1 << bits) - 1;
21597 else if (bits == sizeof (*value) * 8)
21601 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21602 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21609 /* Read a constant value from an attribute. Either set *VALUE, or if
21610 the value does not fit in *VALUE, set *BYTES - either already
21611 allocated on the objfile obstack, or newly allocated on OBSTACK,
21612 or, set *BATON, if we translated the constant to a location
21616 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21617 const char *name, struct obstack *obstack,
21618 struct dwarf2_cu *cu,
21619 LONGEST *value, const gdb_byte **bytes,
21620 struct dwarf2_locexpr_baton **baton)
21622 struct objfile *objfile = cu->objfile;
21623 struct comp_unit_head *cu_header = &cu->header;
21624 struct dwarf_block *blk;
21625 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21626 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21632 switch (attr->form)
21635 case DW_FORM_GNU_addr_index:
21639 if (TYPE_LENGTH (type) != cu_header->addr_size)
21640 dwarf2_const_value_length_mismatch_complaint (name,
21641 cu_header->addr_size,
21642 TYPE_LENGTH (type));
21643 /* Symbols of this form are reasonably rare, so we just
21644 piggyback on the existing location code rather than writing
21645 a new implementation of symbol_computed_ops. */
21646 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21647 (*baton)->per_cu = cu->per_cu;
21648 gdb_assert ((*baton)->per_cu);
21650 (*baton)->size = 2 + cu_header->addr_size;
21651 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21652 (*baton)->data = data;
21654 data[0] = DW_OP_addr;
21655 store_unsigned_integer (&data[1], cu_header->addr_size,
21656 byte_order, DW_ADDR (attr));
21657 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21660 case DW_FORM_string:
21662 case DW_FORM_GNU_str_index:
21663 case DW_FORM_GNU_strp_alt:
21664 /* DW_STRING is already allocated on the objfile obstack, point
21666 *bytes = (const gdb_byte *) DW_STRING (attr);
21668 case DW_FORM_block1:
21669 case DW_FORM_block2:
21670 case DW_FORM_block4:
21671 case DW_FORM_block:
21672 case DW_FORM_exprloc:
21673 case DW_FORM_data16:
21674 blk = DW_BLOCK (attr);
21675 if (TYPE_LENGTH (type) != blk->size)
21676 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21677 TYPE_LENGTH (type));
21678 *bytes = blk->data;
21681 /* The DW_AT_const_value attributes are supposed to carry the
21682 symbol's value "represented as it would be on the target
21683 architecture." By the time we get here, it's already been
21684 converted to host endianness, so we just need to sign- or
21685 zero-extend it as appropriate. */
21686 case DW_FORM_data1:
21687 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21689 case DW_FORM_data2:
21690 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21692 case DW_FORM_data4:
21693 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21695 case DW_FORM_data8:
21696 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21699 case DW_FORM_sdata:
21700 case DW_FORM_implicit_const:
21701 *value = DW_SND (attr);
21704 case DW_FORM_udata:
21705 *value = DW_UNSND (attr);
21709 complaint (&symfile_complaints,
21710 _("unsupported const value attribute form: '%s'"),
21711 dwarf_form_name (attr->form));
21718 /* Copy constant value from an attribute to a symbol. */
21721 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21722 struct dwarf2_cu *cu)
21724 struct objfile *objfile = cu->objfile;
21726 const gdb_byte *bytes;
21727 struct dwarf2_locexpr_baton *baton;
21729 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21730 SYMBOL_PRINT_NAME (sym),
21731 &objfile->objfile_obstack, cu,
21732 &value, &bytes, &baton);
21736 SYMBOL_LOCATION_BATON (sym) = baton;
21737 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21739 else if (bytes != NULL)
21741 SYMBOL_VALUE_BYTES (sym) = bytes;
21742 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21746 SYMBOL_VALUE (sym) = value;
21747 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21751 /* Return the type of the die in question using its DW_AT_type attribute. */
21753 static struct type *
21754 die_type (struct die_info *die, struct dwarf2_cu *cu)
21756 struct attribute *type_attr;
21758 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21761 /* A missing DW_AT_type represents a void type. */
21762 return objfile_type (cu->objfile)->builtin_void;
21765 return lookup_die_type (die, type_attr, cu);
21768 /* True iff CU's producer generates GNAT Ada auxiliary information
21769 that allows to find parallel types through that information instead
21770 of having to do expensive parallel lookups by type name. */
21773 need_gnat_info (struct dwarf2_cu *cu)
21775 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
21776 of GNAT produces this auxiliary information, without any indication
21777 that it is produced. Part of enhancing the FSF version of GNAT
21778 to produce that information will be to put in place an indicator
21779 that we can use in order to determine whether the descriptive type
21780 info is available or not. One suggestion that has been made is
21781 to use a new attribute, attached to the CU die. For now, assume
21782 that the descriptive type info is not available. */
21786 /* Return the auxiliary type of the die in question using its
21787 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21788 attribute is not present. */
21790 static struct type *
21791 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21793 struct attribute *type_attr;
21795 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21799 return lookup_die_type (die, type_attr, cu);
21802 /* If DIE has a descriptive_type attribute, then set the TYPE's
21803 descriptive type accordingly. */
21806 set_descriptive_type (struct type *type, struct die_info *die,
21807 struct dwarf2_cu *cu)
21809 struct type *descriptive_type = die_descriptive_type (die, cu);
21811 if (descriptive_type)
21813 ALLOCATE_GNAT_AUX_TYPE (type);
21814 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21818 /* Return the containing type of the die in question using its
21819 DW_AT_containing_type attribute. */
21821 static struct type *
21822 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21824 struct attribute *type_attr;
21826 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21828 error (_("Dwarf Error: Problem turning containing type into gdb type "
21829 "[in module %s]"), objfile_name (cu->objfile));
21831 return lookup_die_type (die, type_attr, cu);
21834 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21836 static struct type *
21837 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21839 struct objfile *objfile = dwarf2_per_objfile->objfile;
21840 char *message, *saved;
21842 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
21843 objfile_name (objfile),
21844 to_underlying (cu->header.sect_off),
21845 to_underlying (die->sect_off));
21846 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21847 message, strlen (message));
21850 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21853 /* Look up the type of DIE in CU using its type attribute ATTR.
21854 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21855 DW_AT_containing_type.
21856 If there is no type substitute an error marker. */
21858 static struct type *
21859 lookup_die_type (struct die_info *die, const struct attribute *attr,
21860 struct dwarf2_cu *cu)
21862 struct objfile *objfile = cu->objfile;
21863 struct type *this_type;
21865 gdb_assert (attr->name == DW_AT_type
21866 || attr->name == DW_AT_GNAT_descriptive_type
21867 || attr->name == DW_AT_containing_type);
21869 /* First see if we have it cached. */
21871 if (attr->form == DW_FORM_GNU_ref_alt)
21873 struct dwarf2_per_cu_data *per_cu;
21874 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21876 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
21877 this_type = get_die_type_at_offset (sect_off, per_cu);
21879 else if (attr_form_is_ref (attr))
21881 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21883 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21885 else if (attr->form == DW_FORM_ref_sig8)
21887 ULONGEST signature = DW_SIGNATURE (attr);
21889 return get_signatured_type (die, signature, cu);
21893 complaint (&symfile_complaints,
21894 _("Dwarf Error: Bad type attribute %s in DIE"
21895 " at 0x%x [in module %s]"),
21896 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
21897 objfile_name (objfile));
21898 return build_error_marker_type (cu, die);
21901 /* If not cached we need to read it in. */
21903 if (this_type == NULL)
21905 struct die_info *type_die = NULL;
21906 struct dwarf2_cu *type_cu = cu;
21908 if (attr_form_is_ref (attr))
21909 type_die = follow_die_ref (die, attr, &type_cu);
21910 if (type_die == NULL)
21911 return build_error_marker_type (cu, die);
21912 /* If we find the type now, it's probably because the type came
21913 from an inter-CU reference and the type's CU got expanded before
21915 this_type = read_type_die (type_die, type_cu);
21918 /* If we still don't have a type use an error marker. */
21920 if (this_type == NULL)
21921 return build_error_marker_type (cu, die);
21926 /* Return the type in DIE, CU.
21927 Returns NULL for invalid types.
21929 This first does a lookup in die_type_hash,
21930 and only reads the die in if necessary.
21932 NOTE: This can be called when reading in partial or full symbols. */
21934 static struct type *
21935 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21937 struct type *this_type;
21939 this_type = get_die_type (die, cu);
21943 return read_type_die_1 (die, cu);
21946 /* Read the type in DIE, CU.
21947 Returns NULL for invalid types. */
21949 static struct type *
21950 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21952 struct type *this_type = NULL;
21956 case DW_TAG_class_type:
21957 case DW_TAG_interface_type:
21958 case DW_TAG_structure_type:
21959 case DW_TAG_union_type:
21960 this_type = read_structure_type (die, cu);
21962 case DW_TAG_enumeration_type:
21963 this_type = read_enumeration_type (die, cu);
21965 case DW_TAG_subprogram:
21966 case DW_TAG_subroutine_type:
21967 case DW_TAG_inlined_subroutine:
21968 this_type = read_subroutine_type (die, cu);
21970 case DW_TAG_array_type:
21971 this_type = read_array_type (die, cu);
21973 case DW_TAG_set_type:
21974 this_type = read_set_type (die, cu);
21976 case DW_TAG_pointer_type:
21977 this_type = read_tag_pointer_type (die, cu);
21979 case DW_TAG_ptr_to_member_type:
21980 this_type = read_tag_ptr_to_member_type (die, cu);
21982 case DW_TAG_reference_type:
21983 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21985 case DW_TAG_rvalue_reference_type:
21986 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21988 case DW_TAG_const_type:
21989 this_type = read_tag_const_type (die, cu);
21991 case DW_TAG_volatile_type:
21992 this_type = read_tag_volatile_type (die, cu);
21994 case DW_TAG_restrict_type:
21995 this_type = read_tag_restrict_type (die, cu);
21997 case DW_TAG_string_type:
21998 this_type = read_tag_string_type (die, cu);
22000 case DW_TAG_typedef:
22001 this_type = read_typedef (die, cu);
22003 case DW_TAG_subrange_type:
22004 this_type = read_subrange_type (die, cu);
22006 case DW_TAG_base_type:
22007 this_type = read_base_type (die, cu);
22009 case DW_TAG_unspecified_type:
22010 this_type = read_unspecified_type (die, cu);
22012 case DW_TAG_namespace:
22013 this_type = read_namespace_type (die, cu);
22015 case DW_TAG_module:
22016 this_type = read_module_type (die, cu);
22018 case DW_TAG_atomic_type:
22019 this_type = read_tag_atomic_type (die, cu);
22022 complaint (&symfile_complaints,
22023 _("unexpected tag in read_type_die: '%s'"),
22024 dwarf_tag_name (die->tag));
22031 /* See if we can figure out if the class lives in a namespace. We do
22032 this by looking for a member function; its demangled name will
22033 contain namespace info, if there is any.
22034 Return the computed name or NULL.
22035 Space for the result is allocated on the objfile's obstack.
22036 This is the full-die version of guess_partial_die_structure_name.
22037 In this case we know DIE has no useful parent. */
22040 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22042 struct die_info *spec_die;
22043 struct dwarf2_cu *spec_cu;
22044 struct die_info *child;
22047 spec_die = die_specification (die, &spec_cu);
22048 if (spec_die != NULL)
22054 for (child = die->child;
22056 child = child->sibling)
22058 if (child->tag == DW_TAG_subprogram)
22060 const char *linkage_name = dw2_linkage_name (child, cu);
22062 if (linkage_name != NULL)
22065 = language_class_name_from_physname (cu->language_defn,
22069 if (actual_name != NULL)
22071 const char *die_name = dwarf2_name (die, cu);
22073 if (die_name != NULL
22074 && strcmp (die_name, actual_name) != 0)
22076 /* Strip off the class name from the full name.
22077 We want the prefix. */
22078 int die_name_len = strlen (die_name);
22079 int actual_name_len = strlen (actual_name);
22081 /* Test for '::' as a sanity check. */
22082 if (actual_name_len > die_name_len + 2
22083 && actual_name[actual_name_len
22084 - die_name_len - 1] == ':')
22085 name = (char *) obstack_copy0 (
22086 &cu->objfile->per_bfd->storage_obstack,
22087 actual_name, actual_name_len - die_name_len - 2);
22090 xfree (actual_name);
22099 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22100 prefix part in such case. See
22101 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22103 static const char *
22104 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22106 struct attribute *attr;
22109 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22110 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22113 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22116 attr = dw2_linkage_name_attr (die, cu);
22117 if (attr == NULL || DW_STRING (attr) == NULL)
22120 /* dwarf2_name had to be already called. */
22121 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22123 /* Strip the base name, keep any leading namespaces/classes. */
22124 base = strrchr (DW_STRING (attr), ':');
22125 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22128 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
22130 &base[-1] - DW_STRING (attr));
22133 /* Return the name of the namespace/class that DIE is defined within,
22134 or "" if we can't tell. The caller should not xfree the result.
22136 For example, if we're within the method foo() in the following
22146 then determine_prefix on foo's die will return "N::C". */
22148 static const char *
22149 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22151 struct die_info *parent, *spec_die;
22152 struct dwarf2_cu *spec_cu;
22153 struct type *parent_type;
22154 const char *retval;
22156 if (cu->language != language_cplus
22157 && cu->language != language_fortran && cu->language != language_d
22158 && cu->language != language_rust)
22161 retval = anonymous_struct_prefix (die, cu);
22165 /* We have to be careful in the presence of DW_AT_specification.
22166 For example, with GCC 3.4, given the code
22170 // Definition of N::foo.
22174 then we'll have a tree of DIEs like this:
22176 1: DW_TAG_compile_unit
22177 2: DW_TAG_namespace // N
22178 3: DW_TAG_subprogram // declaration of N::foo
22179 4: DW_TAG_subprogram // definition of N::foo
22180 DW_AT_specification // refers to die #3
22182 Thus, when processing die #4, we have to pretend that we're in
22183 the context of its DW_AT_specification, namely the contex of die
22186 spec_die = die_specification (die, &spec_cu);
22187 if (spec_die == NULL)
22188 parent = die->parent;
22191 parent = spec_die->parent;
22195 if (parent == NULL)
22197 else if (parent->building_fullname)
22200 const char *parent_name;
22202 /* It has been seen on RealView 2.2 built binaries,
22203 DW_TAG_template_type_param types actually _defined_ as
22204 children of the parent class:
22207 template class <class Enum> Class{};
22208 Class<enum E> class_e;
22210 1: DW_TAG_class_type (Class)
22211 2: DW_TAG_enumeration_type (E)
22212 3: DW_TAG_enumerator (enum1:0)
22213 3: DW_TAG_enumerator (enum2:1)
22215 2: DW_TAG_template_type_param
22216 DW_AT_type DW_FORM_ref_udata (E)
22218 Besides being broken debug info, it can put GDB into an
22219 infinite loop. Consider:
22221 When we're building the full name for Class<E>, we'll start
22222 at Class, and go look over its template type parameters,
22223 finding E. We'll then try to build the full name of E, and
22224 reach here. We're now trying to build the full name of E,
22225 and look over the parent DIE for containing scope. In the
22226 broken case, if we followed the parent DIE of E, we'd again
22227 find Class, and once again go look at its template type
22228 arguments, etc., etc. Simply don't consider such parent die
22229 as source-level parent of this die (it can't be, the language
22230 doesn't allow it), and break the loop here. */
22231 name = dwarf2_name (die, cu);
22232 parent_name = dwarf2_name (parent, cu);
22233 complaint (&symfile_complaints,
22234 _("template param type '%s' defined within parent '%s'"),
22235 name ? name : "<unknown>",
22236 parent_name ? parent_name : "<unknown>");
22240 switch (parent->tag)
22242 case DW_TAG_namespace:
22243 parent_type = read_type_die (parent, cu);
22244 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22245 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22246 Work around this problem here. */
22247 if (cu->language == language_cplus
22248 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22250 /* We give a name to even anonymous namespaces. */
22251 return TYPE_TAG_NAME (parent_type);
22252 case DW_TAG_class_type:
22253 case DW_TAG_interface_type:
22254 case DW_TAG_structure_type:
22255 case DW_TAG_union_type:
22256 case DW_TAG_module:
22257 parent_type = read_type_die (parent, cu);
22258 if (TYPE_TAG_NAME (parent_type) != NULL)
22259 return TYPE_TAG_NAME (parent_type);
22261 /* An anonymous structure is only allowed non-static data
22262 members; no typedefs, no member functions, et cetera.
22263 So it does not need a prefix. */
22265 case DW_TAG_compile_unit:
22266 case DW_TAG_partial_unit:
22267 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22268 if (cu->language == language_cplus
22269 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22270 && die->child != NULL
22271 && (die->tag == DW_TAG_class_type
22272 || die->tag == DW_TAG_structure_type
22273 || die->tag == DW_TAG_union_type))
22275 char *name = guess_full_die_structure_name (die, cu);
22280 case DW_TAG_enumeration_type:
22281 parent_type = read_type_die (parent, cu);
22282 if (TYPE_DECLARED_CLASS (parent_type))
22284 if (TYPE_TAG_NAME (parent_type) != NULL)
22285 return TYPE_TAG_NAME (parent_type);
22288 /* Fall through. */
22290 return determine_prefix (parent, cu);
22294 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22295 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22296 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22297 an obconcat, otherwise allocate storage for the result. The CU argument is
22298 used to determine the language and hence, the appropriate separator. */
22300 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22303 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22304 int physname, struct dwarf2_cu *cu)
22306 const char *lead = "";
22309 if (suffix == NULL || suffix[0] == '\0'
22310 || prefix == NULL || prefix[0] == '\0')
22312 else if (cu->language == language_d)
22314 /* For D, the 'main' function could be defined in any module, but it
22315 should never be prefixed. */
22316 if (strcmp (suffix, "D main") == 0)
22324 else if (cu->language == language_fortran && physname)
22326 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22327 DW_AT_MIPS_linkage_name is preferred and used instead. */
22335 if (prefix == NULL)
22337 if (suffix == NULL)
22344 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22346 strcpy (retval, lead);
22347 strcat (retval, prefix);
22348 strcat (retval, sep);
22349 strcat (retval, suffix);
22354 /* We have an obstack. */
22355 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22359 /* Return sibling of die, NULL if no sibling. */
22361 static struct die_info *
22362 sibling_die (struct die_info *die)
22364 return die->sibling;
22367 /* Get name of a die, return NULL if not found. */
22369 static const char *
22370 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22371 struct obstack *obstack)
22373 if (name && cu->language == language_cplus)
22375 std::string canon_name = cp_canonicalize_string (name);
22377 if (!canon_name.empty ())
22379 if (canon_name != name)
22380 name = (const char *) obstack_copy0 (obstack,
22381 canon_name.c_str (),
22382 canon_name.length ());
22389 /* Get name of a die, return NULL if not found.
22390 Anonymous namespaces are converted to their magic string. */
22392 static const char *
22393 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22395 struct attribute *attr;
22397 attr = dwarf2_attr (die, DW_AT_name, cu);
22398 if ((!attr || !DW_STRING (attr))
22399 && die->tag != DW_TAG_namespace
22400 && die->tag != DW_TAG_class_type
22401 && die->tag != DW_TAG_interface_type
22402 && die->tag != DW_TAG_structure_type
22403 && die->tag != DW_TAG_union_type)
22408 case DW_TAG_compile_unit:
22409 case DW_TAG_partial_unit:
22410 /* Compilation units have a DW_AT_name that is a filename, not
22411 a source language identifier. */
22412 case DW_TAG_enumeration_type:
22413 case DW_TAG_enumerator:
22414 /* These tags always have simple identifiers already; no need
22415 to canonicalize them. */
22416 return DW_STRING (attr);
22418 case DW_TAG_namespace:
22419 if (attr != NULL && DW_STRING (attr) != NULL)
22420 return DW_STRING (attr);
22421 return CP_ANONYMOUS_NAMESPACE_STR;
22423 case DW_TAG_class_type:
22424 case DW_TAG_interface_type:
22425 case DW_TAG_structure_type:
22426 case DW_TAG_union_type:
22427 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22428 structures or unions. These were of the form "._%d" in GCC 4.1,
22429 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22430 and GCC 4.4. We work around this problem by ignoring these. */
22431 if (attr && DW_STRING (attr)
22432 && (startswith (DW_STRING (attr), "._")
22433 || startswith (DW_STRING (attr), "<anonymous")))
22436 /* GCC might emit a nameless typedef that has a linkage name. See
22437 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22438 if (!attr || DW_STRING (attr) == NULL)
22440 char *demangled = NULL;
22442 attr = dw2_linkage_name_attr (die, cu);
22443 if (attr == NULL || DW_STRING (attr) == NULL)
22446 /* Avoid demangling DW_STRING (attr) the second time on a second
22447 call for the same DIE. */
22448 if (!DW_STRING_IS_CANONICAL (attr))
22449 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22455 /* FIXME: we already did this for the partial symbol... */
22458 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
22459 demangled, strlen (demangled)));
22460 DW_STRING_IS_CANONICAL (attr) = 1;
22463 /* Strip any leading namespaces/classes, keep only the base name.
22464 DW_AT_name for named DIEs does not contain the prefixes. */
22465 base = strrchr (DW_STRING (attr), ':');
22466 if (base && base > DW_STRING (attr) && base[-1] == ':')
22469 return DW_STRING (attr);
22478 if (!DW_STRING_IS_CANONICAL (attr))
22481 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22482 &cu->objfile->per_bfd->storage_obstack);
22483 DW_STRING_IS_CANONICAL (attr) = 1;
22485 return DW_STRING (attr);
22488 /* Return the die that this die in an extension of, or NULL if there
22489 is none. *EXT_CU is the CU containing DIE on input, and the CU
22490 containing the return value on output. */
22492 static struct die_info *
22493 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22495 struct attribute *attr;
22497 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22501 return follow_die_ref (die, attr, ext_cu);
22504 /* Convert a DIE tag into its string name. */
22506 static const char *
22507 dwarf_tag_name (unsigned tag)
22509 const char *name = get_DW_TAG_name (tag);
22512 return "DW_TAG_<unknown>";
22517 /* Convert a DWARF attribute code into its string name. */
22519 static const char *
22520 dwarf_attr_name (unsigned attr)
22524 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22525 if (attr == DW_AT_MIPS_fde)
22526 return "DW_AT_MIPS_fde";
22528 if (attr == DW_AT_HP_block_index)
22529 return "DW_AT_HP_block_index";
22532 name = get_DW_AT_name (attr);
22535 return "DW_AT_<unknown>";
22540 /* Convert a DWARF value form code into its string name. */
22542 static const char *
22543 dwarf_form_name (unsigned form)
22545 const char *name = get_DW_FORM_name (form);
22548 return "DW_FORM_<unknown>";
22553 static const char *
22554 dwarf_bool_name (unsigned mybool)
22562 /* Convert a DWARF type code into its string name. */
22564 static const char *
22565 dwarf_type_encoding_name (unsigned enc)
22567 const char *name = get_DW_ATE_name (enc);
22570 return "DW_ATE_<unknown>";
22576 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22580 print_spaces (indent, f);
22581 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
22582 dwarf_tag_name (die->tag), die->abbrev,
22583 to_underlying (die->sect_off));
22585 if (die->parent != NULL)
22587 print_spaces (indent, f);
22588 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
22589 to_underlying (die->parent->sect_off));
22592 print_spaces (indent, f);
22593 fprintf_unfiltered (f, " has children: %s\n",
22594 dwarf_bool_name (die->child != NULL));
22596 print_spaces (indent, f);
22597 fprintf_unfiltered (f, " attributes:\n");
22599 for (i = 0; i < die->num_attrs; ++i)
22601 print_spaces (indent, f);
22602 fprintf_unfiltered (f, " %s (%s) ",
22603 dwarf_attr_name (die->attrs[i].name),
22604 dwarf_form_name (die->attrs[i].form));
22606 switch (die->attrs[i].form)
22609 case DW_FORM_GNU_addr_index:
22610 fprintf_unfiltered (f, "address: ");
22611 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22613 case DW_FORM_block2:
22614 case DW_FORM_block4:
22615 case DW_FORM_block:
22616 case DW_FORM_block1:
22617 fprintf_unfiltered (f, "block: size %s",
22618 pulongest (DW_BLOCK (&die->attrs[i])->size));
22620 case DW_FORM_exprloc:
22621 fprintf_unfiltered (f, "expression: size %s",
22622 pulongest (DW_BLOCK (&die->attrs[i])->size));
22624 case DW_FORM_data16:
22625 fprintf_unfiltered (f, "constant of 16 bytes");
22627 case DW_FORM_ref_addr:
22628 fprintf_unfiltered (f, "ref address: ");
22629 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22631 case DW_FORM_GNU_ref_alt:
22632 fprintf_unfiltered (f, "alt ref address: ");
22633 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22639 case DW_FORM_ref_udata:
22640 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22641 (long) (DW_UNSND (&die->attrs[i])));
22643 case DW_FORM_data1:
22644 case DW_FORM_data2:
22645 case DW_FORM_data4:
22646 case DW_FORM_data8:
22647 case DW_FORM_udata:
22648 case DW_FORM_sdata:
22649 fprintf_unfiltered (f, "constant: %s",
22650 pulongest (DW_UNSND (&die->attrs[i])));
22652 case DW_FORM_sec_offset:
22653 fprintf_unfiltered (f, "section offset: %s",
22654 pulongest (DW_UNSND (&die->attrs[i])));
22656 case DW_FORM_ref_sig8:
22657 fprintf_unfiltered (f, "signature: %s",
22658 hex_string (DW_SIGNATURE (&die->attrs[i])));
22660 case DW_FORM_string:
22662 case DW_FORM_line_strp:
22663 case DW_FORM_GNU_str_index:
22664 case DW_FORM_GNU_strp_alt:
22665 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22666 DW_STRING (&die->attrs[i])
22667 ? DW_STRING (&die->attrs[i]) : "",
22668 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22671 if (DW_UNSND (&die->attrs[i]))
22672 fprintf_unfiltered (f, "flag: TRUE");
22674 fprintf_unfiltered (f, "flag: FALSE");
22676 case DW_FORM_flag_present:
22677 fprintf_unfiltered (f, "flag: TRUE");
22679 case DW_FORM_indirect:
22680 /* The reader will have reduced the indirect form to
22681 the "base form" so this form should not occur. */
22682 fprintf_unfiltered (f,
22683 "unexpected attribute form: DW_FORM_indirect");
22685 case DW_FORM_implicit_const:
22686 fprintf_unfiltered (f, "constant: %s",
22687 plongest (DW_SND (&die->attrs[i])));
22690 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22691 die->attrs[i].form);
22694 fprintf_unfiltered (f, "\n");
22699 dump_die_for_error (struct die_info *die)
22701 dump_die_shallow (gdb_stderr, 0, die);
22705 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22707 int indent = level * 4;
22709 gdb_assert (die != NULL);
22711 if (level >= max_level)
22714 dump_die_shallow (f, indent, die);
22716 if (die->child != NULL)
22718 print_spaces (indent, f);
22719 fprintf_unfiltered (f, " Children:");
22720 if (level + 1 < max_level)
22722 fprintf_unfiltered (f, "\n");
22723 dump_die_1 (f, level + 1, max_level, die->child);
22727 fprintf_unfiltered (f,
22728 " [not printed, max nesting level reached]\n");
22732 if (die->sibling != NULL && level > 0)
22734 dump_die_1 (f, level, max_level, die->sibling);
22738 /* This is called from the pdie macro in gdbinit.in.
22739 It's not static so gcc will keep a copy callable from gdb. */
22742 dump_die (struct die_info *die, int max_level)
22744 dump_die_1 (gdb_stdlog, 0, max_level, die);
22748 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22752 slot = htab_find_slot_with_hash (cu->die_hash, die,
22753 to_underlying (die->sect_off),
22759 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22763 dwarf2_get_ref_die_offset (const struct attribute *attr)
22765 if (attr_form_is_ref (attr))
22766 return (sect_offset) DW_UNSND (attr);
22768 complaint (&symfile_complaints,
22769 _("unsupported die ref attribute form: '%s'"),
22770 dwarf_form_name (attr->form));
22774 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22775 * the value held by the attribute is not constant. */
22778 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22780 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22781 return DW_SND (attr);
22782 else if (attr->form == DW_FORM_udata
22783 || attr->form == DW_FORM_data1
22784 || attr->form == DW_FORM_data2
22785 || attr->form == DW_FORM_data4
22786 || attr->form == DW_FORM_data8)
22787 return DW_UNSND (attr);
22790 /* For DW_FORM_data16 see attr_form_is_constant. */
22791 complaint (&symfile_complaints,
22792 _("Attribute value is not a constant (%s)"),
22793 dwarf_form_name (attr->form));
22794 return default_value;
22798 /* Follow reference or signature attribute ATTR of SRC_DIE.
22799 On entry *REF_CU is the CU of SRC_DIE.
22800 On exit *REF_CU is the CU of the result. */
22802 static struct die_info *
22803 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22804 struct dwarf2_cu **ref_cu)
22806 struct die_info *die;
22808 if (attr_form_is_ref (attr))
22809 die = follow_die_ref (src_die, attr, ref_cu);
22810 else if (attr->form == DW_FORM_ref_sig8)
22811 die = follow_die_sig (src_die, attr, ref_cu);
22814 dump_die_for_error (src_die);
22815 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22816 objfile_name ((*ref_cu)->objfile));
22822 /* Follow reference OFFSET.
22823 On entry *REF_CU is the CU of the source die referencing OFFSET.
22824 On exit *REF_CU is the CU of the result.
22825 Returns NULL if OFFSET is invalid. */
22827 static struct die_info *
22828 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22829 struct dwarf2_cu **ref_cu)
22831 struct die_info temp_die;
22832 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22834 gdb_assert (cu->per_cu != NULL);
22838 if (cu->per_cu->is_debug_types)
22840 /* .debug_types CUs cannot reference anything outside their CU.
22841 If they need to, they have to reference a signatured type via
22842 DW_FORM_ref_sig8. */
22843 if (!offset_in_cu_p (&cu->header, sect_off))
22846 else if (offset_in_dwz != cu->per_cu->is_dwz
22847 || !offset_in_cu_p (&cu->header, sect_off))
22849 struct dwarf2_per_cu_data *per_cu;
22851 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22854 /* If necessary, add it to the queue and load its DIEs. */
22855 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22856 load_full_comp_unit (per_cu, cu->language);
22858 target_cu = per_cu->cu;
22860 else if (cu->dies == NULL)
22862 /* We're loading full DIEs during partial symbol reading. */
22863 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22864 load_full_comp_unit (cu->per_cu, language_minimal);
22867 *ref_cu = target_cu;
22868 temp_die.sect_off = sect_off;
22869 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22871 to_underlying (sect_off));
22874 /* Follow reference attribute ATTR of SRC_DIE.
22875 On entry *REF_CU is the CU of SRC_DIE.
22876 On exit *REF_CU is the CU of the result. */
22878 static struct die_info *
22879 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22880 struct dwarf2_cu **ref_cu)
22882 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22883 struct dwarf2_cu *cu = *ref_cu;
22884 struct die_info *die;
22886 die = follow_die_offset (sect_off,
22887 (attr->form == DW_FORM_GNU_ref_alt
22888 || cu->per_cu->is_dwz),
22891 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
22892 "at 0x%x [in module %s]"),
22893 to_underlying (sect_off), to_underlying (src_die->sect_off),
22894 objfile_name (cu->objfile));
22899 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22900 Returned value is intended for DW_OP_call*. Returned
22901 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
22903 struct dwarf2_locexpr_baton
22904 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22905 struct dwarf2_per_cu_data *per_cu,
22906 CORE_ADDR (*get_frame_pc) (void *baton),
22909 struct dwarf2_cu *cu;
22910 struct die_info *die;
22911 struct attribute *attr;
22912 struct dwarf2_locexpr_baton retval;
22914 dw2_setup (per_cu->objfile);
22916 if (per_cu->cu == NULL)
22921 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22922 Instead just throw an error, not much else we can do. */
22923 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
22924 to_underlying (sect_off), objfile_name (per_cu->objfile));
22927 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22929 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
22930 to_underlying (sect_off), objfile_name (per_cu->objfile));
22932 attr = dwarf2_attr (die, DW_AT_location, cu);
22935 /* DWARF: "If there is no such attribute, then there is no effect.".
22936 DATA is ignored if SIZE is 0. */
22938 retval.data = NULL;
22941 else if (attr_form_is_section_offset (attr))
22943 struct dwarf2_loclist_baton loclist_baton;
22944 CORE_ADDR pc = (*get_frame_pc) (baton);
22947 fill_in_loclist_baton (cu, &loclist_baton, attr);
22949 retval.data = dwarf2_find_location_expression (&loclist_baton,
22951 retval.size = size;
22955 if (!attr_form_is_block (attr))
22956 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
22957 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22958 to_underlying (sect_off), objfile_name (per_cu->objfile));
22960 retval.data = DW_BLOCK (attr)->data;
22961 retval.size = DW_BLOCK (attr)->size;
22963 retval.per_cu = cu->per_cu;
22965 age_cached_comp_units ();
22970 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
22973 struct dwarf2_locexpr_baton
22974 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22975 struct dwarf2_per_cu_data *per_cu,
22976 CORE_ADDR (*get_frame_pc) (void *baton),
22979 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22981 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22984 /* Write a constant of a given type as target-ordered bytes into
22987 static const gdb_byte *
22988 write_constant_as_bytes (struct obstack *obstack,
22989 enum bfd_endian byte_order,
22996 *len = TYPE_LENGTH (type);
22997 result = (gdb_byte *) obstack_alloc (obstack, *len);
22998 store_unsigned_integer (result, *len, byte_order, value);
23003 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23004 pointer to the constant bytes and set LEN to the length of the
23005 data. If memory is needed, allocate it on OBSTACK. If the DIE
23006 does not have a DW_AT_const_value, return NULL. */
23009 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23010 struct dwarf2_per_cu_data *per_cu,
23011 struct obstack *obstack,
23014 struct dwarf2_cu *cu;
23015 struct die_info *die;
23016 struct attribute *attr;
23017 const gdb_byte *result = NULL;
23020 enum bfd_endian byte_order;
23022 dw2_setup (per_cu->objfile);
23024 if (per_cu->cu == NULL)
23029 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23030 Instead just throw an error, not much else we can do. */
23031 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
23032 to_underlying (sect_off), objfile_name (per_cu->objfile));
23035 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23037 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
23038 to_underlying (sect_off), objfile_name (per_cu->objfile));
23041 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23045 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
23046 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23048 switch (attr->form)
23051 case DW_FORM_GNU_addr_index:
23055 *len = cu->header.addr_size;
23056 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23057 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23061 case DW_FORM_string:
23063 case DW_FORM_GNU_str_index:
23064 case DW_FORM_GNU_strp_alt:
23065 /* DW_STRING is already allocated on the objfile obstack, point
23067 result = (const gdb_byte *) DW_STRING (attr);
23068 *len = strlen (DW_STRING (attr));
23070 case DW_FORM_block1:
23071 case DW_FORM_block2:
23072 case DW_FORM_block4:
23073 case DW_FORM_block:
23074 case DW_FORM_exprloc:
23075 case DW_FORM_data16:
23076 result = DW_BLOCK (attr)->data;
23077 *len = DW_BLOCK (attr)->size;
23080 /* The DW_AT_const_value attributes are supposed to carry the
23081 symbol's value "represented as it would be on the target
23082 architecture." By the time we get here, it's already been
23083 converted to host endianness, so we just need to sign- or
23084 zero-extend it as appropriate. */
23085 case DW_FORM_data1:
23086 type = die_type (die, cu);
23087 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23088 if (result == NULL)
23089 result = write_constant_as_bytes (obstack, byte_order,
23092 case DW_FORM_data2:
23093 type = die_type (die, cu);
23094 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23095 if (result == NULL)
23096 result = write_constant_as_bytes (obstack, byte_order,
23099 case DW_FORM_data4:
23100 type = die_type (die, cu);
23101 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23102 if (result == NULL)
23103 result = write_constant_as_bytes (obstack, byte_order,
23106 case DW_FORM_data8:
23107 type = die_type (die, cu);
23108 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23109 if (result == NULL)
23110 result = write_constant_as_bytes (obstack, byte_order,
23114 case DW_FORM_sdata:
23115 case DW_FORM_implicit_const:
23116 type = die_type (die, cu);
23117 result = write_constant_as_bytes (obstack, byte_order,
23118 type, DW_SND (attr), len);
23121 case DW_FORM_udata:
23122 type = die_type (die, cu);
23123 result = write_constant_as_bytes (obstack, byte_order,
23124 type, DW_UNSND (attr), len);
23128 complaint (&symfile_complaints,
23129 _("unsupported const value attribute form: '%s'"),
23130 dwarf_form_name (attr->form));
23137 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23138 valid type for this die is found. */
23141 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23142 struct dwarf2_per_cu_data *per_cu)
23144 struct dwarf2_cu *cu;
23145 struct die_info *die;
23147 dw2_setup (per_cu->objfile);
23149 if (per_cu->cu == NULL)
23155 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23159 return die_type (die, cu);
23162 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23166 dwarf2_get_die_type (cu_offset die_offset,
23167 struct dwarf2_per_cu_data *per_cu)
23169 dw2_setup (per_cu->objfile);
23171 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23172 return get_die_type_at_offset (die_offset_sect, per_cu);
23175 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23176 On entry *REF_CU is the CU of SRC_DIE.
23177 On exit *REF_CU is the CU of the result.
23178 Returns NULL if the referenced DIE isn't found. */
23180 static struct die_info *
23181 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23182 struct dwarf2_cu **ref_cu)
23184 struct die_info temp_die;
23185 struct dwarf2_cu *sig_cu;
23186 struct die_info *die;
23188 /* While it might be nice to assert sig_type->type == NULL here,
23189 we can get here for DW_AT_imported_declaration where we need
23190 the DIE not the type. */
23192 /* If necessary, add it to the queue and load its DIEs. */
23194 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23195 read_signatured_type (sig_type);
23197 sig_cu = sig_type->per_cu.cu;
23198 gdb_assert (sig_cu != NULL);
23199 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23200 temp_die.sect_off = sig_type->type_offset_in_section;
23201 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23202 to_underlying (temp_die.sect_off));
23205 /* For .gdb_index version 7 keep track of included TUs.
23206 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23207 if (dwarf2_per_objfile->index_table != NULL
23208 && dwarf2_per_objfile->index_table->version <= 7)
23210 VEC_safe_push (dwarf2_per_cu_ptr,
23211 (*ref_cu)->per_cu->imported_symtabs,
23222 /* Follow signatured type referenced by ATTR in SRC_DIE.
23223 On entry *REF_CU is the CU of SRC_DIE.
23224 On exit *REF_CU is the CU of the result.
23225 The result is the DIE of the type.
23226 If the referenced type cannot be found an error is thrown. */
23228 static struct die_info *
23229 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23230 struct dwarf2_cu **ref_cu)
23232 ULONGEST signature = DW_SIGNATURE (attr);
23233 struct signatured_type *sig_type;
23234 struct die_info *die;
23236 gdb_assert (attr->form == DW_FORM_ref_sig8);
23238 sig_type = lookup_signatured_type (*ref_cu, signature);
23239 /* sig_type will be NULL if the signatured type is missing from
23241 if (sig_type == NULL)
23243 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23244 " from DIE at 0x%x [in module %s]"),
23245 hex_string (signature), to_underlying (src_die->sect_off),
23246 objfile_name ((*ref_cu)->objfile));
23249 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23252 dump_die_for_error (src_die);
23253 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23254 " from DIE at 0x%x [in module %s]"),
23255 hex_string (signature), to_underlying (src_die->sect_off),
23256 objfile_name ((*ref_cu)->objfile));
23262 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23263 reading in and processing the type unit if necessary. */
23265 static struct type *
23266 get_signatured_type (struct die_info *die, ULONGEST signature,
23267 struct dwarf2_cu *cu)
23269 struct signatured_type *sig_type;
23270 struct dwarf2_cu *type_cu;
23271 struct die_info *type_die;
23274 sig_type = lookup_signatured_type (cu, signature);
23275 /* sig_type will be NULL if the signatured type is missing from
23277 if (sig_type == NULL)
23279 complaint (&symfile_complaints,
23280 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23281 " from DIE at 0x%x [in module %s]"),
23282 hex_string (signature), to_underlying (die->sect_off),
23283 objfile_name (dwarf2_per_objfile->objfile));
23284 return build_error_marker_type (cu, die);
23287 /* If we already know the type we're done. */
23288 if (sig_type->type != NULL)
23289 return sig_type->type;
23292 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23293 if (type_die != NULL)
23295 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23296 is created. This is important, for example, because for c++ classes
23297 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23298 type = read_type_die (type_die, type_cu);
23301 complaint (&symfile_complaints,
23302 _("Dwarf Error: Cannot build signatured type %s"
23303 " referenced from DIE at 0x%x [in module %s]"),
23304 hex_string (signature), to_underlying (die->sect_off),
23305 objfile_name (dwarf2_per_objfile->objfile));
23306 type = build_error_marker_type (cu, die);
23311 complaint (&symfile_complaints,
23312 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23313 " from DIE at 0x%x [in module %s]"),
23314 hex_string (signature), to_underlying (die->sect_off),
23315 objfile_name (dwarf2_per_objfile->objfile));
23316 type = build_error_marker_type (cu, die);
23318 sig_type->type = type;
23323 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23324 reading in and processing the type unit if necessary. */
23326 static struct type *
23327 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23328 struct dwarf2_cu *cu) /* ARI: editCase function */
23330 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23331 if (attr_form_is_ref (attr))
23333 struct dwarf2_cu *type_cu = cu;
23334 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23336 return read_type_die (type_die, type_cu);
23338 else if (attr->form == DW_FORM_ref_sig8)
23340 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23344 complaint (&symfile_complaints,
23345 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23346 " at 0x%x [in module %s]"),
23347 dwarf_form_name (attr->form), to_underlying (die->sect_off),
23348 objfile_name (dwarf2_per_objfile->objfile));
23349 return build_error_marker_type (cu, die);
23353 /* Load the DIEs associated with type unit PER_CU into memory. */
23356 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23358 struct signatured_type *sig_type;
23360 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23361 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23363 /* We have the per_cu, but we need the signatured_type.
23364 Fortunately this is an easy translation. */
23365 gdb_assert (per_cu->is_debug_types);
23366 sig_type = (struct signatured_type *) per_cu;
23368 gdb_assert (per_cu->cu == NULL);
23370 read_signatured_type (sig_type);
23372 gdb_assert (per_cu->cu != NULL);
23375 /* die_reader_func for read_signatured_type.
23376 This is identical to load_full_comp_unit_reader,
23377 but is kept separate for now. */
23380 read_signatured_type_reader (const struct die_reader_specs *reader,
23381 const gdb_byte *info_ptr,
23382 struct die_info *comp_unit_die,
23386 struct dwarf2_cu *cu = reader->cu;
23388 gdb_assert (cu->die_hash == NULL);
23390 htab_create_alloc_ex (cu->header.length / 12,
23394 &cu->comp_unit_obstack,
23395 hashtab_obstack_allocate,
23396 dummy_obstack_deallocate);
23399 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23400 &info_ptr, comp_unit_die);
23401 cu->dies = comp_unit_die;
23402 /* comp_unit_die is not stored in die_hash, no need. */
23404 /* We try not to read any attributes in this function, because not
23405 all CUs needed for references have been loaded yet, and symbol
23406 table processing isn't initialized. But we have to set the CU language,
23407 or we won't be able to build types correctly.
23408 Similarly, if we do not read the producer, we can not apply
23409 producer-specific interpretation. */
23410 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23413 /* Read in a signatured type and build its CU and DIEs.
23414 If the type is a stub for the real type in a DWO file,
23415 read in the real type from the DWO file as well. */
23418 read_signatured_type (struct signatured_type *sig_type)
23420 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23422 gdb_assert (per_cu->is_debug_types);
23423 gdb_assert (per_cu->cu == NULL);
23425 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23426 read_signatured_type_reader, NULL);
23427 sig_type->per_cu.tu_read = 1;
23430 /* Decode simple location descriptions.
23431 Given a pointer to a dwarf block that defines a location, compute
23432 the location and return the value.
23434 NOTE drow/2003-11-18: This function is called in two situations
23435 now: for the address of static or global variables (partial symbols
23436 only) and for offsets into structures which are expected to be
23437 (more or less) constant. The partial symbol case should go away,
23438 and only the constant case should remain. That will let this
23439 function complain more accurately. A few special modes are allowed
23440 without complaint for global variables (for instance, global
23441 register values and thread-local values).
23443 A location description containing no operations indicates that the
23444 object is optimized out. The return value is 0 for that case.
23445 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23446 callers will only want a very basic result and this can become a
23449 Note that stack[0] is unused except as a default error return. */
23452 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23454 struct objfile *objfile = cu->objfile;
23456 size_t size = blk->size;
23457 const gdb_byte *data = blk->data;
23458 CORE_ADDR stack[64];
23460 unsigned int bytes_read, unsnd;
23466 stack[++stacki] = 0;
23505 stack[++stacki] = op - DW_OP_lit0;
23540 stack[++stacki] = op - DW_OP_reg0;
23542 dwarf2_complex_location_expr_complaint ();
23546 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23548 stack[++stacki] = unsnd;
23550 dwarf2_complex_location_expr_complaint ();
23554 stack[++stacki] = read_address (objfile->obfd, &data[i],
23559 case DW_OP_const1u:
23560 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23564 case DW_OP_const1s:
23565 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23569 case DW_OP_const2u:
23570 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23574 case DW_OP_const2s:
23575 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23579 case DW_OP_const4u:
23580 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23584 case DW_OP_const4s:
23585 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23589 case DW_OP_const8u:
23590 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23595 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23601 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23606 stack[stacki + 1] = stack[stacki];
23611 stack[stacki - 1] += stack[stacki];
23615 case DW_OP_plus_uconst:
23616 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23622 stack[stacki - 1] -= stack[stacki];
23627 /* If we're not the last op, then we definitely can't encode
23628 this using GDB's address_class enum. This is valid for partial
23629 global symbols, although the variable's address will be bogus
23632 dwarf2_complex_location_expr_complaint ();
23635 case DW_OP_GNU_push_tls_address:
23636 case DW_OP_form_tls_address:
23637 /* The top of the stack has the offset from the beginning
23638 of the thread control block at which the variable is located. */
23639 /* Nothing should follow this operator, so the top of stack would
23641 /* This is valid for partial global symbols, but the variable's
23642 address will be bogus in the psymtab. Make it always at least
23643 non-zero to not look as a variable garbage collected by linker
23644 which have DW_OP_addr 0. */
23646 dwarf2_complex_location_expr_complaint ();
23650 case DW_OP_GNU_uninit:
23653 case DW_OP_GNU_addr_index:
23654 case DW_OP_GNU_const_index:
23655 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23662 const char *name = get_DW_OP_name (op);
23665 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23668 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23672 return (stack[stacki]);
23675 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23676 outside of the allocated space. Also enforce minimum>0. */
23677 if (stacki >= ARRAY_SIZE (stack) - 1)
23679 complaint (&symfile_complaints,
23680 _("location description stack overflow"));
23686 complaint (&symfile_complaints,
23687 _("location description stack underflow"));
23691 return (stack[stacki]);
23694 /* memory allocation interface */
23696 static struct dwarf_block *
23697 dwarf_alloc_block (struct dwarf2_cu *cu)
23699 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23702 static struct die_info *
23703 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23705 struct die_info *die;
23706 size_t size = sizeof (struct die_info);
23709 size += (num_attrs - 1) * sizeof (struct attribute);
23711 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23712 memset (die, 0, sizeof (struct die_info));
23717 /* Macro support. */
23719 /* Return file name relative to the compilation directory of file number I in
23720 *LH's file name table. The result is allocated using xmalloc; the caller is
23721 responsible for freeing it. */
23724 file_file_name (int file, struct line_header *lh)
23726 /* Is the file number a valid index into the line header's file name
23727 table? Remember that file numbers start with one, not zero. */
23728 if (1 <= file && file <= lh->file_names.size ())
23730 const file_entry &fe = lh->file_names[file - 1];
23732 if (!IS_ABSOLUTE_PATH (fe.name))
23734 const char *dir = fe.include_dir (lh);
23736 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23738 return xstrdup (fe.name);
23742 /* The compiler produced a bogus file number. We can at least
23743 record the macro definitions made in the file, even if we
23744 won't be able to find the file by name. */
23745 char fake_name[80];
23747 xsnprintf (fake_name, sizeof (fake_name),
23748 "<bad macro file number %d>", file);
23750 complaint (&symfile_complaints,
23751 _("bad file number in macro information (%d)"),
23754 return xstrdup (fake_name);
23758 /* Return the full name of file number I in *LH's file name table.
23759 Use COMP_DIR as the name of the current directory of the
23760 compilation. The result is allocated using xmalloc; the caller is
23761 responsible for freeing it. */
23763 file_full_name (int file, struct line_header *lh, const char *comp_dir)
23765 /* Is the file number a valid index into the line header's file name
23766 table? Remember that file numbers start with one, not zero. */
23767 if (1 <= file && file <= lh->file_names.size ())
23769 char *relative = file_file_name (file, lh);
23771 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23773 return reconcat (relative, comp_dir, SLASH_STRING,
23774 relative, (char *) NULL);
23777 return file_file_name (file, lh);
23781 static struct macro_source_file *
23782 macro_start_file (int file, int line,
23783 struct macro_source_file *current_file,
23784 struct line_header *lh)
23786 /* File name relative to the compilation directory of this source file. */
23787 char *file_name = file_file_name (file, lh);
23789 if (! current_file)
23791 /* Note: We don't create a macro table for this compilation unit
23792 at all until we actually get a filename. */
23793 struct macro_table *macro_table = get_macro_table ();
23795 /* If we have no current file, then this must be the start_file
23796 directive for the compilation unit's main source file. */
23797 current_file = macro_set_main (macro_table, file_name);
23798 macro_define_special (macro_table);
23801 current_file = macro_include (current_file, line, file_name);
23805 return current_file;
23808 static const char *
23809 consume_improper_spaces (const char *p, const char *body)
23813 complaint (&symfile_complaints,
23814 _("macro definition contains spaces "
23815 "in formal argument list:\n`%s'"),
23827 parse_macro_definition (struct macro_source_file *file, int line,
23832 /* The body string takes one of two forms. For object-like macro
23833 definitions, it should be:
23835 <macro name> " " <definition>
23837 For function-like macro definitions, it should be:
23839 <macro name> "() " <definition>
23841 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23843 Spaces may appear only where explicitly indicated, and in the
23846 The Dwarf 2 spec says that an object-like macro's name is always
23847 followed by a space, but versions of GCC around March 2002 omit
23848 the space when the macro's definition is the empty string.
23850 The Dwarf 2 spec says that there should be no spaces between the
23851 formal arguments in a function-like macro's formal argument list,
23852 but versions of GCC around March 2002 include spaces after the
23856 /* Find the extent of the macro name. The macro name is terminated
23857 by either a space or null character (for an object-like macro) or
23858 an opening paren (for a function-like macro). */
23859 for (p = body; *p; p++)
23860 if (*p == ' ' || *p == '(')
23863 if (*p == ' ' || *p == '\0')
23865 /* It's an object-like macro. */
23866 int name_len = p - body;
23867 char *name = savestring (body, name_len);
23868 const char *replacement;
23871 replacement = body + name_len + 1;
23874 dwarf2_macro_malformed_definition_complaint (body);
23875 replacement = body + name_len;
23878 macro_define_object (file, line, name, replacement);
23882 else if (*p == '(')
23884 /* It's a function-like macro. */
23885 char *name = savestring (body, p - body);
23888 char **argv = XNEWVEC (char *, argv_size);
23892 p = consume_improper_spaces (p, body);
23894 /* Parse the formal argument list. */
23895 while (*p && *p != ')')
23897 /* Find the extent of the current argument name. */
23898 const char *arg_start = p;
23900 while (*p && *p != ',' && *p != ')' && *p != ' ')
23903 if (! *p || p == arg_start)
23904 dwarf2_macro_malformed_definition_complaint (body);
23907 /* Make sure argv has room for the new argument. */
23908 if (argc >= argv_size)
23911 argv = XRESIZEVEC (char *, argv, argv_size);
23914 argv[argc++] = savestring (arg_start, p - arg_start);
23917 p = consume_improper_spaces (p, body);
23919 /* Consume the comma, if present. */
23924 p = consume_improper_spaces (p, body);
23933 /* Perfectly formed definition, no complaints. */
23934 macro_define_function (file, line, name,
23935 argc, (const char **) argv,
23937 else if (*p == '\0')
23939 /* Complain, but do define it. */
23940 dwarf2_macro_malformed_definition_complaint (body);
23941 macro_define_function (file, line, name,
23942 argc, (const char **) argv,
23946 /* Just complain. */
23947 dwarf2_macro_malformed_definition_complaint (body);
23950 /* Just complain. */
23951 dwarf2_macro_malformed_definition_complaint (body);
23957 for (i = 0; i < argc; i++)
23963 dwarf2_macro_malformed_definition_complaint (body);
23966 /* Skip some bytes from BYTES according to the form given in FORM.
23967 Returns the new pointer. */
23969 static const gdb_byte *
23970 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
23971 enum dwarf_form form,
23972 unsigned int offset_size,
23973 struct dwarf2_section_info *section)
23975 unsigned int bytes_read;
23979 case DW_FORM_data1:
23984 case DW_FORM_data2:
23988 case DW_FORM_data4:
23992 case DW_FORM_data8:
23996 case DW_FORM_data16:
24000 case DW_FORM_string:
24001 read_direct_string (abfd, bytes, &bytes_read);
24002 bytes += bytes_read;
24005 case DW_FORM_sec_offset:
24007 case DW_FORM_GNU_strp_alt:
24008 bytes += offset_size;
24011 case DW_FORM_block:
24012 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24013 bytes += bytes_read;
24016 case DW_FORM_block1:
24017 bytes += 1 + read_1_byte (abfd, bytes);
24019 case DW_FORM_block2:
24020 bytes += 2 + read_2_bytes (abfd, bytes);
24022 case DW_FORM_block4:
24023 bytes += 4 + read_4_bytes (abfd, bytes);
24026 case DW_FORM_sdata:
24027 case DW_FORM_udata:
24028 case DW_FORM_GNU_addr_index:
24029 case DW_FORM_GNU_str_index:
24030 bytes = gdb_skip_leb128 (bytes, buffer_end);
24033 dwarf2_section_buffer_overflow_complaint (section);
24038 case DW_FORM_implicit_const:
24043 complaint (&symfile_complaints,
24044 _("invalid form 0x%x in `%s'"),
24045 form, get_section_name (section));
24053 /* A helper for dwarf_decode_macros that handles skipping an unknown
24054 opcode. Returns an updated pointer to the macro data buffer; or,
24055 on error, issues a complaint and returns NULL. */
24057 static const gdb_byte *
24058 skip_unknown_opcode (unsigned int opcode,
24059 const gdb_byte **opcode_definitions,
24060 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24062 unsigned int offset_size,
24063 struct dwarf2_section_info *section)
24065 unsigned int bytes_read, i;
24067 const gdb_byte *defn;
24069 if (opcode_definitions[opcode] == NULL)
24071 complaint (&symfile_complaints,
24072 _("unrecognized DW_MACFINO opcode 0x%x"),
24077 defn = opcode_definitions[opcode];
24078 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24079 defn += bytes_read;
24081 for (i = 0; i < arg; ++i)
24083 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24084 (enum dwarf_form) defn[i], offset_size,
24086 if (mac_ptr == NULL)
24088 /* skip_form_bytes already issued the complaint. */
24096 /* A helper function which parses the header of a macro section.
24097 If the macro section is the extended (for now called "GNU") type,
24098 then this updates *OFFSET_SIZE. Returns a pointer to just after
24099 the header, or issues a complaint and returns NULL on error. */
24101 static const gdb_byte *
24102 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24104 const gdb_byte *mac_ptr,
24105 unsigned int *offset_size,
24106 int section_is_gnu)
24108 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24110 if (section_is_gnu)
24112 unsigned int version, flags;
24114 version = read_2_bytes (abfd, mac_ptr);
24115 if (version != 4 && version != 5)
24117 complaint (&symfile_complaints,
24118 _("unrecognized version `%d' in .debug_macro section"),
24124 flags = read_1_byte (abfd, mac_ptr);
24126 *offset_size = (flags & 1) ? 8 : 4;
24128 if ((flags & 2) != 0)
24129 /* We don't need the line table offset. */
24130 mac_ptr += *offset_size;
24132 /* Vendor opcode descriptions. */
24133 if ((flags & 4) != 0)
24135 unsigned int i, count;
24137 count = read_1_byte (abfd, mac_ptr);
24139 for (i = 0; i < count; ++i)
24141 unsigned int opcode, bytes_read;
24144 opcode = read_1_byte (abfd, mac_ptr);
24146 opcode_definitions[opcode] = mac_ptr;
24147 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24148 mac_ptr += bytes_read;
24157 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24158 including DW_MACRO_import. */
24161 dwarf_decode_macro_bytes (bfd *abfd,
24162 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24163 struct macro_source_file *current_file,
24164 struct line_header *lh,
24165 struct dwarf2_section_info *section,
24166 int section_is_gnu, int section_is_dwz,
24167 unsigned int offset_size,
24168 htab_t include_hash)
24170 struct objfile *objfile = dwarf2_per_objfile->objfile;
24171 enum dwarf_macro_record_type macinfo_type;
24172 int at_commandline;
24173 const gdb_byte *opcode_definitions[256];
24175 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24176 &offset_size, section_is_gnu);
24177 if (mac_ptr == NULL)
24179 /* We already issued a complaint. */
24183 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24184 GDB is still reading the definitions from command line. First
24185 DW_MACINFO_start_file will need to be ignored as it was already executed
24186 to create CURRENT_FILE for the main source holding also the command line
24187 definitions. On first met DW_MACINFO_start_file this flag is reset to
24188 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24190 at_commandline = 1;
24194 /* Do we at least have room for a macinfo type byte? */
24195 if (mac_ptr >= mac_end)
24197 dwarf2_section_buffer_overflow_complaint (section);
24201 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24204 /* Note that we rely on the fact that the corresponding GNU and
24205 DWARF constants are the same. */
24207 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24208 switch (macinfo_type)
24210 /* A zero macinfo type indicates the end of the macro
24215 case DW_MACRO_define:
24216 case DW_MACRO_undef:
24217 case DW_MACRO_define_strp:
24218 case DW_MACRO_undef_strp:
24219 case DW_MACRO_define_sup:
24220 case DW_MACRO_undef_sup:
24222 unsigned int bytes_read;
24227 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24228 mac_ptr += bytes_read;
24230 if (macinfo_type == DW_MACRO_define
24231 || macinfo_type == DW_MACRO_undef)
24233 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24234 mac_ptr += bytes_read;
24238 LONGEST str_offset;
24240 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24241 mac_ptr += offset_size;
24243 if (macinfo_type == DW_MACRO_define_sup
24244 || macinfo_type == DW_MACRO_undef_sup
24247 struct dwz_file *dwz = dwarf2_get_dwz_file ();
24249 body = read_indirect_string_from_dwz (dwz, str_offset);
24252 body = read_indirect_string_at_offset (abfd, str_offset);
24255 is_define = (macinfo_type == DW_MACRO_define
24256 || macinfo_type == DW_MACRO_define_strp
24257 || macinfo_type == DW_MACRO_define_sup);
24258 if (! current_file)
24260 /* DWARF violation as no main source is present. */
24261 complaint (&symfile_complaints,
24262 _("debug info with no main source gives macro %s "
24264 is_define ? _("definition") : _("undefinition"),
24268 if ((line == 0 && !at_commandline)
24269 || (line != 0 && at_commandline))
24270 complaint (&symfile_complaints,
24271 _("debug info gives %s macro %s with %s line %d: %s"),
24272 at_commandline ? _("command-line") : _("in-file"),
24273 is_define ? _("definition") : _("undefinition"),
24274 line == 0 ? _("zero") : _("non-zero"), line, body);
24277 parse_macro_definition (current_file, line, body);
24280 gdb_assert (macinfo_type == DW_MACRO_undef
24281 || macinfo_type == DW_MACRO_undef_strp
24282 || macinfo_type == DW_MACRO_undef_sup);
24283 macro_undef (current_file, line, body);
24288 case DW_MACRO_start_file:
24290 unsigned int bytes_read;
24293 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24294 mac_ptr += bytes_read;
24295 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24296 mac_ptr += bytes_read;
24298 if ((line == 0 && !at_commandline)
24299 || (line != 0 && at_commandline))
24300 complaint (&symfile_complaints,
24301 _("debug info gives source %d included "
24302 "from %s at %s line %d"),
24303 file, at_commandline ? _("command-line") : _("file"),
24304 line == 0 ? _("zero") : _("non-zero"), line);
24306 if (at_commandline)
24308 /* This DW_MACRO_start_file was executed in the
24310 at_commandline = 0;
24313 current_file = macro_start_file (file, line, current_file, lh);
24317 case DW_MACRO_end_file:
24318 if (! current_file)
24319 complaint (&symfile_complaints,
24320 _("macro debug info has an unmatched "
24321 "`close_file' directive"));
24324 current_file = current_file->included_by;
24325 if (! current_file)
24327 enum dwarf_macro_record_type next_type;
24329 /* GCC circa March 2002 doesn't produce the zero
24330 type byte marking the end of the compilation
24331 unit. Complain if it's not there, but exit no
24334 /* Do we at least have room for a macinfo type byte? */
24335 if (mac_ptr >= mac_end)
24337 dwarf2_section_buffer_overflow_complaint (section);
24341 /* We don't increment mac_ptr here, so this is just
24344 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24346 if (next_type != 0)
24347 complaint (&symfile_complaints,
24348 _("no terminating 0-type entry for "
24349 "macros in `.debug_macinfo' section"));
24356 case DW_MACRO_import:
24357 case DW_MACRO_import_sup:
24361 bfd *include_bfd = abfd;
24362 struct dwarf2_section_info *include_section = section;
24363 const gdb_byte *include_mac_end = mac_end;
24364 int is_dwz = section_is_dwz;
24365 const gdb_byte *new_mac_ptr;
24367 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24368 mac_ptr += offset_size;
24370 if (macinfo_type == DW_MACRO_import_sup)
24372 struct dwz_file *dwz = dwarf2_get_dwz_file ();
24374 dwarf2_read_section (objfile, &dwz->macro);
24376 include_section = &dwz->macro;
24377 include_bfd = get_section_bfd_owner (include_section);
24378 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24382 new_mac_ptr = include_section->buffer + offset;
24383 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24387 /* This has actually happened; see
24388 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24389 complaint (&symfile_complaints,
24390 _("recursive DW_MACRO_import in "
24391 ".debug_macro section"));
24395 *slot = (void *) new_mac_ptr;
24397 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
24398 include_mac_end, current_file, lh,
24399 section, section_is_gnu, is_dwz,
24400 offset_size, include_hash);
24402 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24407 case DW_MACINFO_vendor_ext:
24408 if (!section_is_gnu)
24410 unsigned int bytes_read;
24412 /* This reads the constant, but since we don't recognize
24413 any vendor extensions, we ignore it. */
24414 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24415 mac_ptr += bytes_read;
24416 read_direct_string (abfd, mac_ptr, &bytes_read);
24417 mac_ptr += bytes_read;
24419 /* We don't recognize any vendor extensions. */
24425 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24426 mac_ptr, mac_end, abfd, offset_size,
24428 if (mac_ptr == NULL)
24433 } while (macinfo_type != 0);
24437 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24438 int section_is_gnu)
24440 struct objfile *objfile = dwarf2_per_objfile->objfile;
24441 struct line_header *lh = cu->line_header;
24443 const gdb_byte *mac_ptr, *mac_end;
24444 struct macro_source_file *current_file = 0;
24445 enum dwarf_macro_record_type macinfo_type;
24446 unsigned int offset_size = cu->header.offset_size;
24447 const gdb_byte *opcode_definitions[256];
24449 struct dwarf2_section_info *section;
24450 const char *section_name;
24452 if (cu->dwo_unit != NULL)
24454 if (section_is_gnu)
24456 section = &cu->dwo_unit->dwo_file->sections.macro;
24457 section_name = ".debug_macro.dwo";
24461 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24462 section_name = ".debug_macinfo.dwo";
24467 if (section_is_gnu)
24469 section = &dwarf2_per_objfile->macro;
24470 section_name = ".debug_macro";
24474 section = &dwarf2_per_objfile->macinfo;
24475 section_name = ".debug_macinfo";
24479 dwarf2_read_section (objfile, section);
24480 if (section->buffer == NULL)
24482 complaint (&symfile_complaints, _("missing %s section"), section_name);
24485 abfd = get_section_bfd_owner (section);
24487 /* First pass: Find the name of the base filename.
24488 This filename is needed in order to process all macros whose definition
24489 (or undefinition) comes from the command line. These macros are defined
24490 before the first DW_MACINFO_start_file entry, and yet still need to be
24491 associated to the base file.
24493 To determine the base file name, we scan the macro definitions until we
24494 reach the first DW_MACINFO_start_file entry. We then initialize
24495 CURRENT_FILE accordingly so that any macro definition found before the
24496 first DW_MACINFO_start_file can still be associated to the base file. */
24498 mac_ptr = section->buffer + offset;
24499 mac_end = section->buffer + section->size;
24501 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24502 &offset_size, section_is_gnu);
24503 if (mac_ptr == NULL)
24505 /* We already issued a complaint. */
24511 /* Do we at least have room for a macinfo type byte? */
24512 if (mac_ptr >= mac_end)
24514 /* Complaint is printed during the second pass as GDB will probably
24515 stop the first pass earlier upon finding
24516 DW_MACINFO_start_file. */
24520 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24523 /* Note that we rely on the fact that the corresponding GNU and
24524 DWARF constants are the same. */
24526 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24527 switch (macinfo_type)
24529 /* A zero macinfo type indicates the end of the macro
24534 case DW_MACRO_define:
24535 case DW_MACRO_undef:
24536 /* Only skip the data by MAC_PTR. */
24538 unsigned int bytes_read;
24540 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24541 mac_ptr += bytes_read;
24542 read_direct_string (abfd, mac_ptr, &bytes_read);
24543 mac_ptr += bytes_read;
24547 case DW_MACRO_start_file:
24549 unsigned int bytes_read;
24552 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24553 mac_ptr += bytes_read;
24554 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24555 mac_ptr += bytes_read;
24557 current_file = macro_start_file (file, line, current_file, lh);
24561 case DW_MACRO_end_file:
24562 /* No data to skip by MAC_PTR. */
24565 case DW_MACRO_define_strp:
24566 case DW_MACRO_undef_strp:
24567 case DW_MACRO_define_sup:
24568 case DW_MACRO_undef_sup:
24570 unsigned int bytes_read;
24572 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24573 mac_ptr += bytes_read;
24574 mac_ptr += offset_size;
24578 case DW_MACRO_import:
24579 case DW_MACRO_import_sup:
24580 /* Note that, according to the spec, a transparent include
24581 chain cannot call DW_MACRO_start_file. So, we can just
24582 skip this opcode. */
24583 mac_ptr += offset_size;
24586 case DW_MACINFO_vendor_ext:
24587 /* Only skip the data by MAC_PTR. */
24588 if (!section_is_gnu)
24590 unsigned int bytes_read;
24592 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24593 mac_ptr += bytes_read;
24594 read_direct_string (abfd, mac_ptr, &bytes_read);
24595 mac_ptr += bytes_read;
24600 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24601 mac_ptr, mac_end, abfd, offset_size,
24603 if (mac_ptr == NULL)
24608 } while (macinfo_type != 0 && current_file == NULL);
24610 /* Second pass: Process all entries.
24612 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24613 command-line macro definitions/undefinitions. This flag is unset when we
24614 reach the first DW_MACINFO_start_file entry. */
24616 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24618 NULL, xcalloc, xfree));
24619 mac_ptr = section->buffer + offset;
24620 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24621 *slot = (void *) mac_ptr;
24622 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
24623 current_file, lh, section,
24624 section_is_gnu, 0, offset_size,
24625 include_hash.get ());
24628 /* Check if the attribute's form is a DW_FORM_block*
24629 if so return true else false. */
24632 attr_form_is_block (const struct attribute *attr)
24634 return (attr == NULL ? 0 :
24635 attr->form == DW_FORM_block1
24636 || attr->form == DW_FORM_block2
24637 || attr->form == DW_FORM_block4
24638 || attr->form == DW_FORM_block
24639 || attr->form == DW_FORM_exprloc);
24642 /* Return non-zero if ATTR's value is a section offset --- classes
24643 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24644 You may use DW_UNSND (attr) to retrieve such offsets.
24646 Section 7.5.4, "Attribute Encodings", explains that no attribute
24647 may have a value that belongs to more than one of these classes; it
24648 would be ambiguous if we did, because we use the same forms for all
24652 attr_form_is_section_offset (const struct attribute *attr)
24654 return (attr->form == DW_FORM_data4
24655 || attr->form == DW_FORM_data8
24656 || attr->form == DW_FORM_sec_offset);
24659 /* Return non-zero if ATTR's value falls in the 'constant' class, or
24660 zero otherwise. When this function returns true, you can apply
24661 dwarf2_get_attr_constant_value to it.
24663 However, note that for some attributes you must check
24664 attr_form_is_section_offset before using this test. DW_FORM_data4
24665 and DW_FORM_data8 are members of both the constant class, and of
24666 the classes that contain offsets into other debug sections
24667 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24668 that, if an attribute's can be either a constant or one of the
24669 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
24670 taken as section offsets, not constants.
24672 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24673 cannot handle that. */
24676 attr_form_is_constant (const struct attribute *attr)
24678 switch (attr->form)
24680 case DW_FORM_sdata:
24681 case DW_FORM_udata:
24682 case DW_FORM_data1:
24683 case DW_FORM_data2:
24684 case DW_FORM_data4:
24685 case DW_FORM_data8:
24686 case DW_FORM_implicit_const:
24694 /* DW_ADDR is always stored already as sect_offset; despite for the forms
24695 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24698 attr_form_is_ref (const struct attribute *attr)
24700 switch (attr->form)
24702 case DW_FORM_ref_addr:
24707 case DW_FORM_ref_udata:
24708 case DW_FORM_GNU_ref_alt:
24715 /* Return the .debug_loc section to use for CU.
24716 For DWO files use .debug_loc.dwo. */
24718 static struct dwarf2_section_info *
24719 cu_debug_loc_section (struct dwarf2_cu *cu)
24723 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24725 return cu->header.version >= 5 ? §ions->loclists : §ions->loc;
24727 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24728 : &dwarf2_per_objfile->loc);
24731 /* A helper function that fills in a dwarf2_loclist_baton. */
24734 fill_in_loclist_baton (struct dwarf2_cu *cu,
24735 struct dwarf2_loclist_baton *baton,
24736 const struct attribute *attr)
24738 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24740 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
24742 baton->per_cu = cu->per_cu;
24743 gdb_assert (baton->per_cu);
24744 /* We don't know how long the location list is, but make sure we
24745 don't run off the edge of the section. */
24746 baton->size = section->size - DW_UNSND (attr);
24747 baton->data = section->buffer + DW_UNSND (attr);
24748 baton->base_address = cu->base_address;
24749 baton->from_dwo = cu->dwo_unit != NULL;
24753 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24754 struct dwarf2_cu *cu, int is_block)
24756 struct objfile *objfile = dwarf2_per_objfile->objfile;
24757 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24759 if (attr_form_is_section_offset (attr)
24760 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24761 the section. If so, fall through to the complaint in the
24763 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
24765 struct dwarf2_loclist_baton *baton;
24767 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24769 fill_in_loclist_baton (cu, baton, attr);
24771 if (cu->base_known == 0)
24772 complaint (&symfile_complaints,
24773 _("Location list used without "
24774 "specifying the CU base address."));
24776 SYMBOL_ACLASS_INDEX (sym) = (is_block
24777 ? dwarf2_loclist_block_index
24778 : dwarf2_loclist_index);
24779 SYMBOL_LOCATION_BATON (sym) = baton;
24783 struct dwarf2_locexpr_baton *baton;
24785 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24786 baton->per_cu = cu->per_cu;
24787 gdb_assert (baton->per_cu);
24789 if (attr_form_is_block (attr))
24791 /* Note that we're just copying the block's data pointer
24792 here, not the actual data. We're still pointing into the
24793 info_buffer for SYM's objfile; right now we never release
24794 that buffer, but when we do clean up properly this may
24796 baton->size = DW_BLOCK (attr)->size;
24797 baton->data = DW_BLOCK (attr)->data;
24801 dwarf2_invalid_attrib_class_complaint ("location description",
24802 SYMBOL_NATURAL_NAME (sym));
24806 SYMBOL_ACLASS_INDEX (sym) = (is_block
24807 ? dwarf2_locexpr_block_index
24808 : dwarf2_locexpr_index);
24809 SYMBOL_LOCATION_BATON (sym) = baton;
24813 /* Return the OBJFILE associated with the compilation unit CU. If CU
24814 came from a separate debuginfo file, then the master objfile is
24818 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24820 struct objfile *objfile = per_cu->objfile;
24822 /* Return the master objfile, so that we can report and look up the
24823 correct file containing this variable. */
24824 if (objfile->separate_debug_objfile_backlink)
24825 objfile = objfile->separate_debug_objfile_backlink;
24830 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24831 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24832 CU_HEADERP first. */
24834 static const struct comp_unit_head *
24835 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24836 struct dwarf2_per_cu_data *per_cu)
24838 const gdb_byte *info_ptr;
24841 return &per_cu->cu->header;
24843 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24845 memset (cu_headerp, 0, sizeof (*cu_headerp));
24846 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24847 rcuh_kind::COMPILE);
24852 /* Return the address size given in the compilation unit header for CU. */
24855 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24857 struct comp_unit_head cu_header_local;
24858 const struct comp_unit_head *cu_headerp;
24860 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24862 return cu_headerp->addr_size;
24865 /* Return the offset size given in the compilation unit header for CU. */
24868 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24870 struct comp_unit_head cu_header_local;
24871 const struct comp_unit_head *cu_headerp;
24873 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24875 return cu_headerp->offset_size;
24878 /* See its dwarf2loc.h declaration. */
24881 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24883 struct comp_unit_head cu_header_local;
24884 const struct comp_unit_head *cu_headerp;
24886 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24888 if (cu_headerp->version == 2)
24889 return cu_headerp->addr_size;
24891 return cu_headerp->offset_size;
24894 /* Return the text offset of the CU. The returned offset comes from
24895 this CU's objfile. If this objfile came from a separate debuginfo
24896 file, then the offset may be different from the corresponding
24897 offset in the parent objfile. */
24900 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24902 struct objfile *objfile = per_cu->objfile;
24904 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24907 /* Return DWARF version number of PER_CU. */
24910 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24912 return per_cu->dwarf_version;
24915 /* Locate the .debug_info compilation unit from CU's objfile which contains
24916 the DIE at OFFSET. Raises an error on failure. */
24918 static struct dwarf2_per_cu_data *
24919 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24920 unsigned int offset_in_dwz,
24921 struct objfile *objfile)
24923 struct dwarf2_per_cu_data *this_cu;
24925 const sect_offset *cu_off;
24928 high = dwarf2_per_objfile->n_comp_units - 1;
24931 struct dwarf2_per_cu_data *mid_cu;
24932 int mid = low + (high - low) / 2;
24934 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
24935 cu_off = &mid_cu->sect_off;
24936 if (mid_cu->is_dwz > offset_in_dwz
24937 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
24942 gdb_assert (low == high);
24943 this_cu = dwarf2_per_objfile->all_comp_units[low];
24944 cu_off = &this_cu->sect_off;
24945 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
24947 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24948 error (_("Dwarf Error: could not find partial DIE containing "
24949 "offset 0x%x [in module %s]"),
24950 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
24952 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24954 return dwarf2_per_objfile->all_comp_units[low-1];
24958 this_cu = dwarf2_per_objfile->all_comp_units[low];
24959 if (low == dwarf2_per_objfile->n_comp_units - 1
24960 && sect_off >= this_cu->sect_off + this_cu->length)
24961 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
24962 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24967 /* Initialize dwarf2_cu CU, owned by PER_CU. */
24970 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
24972 memset (cu, 0, sizeof (*cu));
24974 cu->per_cu = per_cu;
24975 cu->objfile = per_cu->objfile;
24976 obstack_init (&cu->comp_unit_obstack);
24979 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24982 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24983 enum language pretend_language)
24985 struct attribute *attr;
24987 /* Set the language we're debugging. */
24988 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24990 set_cu_language (DW_UNSND (attr), cu);
24993 cu->language = pretend_language;
24994 cu->language_defn = language_def (cu->language);
24997 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25000 /* Release one cached compilation unit, CU. We unlink it from the tree
25001 of compilation units, but we don't remove it from the read_in_chain;
25002 the caller is responsible for that.
25003 NOTE: DATA is a void * because this function is also used as a
25004 cleanup routine. */
25007 free_heap_comp_unit (void *data)
25009 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
25011 gdb_assert (cu->per_cu != NULL);
25012 cu->per_cu->cu = NULL;
25015 obstack_free (&cu->comp_unit_obstack, NULL);
25020 /* This cleanup function is passed the address of a dwarf2_cu on the stack
25021 when we're finished with it. We can't free the pointer itself, but be
25022 sure to unlink it from the cache. Also release any associated storage. */
25025 free_stack_comp_unit (void *data)
25027 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
25029 gdb_assert (cu->per_cu != NULL);
25030 cu->per_cu->cu = NULL;
25033 obstack_free (&cu->comp_unit_obstack, NULL);
25034 cu->partial_dies = NULL;
25037 /* Free all cached compilation units. */
25040 free_cached_comp_units (void *data)
25042 dwarf2_per_objfile->free_cached_comp_units ();
25045 /* Increase the age counter on each cached compilation unit, and free
25046 any that are too old. */
25049 age_cached_comp_units (void)
25051 struct dwarf2_per_cu_data *per_cu, **last_chain;
25053 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25054 per_cu = dwarf2_per_objfile->read_in_chain;
25055 while (per_cu != NULL)
25057 per_cu->cu->last_used ++;
25058 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25059 dwarf2_mark (per_cu->cu);
25060 per_cu = per_cu->cu->read_in_chain;
25063 per_cu = dwarf2_per_objfile->read_in_chain;
25064 last_chain = &dwarf2_per_objfile->read_in_chain;
25065 while (per_cu != NULL)
25067 struct dwarf2_per_cu_data *next_cu;
25069 next_cu = per_cu->cu->read_in_chain;
25071 if (!per_cu->cu->mark)
25073 free_heap_comp_unit (per_cu->cu);
25074 *last_chain = next_cu;
25077 last_chain = &per_cu->cu->read_in_chain;
25083 /* Remove a single compilation unit from the cache. */
25086 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25088 struct dwarf2_per_cu_data *per_cu, **last_chain;
25090 per_cu = dwarf2_per_objfile->read_in_chain;
25091 last_chain = &dwarf2_per_objfile->read_in_chain;
25092 while (per_cu != NULL)
25094 struct dwarf2_per_cu_data *next_cu;
25096 next_cu = per_cu->cu->read_in_chain;
25098 if (per_cu == target_per_cu)
25100 free_heap_comp_unit (per_cu->cu);
25102 *last_chain = next_cu;
25106 last_chain = &per_cu->cu->read_in_chain;
25112 /* Release all extra memory associated with OBJFILE. */
25115 dwarf2_free_objfile (struct objfile *objfile)
25118 = (struct dwarf2_per_objfile *) objfile_data (objfile,
25119 dwarf2_objfile_data_key);
25121 if (dwarf2_per_objfile == NULL)
25124 dwarf2_per_objfile->~dwarf2_per_objfile ();
25127 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25128 We store these in a hash table separate from the DIEs, and preserve them
25129 when the DIEs are flushed out of cache.
25131 The CU "per_cu" pointer is needed because offset alone is not enough to
25132 uniquely identify the type. A file may have multiple .debug_types sections,
25133 or the type may come from a DWO file. Furthermore, while it's more logical
25134 to use per_cu->section+offset, with Fission the section with the data is in
25135 the DWO file but we don't know that section at the point we need it.
25136 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25137 because we can enter the lookup routine, get_die_type_at_offset, from
25138 outside this file, and thus won't necessarily have PER_CU->cu.
25139 Fortunately, PER_CU is stable for the life of the objfile. */
25141 struct dwarf2_per_cu_offset_and_type
25143 const struct dwarf2_per_cu_data *per_cu;
25144 sect_offset sect_off;
25148 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25151 per_cu_offset_and_type_hash (const void *item)
25153 const struct dwarf2_per_cu_offset_and_type *ofs
25154 = (const struct dwarf2_per_cu_offset_and_type *) item;
25156 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25159 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25162 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25164 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25165 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25166 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25167 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25169 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25170 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25173 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25174 table if necessary. For convenience, return TYPE.
25176 The DIEs reading must have careful ordering to:
25177 * Not cause infite loops trying to read in DIEs as a prerequisite for
25178 reading current DIE.
25179 * Not trying to dereference contents of still incompletely read in types
25180 while reading in other DIEs.
25181 * Enable referencing still incompletely read in types just by a pointer to
25182 the type without accessing its fields.
25184 Therefore caller should follow these rules:
25185 * Try to fetch any prerequisite types we may need to build this DIE type
25186 before building the type and calling set_die_type.
25187 * After building type call set_die_type for current DIE as soon as
25188 possible before fetching more types to complete the current type.
25189 * Make the type as complete as possible before fetching more types. */
25191 static struct type *
25192 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25194 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25195 struct objfile *objfile = cu->objfile;
25196 struct attribute *attr;
25197 struct dynamic_prop prop;
25199 /* For Ada types, make sure that the gnat-specific data is always
25200 initialized (if not already set). There are a few types where
25201 we should not be doing so, because the type-specific area is
25202 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25203 where the type-specific area is used to store the floatformat).
25204 But this is not a problem, because the gnat-specific information
25205 is actually not needed for these types. */
25206 if (need_gnat_info (cu)
25207 && TYPE_CODE (type) != TYPE_CODE_FUNC
25208 && TYPE_CODE (type) != TYPE_CODE_FLT
25209 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25210 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25211 && TYPE_CODE (type) != TYPE_CODE_METHOD
25212 && !HAVE_GNAT_AUX_INFO (type))
25213 INIT_GNAT_SPECIFIC (type);
25215 /* Read DW_AT_allocated and set in type. */
25216 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25217 if (attr_form_is_block (attr))
25219 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25220 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
25222 else if (attr != NULL)
25224 complaint (&symfile_complaints,
25225 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
25226 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25227 to_underlying (die->sect_off));
25230 /* Read DW_AT_associated and set in type. */
25231 attr = dwarf2_attr (die, DW_AT_associated, cu);
25232 if (attr_form_is_block (attr))
25234 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25235 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
25237 else if (attr != NULL)
25239 complaint (&symfile_complaints,
25240 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
25241 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25242 to_underlying (die->sect_off));
25245 /* Read DW_AT_data_location and set in type. */
25246 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25247 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25248 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
25250 if (dwarf2_per_objfile->die_type_hash == NULL)
25252 dwarf2_per_objfile->die_type_hash =
25253 htab_create_alloc_ex (127,
25254 per_cu_offset_and_type_hash,
25255 per_cu_offset_and_type_eq,
25257 &objfile->objfile_obstack,
25258 hashtab_obstack_allocate,
25259 dummy_obstack_deallocate);
25262 ofs.per_cu = cu->per_cu;
25263 ofs.sect_off = die->sect_off;
25265 slot = (struct dwarf2_per_cu_offset_and_type **)
25266 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25268 complaint (&symfile_complaints,
25269 _("A problem internal to GDB: DIE 0x%x has type already set"),
25270 to_underlying (die->sect_off));
25271 *slot = XOBNEW (&objfile->objfile_obstack,
25272 struct dwarf2_per_cu_offset_and_type);
25277 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25278 or return NULL if the die does not have a saved type. */
25280 static struct type *
25281 get_die_type_at_offset (sect_offset sect_off,
25282 struct dwarf2_per_cu_data *per_cu)
25284 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25286 if (dwarf2_per_objfile->die_type_hash == NULL)
25289 ofs.per_cu = per_cu;
25290 ofs.sect_off = sect_off;
25291 slot = ((struct dwarf2_per_cu_offset_and_type *)
25292 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25299 /* Look up the type for DIE in CU in die_type_hash,
25300 or return NULL if DIE does not have a saved type. */
25302 static struct type *
25303 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25305 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25308 /* Add a dependence relationship from CU to REF_PER_CU. */
25311 dwarf2_add_dependence (struct dwarf2_cu *cu,
25312 struct dwarf2_per_cu_data *ref_per_cu)
25316 if (cu->dependencies == NULL)
25318 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25319 NULL, &cu->comp_unit_obstack,
25320 hashtab_obstack_allocate,
25321 dummy_obstack_deallocate);
25323 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25325 *slot = ref_per_cu;
25328 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25329 Set the mark field in every compilation unit in the
25330 cache that we must keep because we are keeping CU. */
25333 dwarf2_mark_helper (void **slot, void *data)
25335 struct dwarf2_per_cu_data *per_cu;
25337 per_cu = (struct dwarf2_per_cu_data *) *slot;
25339 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25340 reading of the chain. As such dependencies remain valid it is not much
25341 useful to track and undo them during QUIT cleanups. */
25342 if (per_cu->cu == NULL)
25345 if (per_cu->cu->mark)
25347 per_cu->cu->mark = 1;
25349 if (per_cu->cu->dependencies != NULL)
25350 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25355 /* Set the mark field in CU and in every other compilation unit in the
25356 cache that we must keep because we are keeping CU. */
25359 dwarf2_mark (struct dwarf2_cu *cu)
25364 if (cu->dependencies != NULL)
25365 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25369 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25373 per_cu->cu->mark = 0;
25374 per_cu = per_cu->cu->read_in_chain;
25378 /* Trivial hash function for partial_die_info: the hash value of a DIE
25379 is its offset in .debug_info for this objfile. */
25382 partial_die_hash (const void *item)
25384 const struct partial_die_info *part_die
25385 = (const struct partial_die_info *) item;
25387 return to_underlying (part_die->sect_off);
25390 /* Trivial comparison function for partial_die_info structures: two DIEs
25391 are equal if they have the same offset. */
25394 partial_die_eq (const void *item_lhs, const void *item_rhs)
25396 const struct partial_die_info *part_die_lhs
25397 = (const struct partial_die_info *) item_lhs;
25398 const struct partial_die_info *part_die_rhs
25399 = (const struct partial_die_info *) item_rhs;
25401 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25404 static struct cmd_list_element *set_dwarf_cmdlist;
25405 static struct cmd_list_element *show_dwarf_cmdlist;
25408 set_dwarf_cmd (const char *args, int from_tty)
25410 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25415 show_dwarf_cmd (const char *args, int from_tty)
25417 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25420 /* Free data associated with OBJFILE, if necessary. */
25423 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
25425 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
25428 /* Make sure we don't accidentally use dwarf2_per_objfile while
25430 dwarf2_per_objfile = NULL;
25432 for (ix = 0; ix < data->n_comp_units; ++ix)
25433 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
25435 for (ix = 0; ix < data->n_type_units; ++ix)
25436 VEC_free (dwarf2_per_cu_ptr,
25437 data->all_type_units[ix]->per_cu.imported_symtabs);
25438 xfree (data->all_type_units);
25440 VEC_free (dwarf2_section_info_def, data->types);
25442 if (data->dwo_files)
25443 free_dwo_files (data->dwo_files, objfile);
25444 if (data->dwp_file)
25445 gdb_bfd_unref (data->dwp_file->dbfd);
25447 if (data->dwz_file && data->dwz_file->dwz_bfd)
25448 gdb_bfd_unref (data->dwz_file->dwz_bfd);
25450 if (data->index_table != NULL)
25451 data->index_table->~mapped_index ();
25455 /* The "save gdb-index" command. */
25457 /* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
25461 file_write (FILE *file, const void *data, size_t size)
25463 if (fwrite (data, 1, size, file) != size)
25464 error (_("couldn't data write to file"));
25467 /* Write the contents of VEC to FILE, with error checking. */
25469 template<typename Elem, typename Alloc>
25471 file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
25473 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
25476 /* In-memory buffer to prepare data to be written later to a file. */
25480 /* Copy DATA to the end of the buffer. */
25481 template<typename T>
25482 void append_data (const T &data)
25484 std::copy (reinterpret_cast<const gdb_byte *> (&data),
25485 reinterpret_cast<const gdb_byte *> (&data + 1),
25486 grow (sizeof (data)));
25489 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
25490 terminating zero is appended too. */
25491 void append_cstr0 (const char *cstr)
25493 const size_t size = strlen (cstr) + 1;
25494 std::copy (cstr, cstr + size, grow (size));
25497 /* Store INPUT as ULEB128 to the end of buffer. */
25498 void append_unsigned_leb128 (ULONGEST input)
25502 gdb_byte output = input & 0x7f;
25506 append_data (output);
25512 /* Accept a host-format integer in VAL and append it to the buffer
25513 as a target-format integer which is LEN bytes long. */
25514 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
25516 ::store_unsigned_integer (grow (len), len, byte_order, val);
25519 /* Return the size of the buffer. */
25520 size_t size () const
25522 return m_vec.size ();
25525 /* Return true iff the buffer is empty. */
25526 bool empty () const
25528 return m_vec.empty ();
25531 /* Write the buffer to FILE. */
25532 void file_write (FILE *file) const
25534 ::file_write (file, m_vec);
25538 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
25539 the start of the new block. */
25540 gdb_byte *grow (size_t size)
25542 m_vec.resize (m_vec.size () + size);
25543 return &*m_vec.end () - size;
25546 gdb::byte_vector m_vec;
25549 /* An entry in the symbol table. */
25550 struct symtab_index_entry
25552 /* The name of the symbol. */
25554 /* The offset of the name in the constant pool. */
25555 offset_type index_offset;
25556 /* A sorted vector of the indices of all the CUs that hold an object
25558 std::vector<offset_type> cu_indices;
25561 /* The symbol table. This is a power-of-2-sized hash table. */
25562 struct mapped_symtab
25566 data.resize (1024);
25569 offset_type n_elements = 0;
25570 std::vector<symtab_index_entry> data;
25573 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
25576 Function is used only during write_hash_table so no index format backward
25577 compatibility is needed. */
25579 static symtab_index_entry &
25580 find_slot (struct mapped_symtab *symtab, const char *name)
25582 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
25584 index = hash & (symtab->data.size () - 1);
25585 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
25589 if (symtab->data[index].name == NULL
25590 || strcmp (name, symtab->data[index].name) == 0)
25591 return symtab->data[index];
25592 index = (index + step) & (symtab->data.size () - 1);
25596 /* Expand SYMTAB's hash table. */
25599 hash_expand (struct mapped_symtab *symtab)
25601 auto old_entries = std::move (symtab->data);
25603 symtab->data.clear ();
25604 symtab->data.resize (old_entries.size () * 2);
25606 for (auto &it : old_entries)
25607 if (it.name != NULL)
25609 auto &ref = find_slot (symtab, it.name);
25610 ref = std::move (it);
25614 /* Add an entry to SYMTAB. NAME is the name of the symbol.
25615 CU_INDEX is the index of the CU in which the symbol appears.
25616 IS_STATIC is one if the symbol is static, otherwise zero (global). */
25619 add_index_entry (struct mapped_symtab *symtab, const char *name,
25620 int is_static, gdb_index_symbol_kind kind,
25621 offset_type cu_index)
25623 offset_type cu_index_and_attrs;
25625 ++symtab->n_elements;
25626 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
25627 hash_expand (symtab);
25629 symtab_index_entry &slot = find_slot (symtab, name);
25630 if (slot.name == NULL)
25633 /* index_offset is set later. */
25636 cu_index_and_attrs = 0;
25637 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
25638 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
25639 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
25641 /* We don't want to record an index value twice as we want to avoid the
25643 We process all global symbols and then all static symbols
25644 (which would allow us to avoid the duplication by only having to check
25645 the last entry pushed), but a symbol could have multiple kinds in one CU.
25646 To keep things simple we don't worry about the duplication here and
25647 sort and uniqufy the list after we've processed all symbols. */
25648 slot.cu_indices.push_back (cu_index_and_attrs);
25651 /* Sort and remove duplicates of all symbols' cu_indices lists. */
25654 uniquify_cu_indices (struct mapped_symtab *symtab)
25656 for (auto &entry : symtab->data)
25658 if (entry.name != NULL && !entry.cu_indices.empty ())
25660 auto &cu_indices = entry.cu_indices;
25661 std::sort (cu_indices.begin (), cu_indices.end ());
25662 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
25663 cu_indices.erase (from, cu_indices.end ());
25668 /* A form of 'const char *' suitable for container keys. Only the
25669 pointer is stored. The strings themselves are compared, not the
25674 c_str_view (const char *cstr)
25678 bool operator== (const c_str_view &other) const
25680 return strcmp (m_cstr, other.m_cstr) == 0;
25683 /* Return the underlying C string. Note, the returned string is
25684 only a reference with lifetime of this object. */
25685 const char *c_str () const
25691 friend class c_str_view_hasher;
25692 const char *const m_cstr;
25695 /* A std::unordered_map::hasher for c_str_view that uses the right
25696 hash function for strings in a mapped index. */
25697 class c_str_view_hasher
25700 size_t operator () (const c_str_view &x) const
25702 return mapped_index_string_hash (INT_MAX, x.m_cstr);
25706 /* A std::unordered_map::hasher for std::vector<>. */
25707 template<typename T>
25708 class vector_hasher
25711 size_t operator () (const std::vector<T> &key) const
25713 return iterative_hash (key.data (),
25714 sizeof (key.front ()) * key.size (), 0);
25718 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
25719 constant pool entries going into the data buffer CPOOL. */
25722 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
25725 /* Elements are sorted vectors of the indices of all the CUs that
25726 hold an object of this name. */
25727 std::unordered_map<std::vector<offset_type>, offset_type,
25728 vector_hasher<offset_type>>
25731 /* We add all the index vectors to the constant pool first, to
25732 ensure alignment is ok. */
25733 for (symtab_index_entry &entry : symtab->data)
25735 if (entry.name == NULL)
25737 gdb_assert (entry.index_offset == 0);
25739 /* Finding before inserting is faster than always trying to
25740 insert, because inserting always allocates a node, does the
25741 lookup, and then destroys the new node if another node
25742 already had the same key. C++17 try_emplace will avoid
25745 = symbol_hash_table.find (entry.cu_indices);
25746 if (found != symbol_hash_table.end ())
25748 entry.index_offset = found->second;
25752 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
25753 entry.index_offset = cpool.size ();
25754 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
25755 for (const auto index : entry.cu_indices)
25756 cpool.append_data (MAYBE_SWAP (index));
25760 /* Now write out the hash table. */
25761 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
25762 for (const auto &entry : symtab->data)
25764 offset_type str_off, vec_off;
25766 if (entry.name != NULL)
25768 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
25769 if (insertpair.second)
25770 cpool.append_cstr0 (entry.name);
25771 str_off = insertpair.first->second;
25772 vec_off = entry.index_offset;
25776 /* While 0 is a valid constant pool index, it is not valid
25777 to have 0 for both offsets. */
25782 output.append_data (MAYBE_SWAP (str_off));
25783 output.append_data (MAYBE_SWAP (vec_off));
25787 typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
25789 /* Helper struct for building the address table. */
25790 struct addrmap_index_data
25792 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
25793 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
25796 struct objfile *objfile;
25797 data_buf &addr_vec;
25798 psym_index_map &cu_index_htab;
25800 /* Non-zero if the previous_* fields are valid.
25801 We can't write an entry until we see the next entry (since it is only then
25802 that we know the end of the entry). */
25803 int previous_valid;
25804 /* Index of the CU in the table of all CUs in the index file. */
25805 unsigned int previous_cu_index;
25806 /* Start address of the CU. */
25807 CORE_ADDR previous_cu_start;
25810 /* Write an address entry to ADDR_VEC. */
25813 add_address_entry (struct objfile *objfile, data_buf &addr_vec,
25814 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
25816 CORE_ADDR baseaddr;
25818 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25820 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
25821 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
25822 addr_vec.append_data (MAYBE_SWAP (cu_index));
25825 /* Worker function for traversing an addrmap to build the address table. */
25828 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
25830 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
25831 struct partial_symtab *pst = (struct partial_symtab *) obj;
25833 if (data->previous_valid)
25834 add_address_entry (data->objfile, data->addr_vec,
25835 data->previous_cu_start, start_addr,
25836 data->previous_cu_index);
25838 data->previous_cu_start = start_addr;
25841 const auto it = data->cu_index_htab.find (pst);
25842 gdb_assert (it != data->cu_index_htab.cend ());
25843 data->previous_cu_index = it->second;
25844 data->previous_valid = 1;
25847 data->previous_valid = 0;
25852 /* Write OBJFILE's address map to ADDR_VEC.
25853 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
25854 in the index file. */
25857 write_address_map (struct objfile *objfile, data_buf &addr_vec,
25858 psym_index_map &cu_index_htab)
25860 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
25862 /* When writing the address table, we have to cope with the fact that
25863 the addrmap iterator only provides the start of a region; we have to
25864 wait until the next invocation to get the start of the next region. */
25866 addrmap_index_data.objfile = objfile;
25867 addrmap_index_data.previous_valid = 0;
25869 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
25870 &addrmap_index_data);
25872 /* It's highly unlikely the last entry (end address = 0xff...ff)
25873 is valid, but we should still handle it.
25874 The end address is recorded as the start of the next region, but that
25875 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
25877 if (addrmap_index_data.previous_valid)
25878 add_address_entry (objfile, addr_vec,
25879 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
25880 addrmap_index_data.previous_cu_index);
25883 /* Return the symbol kind of PSYM. */
25885 static gdb_index_symbol_kind
25886 symbol_kind (struct partial_symbol *psym)
25888 domain_enum domain = PSYMBOL_DOMAIN (psym);
25889 enum address_class aclass = PSYMBOL_CLASS (psym);
25897 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
25899 return GDB_INDEX_SYMBOL_KIND_TYPE;
25901 case LOC_CONST_BYTES:
25902 case LOC_OPTIMIZED_OUT:
25904 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25906 /* Note: It's currently impossible to recognize psyms as enum values
25907 short of reading the type info. For now punt. */
25908 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25910 /* There are other LOC_FOO values that one might want to classify
25911 as variables, but dwarf2read.c doesn't currently use them. */
25912 return GDB_INDEX_SYMBOL_KIND_OTHER;
25914 case STRUCT_DOMAIN:
25915 return GDB_INDEX_SYMBOL_KIND_TYPE;
25917 return GDB_INDEX_SYMBOL_KIND_OTHER;
25921 /* Add a list of partial symbols to SYMTAB. */
25924 write_psymbols (struct mapped_symtab *symtab,
25925 std::unordered_set<partial_symbol *> &psyms_seen,
25926 struct partial_symbol **psymp,
25928 offset_type cu_index,
25931 for (; count-- > 0; ++psymp)
25933 struct partial_symbol *psym = *psymp;
25935 if (SYMBOL_LANGUAGE (psym) == language_ada)
25936 error (_("Ada is not currently supported by the index"));
25938 /* Only add a given psymbol once. */
25939 if (psyms_seen.insert (psym).second)
25941 gdb_index_symbol_kind kind = symbol_kind (psym);
25943 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
25944 is_static, kind, cu_index);
25949 /* A helper struct used when iterating over debug_types. */
25950 struct signatured_type_index_data
25952 signatured_type_index_data (data_buf &types_list_,
25953 std::unordered_set<partial_symbol *> &psyms_seen_)
25954 : types_list (types_list_), psyms_seen (psyms_seen_)
25957 struct objfile *objfile;
25958 struct mapped_symtab *symtab;
25959 data_buf &types_list;
25960 std::unordered_set<partial_symbol *> &psyms_seen;
25964 /* A helper function that writes a single signatured_type to an
25968 write_one_signatured_type (void **slot, void *d)
25970 struct signatured_type_index_data *info
25971 = (struct signatured_type_index_data *) d;
25972 struct signatured_type *entry = (struct signatured_type *) *slot;
25973 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
25975 write_psymbols (info->symtab,
25977 &info->objfile->global_psymbols[psymtab->globals_offset],
25978 psymtab->n_global_syms, info->cu_index,
25980 write_psymbols (info->symtab,
25982 &info->objfile->static_psymbols[psymtab->statics_offset],
25983 psymtab->n_static_syms, info->cu_index,
25986 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
25987 to_underlying (entry->per_cu.sect_off));
25988 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
25989 to_underlying (entry->type_offset_in_tu));
25990 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
25997 /* Recurse into all "included" dependencies and count their symbols as
25998 if they appeared in this psymtab. */
26001 recursively_count_psymbols (struct partial_symtab *psymtab,
26002 size_t &psyms_seen)
26004 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26005 if (psymtab->dependencies[i]->user != NULL)
26006 recursively_count_psymbols (psymtab->dependencies[i],
26009 psyms_seen += psymtab->n_global_syms;
26010 psyms_seen += psymtab->n_static_syms;
26013 /* Recurse into all "included" dependencies and write their symbols as
26014 if they appeared in this psymtab. */
26017 recursively_write_psymbols (struct objfile *objfile,
26018 struct partial_symtab *psymtab,
26019 struct mapped_symtab *symtab,
26020 std::unordered_set<partial_symbol *> &psyms_seen,
26021 offset_type cu_index)
26025 for (i = 0; i < psymtab->number_of_dependencies; ++i)
26026 if (psymtab->dependencies[i]->user != NULL)
26027 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26028 symtab, psyms_seen, cu_index);
26030 write_psymbols (symtab,
26032 &objfile->global_psymbols[psymtab->globals_offset],
26033 psymtab->n_global_syms, cu_index,
26035 write_psymbols (symtab,
26037 &objfile->static_psymbols[psymtab->statics_offset],
26038 psymtab->n_static_syms, cu_index,
26042 /* DWARF-5 .debug_names builder. */
26046 debug_names (bool is_dwarf64, bfd_endian dwarf5_byte_order)
26047 : m_dwarf5_byte_order (dwarf5_byte_order),
26048 m_dwarf32 (dwarf5_byte_order),
26049 m_dwarf64 (dwarf5_byte_order),
26050 m_dwarf (is_dwarf64
26051 ? static_cast<dwarf &> (m_dwarf64)
26052 : static_cast<dwarf &> (m_dwarf32)),
26053 m_name_table_string_offs (m_dwarf.name_table_string_offs),
26054 m_name_table_entry_offs (m_dwarf.name_table_entry_offs)
26057 int dwarf5_offset_size () const
26059 const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64;
26060 return dwarf5_is_dwarf64 ? 8 : 4;
26063 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
26064 enum class unit_kind { cu, tu };
26066 /* Insert one symbol. */
26067 void insert (const partial_symbol *psym, int cu_index, bool is_static,
26070 const int dwarf_tag = psymbol_tag (psym);
26071 if (dwarf_tag == 0)
26073 const char *const name = SYMBOL_SEARCH_NAME (psym);
26074 const auto insertpair
26075 = m_name_to_value_set.emplace (c_str_view (name),
26076 std::set<symbol_value> ());
26077 std::set<symbol_value> &value_set = insertpair.first->second;
26078 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static, kind));
26081 /* Build all the tables. All symbols must be already inserted.
26082 This function does not call file_write, caller has to do it
26086 /* Verify the build method has not be called twice. */
26087 gdb_assert (m_abbrev_table.empty ());
26088 const size_t name_count = m_name_to_value_set.size ();
26089 m_bucket_table.resize
26090 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
26091 m_hash_table.reserve (name_count);
26092 m_name_table_string_offs.reserve (name_count);
26093 m_name_table_entry_offs.reserve (name_count);
26095 /* Map each hash of symbol to its name and value. */
26096 struct hash_it_pair
26099 decltype (m_name_to_value_set)::const_iterator it;
26101 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
26102 bucket_hash.resize (m_bucket_table.size ());
26103 for (decltype (m_name_to_value_set)::const_iterator it
26104 = m_name_to_value_set.cbegin ();
26105 it != m_name_to_value_set.cend ();
26108 const char *const name = it->first.c_str ();
26109 const uint32_t hash = dwarf5_djb_hash (name);
26110 hash_it_pair hashitpair;
26111 hashitpair.hash = hash;
26112 hashitpair.it = it;
26113 auto &slot = bucket_hash[hash % bucket_hash.size()];
26114 slot.push_front (std::move (hashitpair));
26116 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
26118 const std::forward_list<hash_it_pair> &hashitlist
26119 = bucket_hash[bucket_ix];
26120 if (hashitlist.empty ())
26122 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
26123 /* The hashes array is indexed starting at 1. */
26124 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
26125 sizeof (bucket_slot), m_dwarf5_byte_order,
26126 m_hash_table.size () + 1);
26127 for (const hash_it_pair &hashitpair : hashitlist)
26129 m_hash_table.push_back (0);
26130 store_unsigned_integer (reinterpret_cast<gdb_byte *>
26131 (&m_hash_table.back ()),
26132 sizeof (m_hash_table.back ()),
26133 m_dwarf5_byte_order, hashitpair.hash);
26134 const c_str_view &name = hashitpair.it->first;
26135 const std::set<symbol_value> &value_set = hashitpair.it->second;
26136 m_name_table_string_offs.push_back_reorder
26137 (m_debugstrlookup.lookup (name.c_str ()));
26138 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
26139 gdb_assert (!value_set.empty ());
26140 for (const symbol_value &value : value_set)
26142 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
26147 idx = m_idx_next++;
26148 m_abbrev_table.append_unsigned_leb128 (idx);
26149 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
26150 m_abbrev_table.append_unsigned_leb128
26151 (value.kind == unit_kind::cu ? DW_IDX_compile_unit
26152 : DW_IDX_type_unit);
26153 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
26154 m_abbrev_table.append_unsigned_leb128 (value.is_static
26155 ? DW_IDX_GNU_internal
26156 : DW_IDX_GNU_external);
26157 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
26159 /* Terminate attributes list. */
26160 m_abbrev_table.append_unsigned_leb128 (0);
26161 m_abbrev_table.append_unsigned_leb128 (0);
26164 m_entry_pool.append_unsigned_leb128 (idx);
26165 m_entry_pool.append_unsigned_leb128 (value.cu_index);
26168 /* Terminate the list of CUs. */
26169 m_entry_pool.append_unsigned_leb128 (0);
26172 gdb_assert (m_hash_table.size () == name_count);
26174 /* Terminate tags list. */
26175 m_abbrev_table.append_unsigned_leb128 (0);
26178 /* Return .debug_names bucket count. This must be called only after
26179 calling the build method. */
26180 uint32_t bucket_count () const
26182 /* Verify the build method has been already called. */
26183 gdb_assert (!m_abbrev_table.empty ());
26184 const uint32_t retval = m_bucket_table.size ();
26186 /* Check for overflow. */
26187 gdb_assert (retval == m_bucket_table.size ());
26191 /* Return .debug_names names count. This must be called only after
26192 calling the build method. */
26193 uint32_t name_count () const
26195 /* Verify the build method has been already called. */
26196 gdb_assert (!m_abbrev_table.empty ());
26197 const uint32_t retval = m_hash_table.size ();
26199 /* Check for overflow. */
26200 gdb_assert (retval == m_hash_table.size ());
26204 /* Return number of bytes of .debug_names abbreviation table. This
26205 must be called only after calling the build method. */
26206 uint32_t abbrev_table_bytes () const
26208 gdb_assert (!m_abbrev_table.empty ());
26209 return m_abbrev_table.size ();
26212 /* Recurse into all "included" dependencies and store their symbols
26213 as if they appeared in this psymtab. */
26214 void recursively_write_psymbols
26215 (struct objfile *objfile,
26216 struct partial_symtab *psymtab,
26217 std::unordered_set<partial_symbol *> &psyms_seen,
26220 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26221 if (psymtab->dependencies[i]->user != NULL)
26222 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26223 psyms_seen, cu_index);
26225 write_psymbols (psyms_seen,
26226 &objfile->global_psymbols[psymtab->globals_offset],
26227 psymtab->n_global_syms, cu_index, false, unit_kind::cu);
26228 write_psymbols (psyms_seen,
26229 &objfile->static_psymbols[psymtab->statics_offset],
26230 psymtab->n_static_syms, cu_index, true, unit_kind::cu);
26233 /* Return number of bytes the .debug_names section will have. This
26234 must be called only after calling the build method. */
26235 size_t bytes () const
26237 /* Verify the build method has been already called. */
26238 gdb_assert (!m_abbrev_table.empty ());
26239 size_t expected_bytes = 0;
26240 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
26241 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
26242 expected_bytes += m_name_table_string_offs.bytes ();
26243 expected_bytes += m_name_table_entry_offs.bytes ();
26244 expected_bytes += m_abbrev_table.size ();
26245 expected_bytes += m_entry_pool.size ();
26246 return expected_bytes;
26249 /* Write .debug_names to FILE_NAMES and .debug_str addition to
26250 FILE_STR. This must be called only after calling the build
26252 void file_write (FILE *file_names, FILE *file_str) const
26254 /* Verify the build method has been already called. */
26255 gdb_assert (!m_abbrev_table.empty ());
26256 ::file_write (file_names, m_bucket_table);
26257 ::file_write (file_names, m_hash_table);
26258 m_name_table_string_offs.file_write (file_names);
26259 m_name_table_entry_offs.file_write (file_names);
26260 m_abbrev_table.file_write (file_names);
26261 m_entry_pool.file_write (file_names);
26262 m_debugstrlookup.file_write (file_str);
26265 /* A helper user data for write_one_signatured_type. */
26266 class write_one_signatured_type_data
26269 write_one_signatured_type_data (debug_names &nametable_,
26270 signatured_type_index_data &&info_)
26271 : nametable (nametable_), info (std::move (info_))
26273 debug_names &nametable;
26274 struct signatured_type_index_data info;
26277 /* A helper function to pass write_one_signatured_type to
26278 htab_traverse_noresize. */
26280 write_one_signatured_type (void **slot, void *d)
26282 write_one_signatured_type_data *data = (write_one_signatured_type_data *) d;
26283 struct signatured_type_index_data *info = &data->info;
26284 struct signatured_type *entry = (struct signatured_type *) *slot;
26286 data->nametable.write_one_signatured_type (entry, info);
26293 /* Storage for symbol names mapping them to their .debug_str section
26295 class debug_str_lookup
26299 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
26300 All .debug_str section strings are automatically stored. */
26301 debug_str_lookup ()
26302 : m_abfd (dwarf2_per_objfile->objfile->obfd)
26304 dwarf2_read_section (dwarf2_per_objfile->objfile,
26305 &dwarf2_per_objfile->str);
26306 if (dwarf2_per_objfile->str.buffer == NULL)
26308 for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
26309 data < (dwarf2_per_objfile->str.buffer
26310 + dwarf2_per_objfile->str.size);)
26312 const char *const s = reinterpret_cast<const char *> (data);
26313 const auto insertpair
26314 = m_str_table.emplace (c_str_view (s),
26315 data - dwarf2_per_objfile->str.buffer);
26316 if (!insertpair.second)
26317 complaint (&symfile_complaints,
26318 _("Duplicate string \"%s\" in "
26319 ".debug_str section [in module %s]"),
26320 s, bfd_get_filename (m_abfd));
26321 data += strlen (s) + 1;
26325 /* Return offset of symbol name S in the .debug_str section. Add
26326 such symbol to the section's end if it does not exist there
26328 size_t lookup (const char *s)
26330 const auto it = m_str_table.find (c_str_view (s));
26331 if (it != m_str_table.end ())
26333 const size_t offset = (dwarf2_per_objfile->str.size
26334 + m_str_add_buf.size ());
26335 m_str_table.emplace (c_str_view (s), offset);
26336 m_str_add_buf.append_cstr0 (s);
26340 /* Append the end of the .debug_str section to FILE. */
26341 void file_write (FILE *file) const
26343 m_str_add_buf.file_write (file);
26347 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
26350 /* Data to add at the end of .debug_str for new needed symbol names. */
26351 data_buf m_str_add_buf;
26354 /* Container to map used DWARF tags to their .debug_names abbreviation
26359 index_key (int dwarf_tag_, bool is_static_, unit_kind kind_)
26360 : dwarf_tag (dwarf_tag_), is_static (is_static_), kind (kind_)
26365 operator== (const index_key &other) const
26367 return (dwarf_tag == other.dwarf_tag && is_static == other.is_static
26368 && kind == other.kind);
26371 const int dwarf_tag;
26372 const bool is_static;
26373 const unit_kind kind;
26376 /* Provide std::unordered_map::hasher for index_key. */
26377 class index_key_hasher
26381 operator () (const index_key &key) const
26383 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
26387 /* Parameters of one symbol entry. */
26391 const int dwarf_tag, cu_index;
26392 const bool is_static;
26393 const unit_kind kind;
26395 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_,
26397 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_),
26402 operator< (const symbol_value &other) const
26422 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
26427 const bfd_endian dwarf5_byte_order;
26429 explicit offset_vec (bfd_endian dwarf5_byte_order_)
26430 : dwarf5_byte_order (dwarf5_byte_order_)
26433 /* Call std::vector::reserve for NELEM elements. */
26434 virtual void reserve (size_t nelem) = 0;
26436 /* Call std::vector::push_back with store_unsigned_integer byte
26437 reordering for ELEM. */
26438 virtual void push_back_reorder (size_t elem) = 0;
26440 /* Return expected output size in bytes. */
26441 virtual size_t bytes () const = 0;
26443 /* Write name table to FILE. */
26444 virtual void file_write (FILE *file) const = 0;
26447 /* Template to unify DWARF-32 and DWARF-64 output. */
26448 template<typename OffsetSize>
26449 class offset_vec_tmpl : public offset_vec
26452 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
26453 : offset_vec (dwarf5_byte_order_)
26456 /* Implement offset_vec::reserve. */
26457 void reserve (size_t nelem) override
26459 m_vec.reserve (nelem);
26462 /* Implement offset_vec::push_back_reorder. */
26463 void push_back_reorder (size_t elem) override
26465 m_vec.push_back (elem);
26466 /* Check for overflow. */
26467 gdb_assert (m_vec.back () == elem);
26468 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
26469 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
26472 /* Implement offset_vec::bytes. */
26473 size_t bytes () const override
26475 return m_vec.size () * sizeof (m_vec[0]);
26478 /* Implement offset_vec::file_write. */
26479 void file_write (FILE *file) const override
26481 ::file_write (file, m_vec);
26485 std::vector<OffsetSize> m_vec;
26488 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
26489 respecting name table width. */
26493 offset_vec &name_table_string_offs, &name_table_entry_offs;
26495 dwarf (offset_vec &name_table_string_offs_,
26496 offset_vec &name_table_entry_offs_)
26497 : name_table_string_offs (name_table_string_offs_),
26498 name_table_entry_offs (name_table_entry_offs_)
26503 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
26504 respecting name table width. */
26505 template<typename OffsetSize>
26506 class dwarf_tmpl : public dwarf
26509 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
26510 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
26511 m_name_table_string_offs (dwarf5_byte_order_),
26512 m_name_table_entry_offs (dwarf5_byte_order_)
26516 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
26517 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
26520 /* Try to reconstruct original DWARF tag for given partial_symbol.
26521 This function is not DWARF-5 compliant but it is sufficient for
26522 GDB as a DWARF-5 index consumer. */
26523 static int psymbol_tag (const struct partial_symbol *psym)
26525 domain_enum domain = PSYMBOL_DOMAIN (psym);
26526 enum address_class aclass = PSYMBOL_CLASS (psym);
26534 return DW_TAG_subprogram;
26536 return DW_TAG_typedef;
26538 case LOC_CONST_BYTES:
26539 case LOC_OPTIMIZED_OUT:
26541 return DW_TAG_variable;
26543 /* Note: It's currently impossible to recognize psyms as enum values
26544 short of reading the type info. For now punt. */
26545 return DW_TAG_variable;
26547 /* There are other LOC_FOO values that one might want to classify
26548 as variables, but dwarf2read.c doesn't currently use them. */
26549 return DW_TAG_variable;
26551 case STRUCT_DOMAIN:
26552 return DW_TAG_structure_type;
26558 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
26559 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
26560 struct partial_symbol **psymp, int count, int cu_index,
26561 bool is_static, unit_kind kind)
26563 for (; count-- > 0; ++psymp)
26565 struct partial_symbol *psym = *psymp;
26567 if (SYMBOL_LANGUAGE (psym) == language_ada)
26568 error (_("Ada is not currently supported by the index"));
26570 /* Only add a given psymbol once. */
26571 if (psyms_seen.insert (psym).second)
26572 insert (psym, cu_index, is_static, kind);
26576 /* A helper function that writes a single signatured_type
26577 to a debug_names. */
26579 write_one_signatured_type (struct signatured_type *entry,
26580 struct signatured_type_index_data *info)
26582 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
26584 write_psymbols (info->psyms_seen,
26585 &info->objfile->global_psymbols[psymtab->globals_offset],
26586 psymtab->n_global_syms, info->cu_index, false,
26588 write_psymbols (info->psyms_seen,
26589 &info->objfile->static_psymbols[psymtab->statics_offset],
26590 psymtab->n_static_syms, info->cu_index, true,
26593 info->types_list.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order,
26594 to_underlying (entry->per_cu.sect_off));
26599 /* Store value of each symbol. */
26600 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
26601 m_name_to_value_set;
26603 /* Tables of DWARF-5 .debug_names. They are in object file byte
26605 std::vector<uint32_t> m_bucket_table;
26606 std::vector<uint32_t> m_hash_table;
26608 const bfd_endian m_dwarf5_byte_order;
26609 dwarf_tmpl<uint32_t> m_dwarf32;
26610 dwarf_tmpl<uint64_t> m_dwarf64;
26612 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
26613 debug_str_lookup m_debugstrlookup;
26615 /* Map each used .debug_names abbreviation tag parameter to its
26617 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
26619 /* Next unused .debug_names abbreviation tag for
26620 m_indexkey_to_idx. */
26621 int m_idx_next = 1;
26623 /* .debug_names abbreviation table. */
26624 data_buf m_abbrev_table;
26626 /* .debug_names entry pool. */
26627 data_buf m_entry_pool;
26630 /* Return iff any of the needed offsets does not fit into 32-bit
26631 .debug_names section. */
26634 check_dwarf64_offsets ()
26636 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26638 const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
26640 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26643 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
26645 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
26646 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
26648 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26654 /* The psyms_seen set is potentially going to be largish (~40k
26655 elements when indexing a -g3 build of GDB itself). Estimate the
26656 number of elements in order to avoid too many rehashes, which
26657 require rebuilding buckets and thus many trips to
26663 size_t psyms_count = 0;
26664 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26666 struct dwarf2_per_cu_data *per_cu
26667 = dwarf2_per_objfile->all_comp_units[i];
26668 struct partial_symtab *psymtab = per_cu->v.psymtab;
26670 if (psymtab != NULL && psymtab->user == NULL)
26671 recursively_count_psymbols (psymtab, psyms_count);
26673 /* Generating an index for gdb itself shows a ratio of
26674 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
26675 return psyms_count / 4;
26678 /* Write new .gdb_index section for OBJFILE into OUT_FILE.
26679 Return how many bytes were expected to be written into OUT_FILE. */
26682 write_gdbindex (struct objfile *objfile, FILE *out_file)
26684 mapped_symtab symtab;
26687 /* While we're scanning CU's create a table that maps a psymtab pointer
26688 (which is what addrmap records) to its index (which is what is recorded
26689 in the index file). This will later be needed to write the address
26691 psym_index_map cu_index_htab;
26692 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
26694 /* The CU list is already sorted, so we don't need to do additional
26695 work here. Also, the debug_types entries do not appear in
26696 all_comp_units, but only in their own hash table. */
26698 std::unordered_set<partial_symbol *> psyms_seen (psyms_seen_size ());
26699 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26701 struct dwarf2_per_cu_data *per_cu
26702 = dwarf2_per_objfile->all_comp_units[i];
26703 struct partial_symtab *psymtab = per_cu->v.psymtab;
26705 /* CU of a shared file from 'dwz -m' may be unused by this main file.
26706 It may be referenced from a local scope but in such case it does not
26707 need to be present in .gdb_index. */
26708 if (psymtab == NULL)
26711 if (psymtab->user == NULL)
26712 recursively_write_psymbols (objfile, psymtab, &symtab,
26715 const auto insertpair = cu_index_htab.emplace (psymtab, i);
26716 gdb_assert (insertpair.second);
26718 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
26719 to_underlying (per_cu->sect_off));
26720 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
26723 /* Dump the address map. */
26725 write_address_map (objfile, addr_vec, cu_index_htab);
26727 /* Write out the .debug_type entries, if any. */
26728 data_buf types_cu_list;
26729 if (dwarf2_per_objfile->signatured_types)
26731 signatured_type_index_data sig_data (types_cu_list,
26734 sig_data.objfile = objfile;
26735 sig_data.symtab = &symtab;
26736 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
26737 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26738 write_one_signatured_type, &sig_data);
26741 /* Now that we've processed all symbols we can shrink their cu_indices
26743 uniquify_cu_indices (&symtab);
26745 data_buf symtab_vec, constant_pool;
26746 write_hash_table (&symtab, symtab_vec, constant_pool);
26749 const offset_type size_of_contents = 6 * sizeof (offset_type);
26750 offset_type total_len = size_of_contents;
26752 /* The version number. */
26753 contents.append_data (MAYBE_SWAP (8));
26755 /* The offset of the CU list from the start of the file. */
26756 contents.append_data (MAYBE_SWAP (total_len));
26757 total_len += cu_list.size ();
26759 /* The offset of the types CU list from the start of the file. */
26760 contents.append_data (MAYBE_SWAP (total_len));
26761 total_len += types_cu_list.size ();
26763 /* The offset of the address table from the start of the file. */
26764 contents.append_data (MAYBE_SWAP (total_len));
26765 total_len += addr_vec.size ();
26767 /* The offset of the symbol table from the start of the file. */
26768 contents.append_data (MAYBE_SWAP (total_len));
26769 total_len += symtab_vec.size ();
26771 /* The offset of the constant pool from the start of the file. */
26772 contents.append_data (MAYBE_SWAP (total_len));
26773 total_len += constant_pool.size ();
26775 gdb_assert (contents.size () == size_of_contents);
26777 contents.file_write (out_file);
26778 cu_list.file_write (out_file);
26779 types_cu_list.file_write (out_file);
26780 addr_vec.file_write (out_file);
26781 symtab_vec.file_write (out_file);
26782 constant_pool.file_write (out_file);
26787 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
26788 static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
26790 /* Write a new .debug_names section for OBJFILE into OUT_FILE, write
26791 needed addition to .debug_str section to OUT_FILE_STR. Return how
26792 many bytes were expected to be written into OUT_FILE. */
26795 write_debug_names (struct objfile *objfile, FILE *out_file, FILE *out_file_str)
26797 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets ();
26798 const enum bfd_endian dwarf5_byte_order
26799 = gdbarch_byte_order (get_objfile_arch (objfile));
26801 /* The CU list is already sorted, so we don't need to do additional
26802 work here. Also, the debug_types entries do not appear in
26803 all_comp_units, but only in their own hash table. */
26805 debug_names nametable (dwarf5_is_dwarf64, dwarf5_byte_order);
26806 std::unordered_set<partial_symbol *> psyms_seen (psyms_seen_size ());
26807 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26809 const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
26810 partial_symtab *psymtab = per_cu->v.psymtab;
26812 /* CU of a shared file from 'dwz -m' may be unused by this main
26813 file. It may be referenced from a local scope but in such
26814 case it does not need to be present in .debug_names. */
26815 if (psymtab == NULL)
26818 if (psymtab->user == NULL)
26819 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
26821 cu_list.append_uint (nametable.dwarf5_offset_size (), dwarf5_byte_order,
26822 to_underlying (per_cu->sect_off));
26825 /* Write out the .debug_type entries, if any. */
26826 data_buf types_cu_list;
26827 if (dwarf2_per_objfile->signatured_types)
26829 debug_names::write_one_signatured_type_data sig_data (nametable,
26830 signatured_type_index_data (types_cu_list, psyms_seen));
26832 sig_data.info.objfile = objfile;
26833 /* It is used only for gdb_index. */
26834 sig_data.info.symtab = nullptr;
26835 sig_data.info.cu_index = 0;
26836 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26837 debug_names::write_one_signatured_type,
26841 nametable.build ();
26843 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
26845 const offset_type bytes_of_header
26846 = ((dwarf5_is_dwarf64 ? 12 : 4)
26848 + sizeof (dwarf5_gdb_augmentation));
26849 size_t expected_bytes = 0;
26850 expected_bytes += bytes_of_header;
26851 expected_bytes += cu_list.size ();
26852 expected_bytes += types_cu_list.size ();
26853 expected_bytes += nametable.bytes ();
26856 if (!dwarf5_is_dwarf64)
26858 const uint64_t size64 = expected_bytes - 4;
26859 gdb_assert (size64 < 0xfffffff0);
26860 header.append_uint (4, dwarf5_byte_order, size64);
26864 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
26865 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
26868 /* The version number. */
26869 header.append_uint (2, dwarf5_byte_order, 5);
26872 header.append_uint (2, dwarf5_byte_order, 0);
26874 /* comp_unit_count - The number of CUs in the CU list. */
26875 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
26877 /* local_type_unit_count - The number of TUs in the local TU
26879 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
26881 /* foreign_type_unit_count - The number of TUs in the foreign TU
26883 header.append_uint (4, dwarf5_byte_order, 0);
26885 /* bucket_count - The number of hash buckets in the hash lookup
26887 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
26889 /* name_count - The number of unique names in the index. */
26890 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
26892 /* abbrev_table_size - The size in bytes of the abbreviations
26894 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
26896 /* augmentation_string_size - The size in bytes of the augmentation
26897 string. This value is rounded up to a multiple of 4. */
26898 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
26899 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
26900 header.append_data (dwarf5_gdb_augmentation);
26902 gdb_assert (header.size () == bytes_of_header);
26904 header.file_write (out_file);
26905 cu_list.file_write (out_file);
26906 types_cu_list.file_write (out_file);
26907 nametable.file_write (out_file, out_file_str);
26909 return expected_bytes;
26912 /* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
26913 position is at the end of the file. */
26916 assert_file_size (FILE *file, const char *filename, size_t expected_size)
26918 const auto file_size = ftell (file);
26919 if (file_size == -1)
26920 error (_("Can't get `%s' size"), filename);
26921 gdb_assert (file_size == expected_size);
26924 /* Create an index file for OBJFILE in the directory DIR. */
26927 write_psymtabs_to_index (struct objfile *objfile, const char *dir,
26928 dw_index_kind index_kind)
26930 if (dwarf2_per_objfile->using_index)
26931 error (_("Cannot use an index to create the index"));
26933 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
26934 error (_("Cannot make an index when the file has multiple .debug_types sections"));
26936 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
26940 if (stat (objfile_name (objfile), &st) < 0)
26941 perror_with_name (objfile_name (objfile));
26943 std::string filename (std::string (dir) + SLASH_STRING
26944 + lbasename (objfile_name (objfile))
26945 + (index_kind == dw_index_kind::DEBUG_NAMES
26946 ? INDEX5_SUFFIX : INDEX4_SUFFIX));
26948 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
26950 error (_("Can't open `%s' for writing"), filename.c_str ());
26952 /* Order matters here; we want FILE to be closed before FILENAME is
26953 unlinked, because on MS-Windows one cannot delete a file that is
26954 still open. (Don't call anything here that might throw until
26955 file_closer is created.) */
26956 gdb::unlinker unlink_file (filename.c_str ());
26957 gdb_file_up close_out_file (out_file);
26959 if (index_kind == dw_index_kind::DEBUG_NAMES)
26961 std::string filename_str (std::string (dir) + SLASH_STRING
26962 + lbasename (objfile_name (objfile))
26963 + DEBUG_STR_SUFFIX);
26965 = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
26967 error (_("Can't open `%s' for writing"), filename_str.c_str ());
26968 gdb::unlinker unlink_file_str (filename_str.c_str ());
26969 gdb_file_up close_out_file_str (out_file_str);
26971 const size_t total_len
26972 = write_debug_names (objfile, out_file, out_file_str);
26973 assert_file_size (out_file, filename.c_str (), total_len);
26975 /* We want to keep the file .debug_str file too. */
26976 unlink_file_str.keep ();
26980 const size_t total_len
26981 = write_gdbindex (objfile, out_file);
26982 assert_file_size (out_file, filename.c_str (), total_len);
26985 /* We want to keep the file. */
26986 unlink_file.keep ();
26989 /* Implementation of the `save gdb-index' command.
26991 Note that the .gdb_index file format used by this command is
26992 documented in the GDB manual. Any changes here must be documented
26996 save_gdb_index_command (const char *arg, int from_tty)
26998 struct objfile *objfile;
26999 const char dwarf5space[] = "-dwarf-5 ";
27000 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
27005 arg = skip_spaces (arg);
27006 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
27008 index_kind = dw_index_kind::DEBUG_NAMES;
27009 arg += strlen (dwarf5space);
27010 arg = skip_spaces (arg);
27014 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
27016 ALL_OBJFILES (objfile)
27020 /* If the objfile does not correspond to an actual file, skip it. */
27021 if (stat (objfile_name (objfile), &st) < 0)
27025 = (struct dwarf2_per_objfile *) objfile_data (objfile,
27026 dwarf2_objfile_data_key);
27027 if (dwarf2_per_objfile)
27032 write_psymtabs_to_index (objfile, arg, index_kind);
27034 CATCH (except, RETURN_MASK_ERROR)
27036 exception_fprintf (gdb_stderr, except,
27037 _("Error while writing index for `%s': "),
27038 objfile_name (objfile));
27047 int dwarf_always_disassemble;
27050 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
27051 struct cmd_list_element *c, const char *value)
27053 fprintf_filtered (file,
27054 _("Whether to always disassemble "
27055 "DWARF expressions is %s.\n"),
27060 show_check_physname (struct ui_file *file, int from_tty,
27061 struct cmd_list_element *c, const char *value)
27063 fprintf_filtered (file,
27064 _("Whether to check \"physname\" is %s.\n"),
27069 _initialize_dwarf2_read (void)
27071 struct cmd_list_element *c;
27073 dwarf2_objfile_data_key
27074 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
27076 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
27077 Set DWARF specific variables.\n\
27078 Configure DWARF variables such as the cache size"),
27079 &set_dwarf_cmdlist, "maintenance set dwarf ",
27080 0/*allow-unknown*/, &maintenance_set_cmdlist);
27082 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
27083 Show DWARF specific variables\n\
27084 Show DWARF variables such as the cache size"),
27085 &show_dwarf_cmdlist, "maintenance show dwarf ",
27086 0/*allow-unknown*/, &maintenance_show_cmdlist);
27088 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
27089 &dwarf_max_cache_age, _("\
27090 Set the upper bound on the age of cached DWARF compilation units."), _("\
27091 Show the upper bound on the age of cached DWARF compilation units."), _("\
27092 A higher limit means that cached compilation units will be stored\n\
27093 in memory longer, and more total memory will be used. Zero disables\n\
27094 caching, which can slow down startup."),
27096 show_dwarf_max_cache_age,
27097 &set_dwarf_cmdlist,
27098 &show_dwarf_cmdlist);
27100 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
27101 &dwarf_always_disassemble, _("\
27102 Set whether `info address' always disassembles DWARF expressions."), _("\
27103 Show whether `info address' always disassembles DWARF expressions."), _("\
27104 When enabled, DWARF expressions are always printed in an assembly-like\n\
27105 syntax. When disabled, expressions will be printed in a more\n\
27106 conversational style, when possible."),
27108 show_dwarf_always_disassemble,
27109 &set_dwarf_cmdlist,
27110 &show_dwarf_cmdlist);
27112 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
27113 Set debugging of the DWARF reader."), _("\
27114 Show debugging of the DWARF reader."), _("\
27115 When enabled (non-zero), debugging messages are printed during DWARF\n\
27116 reading and symtab expansion. A value of 1 (one) provides basic\n\
27117 information. A value greater than 1 provides more verbose information."),
27120 &setdebuglist, &showdebuglist);
27122 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
27123 Set debugging of the DWARF DIE reader."), _("\
27124 Show debugging of the DWARF DIE reader."), _("\
27125 When enabled (non-zero), DIEs are dumped after they are read in.\n\
27126 The value is the maximum depth to print."),
27129 &setdebuglist, &showdebuglist);
27131 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
27132 Set debugging of the dwarf line reader."), _("\
27133 Show debugging of the dwarf line reader."), _("\
27134 When enabled (non-zero), line number entries are dumped as they are read in.\n\
27135 A value of 1 (one) provides basic information.\n\
27136 A value greater than 1 provides more verbose information."),
27139 &setdebuglist, &showdebuglist);
27141 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
27142 Set cross-checking of \"physname\" code against demangler."), _("\
27143 Show cross-checking of \"physname\" code against demangler."), _("\
27144 When enabled, GDB's internal \"physname\" code is checked against\n\
27146 NULL, show_check_physname,
27147 &setdebuglist, &showdebuglist);
27149 add_setshow_boolean_cmd ("use-deprecated-index-sections",
27150 no_class, &use_deprecated_index_sections, _("\
27151 Set whether to use deprecated gdb_index sections."), _("\
27152 Show whether to use deprecated gdb_index sections."), _("\
27153 When enabled, deprecated .gdb_index sections are used anyway.\n\
27154 Normally they are ignored either because of a missing feature or\n\
27155 performance issue.\n\
27156 Warning: This option must be enabled before gdb reads the file."),
27159 &setlist, &showlist);
27161 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
27163 Save a gdb-index file.\n\
27164 Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
27166 No options create one file with .gdb-index extension for pre-DWARF-5\n\
27167 compatible .gdb_index section. With -dwarf-5 creates two files with\n\
27168 extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
27170 set_cmd_completer (c, filename_completer);
27172 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
27173 &dwarf2_locexpr_funcs);
27174 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
27175 &dwarf2_loclist_funcs);
27177 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
27178 &dwarf2_block_frame_base_locexpr_funcs);
27179 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
27180 &dwarf2_block_frame_base_loclist_funcs);
27183 selftests::register_test ("dw2_expand_symtabs_matching",
27184 selftests::dw2_expand_symtabs_matching::run_test);