1 /* DWARF 2 debugging format support for GDB.
3 Copyright (C) 1994-2017 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 /* A description of the mapped index. The file format is described in
238 a comment by the code that writes the index. */
241 /* Index data format version. */
244 /* The total length of the buffer. */
247 /* A pointer to the address table data. */
248 const gdb_byte *address_table;
250 /* Size of the address table data in bytes. */
251 offset_type address_table_size;
253 /* The symbol table, implemented as a hash table. */
254 const offset_type *symbol_table;
256 /* Size in slots, each slot is 2 offset_types. */
257 offset_type symbol_table_slots;
259 /* A pointer to the constant pool. */
260 const char *constant_pool;
262 /* The name_component table (a sorted vector). See name_component's
263 description above. */
264 std::vector<name_component> name_components;
266 /* How NAME_COMPONENTS is sorted. */
267 enum case_sensitivity name_components_casing;
269 /* Convenience method to get at the name of the symbol at IDX in the
271 const char *symbol_name_at (offset_type idx) const
272 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx]); }
274 /* Build the symbol name component sorted vector, if we haven't
276 void build_name_components ();
278 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
279 possible matches for LN_NO_PARAMS in the name component
281 std::pair<std::vector<name_component>::const_iterator,
282 std::vector<name_component>::const_iterator>
283 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
286 /* A description of the mapped .debug_names.
287 Uninitialized map has CU_COUNT 0. */
288 struct mapped_debug_names
290 bfd_endian dwarf5_byte_order;
291 bool dwarf5_is_dwarf64;
292 bool augmentation_is_gdb;
294 uint32_t cu_count = 0;
295 uint32_t tu_count, bucket_count, name_count;
296 const gdb_byte *cu_table_reordered, *tu_table_reordered;
297 const uint32_t *bucket_table_reordered, *hash_table_reordered;
298 const gdb_byte *name_table_string_offs_reordered;
299 const gdb_byte *name_table_entry_offs_reordered;
300 const gdb_byte *entry_pool;
307 /* Attribute name DW_IDX_*. */
310 /* Attribute form DW_FORM_*. */
313 /* Value if FORM is DW_FORM_implicit_const. */
314 LONGEST implicit_const;
316 std::vector<attr> attr_vec;
319 std::unordered_map<ULONGEST, index_val> abbrev_map;
321 const char *namei_to_name (uint32_t namei) const;
324 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
325 DEF_VEC_P (dwarf2_per_cu_ptr);
329 int nr_uniq_abbrev_tables;
331 int nr_symtab_sharers;
332 int nr_stmt_less_type_units;
333 int nr_all_type_units_reallocs;
336 /* Collection of data recorded per objfile.
337 This hangs off of dwarf2_objfile_data_key. */
339 struct dwarf2_per_objfile
341 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
342 dwarf2 section names, or is NULL if the standard ELF names are
344 dwarf2_per_objfile (struct objfile *objfile,
345 const dwarf2_debug_sections *names);
347 ~dwarf2_per_objfile ();
349 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
351 /* Free all cached compilation units. */
352 void free_cached_comp_units ();
354 /* This function is mapped across the sections and remembers the
355 offset and size of each of the debugging sections we are
357 void locate_sections (bfd *abfd, asection *sectp,
358 const dwarf2_debug_sections &names);
361 dwarf2_section_info info {};
362 dwarf2_section_info abbrev {};
363 dwarf2_section_info line {};
364 dwarf2_section_info loc {};
365 dwarf2_section_info loclists {};
366 dwarf2_section_info macinfo {};
367 dwarf2_section_info macro {};
368 dwarf2_section_info str {};
369 dwarf2_section_info line_str {};
370 dwarf2_section_info ranges {};
371 dwarf2_section_info rnglists {};
372 dwarf2_section_info addr {};
373 dwarf2_section_info frame {};
374 dwarf2_section_info eh_frame {};
375 dwarf2_section_info gdb_index {};
376 dwarf2_section_info debug_names {};
377 dwarf2_section_info debug_aranges {};
379 VEC (dwarf2_section_info_def) *types = NULL;
382 struct objfile *objfile = NULL;
384 /* Table of all the compilation units. This is used to locate
385 the target compilation unit of a particular reference. */
386 struct dwarf2_per_cu_data **all_comp_units = NULL;
388 /* The number of compilation units in ALL_COMP_UNITS. */
389 int n_comp_units = 0;
391 /* The number of .debug_types-related CUs. */
392 int n_type_units = 0;
394 /* The number of elements allocated in all_type_units.
395 If there are skeleton-less TUs, we add them to all_type_units lazily. */
396 int n_allocated_type_units = 0;
398 /* The .debug_types-related CUs (TUs).
399 This is stored in malloc space because we may realloc it. */
400 struct signatured_type **all_type_units = NULL;
402 /* Table of struct type_unit_group objects.
403 The hash key is the DW_AT_stmt_list value. */
404 htab_t type_unit_groups {};
406 /* A table mapping .debug_types signatures to its signatured_type entry.
407 This is NULL if the .debug_types section hasn't been read in yet. */
408 htab_t signatured_types {};
410 /* Type unit statistics, to see how well the scaling improvements
412 struct tu_stats tu_stats {};
414 /* A chain of compilation units that are currently read in, so that
415 they can be freed later. */
416 dwarf2_per_cu_data *read_in_chain = NULL;
418 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
419 This is NULL if the table hasn't been allocated yet. */
422 /* True if we've checked for whether there is a DWP file. */
423 bool dwp_checked = false;
425 /* The DWP file if there is one, or NULL. */
426 struct dwp_file *dwp_file = NULL;
428 /* The shared '.dwz' file, if one exists. This is used when the
429 original data was compressed using 'dwz -m'. */
430 struct dwz_file *dwz_file = NULL;
432 /* A flag indicating whether this objfile has a section loaded at a
434 bool has_section_at_zero = false;
436 /* True if we are using the mapped index,
437 or we are faking it for OBJF_READNOW's sake. */
438 bool using_index = false;
440 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
441 mapped_index *index_table = NULL;
443 /* The mapped index, or NULL if .debug_names is missing or not being used. */
444 std::unique_ptr<mapped_debug_names> debug_names_table;
446 /* When using index_table, this keeps track of all quick_file_names entries.
447 TUs typically share line table entries with a CU, so we maintain a
448 separate table of all line table entries to support the sharing.
449 Note that while there can be way more TUs than CUs, we've already
450 sorted all the TUs into "type unit groups", grouped by their
451 DW_AT_stmt_list value. Therefore the only sharing done here is with a
452 CU and its associated TU group if there is one. */
453 htab_t quick_file_names_table {};
455 /* Set during partial symbol reading, to prevent queueing of full
457 bool reading_partial_symbols = false;
459 /* Table mapping type DIEs to their struct type *.
460 This is NULL if not allocated yet.
461 The mapping is done via (CU/TU + DIE offset) -> type. */
462 htab_t die_type_hash {};
464 /* The CUs we recently read. */
465 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
467 /* Table containing line_header indexed by offset and offset_in_dwz. */
468 htab_t line_header_hash {};
470 /* Table containing all filenames. This is an optional because the
471 table is lazily constructed on first access. */
472 gdb::optional<filename_seen_cache> filenames_cache;
475 static struct dwarf2_per_objfile *dwarf2_per_objfile;
477 /* Default names of the debugging sections. */
479 /* Note that if the debugging section has been compressed, it might
480 have a name like .zdebug_info. */
482 static const struct dwarf2_debug_sections dwarf2_elf_names =
484 { ".debug_info", ".zdebug_info" },
485 { ".debug_abbrev", ".zdebug_abbrev" },
486 { ".debug_line", ".zdebug_line" },
487 { ".debug_loc", ".zdebug_loc" },
488 { ".debug_loclists", ".zdebug_loclists" },
489 { ".debug_macinfo", ".zdebug_macinfo" },
490 { ".debug_macro", ".zdebug_macro" },
491 { ".debug_str", ".zdebug_str" },
492 { ".debug_line_str", ".zdebug_line_str" },
493 { ".debug_ranges", ".zdebug_ranges" },
494 { ".debug_rnglists", ".zdebug_rnglists" },
495 { ".debug_types", ".zdebug_types" },
496 { ".debug_addr", ".zdebug_addr" },
497 { ".debug_frame", ".zdebug_frame" },
498 { ".eh_frame", NULL },
499 { ".gdb_index", ".zgdb_index" },
500 { ".debug_names", ".zdebug_names" },
501 { ".debug_aranges", ".zdebug_aranges" },
505 /* List of DWO/DWP sections. */
507 static const struct dwop_section_names
509 struct dwarf2_section_names abbrev_dwo;
510 struct dwarf2_section_names info_dwo;
511 struct dwarf2_section_names line_dwo;
512 struct dwarf2_section_names loc_dwo;
513 struct dwarf2_section_names loclists_dwo;
514 struct dwarf2_section_names macinfo_dwo;
515 struct dwarf2_section_names macro_dwo;
516 struct dwarf2_section_names str_dwo;
517 struct dwarf2_section_names str_offsets_dwo;
518 struct dwarf2_section_names types_dwo;
519 struct dwarf2_section_names cu_index;
520 struct dwarf2_section_names tu_index;
524 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
525 { ".debug_info.dwo", ".zdebug_info.dwo" },
526 { ".debug_line.dwo", ".zdebug_line.dwo" },
527 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
528 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
529 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
530 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
531 { ".debug_str.dwo", ".zdebug_str.dwo" },
532 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
533 { ".debug_types.dwo", ".zdebug_types.dwo" },
534 { ".debug_cu_index", ".zdebug_cu_index" },
535 { ".debug_tu_index", ".zdebug_tu_index" },
538 /* local data types */
540 /* The data in a compilation unit header, after target2host
541 translation, looks like this. */
542 struct comp_unit_head
546 unsigned char addr_size;
547 unsigned char signed_addr_p;
548 sect_offset abbrev_sect_off;
550 /* Size of file offsets; either 4 or 8. */
551 unsigned int offset_size;
553 /* Size of the length field; either 4 or 12. */
554 unsigned int initial_length_size;
556 enum dwarf_unit_type unit_type;
558 /* Offset to the first byte of this compilation unit header in the
559 .debug_info section, for resolving relative reference dies. */
560 sect_offset sect_off;
562 /* Offset to first die in this cu from the start of the cu.
563 This will be the first byte following the compilation unit header. */
564 cu_offset first_die_cu_offset;
566 /* 64-bit signature of this type unit - it is valid only for
567 UNIT_TYPE DW_UT_type. */
570 /* For types, offset in the type's DIE of the type defined by this TU. */
571 cu_offset type_cu_offset_in_tu;
574 /* Type used for delaying computation of method physnames.
575 See comments for compute_delayed_physnames. */
576 struct delayed_method_info
578 /* The type to which the method is attached, i.e., its parent class. */
581 /* The index of the method in the type's function fieldlists. */
584 /* The index of the method in the fieldlist. */
587 /* The name of the DIE. */
590 /* The DIE associated with this method. */
591 struct die_info *die;
594 typedef struct delayed_method_info delayed_method_info;
595 DEF_VEC_O (delayed_method_info);
597 /* Internal state when decoding a particular compilation unit. */
600 /* The objfile containing this compilation unit. */
601 struct objfile *objfile;
603 /* The header of the compilation unit. */
604 struct comp_unit_head header;
606 /* Base address of this compilation unit. */
607 CORE_ADDR base_address;
609 /* Non-zero if base_address has been set. */
612 /* The language we are debugging. */
613 enum language language;
614 const struct language_defn *language_defn;
616 const char *producer;
618 /* The generic symbol table building routines have separate lists for
619 file scope symbols and all all other scopes (local scopes). So
620 we need to select the right one to pass to add_symbol_to_list().
621 We do it by keeping a pointer to the correct list in list_in_scope.
623 FIXME: The original dwarf code just treated the file scope as the
624 first local scope, and all other local scopes as nested local
625 scopes, and worked fine. Check to see if we really need to
626 distinguish these in buildsym.c. */
627 struct pending **list_in_scope;
629 /* The abbrev table for this CU.
630 Normally this points to the abbrev table in the objfile.
631 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
632 struct abbrev_table *abbrev_table;
634 /* Hash table holding all the loaded partial DIEs
635 with partial_die->offset.SECT_OFF as hash. */
638 /* Storage for things with the same lifetime as this read-in compilation
639 unit, including partial DIEs. */
640 struct obstack comp_unit_obstack;
642 /* When multiple dwarf2_cu structures are living in memory, this field
643 chains them all together, so that they can be released efficiently.
644 We will probably also want a generation counter so that most-recently-used
645 compilation units are cached... */
646 struct dwarf2_per_cu_data *read_in_chain;
648 /* Backlink to our per_cu entry. */
649 struct dwarf2_per_cu_data *per_cu;
651 /* How many compilation units ago was this CU last referenced? */
654 /* A hash table of DIE cu_offset for following references with
655 die_info->offset.sect_off as hash. */
658 /* Full DIEs if read in. */
659 struct die_info *dies;
661 /* A set of pointers to dwarf2_per_cu_data objects for compilation
662 units referenced by this one. Only set during full symbol processing;
663 partial symbol tables do not have dependencies. */
666 /* Header data from the line table, during full symbol processing. */
667 struct line_header *line_header;
668 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
669 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
670 this is the DW_TAG_compile_unit die for this CU. We'll hold on
671 to the line header as long as this DIE is being processed. See
672 process_die_scope. */
673 die_info *line_header_die_owner;
675 /* A list of methods which need to have physnames computed
676 after all type information has been read. */
677 VEC (delayed_method_info) *method_list;
679 /* To be copied to symtab->call_site_htab. */
680 htab_t call_site_htab;
682 /* Non-NULL if this CU came from a DWO file.
683 There is an invariant here that is important to remember:
684 Except for attributes copied from the top level DIE in the "main"
685 (or "stub") file in preparation for reading the DWO file
686 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
687 Either there isn't a DWO file (in which case this is NULL and the point
688 is moot), or there is and either we're not going to read it (in which
689 case this is NULL) or there is and we are reading it (in which case this
691 struct dwo_unit *dwo_unit;
693 /* The DW_AT_addr_base attribute if present, zero otherwise
694 (zero is a valid value though).
695 Note this value comes from the Fission stub CU/TU's DIE. */
698 /* The DW_AT_ranges_base attribute if present, zero otherwise
699 (zero is a valid value though).
700 Note this value comes from the Fission stub CU/TU's DIE.
701 Also note that the value is zero in the non-DWO case so this value can
702 be used without needing to know whether DWO files are in use or not.
703 N.B. This does not apply to DW_AT_ranges appearing in
704 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
705 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
706 DW_AT_ranges_base *would* have to be applied, and we'd have to care
707 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
708 ULONGEST ranges_base;
710 /* Mark used when releasing cached dies. */
711 unsigned int mark : 1;
713 /* This CU references .debug_loc. See the symtab->locations_valid field.
714 This test is imperfect as there may exist optimized debug code not using
715 any location list and still facing inlining issues if handled as
716 unoptimized code. For a future better test see GCC PR other/32998. */
717 unsigned int has_loclist : 1;
719 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
720 if all the producer_is_* fields are valid. This information is cached
721 because profiling CU expansion showed excessive time spent in
722 producer_is_gxx_lt_4_6. */
723 unsigned int checked_producer : 1;
724 unsigned int producer_is_gxx_lt_4_6 : 1;
725 unsigned int producer_is_gcc_lt_4_3 : 1;
726 unsigned int producer_is_icc_lt_14 : 1;
728 /* When set, the file that we're processing is known to have
729 debugging info for C++ namespaces. GCC 3.3.x did not produce
730 this information, but later versions do. */
732 unsigned int processing_has_namespace_info : 1;
735 /* Persistent data held for a compilation unit, even when not
736 processing it. We put a pointer to this structure in the
737 read_symtab_private field of the psymtab. */
739 struct dwarf2_per_cu_data
741 /* The start offset and length of this compilation unit.
742 NOTE: Unlike comp_unit_head.length, this length includes
744 If the DIE refers to a DWO file, this is always of the original die,
746 sect_offset sect_off;
749 /* DWARF standard version this data has been read from (such as 4 or 5). */
752 /* Flag indicating this compilation unit will be read in before
753 any of the current compilation units are processed. */
754 unsigned int queued : 1;
756 /* This flag will be set when reading partial DIEs if we need to load
757 absolutely all DIEs for this compilation unit, instead of just the ones
758 we think are interesting. It gets set if we look for a DIE in the
759 hash table and don't find it. */
760 unsigned int load_all_dies : 1;
762 /* Non-zero if this CU is from .debug_types.
763 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
765 unsigned int is_debug_types : 1;
767 /* Non-zero if this CU is from the .dwz file. */
768 unsigned int is_dwz : 1;
770 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
771 This flag is only valid if is_debug_types is true.
772 We can't read a CU directly from a DWO file: There are required
773 attributes in the stub. */
774 unsigned int reading_dwo_directly : 1;
776 /* Non-zero if the TU has been read.
777 This is used to assist the "Stay in DWO Optimization" for Fission:
778 When reading a DWO, it's faster to read TUs from the DWO instead of
779 fetching them from random other DWOs (due to comdat folding).
780 If the TU has already been read, the optimization is unnecessary
781 (and unwise - we don't want to change where gdb thinks the TU lives
783 This flag is only valid if is_debug_types is true. */
784 unsigned int tu_read : 1;
786 /* The section this CU/TU lives in.
787 If the DIE refers to a DWO file, this is always the original die,
789 struct dwarf2_section_info *section;
791 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
792 of the CU cache it gets reset to NULL again. This is left as NULL for
793 dummy CUs (a CU header, but nothing else). */
794 struct dwarf2_cu *cu;
796 /* The corresponding objfile.
797 Normally we can get the objfile from dwarf2_per_objfile.
798 However we can enter this file with just a "per_cu" handle. */
799 struct objfile *objfile;
801 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
802 is active. Otherwise, the 'psymtab' field is active. */
805 /* The partial symbol table associated with this compilation unit,
806 or NULL for unread partial units. */
807 struct partial_symtab *psymtab;
809 /* Data needed by the "quick" functions. */
810 struct dwarf2_per_cu_quick_data *quick;
813 /* The CUs we import using DW_TAG_imported_unit. This is filled in
814 while reading psymtabs, used to compute the psymtab dependencies,
815 and then cleared. Then it is filled in again while reading full
816 symbols, and only deleted when the objfile is destroyed.
818 This is also used to work around a difference between the way gold
819 generates .gdb_index version <=7 and the way gdb does. Arguably this
820 is a gold bug. For symbols coming from TUs, gold records in the index
821 the CU that includes the TU instead of the TU itself. This breaks
822 dw2_lookup_symbol: It assumes that if the index says symbol X lives
823 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
824 will find X. Alas TUs live in their own symtab, so after expanding CU Y
825 we need to look in TU Z to find X. Fortunately, this is akin to
826 DW_TAG_imported_unit, so we just use the same mechanism: For
827 .gdb_index version <=7 this also records the TUs that the CU referred
828 to. Concurrently with this change gdb was modified to emit version 8
829 indices so we only pay a price for gold generated indices.
830 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
831 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
834 /* Entry in the signatured_types hash table. */
836 struct signatured_type
838 /* The "per_cu" object of this type.
839 This struct is used iff per_cu.is_debug_types.
840 N.B.: This is the first member so that it's easy to convert pointers
842 struct dwarf2_per_cu_data per_cu;
844 /* The type's signature. */
847 /* Offset in the TU of the type's DIE, as read from the TU header.
848 If this TU is a DWO stub and the definition lives in a DWO file
849 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
850 cu_offset type_offset_in_tu;
852 /* Offset in the section of the type's DIE.
853 If the definition lives in a DWO file, this is the offset in the
854 .debug_types.dwo section.
855 The value is zero until the actual value is known.
856 Zero is otherwise not a valid section offset. */
857 sect_offset type_offset_in_section;
859 /* Type units are grouped by their DW_AT_stmt_list entry so that they
860 can share them. This points to the containing symtab. */
861 struct type_unit_group *type_unit_group;
864 The first time we encounter this type we fully read it in and install it
865 in the symbol tables. Subsequent times we only need the type. */
868 /* Containing DWO unit.
869 This field is valid iff per_cu.reading_dwo_directly. */
870 struct dwo_unit *dwo_unit;
873 typedef struct signatured_type *sig_type_ptr;
874 DEF_VEC_P (sig_type_ptr);
876 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
877 This includes type_unit_group and quick_file_names. */
879 struct stmt_list_hash
881 /* The DWO unit this table is from or NULL if there is none. */
882 struct dwo_unit *dwo_unit;
884 /* Offset in .debug_line or .debug_line.dwo. */
885 sect_offset line_sect_off;
888 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
889 an object of this type. */
891 struct type_unit_group
893 /* dwarf2read.c's main "handle" on a TU symtab.
894 To simplify things we create an artificial CU that "includes" all the
895 type units using this stmt_list so that the rest of the code still has
896 a "per_cu" handle on the symtab.
897 This PER_CU is recognized by having no section. */
898 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
899 struct dwarf2_per_cu_data per_cu;
901 /* The TUs that share this DW_AT_stmt_list entry.
902 This is added to while parsing type units to build partial symtabs,
903 and is deleted afterwards and not used again. */
904 VEC (sig_type_ptr) *tus;
906 /* The compunit symtab.
907 Type units in a group needn't all be defined in the same source file,
908 so we create an essentially anonymous symtab as the compunit symtab. */
909 struct compunit_symtab *compunit_symtab;
911 /* The data used to construct the hash key. */
912 struct stmt_list_hash hash;
914 /* The number of symtabs from the line header.
915 The value here must match line_header.num_file_names. */
916 unsigned int num_symtabs;
918 /* The symbol tables for this TU (obtained from the files listed in
920 WARNING: The order of entries here must match the order of entries
921 in the line header. After the first TU using this type_unit_group, the
922 line header for the subsequent TUs is recreated from this. This is done
923 because we need to use the same symtabs for each TU using the same
924 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
925 there's no guarantee the line header doesn't have duplicate entries. */
926 struct symtab **symtabs;
929 /* These sections are what may appear in a (real or virtual) DWO file. */
933 struct dwarf2_section_info abbrev;
934 struct dwarf2_section_info line;
935 struct dwarf2_section_info loc;
936 struct dwarf2_section_info loclists;
937 struct dwarf2_section_info macinfo;
938 struct dwarf2_section_info macro;
939 struct dwarf2_section_info str;
940 struct dwarf2_section_info str_offsets;
941 /* In the case of a virtual DWO file, these two are unused. */
942 struct dwarf2_section_info info;
943 VEC (dwarf2_section_info_def) *types;
946 /* CUs/TUs in DWP/DWO files. */
950 /* Backlink to the containing struct dwo_file. */
951 struct dwo_file *dwo_file;
953 /* The "id" that distinguishes this CU/TU.
954 .debug_info calls this "dwo_id", .debug_types calls this "signature".
955 Since signatures came first, we stick with it for consistency. */
958 /* The section this CU/TU lives in, in the DWO file. */
959 struct dwarf2_section_info *section;
961 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
962 sect_offset sect_off;
965 /* For types, offset in the type's DIE of the type defined by this TU. */
966 cu_offset type_offset_in_tu;
969 /* include/dwarf2.h defines the DWP section codes.
970 It defines a max value but it doesn't define a min value, which we
971 use for error checking, so provide one. */
973 enum dwp_v2_section_ids
978 /* Data for one DWO file.
980 This includes virtual DWO files (a virtual DWO file is a DWO file as it
981 appears in a DWP file). DWP files don't really have DWO files per se -
982 comdat folding of types "loses" the DWO file they came from, and from
983 a high level view DWP files appear to contain a mass of random types.
984 However, to maintain consistency with the non-DWP case we pretend DWP
985 files contain virtual DWO files, and we assign each TU with one virtual
986 DWO file (generally based on the line and abbrev section offsets -
987 a heuristic that seems to work in practice). */
991 /* The DW_AT_GNU_dwo_name attribute.
992 For virtual DWO files the name is constructed from the section offsets
993 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
994 from related CU+TUs. */
995 const char *dwo_name;
997 /* The DW_AT_comp_dir attribute. */
998 const char *comp_dir;
1000 /* The bfd, when the file is open. Otherwise this is NULL.
1001 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
1004 /* The sections that make up this DWO file.
1005 Remember that for virtual DWO files in DWP V2, these are virtual
1006 sections (for lack of a better name). */
1007 struct dwo_sections sections;
1009 /* The CUs in the file.
1010 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
1011 an extension to handle LLVM's Link Time Optimization output (where
1012 multiple source files may be compiled into a single object/dwo pair). */
1015 /* Table of TUs in the file.
1016 Each element is a struct dwo_unit. */
1020 /* These sections are what may appear in a DWP file. */
1024 /* These are used by both DWP version 1 and 2. */
1025 struct dwarf2_section_info str;
1026 struct dwarf2_section_info cu_index;
1027 struct dwarf2_section_info tu_index;
1029 /* These are only used by DWP version 2 files.
1030 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
1031 sections are referenced by section number, and are not recorded here.
1032 In DWP version 2 there is at most one copy of all these sections, each
1033 section being (effectively) comprised of the concatenation of all of the
1034 individual sections that exist in the version 1 format.
1035 To keep the code simple we treat each of these concatenated pieces as a
1036 section itself (a virtual section?). */
1037 struct dwarf2_section_info abbrev;
1038 struct dwarf2_section_info info;
1039 struct dwarf2_section_info line;
1040 struct dwarf2_section_info loc;
1041 struct dwarf2_section_info macinfo;
1042 struct dwarf2_section_info macro;
1043 struct dwarf2_section_info str_offsets;
1044 struct dwarf2_section_info types;
1047 /* These sections are what may appear in a virtual DWO file in DWP version 1.
1048 A virtual DWO file is a DWO file as it appears in a DWP file. */
1050 struct virtual_v1_dwo_sections
1052 struct dwarf2_section_info abbrev;
1053 struct dwarf2_section_info line;
1054 struct dwarf2_section_info loc;
1055 struct dwarf2_section_info macinfo;
1056 struct dwarf2_section_info macro;
1057 struct dwarf2_section_info str_offsets;
1058 /* Each DWP hash table entry records one CU or one TU.
1059 That is recorded here, and copied to dwo_unit.section. */
1060 struct dwarf2_section_info info_or_types;
1063 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
1064 In version 2, the sections of the DWO files are concatenated together
1065 and stored in one section of that name. Thus each ELF section contains
1066 several "virtual" sections. */
1068 struct virtual_v2_dwo_sections
1070 bfd_size_type abbrev_offset;
1071 bfd_size_type abbrev_size;
1073 bfd_size_type line_offset;
1074 bfd_size_type line_size;
1076 bfd_size_type loc_offset;
1077 bfd_size_type loc_size;
1079 bfd_size_type macinfo_offset;
1080 bfd_size_type macinfo_size;
1082 bfd_size_type macro_offset;
1083 bfd_size_type macro_size;
1085 bfd_size_type str_offsets_offset;
1086 bfd_size_type str_offsets_size;
1088 /* Each DWP hash table entry records one CU or one TU.
1089 That is recorded here, and copied to dwo_unit.section. */
1090 bfd_size_type info_or_types_offset;
1091 bfd_size_type info_or_types_size;
1094 /* Contents of DWP hash tables. */
1096 struct dwp_hash_table
1098 uint32_t version, nr_columns;
1099 uint32_t nr_units, nr_slots;
1100 const gdb_byte *hash_table, *unit_table;
1105 const gdb_byte *indices;
1109 /* This is indexed by column number and gives the id of the section
1111 #define MAX_NR_V2_DWO_SECTIONS \
1112 (1 /* .debug_info or .debug_types */ \
1113 + 1 /* .debug_abbrev */ \
1114 + 1 /* .debug_line */ \
1115 + 1 /* .debug_loc */ \
1116 + 1 /* .debug_str_offsets */ \
1117 + 1 /* .debug_macro or .debug_macinfo */)
1118 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1119 const gdb_byte *offsets;
1120 const gdb_byte *sizes;
1125 /* Data for one DWP file. */
1129 /* Name of the file. */
1132 /* File format version. */
1138 /* Section info for this file. */
1139 struct dwp_sections sections;
1141 /* Table of CUs in the file. */
1142 const struct dwp_hash_table *cus;
1144 /* Table of TUs in the file. */
1145 const struct dwp_hash_table *tus;
1147 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1151 /* Table to map ELF section numbers to their sections.
1152 This is only needed for the DWP V1 file format. */
1153 unsigned int num_sections;
1154 asection **elf_sections;
1157 /* This represents a '.dwz' file. */
1161 /* A dwz file can only contain a few sections. */
1162 struct dwarf2_section_info abbrev;
1163 struct dwarf2_section_info info;
1164 struct dwarf2_section_info str;
1165 struct dwarf2_section_info line;
1166 struct dwarf2_section_info macro;
1167 struct dwarf2_section_info gdb_index;
1168 struct dwarf2_section_info debug_names;
1170 /* The dwz's BFD. */
1174 /* Struct used to pass misc. parameters to read_die_and_children, et
1175 al. which are used for both .debug_info and .debug_types dies.
1176 All parameters here are unchanging for the life of the call. This
1177 struct exists to abstract away the constant parameters of die reading. */
1179 struct die_reader_specs
1181 /* The bfd of die_section. */
1184 /* The CU of the DIE we are parsing. */
1185 struct dwarf2_cu *cu;
1187 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1188 struct dwo_file *dwo_file;
1190 /* The section the die comes from.
1191 This is either .debug_info or .debug_types, or the .dwo variants. */
1192 struct dwarf2_section_info *die_section;
1194 /* die_section->buffer. */
1195 const gdb_byte *buffer;
1197 /* The end of the buffer. */
1198 const gdb_byte *buffer_end;
1200 /* The value of the DW_AT_comp_dir attribute. */
1201 const char *comp_dir;
1204 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1205 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1206 const gdb_byte *info_ptr,
1207 struct die_info *comp_unit_die,
1211 /* A 1-based directory index. This is a strong typedef to prevent
1212 accidentally using a directory index as a 0-based index into an
1214 enum class dir_index : unsigned int {};
1216 /* Likewise, a 1-based file name index. */
1217 enum class file_name_index : unsigned int {};
1221 file_entry () = default;
1223 file_entry (const char *name_, dir_index d_index_,
1224 unsigned int mod_time_, unsigned int length_)
1227 mod_time (mod_time_),
1231 /* Return the include directory at D_INDEX stored in LH. Returns
1232 NULL if D_INDEX is out of bounds. */
1233 const char *include_dir (const line_header *lh) const;
1235 /* The file name. Note this is an observing pointer. The memory is
1236 owned by debug_line_buffer. */
1237 const char *name {};
1239 /* The directory index (1-based). */
1240 dir_index d_index {};
1242 unsigned int mod_time {};
1244 unsigned int length {};
1246 /* True if referenced by the Line Number Program. */
1249 /* The associated symbol table, if any. */
1250 struct symtab *symtab {};
1253 /* The line number information for a compilation unit (found in the
1254 .debug_line section) begins with a "statement program header",
1255 which contains the following information. */
1262 /* Add an entry to the include directory table. */
1263 void add_include_dir (const char *include_dir);
1265 /* Add an entry to the file name table. */
1266 void add_file_name (const char *name, dir_index d_index,
1267 unsigned int mod_time, unsigned int length);
1269 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
1270 is out of bounds. */
1271 const char *include_dir_at (dir_index index) const
1273 /* Convert directory index number (1-based) to vector index
1275 size_t vec_index = to_underlying (index) - 1;
1277 if (vec_index >= include_dirs.size ())
1279 return include_dirs[vec_index];
1282 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
1283 is out of bounds. */
1284 file_entry *file_name_at (file_name_index index)
1286 /* Convert file name index number (1-based) to vector index
1288 size_t vec_index = to_underlying (index) - 1;
1290 if (vec_index >= file_names.size ())
1292 return &file_names[vec_index];
1295 /* Const version of the above. */
1296 const file_entry *file_name_at (unsigned int index) const
1298 if (index >= file_names.size ())
1300 return &file_names[index];
1303 /* Offset of line number information in .debug_line section. */
1304 sect_offset sect_off {};
1306 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1307 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1309 unsigned int total_length {};
1310 unsigned short version {};
1311 unsigned int header_length {};
1312 unsigned char minimum_instruction_length {};
1313 unsigned char maximum_ops_per_instruction {};
1314 unsigned char default_is_stmt {};
1316 unsigned char line_range {};
1317 unsigned char opcode_base {};
1319 /* standard_opcode_lengths[i] is the number of operands for the
1320 standard opcode whose value is i. This means that
1321 standard_opcode_lengths[0] is unused, and the last meaningful
1322 element is standard_opcode_lengths[opcode_base - 1]. */
1323 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1325 /* The include_directories table. Note these are observing
1326 pointers. The memory is owned by debug_line_buffer. */
1327 std::vector<const char *> include_dirs;
1329 /* The file_names table. */
1330 std::vector<file_entry> file_names;
1332 /* The start and end of the statement program following this
1333 header. These point into dwarf2_per_objfile->line_buffer. */
1334 const gdb_byte *statement_program_start {}, *statement_program_end {};
1337 typedef std::unique_ptr<line_header> line_header_up;
1340 file_entry::include_dir (const line_header *lh) const
1342 return lh->include_dir_at (d_index);
1345 /* When we construct a partial symbol table entry we only
1346 need this much information. */
1347 struct partial_die_info
1349 /* Offset of this DIE. */
1350 sect_offset sect_off;
1352 /* DWARF-2 tag for this DIE. */
1353 ENUM_BITFIELD(dwarf_tag) tag : 16;
1355 /* Assorted flags describing the data found in this DIE. */
1356 unsigned int has_children : 1;
1357 unsigned int is_external : 1;
1358 unsigned int is_declaration : 1;
1359 unsigned int has_type : 1;
1360 unsigned int has_specification : 1;
1361 unsigned int has_pc_info : 1;
1362 unsigned int may_be_inlined : 1;
1364 /* This DIE has been marked DW_AT_main_subprogram. */
1365 unsigned int main_subprogram : 1;
1367 /* Flag set if the SCOPE field of this structure has been
1369 unsigned int scope_set : 1;
1371 /* Flag set if the DIE has a byte_size attribute. */
1372 unsigned int has_byte_size : 1;
1374 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1375 unsigned int has_const_value : 1;
1377 /* Flag set if any of the DIE's children are template arguments. */
1378 unsigned int has_template_arguments : 1;
1380 /* Flag set if fixup_partial_die has been called on this die. */
1381 unsigned int fixup_called : 1;
1383 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1384 unsigned int is_dwz : 1;
1386 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1387 unsigned int spec_is_dwz : 1;
1389 /* The name of this DIE. Normally the value of DW_AT_name, but
1390 sometimes a default name for unnamed DIEs. */
1393 /* The linkage name, if present. */
1394 const char *linkage_name;
1396 /* The scope to prepend to our children. This is generally
1397 allocated on the comp_unit_obstack, so will disappear
1398 when this compilation unit leaves the cache. */
1401 /* Some data associated with the partial DIE. The tag determines
1402 which field is live. */
1405 /* The location description associated with this DIE, if any. */
1406 struct dwarf_block *locdesc;
1407 /* The offset of an import, for DW_TAG_imported_unit. */
1408 sect_offset sect_off;
1411 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1415 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1416 DW_AT_sibling, if any. */
1417 /* NOTE: This member isn't strictly necessary, read_partial_die could
1418 return DW_AT_sibling values to its caller load_partial_dies. */
1419 const gdb_byte *sibling;
1421 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1422 DW_AT_specification (or DW_AT_abstract_origin or
1423 DW_AT_extension). */
1424 sect_offset spec_offset;
1426 /* Pointers to this DIE's parent, first child, and next sibling,
1428 struct partial_die_info *die_parent, *die_child, *die_sibling;
1431 /* This data structure holds the information of an abbrev. */
1434 unsigned int number; /* number identifying abbrev */
1435 enum dwarf_tag tag; /* dwarf tag */
1436 unsigned short has_children; /* boolean */
1437 unsigned short num_attrs; /* number of attributes */
1438 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1439 struct abbrev_info *next; /* next in chain */
1444 ENUM_BITFIELD(dwarf_attribute) name : 16;
1445 ENUM_BITFIELD(dwarf_form) form : 16;
1447 /* It is valid only if FORM is DW_FORM_implicit_const. */
1448 LONGEST implicit_const;
1451 /* Size of abbrev_table.abbrev_hash_table. */
1452 #define ABBREV_HASH_SIZE 121
1454 /* Top level data structure to contain an abbreviation table. */
1458 /* Where the abbrev table came from.
1459 This is used as a sanity check when the table is used. */
1460 sect_offset sect_off;
1462 /* Storage for the abbrev table. */
1463 struct obstack abbrev_obstack;
1465 /* Hash table of abbrevs.
1466 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1467 It could be statically allocated, but the previous code didn't so we
1469 struct abbrev_info **abbrevs;
1472 /* Attributes have a name and a value. */
1475 ENUM_BITFIELD(dwarf_attribute) name : 16;
1476 ENUM_BITFIELD(dwarf_form) form : 15;
1478 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1479 field should be in u.str (existing only for DW_STRING) but it is kept
1480 here for better struct attribute alignment. */
1481 unsigned int string_is_canonical : 1;
1486 struct dwarf_block *blk;
1495 /* This data structure holds a complete die structure. */
1498 /* DWARF-2 tag for this DIE. */
1499 ENUM_BITFIELD(dwarf_tag) tag : 16;
1501 /* Number of attributes */
1502 unsigned char num_attrs;
1504 /* True if we're presently building the full type name for the
1505 type derived from this DIE. */
1506 unsigned char building_fullname : 1;
1508 /* True if this die is in process. PR 16581. */
1509 unsigned char in_process : 1;
1512 unsigned int abbrev;
1514 /* Offset in .debug_info or .debug_types section. */
1515 sect_offset sect_off;
1517 /* The dies in a compilation unit form an n-ary tree. PARENT
1518 points to this die's parent; CHILD points to the first child of
1519 this node; and all the children of a given node are chained
1520 together via their SIBLING fields. */
1521 struct die_info *child; /* Its first child, if any. */
1522 struct die_info *sibling; /* Its next sibling, if any. */
1523 struct die_info *parent; /* Its parent, if any. */
1525 /* An array of attributes, with NUM_ATTRS elements. There may be
1526 zero, but it's not common and zero-sized arrays are not
1527 sufficiently portable C. */
1528 struct attribute attrs[1];
1531 /* Get at parts of an attribute structure. */
1533 #define DW_STRING(attr) ((attr)->u.str)
1534 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1535 #define DW_UNSND(attr) ((attr)->u.unsnd)
1536 #define DW_BLOCK(attr) ((attr)->u.blk)
1537 #define DW_SND(attr) ((attr)->u.snd)
1538 #define DW_ADDR(attr) ((attr)->u.addr)
1539 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1541 /* Blocks are a bunch of untyped bytes. */
1546 /* Valid only if SIZE is not zero. */
1547 const gdb_byte *data;
1550 #ifndef ATTR_ALLOC_CHUNK
1551 #define ATTR_ALLOC_CHUNK 4
1554 /* Allocate fields for structs, unions and enums in this size. */
1555 #ifndef DW_FIELD_ALLOC_CHUNK
1556 #define DW_FIELD_ALLOC_CHUNK 4
1559 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1560 but this would require a corresponding change in unpack_field_as_long
1562 static int bits_per_byte = 8;
1566 struct nextfield *next;
1574 struct nextfnfield *next;
1575 struct fn_field fnfield;
1582 struct nextfnfield *head;
1585 struct decl_field_list
1587 struct decl_field field;
1588 struct decl_field_list *next;
1591 /* The routines that read and process dies for a C struct or C++ class
1592 pass lists of data member fields and lists of member function fields
1593 in an instance of a field_info structure, as defined below. */
1596 /* List of data member and baseclasses fields. */
1597 struct nextfield *fields, *baseclasses;
1599 /* Number of fields (including baseclasses). */
1602 /* Number of baseclasses. */
1605 /* Set if the accesibility of one of the fields is not public. */
1606 int non_public_fields;
1608 /* Member function fieldlist array, contains name of possibly overloaded
1609 member function, number of overloaded member functions and a pointer
1610 to the head of the member function field chain. */
1611 struct fnfieldlist *fnfieldlists;
1613 /* Number of entries in the fnfieldlists array. */
1616 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1617 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1618 struct decl_field_list *typedef_field_list;
1619 unsigned typedef_field_list_count;
1621 /* Nested types defined by this class and the number of elements in this
1623 struct decl_field_list *nested_types_list;
1624 unsigned nested_types_list_count;
1627 /* One item on the queue of compilation units to read in full symbols
1629 struct dwarf2_queue_item
1631 struct dwarf2_per_cu_data *per_cu;
1632 enum language pretend_language;
1633 struct dwarf2_queue_item *next;
1636 /* The current queue. */
1637 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1639 /* Loaded secondary compilation units are kept in memory until they
1640 have not been referenced for the processing of this many
1641 compilation units. Set this to zero to disable caching. Cache
1642 sizes of up to at least twenty will improve startup time for
1643 typical inter-CU-reference binaries, at an obvious memory cost. */
1644 static int dwarf_max_cache_age = 5;
1646 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1647 struct cmd_list_element *c, const char *value)
1649 fprintf_filtered (file, _("The upper bound on the age of cached "
1650 "DWARF compilation units is %s.\n"),
1654 /* local function prototypes */
1656 static const char *get_section_name (const struct dwarf2_section_info *);
1658 static const char *get_section_file_name (const struct dwarf2_section_info *);
1660 static void dwarf2_find_base_address (struct die_info *die,
1661 struct dwarf2_cu *cu);
1663 static struct partial_symtab *create_partial_symtab
1664 (struct dwarf2_per_cu_data *per_cu, const char *name);
1666 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1667 const gdb_byte *info_ptr,
1668 struct die_info *type_unit_die,
1669 int has_children, void *data);
1671 static void dwarf2_build_psymtabs_hard (struct objfile *);
1673 static void scan_partial_symbols (struct partial_die_info *,
1674 CORE_ADDR *, CORE_ADDR *,
1675 int, struct dwarf2_cu *);
1677 static void add_partial_symbol (struct partial_die_info *,
1678 struct dwarf2_cu *);
1680 static void add_partial_namespace (struct partial_die_info *pdi,
1681 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1682 int set_addrmap, struct dwarf2_cu *cu);
1684 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1685 CORE_ADDR *highpc, int set_addrmap,
1686 struct dwarf2_cu *cu);
1688 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1689 struct dwarf2_cu *cu);
1691 static void add_partial_subprogram (struct partial_die_info *pdi,
1692 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1693 int need_pc, struct dwarf2_cu *cu);
1695 static void dwarf2_read_symtab (struct partial_symtab *,
1698 static void psymtab_to_symtab_1 (struct partial_symtab *);
1700 static struct abbrev_info *abbrev_table_lookup_abbrev
1701 (const struct abbrev_table *, unsigned int);
1703 static struct abbrev_table *abbrev_table_read_table
1704 (struct dwarf2_section_info *, sect_offset);
1706 static void abbrev_table_free (struct abbrev_table *);
1708 static void abbrev_table_free_cleanup (void *);
1710 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1711 struct dwarf2_section_info *);
1713 static void dwarf2_free_abbrev_table (void *);
1715 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1717 static struct partial_die_info *load_partial_dies
1718 (const struct die_reader_specs *, const gdb_byte *, int);
1720 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1721 struct partial_die_info *,
1722 struct abbrev_info *,
1726 static struct partial_die_info *find_partial_die (sect_offset, int,
1727 struct dwarf2_cu *);
1729 static void fixup_partial_die (struct partial_die_info *,
1730 struct dwarf2_cu *);
1732 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1733 struct attribute *, struct attr_abbrev *,
1736 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1738 static int read_1_signed_byte (bfd *, const gdb_byte *);
1740 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1742 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1744 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1746 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1749 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1751 static LONGEST read_checked_initial_length_and_offset
1752 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1753 unsigned int *, unsigned int *);
1755 static LONGEST read_offset (bfd *, const gdb_byte *,
1756 const struct comp_unit_head *,
1759 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1761 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1764 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1766 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1768 static const char *read_indirect_string (bfd *, const gdb_byte *,
1769 const struct comp_unit_head *,
1772 static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1773 const struct comp_unit_head *,
1776 static const char *read_indirect_string_at_offset (bfd *abfd,
1777 LONGEST str_offset);
1779 static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1781 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1783 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1787 static const char *read_str_index (const struct die_reader_specs *reader,
1788 ULONGEST str_index);
1790 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1792 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1793 struct dwarf2_cu *);
1795 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1798 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1799 struct dwarf2_cu *cu);
1801 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1802 struct dwarf2_cu *cu);
1804 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1806 static struct die_info *die_specification (struct die_info *die,
1807 struct dwarf2_cu **);
1809 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1810 struct dwarf2_cu *cu);
1812 static void dwarf_decode_lines (struct line_header *, const char *,
1813 struct dwarf2_cu *, struct partial_symtab *,
1814 CORE_ADDR, int decode_mapping);
1816 static void dwarf2_start_subfile (const char *, const char *);
1818 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1819 const char *, const char *,
1822 static struct symbol *new_symbol (struct die_info *, struct type *,
1823 struct dwarf2_cu *);
1825 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1826 struct dwarf2_cu *, struct symbol *);
1828 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1829 struct dwarf2_cu *);
1831 static void dwarf2_const_value_attr (const struct attribute *attr,
1834 struct obstack *obstack,
1835 struct dwarf2_cu *cu, LONGEST *value,
1836 const gdb_byte **bytes,
1837 struct dwarf2_locexpr_baton **baton);
1839 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1841 static int need_gnat_info (struct dwarf2_cu *);
1843 static struct type *die_descriptive_type (struct die_info *,
1844 struct dwarf2_cu *);
1846 static void set_descriptive_type (struct type *, struct die_info *,
1847 struct dwarf2_cu *);
1849 static struct type *die_containing_type (struct die_info *,
1850 struct dwarf2_cu *);
1852 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1853 struct dwarf2_cu *);
1855 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1857 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1859 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1861 static char *typename_concat (struct obstack *obs, const char *prefix,
1862 const char *suffix, int physname,
1863 struct dwarf2_cu *cu);
1865 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1867 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1869 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1871 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1873 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1875 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1877 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1878 struct dwarf2_cu *, struct partial_symtab *);
1880 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1881 values. Keep the items ordered with increasing constraints compliance. */
1884 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1885 PC_BOUNDS_NOT_PRESENT,
1887 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1888 were present but they do not form a valid range of PC addresses. */
1891 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1894 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1898 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1899 CORE_ADDR *, CORE_ADDR *,
1901 struct partial_symtab *);
1903 static void get_scope_pc_bounds (struct die_info *,
1904 CORE_ADDR *, CORE_ADDR *,
1905 struct dwarf2_cu *);
1907 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1908 CORE_ADDR, struct dwarf2_cu *);
1910 static void dwarf2_add_field (struct field_info *, struct die_info *,
1911 struct dwarf2_cu *);
1913 static void dwarf2_attach_fields_to_type (struct field_info *,
1914 struct type *, struct dwarf2_cu *);
1916 static void dwarf2_add_member_fn (struct field_info *,
1917 struct die_info *, struct type *,
1918 struct dwarf2_cu *);
1920 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1922 struct dwarf2_cu *);
1924 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1926 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1928 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1930 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1932 static struct using_direct **using_directives (enum language);
1934 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1936 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1938 static struct type *read_module_type (struct die_info *die,
1939 struct dwarf2_cu *cu);
1941 static const char *namespace_name (struct die_info *die,
1942 int *is_anonymous, struct dwarf2_cu *);
1944 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1946 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1948 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1949 struct dwarf2_cu *);
1951 static struct die_info *read_die_and_siblings_1
1952 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1955 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1956 const gdb_byte *info_ptr,
1957 const gdb_byte **new_info_ptr,
1958 struct die_info *parent);
1960 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1961 struct die_info **, const gdb_byte *,
1964 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1965 struct die_info **, const gdb_byte *,
1968 static void process_die (struct die_info *, struct dwarf2_cu *);
1970 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1973 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1975 static const char *dwarf2_full_name (const char *name,
1976 struct die_info *die,
1977 struct dwarf2_cu *cu);
1979 static const char *dwarf2_physname (const char *name, struct die_info *die,
1980 struct dwarf2_cu *cu);
1982 static struct die_info *dwarf2_extension (struct die_info *die,
1983 struct dwarf2_cu **);
1985 static const char *dwarf_tag_name (unsigned int);
1987 static const char *dwarf_attr_name (unsigned int);
1989 static const char *dwarf_form_name (unsigned int);
1991 static const char *dwarf_bool_name (unsigned int);
1993 static const char *dwarf_type_encoding_name (unsigned int);
1995 static struct die_info *sibling_die (struct die_info *);
1997 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1999 static void dump_die_for_error (struct die_info *);
2001 static void dump_die_1 (struct ui_file *, int level, int max_level,
2004 /*static*/ void dump_die (struct die_info *, int max_level);
2006 static void store_in_ref_table (struct die_info *,
2007 struct dwarf2_cu *);
2009 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
2011 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
2013 static struct die_info *follow_die_ref_or_sig (struct die_info *,
2014 const struct attribute *,
2015 struct dwarf2_cu **);
2017 static struct die_info *follow_die_ref (struct die_info *,
2018 const struct attribute *,
2019 struct dwarf2_cu **);
2021 static struct die_info *follow_die_sig (struct die_info *,
2022 const struct attribute *,
2023 struct dwarf2_cu **);
2025 static struct type *get_signatured_type (struct die_info *, ULONGEST,
2026 struct dwarf2_cu *);
2028 static struct type *get_DW_AT_signature_type (struct die_info *,
2029 const struct attribute *,
2030 struct dwarf2_cu *);
2032 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
2034 static void read_signatured_type (struct signatured_type *);
2036 static int attr_to_dynamic_prop (const struct attribute *attr,
2037 struct die_info *die, struct dwarf2_cu *cu,
2038 struct dynamic_prop *prop);
2040 /* memory allocation interface */
2042 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
2044 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
2046 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2048 static int attr_form_is_block (const struct attribute *);
2050 static int attr_form_is_section_offset (const struct attribute *);
2052 static int attr_form_is_constant (const struct attribute *);
2054 static int attr_form_is_ref (const struct attribute *);
2056 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
2057 struct dwarf2_loclist_baton *baton,
2058 const struct attribute *attr);
2060 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
2062 struct dwarf2_cu *cu,
2065 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
2066 const gdb_byte *info_ptr,
2067 struct abbrev_info *abbrev);
2069 static void free_stack_comp_unit (void *);
2071 static hashval_t partial_die_hash (const void *item);
2073 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2075 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
2076 (sect_offset sect_off, unsigned int offset_in_dwz, struct objfile *objfile);
2078 static void init_one_comp_unit (struct dwarf2_cu *cu,
2079 struct dwarf2_per_cu_data *per_cu);
2081 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
2082 struct die_info *comp_unit_die,
2083 enum language pretend_language);
2085 static void free_heap_comp_unit (void *);
2087 static void free_cached_comp_units (void *);
2089 static void age_cached_comp_units (void);
2091 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
2093 static struct type *set_die_type (struct die_info *, struct type *,
2094 struct dwarf2_cu *);
2096 static void create_all_comp_units (struct objfile *);
2098 static int create_all_type_units (struct objfile *);
2100 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2103 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2106 static void process_full_type_unit (struct dwarf2_per_cu_data *,
2109 static void dwarf2_add_dependence (struct dwarf2_cu *,
2110 struct dwarf2_per_cu_data *);
2112 static void dwarf2_mark (struct dwarf2_cu *);
2114 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2116 static struct type *get_die_type_at_offset (sect_offset,
2117 struct dwarf2_per_cu_data *);
2119 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
2121 static void dwarf2_release_queue (void *dummy);
2123 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2124 enum language pretend_language);
2126 static void process_queue (void);
2128 /* The return type of find_file_and_directory. Note, the enclosed
2129 string pointers are only valid while this object is valid. */
2131 struct file_and_directory
2133 /* The filename. This is never NULL. */
2136 /* The compilation directory. NULL if not known. If we needed to
2137 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2138 points directly to the DW_AT_comp_dir string attribute owned by
2139 the obstack that owns the DIE. */
2140 const char *comp_dir;
2142 /* If we needed to build a new string for comp_dir, this is what
2143 owns the storage. */
2144 std::string comp_dir_storage;
2147 static file_and_directory find_file_and_directory (struct die_info *die,
2148 struct dwarf2_cu *cu);
2150 static char *file_full_name (int file, struct line_header *lh,
2151 const char *comp_dir);
2153 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
2154 enum class rcuh_kind { COMPILE, TYPE };
2156 static const gdb_byte *read_and_check_comp_unit_head
2157 (struct comp_unit_head *header,
2158 struct dwarf2_section_info *section,
2159 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
2160 rcuh_kind section_kind);
2162 static void init_cutu_and_read_dies
2163 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2164 int use_existing_cu, int keep,
2165 die_reader_func_ftype *die_reader_func, void *data);
2167 static void init_cutu_and_read_dies_simple
2168 (struct dwarf2_per_cu_data *this_cu,
2169 die_reader_func_ftype *die_reader_func, void *data);
2171 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2173 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2175 static struct dwo_unit *lookup_dwo_unit_in_dwp
2176 (struct dwp_file *dwp_file, const char *comp_dir,
2177 ULONGEST signature, int is_debug_types);
2179 static struct dwp_file *get_dwp_file (void);
2181 static struct dwo_unit *lookup_dwo_comp_unit
2182 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2184 static struct dwo_unit *lookup_dwo_type_unit
2185 (struct signatured_type *, const char *, const char *);
2187 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2189 static void free_dwo_file_cleanup (void *);
2191 static void process_cu_includes (void);
2193 static void check_producer (struct dwarf2_cu *cu);
2195 static void free_line_header_voidp (void *arg);
2197 /* Various complaints about symbol reading that don't abort the process. */
2200 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2202 complaint (&symfile_complaints,
2203 _("statement list doesn't fit in .debug_line section"));
2207 dwarf2_debug_line_missing_file_complaint (void)
2209 complaint (&symfile_complaints,
2210 _(".debug_line section has line data without a file"));
2214 dwarf2_debug_line_missing_end_sequence_complaint (void)
2216 complaint (&symfile_complaints,
2217 _(".debug_line section has line "
2218 "program sequence without an end"));
2222 dwarf2_complex_location_expr_complaint (void)
2224 complaint (&symfile_complaints, _("location expression too complex"));
2228 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2231 complaint (&symfile_complaints,
2232 _("const value length mismatch for '%s', got %d, expected %d"),
2237 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2239 complaint (&symfile_complaints,
2240 _("debug info runs off end of %s section"
2242 get_section_name (section),
2243 get_section_file_name (section));
2247 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2249 complaint (&symfile_complaints,
2250 _("macro debug info contains a "
2251 "malformed macro definition:\n`%s'"),
2256 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2258 complaint (&symfile_complaints,
2259 _("invalid attribute class or form for '%s' in '%s'"),
2263 /* Hash function for line_header_hash. */
2266 line_header_hash (const struct line_header *ofs)
2268 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2271 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2274 line_header_hash_voidp (const void *item)
2276 const struct line_header *ofs = (const struct line_header *) item;
2278 return line_header_hash (ofs);
2281 /* Equality function for line_header_hash. */
2284 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2286 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2287 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2289 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2290 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2295 /* Read the given attribute value as an address, taking the attribute's
2296 form into account. */
2299 attr_value_as_address (struct attribute *attr)
2303 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2305 /* Aside from a few clearly defined exceptions, attributes that
2306 contain an address must always be in DW_FORM_addr form.
2307 Unfortunately, some compilers happen to be violating this
2308 requirement by encoding addresses using other forms, such
2309 as DW_FORM_data4 for example. For those broken compilers,
2310 we try to do our best, without any guarantee of success,
2311 to interpret the address correctly. It would also be nice
2312 to generate a complaint, but that would require us to maintain
2313 a list of legitimate cases where a non-address form is allowed,
2314 as well as update callers to pass in at least the CU's DWARF
2315 version. This is more overhead than what we're willing to
2316 expand for a pretty rare case. */
2317 addr = DW_UNSND (attr);
2320 addr = DW_ADDR (attr);
2325 /* The suffix for an index file. */
2326 #define INDEX4_SUFFIX ".gdb-index"
2327 #define INDEX5_SUFFIX ".debug_names"
2328 #define DEBUG_STR_SUFFIX ".debug_str"
2330 /* See declaration. */
2332 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2333 const dwarf2_debug_sections *names)
2334 : objfile (objfile_)
2337 names = &dwarf2_elf_names;
2339 bfd *obfd = objfile->obfd;
2341 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2342 locate_sections (obfd, sec, *names);
2345 dwarf2_per_objfile::~dwarf2_per_objfile ()
2347 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2348 free_cached_comp_units ();
2350 if (quick_file_names_table)
2351 htab_delete (quick_file_names_table);
2353 if (line_header_hash)
2354 htab_delete (line_header_hash);
2356 /* Everything else should be on the objfile obstack. */
2359 /* See declaration. */
2362 dwarf2_per_objfile::free_cached_comp_units ()
2364 dwarf2_per_cu_data *per_cu = read_in_chain;
2365 dwarf2_per_cu_data **last_chain = &read_in_chain;
2366 while (per_cu != NULL)
2368 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2370 free_heap_comp_unit (per_cu->cu);
2371 *last_chain = next_cu;
2376 /* Try to locate the sections we need for DWARF 2 debugging
2377 information and return true if we have enough to do something.
2378 NAMES points to the dwarf2 section names, or is NULL if the standard
2379 ELF names are used. */
2382 dwarf2_has_info (struct objfile *objfile,
2383 const struct dwarf2_debug_sections *names)
2385 if (objfile->flags & OBJF_READNEVER)
2388 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2389 objfile_data (objfile, dwarf2_objfile_data_key));
2390 if (!dwarf2_per_objfile)
2392 /* Initialize per-objfile state. */
2393 struct dwarf2_per_objfile *data
2394 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
2396 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
2397 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
2399 return (!dwarf2_per_objfile->info.is_virtual
2400 && dwarf2_per_objfile->info.s.section != NULL
2401 && !dwarf2_per_objfile->abbrev.is_virtual
2402 && dwarf2_per_objfile->abbrev.s.section != NULL);
2405 /* Return the containing section of virtual section SECTION. */
2407 static struct dwarf2_section_info *
2408 get_containing_section (const struct dwarf2_section_info *section)
2410 gdb_assert (section->is_virtual);
2411 return section->s.containing_section;
2414 /* Return the bfd owner of SECTION. */
2417 get_section_bfd_owner (const struct dwarf2_section_info *section)
2419 if (section->is_virtual)
2421 section = get_containing_section (section);
2422 gdb_assert (!section->is_virtual);
2424 return section->s.section->owner;
2427 /* Return the bfd section of SECTION.
2428 Returns NULL if the section is not present. */
2431 get_section_bfd_section (const struct dwarf2_section_info *section)
2433 if (section->is_virtual)
2435 section = get_containing_section (section);
2436 gdb_assert (!section->is_virtual);
2438 return section->s.section;
2441 /* Return the name of SECTION. */
2444 get_section_name (const struct dwarf2_section_info *section)
2446 asection *sectp = get_section_bfd_section (section);
2448 gdb_assert (sectp != NULL);
2449 return bfd_section_name (get_section_bfd_owner (section), sectp);
2452 /* Return the name of the file SECTION is in. */
2455 get_section_file_name (const struct dwarf2_section_info *section)
2457 bfd *abfd = get_section_bfd_owner (section);
2459 return bfd_get_filename (abfd);
2462 /* Return the id of SECTION.
2463 Returns 0 if SECTION doesn't exist. */
2466 get_section_id (const struct dwarf2_section_info *section)
2468 asection *sectp = get_section_bfd_section (section);
2475 /* Return the flags of SECTION.
2476 SECTION (or containing section if this is a virtual section) must exist. */
2479 get_section_flags (const struct dwarf2_section_info *section)
2481 asection *sectp = get_section_bfd_section (section);
2483 gdb_assert (sectp != NULL);
2484 return bfd_get_section_flags (sectp->owner, sectp);
2487 /* When loading sections, we look either for uncompressed section or for
2488 compressed section names. */
2491 section_is_p (const char *section_name,
2492 const struct dwarf2_section_names *names)
2494 if (names->normal != NULL
2495 && strcmp (section_name, names->normal) == 0)
2497 if (names->compressed != NULL
2498 && strcmp (section_name, names->compressed) == 0)
2503 /* See declaration. */
2506 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2507 const dwarf2_debug_sections &names)
2509 flagword aflag = bfd_get_section_flags (abfd, sectp);
2511 if ((aflag & SEC_HAS_CONTENTS) == 0)
2514 else if (section_is_p (sectp->name, &names.info))
2516 this->info.s.section = sectp;
2517 this->info.size = bfd_get_section_size (sectp);
2519 else if (section_is_p (sectp->name, &names.abbrev))
2521 this->abbrev.s.section = sectp;
2522 this->abbrev.size = bfd_get_section_size (sectp);
2524 else if (section_is_p (sectp->name, &names.line))
2526 this->line.s.section = sectp;
2527 this->line.size = bfd_get_section_size (sectp);
2529 else if (section_is_p (sectp->name, &names.loc))
2531 this->loc.s.section = sectp;
2532 this->loc.size = bfd_get_section_size (sectp);
2534 else if (section_is_p (sectp->name, &names.loclists))
2536 this->loclists.s.section = sectp;
2537 this->loclists.size = bfd_get_section_size (sectp);
2539 else if (section_is_p (sectp->name, &names.macinfo))
2541 this->macinfo.s.section = sectp;
2542 this->macinfo.size = bfd_get_section_size (sectp);
2544 else if (section_is_p (sectp->name, &names.macro))
2546 this->macro.s.section = sectp;
2547 this->macro.size = bfd_get_section_size (sectp);
2549 else if (section_is_p (sectp->name, &names.str))
2551 this->str.s.section = sectp;
2552 this->str.size = bfd_get_section_size (sectp);
2554 else if (section_is_p (sectp->name, &names.line_str))
2556 this->line_str.s.section = sectp;
2557 this->line_str.size = bfd_get_section_size (sectp);
2559 else if (section_is_p (sectp->name, &names.addr))
2561 this->addr.s.section = sectp;
2562 this->addr.size = bfd_get_section_size (sectp);
2564 else if (section_is_p (sectp->name, &names.frame))
2566 this->frame.s.section = sectp;
2567 this->frame.size = bfd_get_section_size (sectp);
2569 else if (section_is_p (sectp->name, &names.eh_frame))
2571 this->eh_frame.s.section = sectp;
2572 this->eh_frame.size = bfd_get_section_size (sectp);
2574 else if (section_is_p (sectp->name, &names.ranges))
2576 this->ranges.s.section = sectp;
2577 this->ranges.size = bfd_get_section_size (sectp);
2579 else if (section_is_p (sectp->name, &names.rnglists))
2581 this->rnglists.s.section = sectp;
2582 this->rnglists.size = bfd_get_section_size (sectp);
2584 else if (section_is_p (sectp->name, &names.types))
2586 struct dwarf2_section_info type_section;
2588 memset (&type_section, 0, sizeof (type_section));
2589 type_section.s.section = sectp;
2590 type_section.size = bfd_get_section_size (sectp);
2592 VEC_safe_push (dwarf2_section_info_def, this->types,
2595 else if (section_is_p (sectp->name, &names.gdb_index))
2597 this->gdb_index.s.section = sectp;
2598 this->gdb_index.size = bfd_get_section_size (sectp);
2600 else if (section_is_p (sectp->name, &names.debug_names))
2602 this->debug_names.s.section = sectp;
2603 this->debug_names.size = bfd_get_section_size (sectp);
2605 else if (section_is_p (sectp->name, &names.debug_aranges))
2607 this->debug_aranges.s.section = sectp;
2608 this->debug_aranges.size = bfd_get_section_size (sectp);
2611 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2612 && bfd_section_vma (abfd, sectp) == 0)
2613 this->has_section_at_zero = true;
2616 /* A helper function that decides whether a section is empty,
2620 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2622 if (section->is_virtual)
2623 return section->size == 0;
2624 return section->s.section == NULL || section->size == 0;
2627 /* Read the contents of the section INFO.
2628 OBJFILE is the main object file, but not necessarily the file where
2629 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2631 If the section is compressed, uncompress it before returning. */
2634 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2638 gdb_byte *buf, *retbuf;
2642 info->buffer = NULL;
2645 if (dwarf2_section_empty_p (info))
2648 sectp = get_section_bfd_section (info);
2650 /* If this is a virtual section we need to read in the real one first. */
2651 if (info->is_virtual)
2653 struct dwarf2_section_info *containing_section =
2654 get_containing_section (info);
2656 gdb_assert (sectp != NULL);
2657 if ((sectp->flags & SEC_RELOC) != 0)
2659 error (_("Dwarf Error: DWP format V2 with relocations is not"
2660 " supported in section %s [in module %s]"),
2661 get_section_name (info), get_section_file_name (info));
2663 dwarf2_read_section (objfile, containing_section);
2664 /* Other code should have already caught virtual sections that don't
2666 gdb_assert (info->virtual_offset + info->size
2667 <= containing_section->size);
2668 /* If the real section is empty or there was a problem reading the
2669 section we shouldn't get here. */
2670 gdb_assert (containing_section->buffer != NULL);
2671 info->buffer = containing_section->buffer + info->virtual_offset;
2675 /* If the section has relocations, we must read it ourselves.
2676 Otherwise we attach it to the BFD. */
2677 if ((sectp->flags & SEC_RELOC) == 0)
2679 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2683 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2686 /* When debugging .o files, we may need to apply relocations; see
2687 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2688 We never compress sections in .o files, so we only need to
2689 try this when the section is not compressed. */
2690 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2693 info->buffer = retbuf;
2697 abfd = get_section_bfd_owner (info);
2698 gdb_assert (abfd != NULL);
2700 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2701 || bfd_bread (buf, info->size, abfd) != info->size)
2703 error (_("Dwarf Error: Can't read DWARF data"
2704 " in section %s [in module %s]"),
2705 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2709 /* A helper function that returns the size of a section in a safe way.
2710 If you are positive that the section has been read before using the
2711 size, then it is safe to refer to the dwarf2_section_info object's
2712 "size" field directly. In other cases, you must call this
2713 function, because for compressed sections the size field is not set
2714 correctly until the section has been read. */
2716 static bfd_size_type
2717 dwarf2_section_size (struct objfile *objfile,
2718 struct dwarf2_section_info *info)
2721 dwarf2_read_section (objfile, info);
2725 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2729 dwarf2_get_section_info (struct objfile *objfile,
2730 enum dwarf2_section_enum sect,
2731 asection **sectp, const gdb_byte **bufp,
2732 bfd_size_type *sizep)
2734 struct dwarf2_per_objfile *data
2735 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2736 dwarf2_objfile_data_key);
2737 struct dwarf2_section_info *info;
2739 /* We may see an objfile without any DWARF, in which case we just
2750 case DWARF2_DEBUG_FRAME:
2751 info = &data->frame;
2753 case DWARF2_EH_FRAME:
2754 info = &data->eh_frame;
2757 gdb_assert_not_reached ("unexpected section");
2760 dwarf2_read_section (objfile, info);
2762 *sectp = get_section_bfd_section (info);
2763 *bufp = info->buffer;
2764 *sizep = info->size;
2767 /* A helper function to find the sections for a .dwz file. */
2770 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2772 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2774 /* Note that we only support the standard ELF names, because .dwz
2775 is ELF-only (at the time of writing). */
2776 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2778 dwz_file->abbrev.s.section = sectp;
2779 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2781 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2783 dwz_file->info.s.section = sectp;
2784 dwz_file->info.size = bfd_get_section_size (sectp);
2786 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2788 dwz_file->str.s.section = sectp;
2789 dwz_file->str.size = bfd_get_section_size (sectp);
2791 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2793 dwz_file->line.s.section = sectp;
2794 dwz_file->line.size = bfd_get_section_size (sectp);
2796 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2798 dwz_file->macro.s.section = sectp;
2799 dwz_file->macro.size = bfd_get_section_size (sectp);
2801 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2803 dwz_file->gdb_index.s.section = sectp;
2804 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2806 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2808 dwz_file->debug_names.s.section = sectp;
2809 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2813 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2814 there is no .gnu_debugaltlink section in the file. Error if there
2815 is such a section but the file cannot be found. */
2817 static struct dwz_file *
2818 dwarf2_get_dwz_file (void)
2820 const char *filename;
2821 struct dwz_file *result;
2822 bfd_size_type buildid_len_arg;
2826 if (dwarf2_per_objfile->dwz_file != NULL)
2827 return dwarf2_per_objfile->dwz_file;
2829 bfd_set_error (bfd_error_no_error);
2830 gdb::unique_xmalloc_ptr<char> data
2831 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2832 &buildid_len_arg, &buildid));
2835 if (bfd_get_error () == bfd_error_no_error)
2837 error (_("could not read '.gnu_debugaltlink' section: %s"),
2838 bfd_errmsg (bfd_get_error ()));
2841 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2843 buildid_len = (size_t) buildid_len_arg;
2845 filename = data.get ();
2847 std::string abs_storage;
2848 if (!IS_ABSOLUTE_PATH (filename))
2850 gdb::unique_xmalloc_ptr<char> abs
2851 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2853 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2854 filename = abs_storage.c_str ();
2857 /* First try the file name given in the section. If that doesn't
2858 work, try to use the build-id instead. */
2859 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2860 if (dwz_bfd != NULL)
2862 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2866 if (dwz_bfd == NULL)
2867 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2869 if (dwz_bfd == NULL)
2870 error (_("could not find '.gnu_debugaltlink' file for %s"),
2871 objfile_name (dwarf2_per_objfile->objfile));
2873 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2875 result->dwz_bfd = dwz_bfd.release ();
2877 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
2879 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
2880 dwarf2_per_objfile->dwz_file = result;
2884 /* DWARF quick_symbols_functions support. */
2886 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2887 unique line tables, so we maintain a separate table of all .debug_line
2888 derived entries to support the sharing.
2889 All the quick functions need is the list of file names. We discard the
2890 line_header when we're done and don't need to record it here. */
2891 struct quick_file_names
2893 /* The data used to construct the hash key. */
2894 struct stmt_list_hash hash;
2896 /* The number of entries in file_names, real_names. */
2897 unsigned int num_file_names;
2899 /* The file names from the line table, after being run through
2901 const char **file_names;
2903 /* The file names from the line table after being run through
2904 gdb_realpath. These are computed lazily. */
2905 const char **real_names;
2908 /* When using the index (and thus not using psymtabs), each CU has an
2909 object of this type. This is used to hold information needed by
2910 the various "quick" methods. */
2911 struct dwarf2_per_cu_quick_data
2913 /* The file table. This can be NULL if there was no file table
2914 or it's currently not read in.
2915 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2916 struct quick_file_names *file_names;
2918 /* The corresponding symbol table. This is NULL if symbols for this
2919 CU have not yet been read. */
2920 struct compunit_symtab *compunit_symtab;
2922 /* A temporary mark bit used when iterating over all CUs in
2923 expand_symtabs_matching. */
2924 unsigned int mark : 1;
2926 /* True if we've tried to read the file table and found there isn't one.
2927 There will be no point in trying to read it again next time. */
2928 unsigned int no_file_data : 1;
2931 /* Utility hash function for a stmt_list_hash. */
2934 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2938 if (stmt_list_hash->dwo_unit != NULL)
2939 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2940 v += to_underlying (stmt_list_hash->line_sect_off);
2944 /* Utility equality function for a stmt_list_hash. */
2947 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2948 const struct stmt_list_hash *rhs)
2950 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2952 if (lhs->dwo_unit != NULL
2953 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2956 return lhs->line_sect_off == rhs->line_sect_off;
2959 /* Hash function for a quick_file_names. */
2962 hash_file_name_entry (const void *e)
2964 const struct quick_file_names *file_data
2965 = (const struct quick_file_names *) e;
2967 return hash_stmt_list_entry (&file_data->hash);
2970 /* Equality function for a quick_file_names. */
2973 eq_file_name_entry (const void *a, const void *b)
2975 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2976 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2978 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2981 /* Delete function for a quick_file_names. */
2984 delete_file_name_entry (void *e)
2986 struct quick_file_names *file_data = (struct quick_file_names *) e;
2989 for (i = 0; i < file_data->num_file_names; ++i)
2991 xfree ((void*) file_data->file_names[i]);
2992 if (file_data->real_names)
2993 xfree ((void*) file_data->real_names[i]);
2996 /* The space for the struct itself lives on objfile_obstack,
2997 so we don't free it here. */
3000 /* Create a quick_file_names hash table. */
3003 create_quick_file_names_table (unsigned int nr_initial_entries)
3005 return htab_create_alloc (nr_initial_entries,
3006 hash_file_name_entry, eq_file_name_entry,
3007 delete_file_name_entry, xcalloc, xfree);
3010 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
3011 have to be created afterwards. You should call age_cached_comp_units after
3012 processing PER_CU->CU. dw2_setup must have been already called. */
3015 load_cu (struct dwarf2_per_cu_data *per_cu)
3017 if (per_cu->is_debug_types)
3018 load_full_type_unit (per_cu);
3020 load_full_comp_unit (per_cu, language_minimal);
3022 if (per_cu->cu == NULL)
3023 return; /* Dummy CU. */
3025 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
3028 /* Read in the symbols for PER_CU. */
3031 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3033 struct cleanup *back_to;
3035 /* Skip type_unit_groups, reading the type units they contain
3036 is handled elsewhere. */
3037 if (IS_TYPE_UNIT_GROUP (per_cu))
3040 back_to = make_cleanup (dwarf2_release_queue, NULL);
3042 if (dwarf2_per_objfile->using_index
3043 ? per_cu->v.quick->compunit_symtab == NULL
3044 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
3046 queue_comp_unit (per_cu, language_minimal);
3049 /* If we just loaded a CU from a DWO, and we're working with an index
3050 that may badly handle TUs, load all the TUs in that DWO as well.
3051 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
3052 if (!per_cu->is_debug_types
3053 && per_cu->cu != NULL
3054 && per_cu->cu->dwo_unit != NULL
3055 && dwarf2_per_objfile->index_table != NULL
3056 && dwarf2_per_objfile->index_table->version <= 7
3057 /* DWP files aren't supported yet. */
3058 && get_dwp_file () == NULL)
3059 queue_and_load_all_dwo_tus (per_cu);
3064 /* Age the cache, releasing compilation units that have not
3065 been used recently. */
3066 age_cached_comp_units ();
3068 do_cleanups (back_to);
3071 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
3072 the objfile from which this CU came. Returns the resulting symbol
3075 static struct compunit_symtab *
3076 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3078 gdb_assert (dwarf2_per_objfile->using_index);
3079 if (!per_cu->v.quick->compunit_symtab)
3081 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
3082 scoped_restore decrementer = increment_reading_symtab ();
3083 dw2_do_instantiate_symtab (per_cu);
3084 process_cu_includes ();
3085 do_cleanups (back_to);
3088 return per_cu->v.quick->compunit_symtab;
3091 /* Return the CU/TU given its index.
3093 This is intended for loops like:
3095 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3096 + dwarf2_per_objfile->n_type_units); ++i)
3098 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3104 static struct dwarf2_per_cu_data *
3105 dw2_get_cutu (int index)
3107 if (index >= dwarf2_per_objfile->n_comp_units)
3109 index -= dwarf2_per_objfile->n_comp_units;
3110 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3111 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
3114 return dwarf2_per_objfile->all_comp_units[index];
3117 /* Return the CU given its index.
3118 This differs from dw2_get_cutu in that it's for when you know INDEX
3121 static struct dwarf2_per_cu_data *
3122 dw2_get_cu (int index)
3124 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
3126 return dwarf2_per_objfile->all_comp_units[index];
3129 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3130 objfile_obstack, and constructed with the specified field
3133 static dwarf2_per_cu_data *
3134 create_cu_from_index_list (struct objfile *objfile,
3135 struct dwarf2_section_info *section,
3137 sect_offset sect_off, ULONGEST length)
3139 dwarf2_per_cu_data *the_cu
3140 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3141 struct dwarf2_per_cu_data);
3142 the_cu->sect_off = sect_off;
3143 the_cu->length = length;
3144 the_cu->objfile = objfile;
3145 the_cu->section = section;
3146 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3147 struct dwarf2_per_cu_quick_data);
3148 the_cu->is_dwz = is_dwz;
3152 /* A helper for create_cus_from_index that handles a given list of
3156 create_cus_from_index_list (struct objfile *objfile,
3157 const gdb_byte *cu_list, offset_type n_elements,
3158 struct dwarf2_section_info *section,
3164 for (i = 0; i < n_elements; i += 2)
3166 gdb_static_assert (sizeof (ULONGEST) >= 8);
3168 sect_offset sect_off
3169 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3170 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3173 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
3174 = create_cu_from_index_list (objfile, section, is_dwz, sect_off, length);
3178 /* Read the CU list from the mapped index, and use it to create all
3179 the CU objects for this objfile. */
3182 create_cus_from_index (struct objfile *objfile,
3183 const gdb_byte *cu_list, offset_type cu_list_elements,
3184 const gdb_byte *dwz_list, offset_type dwz_elements)
3186 struct dwz_file *dwz;
3188 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
3189 dwarf2_per_objfile->all_comp_units =
3190 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3191 dwarf2_per_objfile->n_comp_units);
3193 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3194 &dwarf2_per_objfile->info, 0, 0);
3196 if (dwz_elements == 0)
3199 dwz = dwarf2_get_dwz_file ();
3200 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3201 cu_list_elements / 2);
3204 /* Create the signatured type hash table from the index. */
3207 create_signatured_type_table_from_index (struct objfile *objfile,
3208 struct dwarf2_section_info *section,
3209 const gdb_byte *bytes,
3210 offset_type elements)
3213 htab_t sig_types_hash;
3215 dwarf2_per_objfile->n_type_units
3216 = dwarf2_per_objfile->n_allocated_type_units
3218 dwarf2_per_objfile->all_type_units =
3219 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3221 sig_types_hash = allocate_signatured_type_table (objfile);
3223 for (i = 0; i < elements; i += 3)
3225 struct signatured_type *sig_type;
3228 cu_offset type_offset_in_tu;
3230 gdb_static_assert (sizeof (ULONGEST) >= 8);
3231 sect_offset sect_off
3232 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3234 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3236 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3239 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3240 struct signatured_type);
3241 sig_type->signature = signature;
3242 sig_type->type_offset_in_tu = type_offset_in_tu;
3243 sig_type->per_cu.is_debug_types = 1;
3244 sig_type->per_cu.section = section;
3245 sig_type->per_cu.sect_off = sect_off;
3246 sig_type->per_cu.objfile = objfile;
3247 sig_type->per_cu.v.quick
3248 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3249 struct dwarf2_per_cu_quick_data);
3251 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3254 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3257 dwarf2_per_objfile->signatured_types = sig_types_hash;
3260 /* Create the signatured type hash table from .debug_names. */
3263 create_signatured_type_table_from_debug_names
3264 (struct objfile *objfile,
3265 const mapped_debug_names &map,
3266 struct dwarf2_section_info *section,
3267 struct dwarf2_section_info *abbrev_section)
3269 dwarf2_read_section (objfile, section);
3270 dwarf2_read_section (objfile, abbrev_section);
3272 dwarf2_per_objfile->n_type_units
3273 = dwarf2_per_objfile->n_allocated_type_units
3275 dwarf2_per_objfile->all_type_units
3276 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3278 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3280 for (uint32_t i = 0; i < map.tu_count; ++i)
3282 struct signatured_type *sig_type;
3285 cu_offset type_offset_in_tu;
3287 sect_offset sect_off
3288 = (sect_offset) (extract_unsigned_integer
3289 (map.tu_table_reordered + i * map.offset_size,
3291 map.dwarf5_byte_order));
3293 comp_unit_head cu_header;
3294 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
3295 section->buffer + to_underlying (sect_off),
3298 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3299 struct signatured_type);
3300 sig_type->signature = cu_header.signature;
3301 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3302 sig_type->per_cu.is_debug_types = 1;
3303 sig_type->per_cu.section = section;
3304 sig_type->per_cu.sect_off = sect_off;
3305 sig_type->per_cu.objfile = objfile;
3306 sig_type->per_cu.v.quick
3307 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3308 struct dwarf2_per_cu_quick_data);
3310 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3313 dwarf2_per_objfile->all_type_units[i] = sig_type;
3316 dwarf2_per_objfile->signatured_types = sig_types_hash;
3319 /* Read the address map data from the mapped index, and use it to
3320 populate the objfile's psymtabs_addrmap. */
3323 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3325 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3326 const gdb_byte *iter, *end;
3327 struct addrmap *mutable_map;
3330 auto_obstack temp_obstack;
3332 mutable_map = addrmap_create_mutable (&temp_obstack);
3334 iter = index->address_table;
3335 end = iter + index->address_table_size;
3337 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3341 ULONGEST hi, lo, cu_index;
3342 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3344 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3346 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3351 complaint (&symfile_complaints,
3352 _(".gdb_index address table has invalid range (%s - %s)"),
3353 hex_string (lo), hex_string (hi));
3357 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3359 complaint (&symfile_complaints,
3360 _(".gdb_index address table has invalid CU number %u"),
3361 (unsigned) cu_index);
3365 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3366 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3367 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
3370 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3371 &objfile->objfile_obstack);
3374 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3375 populate the objfile's psymtabs_addrmap. */
3378 create_addrmap_from_aranges (struct objfile *objfile,
3379 struct dwarf2_section_info *section)
3381 bfd *abfd = objfile->obfd;
3382 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3383 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3384 SECT_OFF_TEXT (objfile));
3386 auto_obstack temp_obstack;
3387 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3389 std::unordered_map<sect_offset,
3390 dwarf2_per_cu_data *,
3391 gdb::hash_enum<sect_offset>>
3392 debug_info_offset_to_per_cu;
3393 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3395 dwarf2_per_cu_data *per_cu = dw2_get_cutu (cui);
3396 const auto insertpair
3397 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3398 if (!insertpair.second)
3400 warning (_("Section .debug_aranges in %s has duplicate "
3401 "debug_info_offset %u, ignoring .debug_aranges."),
3402 objfile_name (objfile), to_underlying (per_cu->sect_off));
3407 dwarf2_read_section (objfile, section);
3409 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3411 const gdb_byte *addr = section->buffer;
3413 while (addr < section->buffer + section->size)
3415 const gdb_byte *const entry_addr = addr;
3416 unsigned int bytes_read;
3418 const LONGEST entry_length = read_initial_length (abfd, addr,
3422 const gdb_byte *const entry_end = addr + entry_length;
3423 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3424 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3425 if (addr + entry_length > section->buffer + section->size)
3427 warning (_("Section .debug_aranges in %s entry at offset %zu "
3428 "length %s exceeds section length %s, "
3429 "ignoring .debug_aranges."),
3430 objfile_name (objfile), entry_addr - section->buffer,
3431 plongest (bytes_read + entry_length),
3432 pulongest (section->size));
3436 /* The version number. */
3437 const uint16_t version = read_2_bytes (abfd, addr);
3441 warning (_("Section .debug_aranges in %s entry at offset %zu "
3442 "has unsupported version %d, ignoring .debug_aranges."),
3443 objfile_name (objfile), entry_addr - section->buffer,
3448 const uint64_t debug_info_offset
3449 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3450 addr += offset_size;
3451 const auto per_cu_it
3452 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3453 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3455 warning (_("Section .debug_aranges in %s entry at offset %zu "
3456 "debug_info_offset %s does not exists, "
3457 "ignoring .debug_aranges."),
3458 objfile_name (objfile), entry_addr - section->buffer,
3459 pulongest (debug_info_offset));
3462 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3464 const uint8_t address_size = *addr++;
3465 if (address_size < 1 || address_size > 8)
3467 warning (_("Section .debug_aranges in %s entry at offset %zu "
3468 "address_size %u is invalid, ignoring .debug_aranges."),
3469 objfile_name (objfile), entry_addr - section->buffer,
3474 const uint8_t segment_selector_size = *addr++;
3475 if (segment_selector_size != 0)
3477 warning (_("Section .debug_aranges in %s entry at offset %zu "
3478 "segment_selector_size %u is not supported, "
3479 "ignoring .debug_aranges."),
3480 objfile_name (objfile), entry_addr - section->buffer,
3481 segment_selector_size);
3485 /* Must pad to an alignment boundary that is twice the address
3486 size. It is undocumented by the DWARF standard but GCC does
3488 for (size_t padding = ((-(addr - section->buffer))
3489 & (2 * address_size - 1));
3490 padding > 0; padding--)
3493 warning (_("Section .debug_aranges in %s entry at offset %zu "
3494 "padding is not zero, ignoring .debug_aranges."),
3495 objfile_name (objfile), entry_addr - section->buffer);
3501 if (addr + 2 * address_size > entry_end)
3503 warning (_("Section .debug_aranges in %s entry at offset %zu "
3504 "address list is not properly terminated, "
3505 "ignoring .debug_aranges."),
3506 objfile_name (objfile), entry_addr - section->buffer);
3509 ULONGEST start = extract_unsigned_integer (addr, address_size,
3511 addr += address_size;
3512 ULONGEST length = extract_unsigned_integer (addr, address_size,
3514 addr += address_size;
3515 if (start == 0 && length == 0)
3517 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3519 /* Symbol was eliminated due to a COMDAT group. */
3522 ULONGEST end = start + length;
3523 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3524 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3525 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3529 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3530 &objfile->objfile_obstack);
3533 /* The hash function for strings in the mapped index. This is the same as
3534 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3535 implementation. This is necessary because the hash function is tied to the
3536 format of the mapped index file. The hash values do not have to match with
3539 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3542 mapped_index_string_hash (int index_version, const void *p)
3544 const unsigned char *str = (const unsigned char *) p;
3548 while ((c = *str++) != 0)
3550 if (index_version >= 5)
3552 r = r * 67 + c - 113;
3558 /* Find a slot in the mapped index INDEX for the object named NAME.
3559 If NAME is found, set *VEC_OUT to point to the CU vector in the
3560 constant pool and return true. If NAME cannot be found, return
3564 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3565 offset_type **vec_out)
3568 offset_type slot, step;
3569 int (*cmp) (const char *, const char *);
3571 gdb::unique_xmalloc_ptr<char> without_params;
3572 if (current_language->la_language == language_cplus
3573 || current_language->la_language == language_fortran
3574 || current_language->la_language == language_d)
3576 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3579 if (strchr (name, '(') != NULL)
3581 without_params = cp_remove_params (name);
3583 if (without_params != NULL)
3584 name = without_params.get ();
3588 /* Index version 4 did not support case insensitive searches. But the
3589 indices for case insensitive languages are built in lowercase, therefore
3590 simulate our NAME being searched is also lowercased. */
3591 hash = mapped_index_string_hash ((index->version == 4
3592 && case_sensitivity == case_sensitive_off
3593 ? 5 : index->version),
3596 slot = hash & (index->symbol_table_slots - 1);
3597 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
3598 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3602 /* Convert a slot number to an offset into the table. */
3603 offset_type i = 2 * slot;
3605 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
3608 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
3609 if (!cmp (name, str))
3611 *vec_out = (offset_type *) (index->constant_pool
3612 + MAYBE_SWAP (index->symbol_table[i + 1]));
3616 slot = (slot + step) & (index->symbol_table_slots - 1);
3620 /* A helper function that reads the .gdb_index from SECTION and fills
3621 in MAP. FILENAME is the name of the file containing the section;
3622 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3623 ok to use deprecated sections.
3625 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3626 out parameters that are filled in with information about the CU and
3627 TU lists in the section.
3629 Returns 1 if all went well, 0 otherwise. */
3632 read_index_from_section (struct objfile *objfile,
3633 const char *filename,
3635 struct dwarf2_section_info *section,
3636 struct mapped_index *map,
3637 const gdb_byte **cu_list,
3638 offset_type *cu_list_elements,
3639 const gdb_byte **types_list,
3640 offset_type *types_list_elements)
3642 const gdb_byte *addr;
3643 offset_type version;
3644 offset_type *metadata;
3647 if (dwarf2_section_empty_p (section))
3650 /* Older elfutils strip versions could keep the section in the main
3651 executable while splitting it for the separate debug info file. */
3652 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3655 dwarf2_read_section (objfile, section);
3657 addr = section->buffer;
3658 /* Version check. */
3659 version = MAYBE_SWAP (*(offset_type *) addr);
3660 /* Versions earlier than 3 emitted every copy of a psymbol. This
3661 causes the index to behave very poorly for certain requests. Version 3
3662 contained incomplete addrmap. So, it seems better to just ignore such
3666 static int warning_printed = 0;
3667 if (!warning_printed)
3669 warning (_("Skipping obsolete .gdb_index section in %s."),
3671 warning_printed = 1;
3675 /* Index version 4 uses a different hash function than index version
3678 Versions earlier than 6 did not emit psymbols for inlined
3679 functions. Using these files will cause GDB not to be able to
3680 set breakpoints on inlined functions by name, so we ignore these
3681 indices unless the user has done
3682 "set use-deprecated-index-sections on". */
3683 if (version < 6 && !deprecated_ok)
3685 static int warning_printed = 0;
3686 if (!warning_printed)
3689 Skipping deprecated .gdb_index section in %s.\n\
3690 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3691 to use the section anyway."),
3693 warning_printed = 1;
3697 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3698 of the TU (for symbols coming from TUs),
3699 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3700 Plus gold-generated indices can have duplicate entries for global symbols,
3701 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3702 These are just performance bugs, and we can't distinguish gdb-generated
3703 indices from gold-generated ones, so issue no warning here. */
3705 /* Indexes with higher version than the one supported by GDB may be no
3706 longer backward compatible. */
3710 map->version = version;
3711 map->total_size = section->size;
3713 metadata = (offset_type *) (addr + sizeof (offset_type));
3716 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3717 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3721 *types_list = addr + MAYBE_SWAP (metadata[i]);
3722 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3723 - MAYBE_SWAP (metadata[i]))
3727 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3728 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3729 - MAYBE_SWAP (metadata[i]));
3732 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3733 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3734 - MAYBE_SWAP (metadata[i]))
3735 / (2 * sizeof (offset_type)));
3738 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3743 /* Read .gdb_index. If everything went ok, initialize the "quick"
3744 elements of all the CUs and return 1. Otherwise, return 0. */
3747 dwarf2_read_index (struct objfile *objfile)
3749 struct mapped_index local_map, *map;
3750 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3751 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3752 struct dwz_file *dwz;
3754 if (!read_index_from_section (objfile, objfile_name (objfile),
3755 use_deprecated_index_sections,
3756 &dwarf2_per_objfile->gdb_index, &local_map,
3757 &cu_list, &cu_list_elements,
3758 &types_list, &types_list_elements))
3761 /* Don't use the index if it's empty. */
3762 if (local_map.symbol_table_slots == 0)
3765 /* If there is a .dwz file, read it so we can get its CU list as
3767 dwz = dwarf2_get_dwz_file ();
3770 struct mapped_index dwz_map;
3771 const gdb_byte *dwz_types_ignore;
3772 offset_type dwz_types_elements_ignore;
3774 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3776 &dwz->gdb_index, &dwz_map,
3777 &dwz_list, &dwz_list_elements,
3779 &dwz_types_elements_ignore))
3781 warning (_("could not read '.gdb_index' section from %s; skipping"),
3782 bfd_get_filename (dwz->dwz_bfd));
3787 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3790 if (types_list_elements)
3792 struct dwarf2_section_info *section;
3794 /* We can only handle a single .debug_types when we have an
3796 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3799 section = VEC_index (dwarf2_section_info_def,
3800 dwarf2_per_objfile->types, 0);
3802 create_signatured_type_table_from_index (objfile, section, types_list,
3803 types_list_elements);
3806 create_addrmap_from_index (objfile, &local_map);
3808 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3809 map = new (map) mapped_index ();
3812 dwarf2_per_objfile->index_table = map;
3813 dwarf2_per_objfile->using_index = 1;
3814 dwarf2_per_objfile->quick_file_names_table =
3815 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3820 /* A helper for the "quick" functions which sets the global
3821 dwarf2_per_objfile according to OBJFILE. */
3824 dw2_setup (struct objfile *objfile)
3826 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3827 objfile_data (objfile, dwarf2_objfile_data_key));
3828 gdb_assert (dwarf2_per_objfile);
3831 /* die_reader_func for dw2_get_file_names. */
3834 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3835 const gdb_byte *info_ptr,
3836 struct die_info *comp_unit_die,
3840 struct dwarf2_cu *cu = reader->cu;
3841 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3842 struct objfile *objfile = dwarf2_per_objfile->objfile;
3843 struct dwarf2_per_cu_data *lh_cu;
3844 struct attribute *attr;
3847 struct quick_file_names *qfn;
3849 gdb_assert (! this_cu->is_debug_types);
3851 /* Our callers never want to match partial units -- instead they
3852 will match the enclosing full CU. */
3853 if (comp_unit_die->tag == DW_TAG_partial_unit)
3855 this_cu->v.quick->no_file_data = 1;
3863 sect_offset line_offset {};
3865 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3868 struct quick_file_names find_entry;
3870 line_offset = (sect_offset) DW_UNSND (attr);
3872 /* We may have already read in this line header (TU line header sharing).
3873 If we have we're done. */
3874 find_entry.hash.dwo_unit = cu->dwo_unit;
3875 find_entry.hash.line_sect_off = line_offset;
3876 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3877 &find_entry, INSERT);
3880 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3884 lh = dwarf_decode_line_header (line_offset, cu);
3888 lh_cu->v.quick->no_file_data = 1;
3892 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3893 qfn->hash.dwo_unit = cu->dwo_unit;
3894 qfn->hash.line_sect_off = line_offset;
3895 gdb_assert (slot != NULL);
3898 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3900 qfn->num_file_names = lh->file_names.size ();
3902 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3903 for (i = 0; i < lh->file_names.size (); ++i)
3904 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3905 qfn->real_names = NULL;
3907 lh_cu->v.quick->file_names = qfn;
3910 /* A helper for the "quick" functions which attempts to read the line
3911 table for THIS_CU. */
3913 static struct quick_file_names *
3914 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3916 /* This should never be called for TUs. */
3917 gdb_assert (! this_cu->is_debug_types);
3918 /* Nor type unit groups. */
3919 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3921 if (this_cu->v.quick->file_names != NULL)
3922 return this_cu->v.quick->file_names;
3923 /* If we know there is no line data, no point in looking again. */
3924 if (this_cu->v.quick->no_file_data)
3927 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3929 if (this_cu->v.quick->no_file_data)
3931 return this_cu->v.quick->file_names;
3934 /* A helper for the "quick" functions which computes and caches the
3935 real path for a given file name from the line table. */
3938 dw2_get_real_path (struct objfile *objfile,
3939 struct quick_file_names *qfn, int index)
3941 if (qfn->real_names == NULL)
3942 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3943 qfn->num_file_names, const char *);
3945 if (qfn->real_names[index] == NULL)
3946 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3948 return qfn->real_names[index];
3951 static struct symtab *
3952 dw2_find_last_source_symtab (struct objfile *objfile)
3954 struct compunit_symtab *cust;
3957 dw2_setup (objfile);
3958 index = dwarf2_per_objfile->n_comp_units - 1;
3959 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3962 return compunit_primary_filetab (cust);
3965 /* Traversal function for dw2_forget_cached_source_info. */
3968 dw2_free_cached_file_names (void **slot, void *info)
3970 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3972 if (file_data->real_names)
3976 for (i = 0; i < file_data->num_file_names; ++i)
3978 xfree ((void*) file_data->real_names[i]);
3979 file_data->real_names[i] = NULL;
3987 dw2_forget_cached_source_info (struct objfile *objfile)
3989 dw2_setup (objfile);
3991 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3992 dw2_free_cached_file_names, NULL);
3995 /* Helper function for dw2_map_symtabs_matching_filename that expands
3996 the symtabs and calls the iterator. */
3999 dw2_map_expand_apply (struct objfile *objfile,
4000 struct dwarf2_per_cu_data *per_cu,
4001 const char *name, const char *real_path,
4002 gdb::function_view<bool (symtab *)> callback)
4004 struct compunit_symtab *last_made = objfile->compunit_symtabs;
4006 /* Don't visit already-expanded CUs. */
4007 if (per_cu->v.quick->compunit_symtab)
4010 /* This may expand more than one symtab, and we want to iterate over
4012 dw2_instantiate_symtab (per_cu);
4014 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
4015 last_made, callback);
4018 /* Implementation of the map_symtabs_matching_filename method. */
4021 dw2_map_symtabs_matching_filename
4022 (struct objfile *objfile, const char *name, const char *real_path,
4023 gdb::function_view<bool (symtab *)> callback)
4026 const char *name_basename = lbasename (name);
4028 dw2_setup (objfile);
4030 /* The rule is CUs specify all the files, including those used by
4031 any TU, so there's no need to scan TUs here. */
4033 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4036 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4037 struct quick_file_names *file_data;
4039 /* We only need to look at symtabs not already expanded. */
4040 if (per_cu->v.quick->compunit_symtab)
4043 file_data = dw2_get_file_names (per_cu);
4044 if (file_data == NULL)
4047 for (j = 0; j < file_data->num_file_names; ++j)
4049 const char *this_name = file_data->file_names[j];
4050 const char *this_real_name;
4052 if (compare_filenames_for_search (this_name, name))
4054 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4060 /* Before we invoke realpath, which can get expensive when many
4061 files are involved, do a quick comparison of the basenames. */
4062 if (! basenames_may_differ
4063 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
4066 this_real_name = dw2_get_real_path (objfile, file_data, j);
4067 if (compare_filenames_for_search (this_real_name, name))
4069 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4075 if (real_path != NULL)
4077 gdb_assert (IS_ABSOLUTE_PATH (real_path));
4078 gdb_assert (IS_ABSOLUTE_PATH (name));
4079 if (this_real_name != NULL
4080 && FILENAME_CMP (real_path, this_real_name) == 0)
4082 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4094 /* Struct used to manage iterating over all CUs looking for a symbol. */
4096 struct dw2_symtab_iterator
4098 /* The internalized form of .gdb_index. */
4099 struct mapped_index *index;
4100 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
4101 int want_specific_block;
4102 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
4103 Unused if !WANT_SPECIFIC_BLOCK. */
4105 /* The kind of symbol we're looking for. */
4107 /* The list of CUs from the index entry of the symbol,
4108 or NULL if not found. */
4110 /* The next element in VEC to look at. */
4112 /* The number of elements in VEC, or zero if there is no match. */
4114 /* Have we seen a global version of the symbol?
4115 If so we can ignore all further global instances.
4116 This is to work around gold/15646, inefficient gold-generated
4121 /* Initialize the index symtab iterator ITER.
4122 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
4123 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
4126 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
4127 struct mapped_index *index,
4128 int want_specific_block,
4133 iter->index = index;
4134 iter->want_specific_block = want_specific_block;
4135 iter->block_index = block_index;
4136 iter->domain = domain;
4138 iter->global_seen = 0;
4140 if (find_slot_in_mapped_hash (index, name, &iter->vec))
4141 iter->length = MAYBE_SWAP (*iter->vec);
4149 /* Return the next matching CU or NULL if there are no more. */
4151 static struct dwarf2_per_cu_data *
4152 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4154 for ( ; iter->next < iter->length; ++iter->next)
4156 offset_type cu_index_and_attrs =
4157 MAYBE_SWAP (iter->vec[iter->next + 1]);
4158 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4159 struct dwarf2_per_cu_data *per_cu;
4160 int want_static = iter->block_index != GLOBAL_BLOCK;
4161 /* This value is only valid for index versions >= 7. */
4162 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4163 gdb_index_symbol_kind symbol_kind =
4164 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4165 /* Only check the symbol attributes if they're present.
4166 Indices prior to version 7 don't record them,
4167 and indices >= 7 may elide them for certain symbols
4168 (gold does this). */
4170 (iter->index->version >= 7
4171 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4173 /* Don't crash on bad data. */
4174 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4175 + dwarf2_per_objfile->n_type_units))
4177 complaint (&symfile_complaints,
4178 _(".gdb_index entry has bad CU index"
4180 objfile_name (dwarf2_per_objfile->objfile));
4184 per_cu = dw2_get_cutu (cu_index);
4186 /* Skip if already read in. */
4187 if (per_cu->v.quick->compunit_symtab)
4190 /* Check static vs global. */
4193 if (iter->want_specific_block
4194 && want_static != is_static)
4196 /* Work around gold/15646. */
4197 if (!is_static && iter->global_seen)
4200 iter->global_seen = 1;
4203 /* Only check the symbol's kind if it has one. */
4206 switch (iter->domain)
4209 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4210 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4211 /* Some types are also in VAR_DOMAIN. */
4212 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4216 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4220 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4235 static struct compunit_symtab *
4236 dw2_lookup_symbol (struct objfile *objfile, int block_index,
4237 const char *name, domain_enum domain)
4239 struct compunit_symtab *stab_best = NULL;
4240 struct mapped_index *index;
4242 dw2_setup (objfile);
4244 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4246 index = dwarf2_per_objfile->index_table;
4248 /* index is NULL if OBJF_READNOW. */
4251 struct dw2_symtab_iterator iter;
4252 struct dwarf2_per_cu_data *per_cu;
4254 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
4256 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4258 struct symbol *sym, *with_opaque = NULL;
4259 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4260 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4261 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4263 sym = block_find_symbol (block, name, domain,
4264 block_find_non_opaque_type_preferred,
4267 /* Some caution must be observed with overloaded functions
4268 and methods, since the index will not contain any overload
4269 information (but NAME might contain it). */
4272 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4274 if (with_opaque != NULL
4275 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4278 /* Keep looking through other CUs. */
4286 dw2_print_stats (struct objfile *objfile)
4288 int i, total, count;
4290 dw2_setup (objfile);
4291 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4293 for (i = 0; i < total; ++i)
4295 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4297 if (!per_cu->v.quick->compunit_symtab)
4300 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4301 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4304 /* This dumps minimal information about the index.
4305 It is called via "mt print objfiles".
4306 One use is to verify .gdb_index has been loaded by the
4307 gdb.dwarf2/gdb-index.exp testcase. */
4310 dw2_dump (struct objfile *objfile)
4312 dw2_setup (objfile);
4313 gdb_assert (dwarf2_per_objfile->using_index);
4314 printf_filtered (".gdb_index:");
4315 if (dwarf2_per_objfile->index_table != NULL)
4317 printf_filtered (" version %d\n",
4318 dwarf2_per_objfile->index_table->version);
4321 printf_filtered (" faked for \"readnow\"\n");
4322 printf_filtered ("\n");
4326 dw2_relocate (struct objfile *objfile,
4327 const struct section_offsets *new_offsets,
4328 const struct section_offsets *delta)
4330 /* There's nothing to relocate here. */
4334 dw2_expand_symtabs_for_function (struct objfile *objfile,
4335 const char *func_name)
4337 struct mapped_index *index;
4339 dw2_setup (objfile);
4341 index = dwarf2_per_objfile->index_table;
4343 /* index is NULL if OBJF_READNOW. */
4346 struct dw2_symtab_iterator iter;
4347 struct dwarf2_per_cu_data *per_cu;
4349 /* Note: It doesn't matter what we pass for block_index here. */
4350 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4353 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4354 dw2_instantiate_symtab (per_cu);
4359 dw2_expand_all_symtabs (struct objfile *objfile)
4363 dw2_setup (objfile);
4365 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
4366 + dwarf2_per_objfile->n_type_units); ++i)
4368 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4370 dw2_instantiate_symtab (per_cu);
4375 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4376 const char *fullname)
4380 dw2_setup (objfile);
4382 /* We don't need to consider type units here.
4383 This is only called for examining code, e.g. expand_line_sal.
4384 There can be an order of magnitude (or more) more type units
4385 than comp units, and we avoid them if we can. */
4387 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4390 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4391 struct quick_file_names *file_data;
4393 /* We only need to look at symtabs not already expanded. */
4394 if (per_cu->v.quick->compunit_symtab)
4397 file_data = dw2_get_file_names (per_cu);
4398 if (file_data == NULL)
4401 for (j = 0; j < file_data->num_file_names; ++j)
4403 const char *this_fullname = file_data->file_names[j];
4405 if (filename_cmp (this_fullname, fullname) == 0)
4407 dw2_instantiate_symtab (per_cu);
4415 dw2_map_matching_symbols (struct objfile *objfile,
4416 const char * name, domain_enum domain,
4418 int (*callback) (struct block *,
4419 struct symbol *, void *),
4420 void *data, symbol_name_match_type match,
4421 symbol_compare_ftype *ordered_compare)
4423 /* Currently unimplemented; used for Ada. The function can be called if the
4424 current language is Ada for a non-Ada objfile using GNU index. As Ada
4425 does not look for non-Ada symbols this function should just return. */
4428 /* Symbol name matcher for .gdb_index names.
4430 Symbol names in .gdb_index have a few particularities:
4432 - There's no indication of which is the language of each symbol.
4434 Since each language has its own symbol name matching algorithm,
4435 and we don't know which language is the right one, we must match
4436 each symbol against all languages. This would be a potential
4437 performance problem if it were not mitigated by the
4438 mapped_index::name_components lookup table, which significantly
4439 reduces the number of times we need to call into this matcher,
4440 making it a non-issue.
4442 - Symbol names in the index have no overload (parameter)
4443 information. I.e., in C++, "foo(int)" and "foo(long)" both
4444 appear as "foo" in the index, for example.
4446 This means that the lookup names passed to the symbol name
4447 matcher functions must have no parameter information either
4448 because (e.g.) symbol search name "foo" does not match
4449 lookup-name "foo(int)" [while swapping search name for lookup
4452 class gdb_index_symbol_name_matcher
4455 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4456 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4458 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4459 Returns true if any matcher matches. */
4460 bool matches (const char *symbol_name);
4463 /* A reference to the lookup name we're matching against. */
4464 const lookup_name_info &m_lookup_name;
4466 /* A vector holding all the different symbol name matchers, for all
4468 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4471 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4472 (const lookup_name_info &lookup_name)
4473 : m_lookup_name (lookup_name)
4475 /* Prepare the vector of comparison functions upfront, to avoid
4476 doing the same work for each symbol. Care is taken to avoid
4477 matching with the same matcher more than once if/when multiple
4478 languages use the same matcher function. */
4479 auto &matchers = m_symbol_name_matcher_funcs;
4480 matchers.reserve (nr_languages);
4482 matchers.push_back (default_symbol_name_matcher);
4484 for (int i = 0; i < nr_languages; i++)
4486 const language_defn *lang = language_def ((enum language) i);
4487 if (lang->la_get_symbol_name_matcher != NULL)
4489 symbol_name_matcher_ftype *name_matcher
4490 = lang->la_get_symbol_name_matcher (m_lookup_name);
4492 /* Don't insert the same comparison routine more than once.
4493 Note that we do this linear walk instead of a cheaper
4494 sorted insert, or use a std::set or something like that,
4495 because relative order of function addresses is not
4496 stable. This is not a problem in practice because the
4497 number of supported languages is low, and the cost here
4498 is tiny compared to the number of searches we'll do
4499 afterwards using this object. */
4500 if (std::find (matchers.begin (), matchers.end (), name_matcher)
4502 matchers.push_back (name_matcher);
4508 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4510 for (auto matches_name : m_symbol_name_matcher_funcs)
4511 if (matches_name (symbol_name, m_lookup_name, NULL))
4517 /* Starting from a search name, return the string that finds the upper
4518 bound of all strings that start with SEARCH_NAME in a sorted name
4519 list. Returns the empty string to indicate that the upper bound is
4520 the end of the list. */
4523 make_sort_after_prefix_name (const char *search_name)
4525 /* When looking to complete "func", we find the upper bound of all
4526 symbols that start with "func" by looking for where we'd insert
4527 the closest string that would follow "func" in lexicographical
4528 order. Usually, that's "func"-with-last-character-incremented,
4529 i.e. "fund". Mind non-ASCII characters, though. Usually those
4530 will be UTF-8 multi-byte sequences, but we can't be certain.
4531 Especially mind the 0xff character, which is a valid character in
4532 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4533 rule out compilers allowing it in identifiers. Note that
4534 conveniently, strcmp/strcasecmp are specified to compare
4535 characters interpreted as unsigned char. So what we do is treat
4536 the whole string as a base 256 number composed of a sequence of
4537 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4538 to 0, and carries 1 to the following more-significant position.
4539 If the very first character in SEARCH_NAME ends up incremented
4540 and carries/overflows, then the upper bound is the end of the
4541 list. The string after the empty string is also the empty
4544 Some examples of this operation:
4546 SEARCH_NAME => "+1" RESULT
4550 "\xff" "a" "\xff" => "\xff" "b"
4555 Then, with these symbols for example:
4561 completing "func" looks for symbols between "func" and
4562 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4563 which finds "func" and "func1", but not "fund".
4567 funcÿ (Latin1 'ÿ' [0xff])
4571 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4572 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4576 ÿÿ (Latin1 'ÿ' [0xff])
4579 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4580 the end of the list.
4582 std::string after = search_name;
4583 while (!after.empty () && (unsigned char) after.back () == 0xff)
4585 if (!after.empty ())
4586 after.back () = (unsigned char) after.back () + 1;
4590 /* See declaration. */
4592 std::pair<std::vector<name_component>::const_iterator,
4593 std::vector<name_component>::const_iterator>
4594 mapped_index::find_name_components_bounds
4595 (const lookup_name_info &lookup_name_without_params) const
4598 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4601 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4603 /* Comparison function object for lower_bound that matches against a
4604 given symbol name. */
4605 auto lookup_compare_lower = [&] (const name_component &elem,
4608 const char *elem_qualified = this->symbol_name_at (elem.idx);
4609 const char *elem_name = elem_qualified + elem.name_offset;
4610 return name_cmp (elem_name, name) < 0;
4613 /* Comparison function object for upper_bound that matches against a
4614 given symbol name. */
4615 auto lookup_compare_upper = [&] (const char *name,
4616 const name_component &elem)
4618 const char *elem_qualified = this->symbol_name_at (elem.idx);
4619 const char *elem_name = elem_qualified + elem.name_offset;
4620 return name_cmp (name, elem_name) < 0;
4623 auto begin = this->name_components.begin ();
4624 auto end = this->name_components.end ();
4626 /* Find the lower bound. */
4629 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4632 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4635 /* Find the upper bound. */
4638 if (lookup_name_without_params.completion_mode ())
4640 /* In completion mode, we want UPPER to point past all
4641 symbols names that have the same prefix. I.e., with
4642 these symbols, and completing "func":
4644 function << lower bound
4646 other_function << upper bound
4648 We find the upper bound by looking for the insertion
4649 point of "func"-with-last-character-incremented,
4651 std::string after = make_sort_after_prefix_name (cplus);
4654 return std::lower_bound (lower, end, after.c_str (),
4655 lookup_compare_lower);
4658 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4661 return {lower, upper};
4664 /* See declaration. */
4667 mapped_index::build_name_components ()
4669 if (!this->name_components.empty ())
4672 this->name_components_casing = case_sensitivity;
4674 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4676 /* The code below only knows how to break apart components of C++
4677 symbol names (and other languages that use '::' as
4678 namespace/module separator). If we add support for wild matching
4679 to some language that uses some other operator (E.g., Ada, Go and
4680 D use '.'), then we'll need to try splitting the symbol name
4681 according to that language too. Note that Ada does support wild
4682 matching, but doesn't currently support .gdb_index. */
4683 for (size_t iter = 0; iter < this->symbol_table_slots; ++iter)
4685 offset_type idx = 2 * iter;
4687 if (this->symbol_table[idx] == 0
4688 && this->symbol_table[idx + 1] == 0)
4691 const char *name = this->symbol_name_at (idx);
4693 /* Add each name component to the name component table. */
4694 unsigned int previous_len = 0;
4695 for (unsigned int current_len = cp_find_first_component (name);
4696 name[current_len] != '\0';
4697 current_len += cp_find_first_component (name + current_len))
4699 gdb_assert (name[current_len] == ':');
4700 this->name_components.push_back ({previous_len, idx});
4701 /* Skip the '::'. */
4703 previous_len = current_len;
4705 this->name_components.push_back ({previous_len, idx});
4708 /* Sort name_components elements by name. */
4709 auto name_comp_compare = [&] (const name_component &left,
4710 const name_component &right)
4712 const char *left_qualified = this->symbol_name_at (left.idx);
4713 const char *right_qualified = this->symbol_name_at (right.idx);
4715 const char *left_name = left_qualified + left.name_offset;
4716 const char *right_name = right_qualified + right.name_offset;
4718 return name_cmp (left_name, right_name) < 0;
4721 std::sort (this->name_components.begin (),
4722 this->name_components.end (),
4726 /* Helper for dw2_expand_symtabs_matching that works with a
4727 mapped_index instead of the containing objfile. This is split to a
4728 separate function in order to be able to unit test the
4729 name_components matching using a mock mapped_index. For each
4730 symbol name that matches, calls MATCH_CALLBACK, passing it the
4731 symbol's index in the mapped_index symbol table. */
4734 dw2_expand_symtabs_matching_symbol
4735 (mapped_index &index,
4736 const lookup_name_info &lookup_name_in,
4737 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4738 enum search_domain kind,
4739 gdb::function_view<void (offset_type)> match_callback)
4741 lookup_name_info lookup_name_without_params
4742 = lookup_name_in.make_ignore_params ();
4743 gdb_index_symbol_name_matcher lookup_name_matcher
4744 (lookup_name_without_params);
4746 /* Build the symbol name component sorted vector, if we haven't
4748 index.build_name_components ();
4750 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4752 /* Now for each symbol name in range, check to see if we have a name
4753 match, and if so, call the MATCH_CALLBACK callback. */
4755 /* The same symbol may appear more than once in the range though.
4756 E.g., if we're looking for symbols that complete "w", and we have
4757 a symbol named "w1::w2", we'll find the two name components for
4758 that same symbol in the range. To be sure we only call the
4759 callback once per symbol, we first collect the symbol name
4760 indexes that matched in a temporary vector and ignore
4762 std::vector<offset_type> matches;
4763 matches.reserve (std::distance (bounds.first, bounds.second));
4765 for (; bounds.first != bounds.second; ++bounds.first)
4767 const char *qualified = index.symbol_name_at (bounds.first->idx);
4769 if (!lookup_name_matcher.matches (qualified)
4770 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4773 matches.push_back (bounds.first->idx);
4776 std::sort (matches.begin (), matches.end ());
4778 /* Finally call the callback, once per match. */
4780 for (offset_type idx : matches)
4784 match_callback (idx);
4789 /* Above we use a type wider than idx's for 'prev', since 0 and
4790 (offset_type)-1 are both possible values. */
4791 static_assert (sizeof (prev) > sizeof (offset_type), "");
4796 namespace selftests { namespace dw2_expand_symtabs_matching {
4798 /* A wrapper around mapped_index that builds a mock mapped_index, from
4799 the symbol list passed as parameter to the constructor. */
4800 class mock_mapped_index
4804 mock_mapped_index (const char *(&symbols)[N])
4805 : mock_mapped_index (symbols, N)
4808 /* Access the built index. */
4809 mapped_index &index ()
4813 mock_mapped_index(const mock_mapped_index &) = delete;
4814 void operator= (const mock_mapped_index &) = delete;
4817 mock_mapped_index (const char **symbols, size_t symbols_size)
4819 /* No string can live at offset zero. Add a dummy entry. */
4820 obstack_grow_str0 (&m_constant_pool, "");
4822 for (size_t i = 0; i < symbols_size; i++)
4824 const char *sym = symbols[i];
4825 size_t offset = obstack_object_size (&m_constant_pool);
4826 obstack_grow_str0 (&m_constant_pool, sym);
4827 m_symbol_table.push_back (offset);
4828 m_symbol_table.push_back (0);
4831 m_index.constant_pool = (const char *) obstack_base (&m_constant_pool);
4832 m_index.symbol_table = m_symbol_table.data ();
4833 m_index.symbol_table_slots = m_symbol_table.size () / 2;
4837 /* The built mapped_index. */
4838 mapped_index m_index{};
4840 /* The storage that the built mapped_index uses for symbol and
4841 constant pool tables. */
4842 std::vector<offset_type> m_symbol_table;
4843 auto_obstack m_constant_pool;
4846 /* Convenience function that converts a NULL pointer to a "<null>"
4847 string, to pass to print routines. */
4850 string_or_null (const char *str)
4852 return str != NULL ? str : "<null>";
4855 /* Check if a lookup_name_info built from
4856 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4857 index. EXPECTED_LIST is the list of expected matches, in expected
4858 matching order. If no match expected, then an empty list is
4859 specified. Returns true on success. On failure prints a warning
4860 indicating the file:line that failed, and returns false. */
4863 check_match (const char *file, int line,
4864 mock_mapped_index &mock_index,
4865 const char *name, symbol_name_match_type match_type,
4866 bool completion_mode,
4867 std::initializer_list<const char *> expected_list)
4869 lookup_name_info lookup_name (name, match_type, completion_mode);
4871 bool matched = true;
4873 auto mismatch = [&] (const char *expected_str,
4876 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4877 "expected=\"%s\", got=\"%s\"\n"),
4879 (match_type == symbol_name_match_type::FULL
4881 name, string_or_null (expected_str), string_or_null (got));
4885 auto expected_it = expected_list.begin ();
4886 auto expected_end = expected_list.end ();
4888 dw2_expand_symtabs_matching_symbol (mock_index.index (), lookup_name,
4890 [&] (offset_type idx)
4892 const char *matched_name = mock_index.index ().symbol_name_at (idx);
4893 const char *expected_str
4894 = expected_it == expected_end ? NULL : *expected_it++;
4896 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4897 mismatch (expected_str, matched_name);
4900 const char *expected_str
4901 = expected_it == expected_end ? NULL : *expected_it++;
4902 if (expected_str != NULL)
4903 mismatch (expected_str, NULL);
4908 /* The symbols added to the mock mapped_index for testing (in
4910 static const char *test_symbols[] = {
4919 "ns2::tmpl<int>::foo2",
4920 "(anonymous namespace)::A::B::C",
4922 /* These are used to check that the increment-last-char in the
4923 matching algorithm for completion doesn't match "t1_fund" when
4924 completing "t1_func". */
4930 /* A UTF-8 name with multi-byte sequences to make sure that
4931 cp-name-parser understands this as a single identifier ("função"
4932 is "function" in PT). */
4935 /* \377 (0xff) is Latin1 'ÿ'. */
4938 /* \377 (0xff) is Latin1 'ÿ'. */
4942 /* A name with all sorts of complications. Starts with "z" to make
4943 it easier for the completion tests below. */
4944 #define Z_SYM_NAME \
4945 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4946 "::tuple<(anonymous namespace)::ui*, " \
4947 "std::default_delete<(anonymous namespace)::ui>, void>"
4952 /* Returns true if the mapped_index::find_name_component_bounds method
4953 finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME, in
4957 check_find_bounds_finds (mapped_index &index,
4958 const char *search_name,
4959 gdb::array_view<const char *> expected_syms)
4961 lookup_name_info lookup_name (search_name,
4962 symbol_name_match_type::FULL, true);
4964 auto bounds = index.find_name_components_bounds (lookup_name);
4966 size_t distance = std::distance (bounds.first, bounds.second);
4967 if (distance != expected_syms.size ())
4970 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4972 auto nc_elem = bounds.first + exp_elem;
4973 const char *qualified = index.symbol_name_at (nc_elem->idx);
4974 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4981 /* Test the lower-level mapped_index::find_name_component_bounds
4985 test_mapped_index_find_name_component_bounds ()
4987 mock_mapped_index mock_index (test_symbols);
4989 mock_index.index ().build_name_components ();
4991 /* Test the lower-level mapped_index::find_name_component_bounds
4992 method in completion mode. */
4994 static const char *expected_syms[] = {
4999 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
5000 "t1_func", expected_syms));
5003 /* Check that the increment-last-char in the name matching algorithm
5004 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
5006 static const char *expected_syms1[] = {
5010 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
5011 "\377", expected_syms1));
5013 static const char *expected_syms2[] = {
5016 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
5017 "\377\377", expected_syms2));
5021 /* Test dw2_expand_symtabs_matching_symbol. */
5024 test_dw2_expand_symtabs_matching_symbol ()
5026 mock_mapped_index mock_index (test_symbols);
5028 /* We let all tests run until the end even if some fails, for debug
5030 bool any_mismatch = false;
5032 /* Create the expected symbols list (an initializer_list). Needed
5033 because lists have commas, and we need to pass them to CHECK,
5034 which is a macro. */
5035 #define EXPECT(...) { __VA_ARGS__ }
5037 /* Wrapper for check_match that passes down the current
5038 __FILE__/__LINE__. */
5039 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
5040 any_mismatch |= !check_match (__FILE__, __LINE__, \
5042 NAME, MATCH_TYPE, COMPLETION_MODE, \
5045 /* Identity checks. */
5046 for (const char *sym : test_symbols)
5048 /* Should be able to match all existing symbols. */
5049 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
5052 /* Should be able to match all existing symbols with
5054 std::string with_params = std::string (sym) + "(int)";
5055 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5058 /* Should be able to match all existing symbols with
5059 parameters and qualifiers. */
5060 with_params = std::string (sym) + " ( int ) const";
5061 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5064 /* This should really find sym, but cp-name-parser.y doesn't
5065 know about lvalue/rvalue qualifiers yet. */
5066 with_params = std::string (sym) + " ( int ) &&";
5067 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5071 /* Check that the name matching algorithm for completion doesn't get
5072 confused with Latin1 'ÿ' / 0xff. */
5074 static const char str[] = "\377";
5075 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5076 EXPECT ("\377", "\377\377123"));
5079 /* Check that the increment-last-char in the matching algorithm for
5080 completion doesn't match "t1_fund" when completing "t1_func". */
5082 static const char str[] = "t1_func";
5083 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5084 EXPECT ("t1_func", "t1_func1"));
5087 /* Check that completion mode works at each prefix of the expected
5090 static const char str[] = "function(int)";
5091 size_t len = strlen (str);
5094 for (size_t i = 1; i < len; i++)
5096 lookup.assign (str, i);
5097 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5098 EXPECT ("function"));
5102 /* While "w" is a prefix of both components, the match function
5103 should still only be called once. */
5105 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
5107 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
5111 /* Same, with a "complicated" symbol. */
5113 static const char str[] = Z_SYM_NAME;
5114 size_t len = strlen (str);
5117 for (size_t i = 1; i < len; i++)
5119 lookup.assign (str, i);
5120 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5121 EXPECT (Z_SYM_NAME));
5125 /* In FULL mode, an incomplete symbol doesn't match. */
5127 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
5131 /* A complete symbol with parameters matches any overload, since the
5132 index has no overload info. */
5134 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
5135 EXPECT ("std::zfunction", "std::zfunction2"));
5136 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
5137 EXPECT ("std::zfunction", "std::zfunction2"));
5138 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
5139 EXPECT ("std::zfunction", "std::zfunction2"));
5142 /* Check that whitespace is ignored appropriately. A symbol with a
5143 template argument list. */
5145 static const char expected[] = "ns::foo<int>";
5146 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
5148 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
5152 /* Check that whitespace is ignored appropriately. A symbol with a
5153 template argument list that includes a pointer. */
5155 static const char expected[] = "ns::foo<char*>";
5156 /* Try both completion and non-completion modes. */
5157 static const bool completion_mode[2] = {false, true};
5158 for (size_t i = 0; i < 2; i++)
5160 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
5161 completion_mode[i], EXPECT (expected));
5162 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
5163 completion_mode[i], EXPECT (expected));
5165 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
5166 completion_mode[i], EXPECT (expected));
5167 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
5168 completion_mode[i], EXPECT (expected));
5173 /* Check method qualifiers are ignored. */
5174 static const char expected[] = "ns::foo<char*>";
5175 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
5176 symbol_name_match_type::FULL, true, EXPECT (expected));
5177 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
5178 symbol_name_match_type::FULL, true, EXPECT (expected));
5179 CHECK_MATCH ("foo < char * > ( int ) const",
5180 symbol_name_match_type::WILD, true, EXPECT (expected));
5181 CHECK_MATCH ("foo < char * > ( int ) &&",
5182 symbol_name_match_type::WILD, true, EXPECT (expected));
5185 /* Test lookup names that don't match anything. */
5187 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
5190 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
5194 /* Some wild matching tests, exercising "(anonymous namespace)",
5195 which should not be confused with a parameter list. */
5197 static const char *syms[] = {
5201 "A :: B :: C ( int )",
5206 for (const char *s : syms)
5208 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
5209 EXPECT ("(anonymous namespace)::A::B::C"));
5214 static const char expected[] = "ns2::tmpl<int>::foo2";
5215 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5217 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5221 SELF_CHECK (!any_mismatch);
5230 test_mapped_index_find_name_component_bounds ();
5231 test_dw2_expand_symtabs_matching_symbol ();
5234 }} // namespace selftests::dw2_expand_symtabs_matching
5236 #endif /* GDB_SELF_TEST */
5238 /* If FILE_MATCHER is NULL or if PER_CU has
5239 dwarf2_per_cu_quick_data::MARK set (see
5240 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5241 EXPANSION_NOTIFY on it. */
5244 dw2_expand_symtabs_matching_one
5245 (struct dwarf2_per_cu_data *per_cu,
5246 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5247 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5249 if (file_matcher == NULL || per_cu->v.quick->mark)
5251 bool symtab_was_null
5252 = (per_cu->v.quick->compunit_symtab == NULL);
5254 dw2_instantiate_symtab (per_cu);
5256 if (expansion_notify != NULL
5258 && per_cu->v.quick->compunit_symtab != NULL)
5259 expansion_notify (per_cu->v.quick->compunit_symtab);
5263 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5264 matched, to expand corresponding CUs that were marked. IDX is the
5265 index of the symbol name that matched. */
5268 dw2_expand_marked_cus
5269 (mapped_index &index, offset_type idx,
5270 struct objfile *objfile,
5271 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5272 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5275 offset_type *vec, vec_len, vec_idx;
5276 bool global_seen = false;
5278 vec = (offset_type *) (index.constant_pool
5279 + MAYBE_SWAP (index.symbol_table[idx + 1]));
5280 vec_len = MAYBE_SWAP (vec[0]);
5281 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5283 struct dwarf2_per_cu_data *per_cu;
5284 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5285 /* This value is only valid for index versions >= 7. */
5286 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5287 gdb_index_symbol_kind symbol_kind =
5288 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5289 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5290 /* Only check the symbol attributes if they're present.
5291 Indices prior to version 7 don't record them,
5292 and indices >= 7 may elide them for certain symbols
5293 (gold does this). */
5296 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5298 /* Work around gold/15646. */
5301 if (!is_static && global_seen)
5307 /* Only check the symbol's kind if it has one. */
5312 case VARIABLES_DOMAIN:
5313 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5316 case FUNCTIONS_DOMAIN:
5317 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5321 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5329 /* Don't crash on bad data. */
5330 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5331 + dwarf2_per_objfile->n_type_units))
5333 complaint (&symfile_complaints,
5334 _(".gdb_index entry has bad CU index"
5335 " [in module %s]"), objfile_name (objfile));
5339 per_cu = dw2_get_cutu (cu_index);
5340 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5345 /* If FILE_MATCHER is non-NULL, set all the
5346 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5347 that match FILE_MATCHER. */
5350 dw_expand_symtabs_matching_file_matcher
5351 (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5353 if (file_matcher == NULL)
5356 objfile *const objfile = dwarf2_per_objfile->objfile;
5358 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5360 NULL, xcalloc, xfree));
5361 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5363 NULL, xcalloc, xfree));
5365 /* The rule is CUs specify all the files, including those used by
5366 any TU, so there's no need to scan TUs here. */
5368 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5371 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5372 struct quick_file_names *file_data;
5377 per_cu->v.quick->mark = 0;
5379 /* We only need to look at symtabs not already expanded. */
5380 if (per_cu->v.quick->compunit_symtab)
5383 file_data = dw2_get_file_names (per_cu);
5384 if (file_data == NULL)
5387 if (htab_find (visited_not_found.get (), file_data) != NULL)
5389 else if (htab_find (visited_found.get (), file_data) != NULL)
5391 per_cu->v.quick->mark = 1;
5395 for (j = 0; j < file_data->num_file_names; ++j)
5397 const char *this_real_name;
5399 if (file_matcher (file_data->file_names[j], false))
5401 per_cu->v.quick->mark = 1;
5405 /* Before we invoke realpath, which can get expensive when many
5406 files are involved, do a quick comparison of the basenames. */
5407 if (!basenames_may_differ
5408 && !file_matcher (lbasename (file_data->file_names[j]),
5412 this_real_name = dw2_get_real_path (objfile, file_data, j);
5413 if (file_matcher (this_real_name, false))
5415 per_cu->v.quick->mark = 1;
5420 slot = htab_find_slot (per_cu->v.quick->mark
5421 ? visited_found.get ()
5422 : visited_not_found.get (),
5429 dw2_expand_symtabs_matching
5430 (struct objfile *objfile,
5431 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5432 const lookup_name_info &lookup_name,
5433 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5434 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5435 enum search_domain kind)
5439 dw2_setup (objfile);
5441 /* index_table is NULL if OBJF_READNOW. */
5442 if (!dwarf2_per_objfile->index_table)
5445 dw_expand_symtabs_matching_file_matcher (file_matcher);
5447 mapped_index &index = *dwarf2_per_objfile->index_table;
5449 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5451 kind, [&] (offset_type idx)
5453 dw2_expand_marked_cus (index, idx, objfile, file_matcher,
5454 expansion_notify, kind);
5458 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5461 static struct compunit_symtab *
5462 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5467 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5468 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5471 if (cust->includes == NULL)
5474 for (i = 0; cust->includes[i]; ++i)
5476 struct compunit_symtab *s = cust->includes[i];
5478 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5486 static struct compunit_symtab *
5487 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5488 struct bound_minimal_symbol msymbol,
5490 struct obj_section *section,
5493 struct dwarf2_per_cu_data *data;
5494 struct compunit_symtab *result;
5496 dw2_setup (objfile);
5498 if (!objfile->psymtabs_addrmap)
5501 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5506 if (warn_if_readin && data->v.quick->compunit_symtab)
5507 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5508 paddress (get_objfile_arch (objfile), pc));
5511 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5513 gdb_assert (result != NULL);
5518 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5519 void *data, int need_fullname)
5521 dw2_setup (objfile);
5523 if (!dwarf2_per_objfile->filenames_cache)
5525 dwarf2_per_objfile->filenames_cache.emplace ();
5527 htab_up visited (htab_create_alloc (10,
5528 htab_hash_pointer, htab_eq_pointer,
5529 NULL, xcalloc, xfree));
5531 /* The rule is CUs specify all the files, including those used
5532 by any TU, so there's no need to scan TUs here. We can
5533 ignore file names coming from already-expanded CUs. */
5535 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5537 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
5539 if (per_cu->v.quick->compunit_symtab)
5541 void **slot = htab_find_slot (visited.get (),
5542 per_cu->v.quick->file_names,
5545 *slot = per_cu->v.quick->file_names;
5549 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5551 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5552 struct quick_file_names *file_data;
5555 /* We only need to look at symtabs not already expanded. */
5556 if (per_cu->v.quick->compunit_symtab)
5559 file_data = dw2_get_file_names (per_cu);
5560 if (file_data == NULL)
5563 slot = htab_find_slot (visited.get (), file_data, INSERT);
5566 /* Already visited. */
5571 for (int j = 0; j < file_data->num_file_names; ++j)
5573 const char *filename = file_data->file_names[j];
5574 dwarf2_per_objfile->filenames_cache->seen (filename);
5579 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5581 gdb::unique_xmalloc_ptr<char> this_real_name;
5584 this_real_name = gdb_realpath (filename);
5585 (*fun) (filename, this_real_name.get (), data);
5590 dw2_has_symbols (struct objfile *objfile)
5595 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5598 dw2_find_last_source_symtab,
5599 dw2_forget_cached_source_info,
5600 dw2_map_symtabs_matching_filename,
5605 dw2_expand_symtabs_for_function,
5606 dw2_expand_all_symtabs,
5607 dw2_expand_symtabs_with_fullname,
5608 dw2_map_matching_symbols,
5609 dw2_expand_symtabs_matching,
5610 dw2_find_pc_sect_compunit_symtab,
5612 dw2_map_symbol_filenames
5615 /* DWARF-5 debug_names reader. */
5617 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5618 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5620 /* A helper function that reads the .debug_names section in SECTION
5621 and fills in MAP. FILENAME is the name of the file containing the
5622 section; it is used for error reporting.
5624 Returns true if all went well, false otherwise. */
5627 read_debug_names_from_section (struct objfile *objfile,
5628 const char *filename,
5629 struct dwarf2_section_info *section,
5630 mapped_debug_names &map)
5632 if (dwarf2_section_empty_p (section))
5635 /* Older elfutils strip versions could keep the section in the main
5636 executable while splitting it for the separate debug info file. */
5637 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5640 dwarf2_read_section (objfile, section);
5642 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5644 const gdb_byte *addr = section->buffer;
5646 bfd *const abfd = get_section_bfd_owner (section);
5648 unsigned int bytes_read;
5649 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5652 map.dwarf5_is_dwarf64 = bytes_read != 4;
5653 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5654 if (bytes_read + length != section->size)
5656 /* There may be multiple per-CU indices. */
5657 warning (_("Section .debug_names in %s length %s does not match "
5658 "section length %s, ignoring .debug_names."),
5659 filename, plongest (bytes_read + length),
5660 pulongest (section->size));
5664 /* The version number. */
5665 uint16_t version = read_2_bytes (abfd, addr);
5669 warning (_("Section .debug_names in %s has unsupported version %d, "
5670 "ignoring .debug_names."),
5676 uint16_t padding = read_2_bytes (abfd, addr);
5680 warning (_("Section .debug_names in %s has unsupported padding %d, "
5681 "ignoring .debug_names."),
5686 /* comp_unit_count - The number of CUs in the CU list. */
5687 map.cu_count = read_4_bytes (abfd, addr);
5690 /* local_type_unit_count - The number of TUs in the local TU
5692 map.tu_count = read_4_bytes (abfd, addr);
5695 /* foreign_type_unit_count - The number of TUs in the foreign TU
5697 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5699 if (foreign_tu_count != 0)
5701 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5702 "ignoring .debug_names."),
5703 filename, static_cast<unsigned long> (foreign_tu_count));
5707 /* bucket_count - The number of hash buckets in the hash lookup
5709 map.bucket_count = read_4_bytes (abfd, addr);
5712 /* name_count - The number of unique names in the index. */
5713 map.name_count = read_4_bytes (abfd, addr);
5716 /* abbrev_table_size - The size in bytes of the abbreviations
5718 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5721 /* augmentation_string_size - The size in bytes of the augmentation
5722 string. This value is rounded up to a multiple of 4. */
5723 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5725 map.augmentation_is_gdb = ((augmentation_string_size
5726 == sizeof (dwarf5_augmentation))
5727 && memcmp (addr, dwarf5_augmentation,
5728 sizeof (dwarf5_augmentation)) == 0);
5729 augmentation_string_size += (-augmentation_string_size) & 3;
5730 addr += augmentation_string_size;
5733 map.cu_table_reordered = addr;
5734 addr += map.cu_count * map.offset_size;
5736 /* List of Local TUs */
5737 map.tu_table_reordered = addr;
5738 addr += map.tu_count * map.offset_size;
5740 /* Hash Lookup Table */
5741 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5742 addr += map.bucket_count * 4;
5743 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5744 addr += map.name_count * 4;
5747 map.name_table_string_offs_reordered = addr;
5748 addr += map.name_count * map.offset_size;
5749 map.name_table_entry_offs_reordered = addr;
5750 addr += map.name_count * map.offset_size;
5752 const gdb_byte *abbrev_table_start = addr;
5755 unsigned int bytes_read;
5756 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5761 const auto insertpair
5762 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5763 if (!insertpair.second)
5765 warning (_("Section .debug_names in %s has duplicate index %s, "
5766 "ignoring .debug_names."),
5767 filename, pulongest (index_num));
5770 mapped_debug_names::index_val &indexval = insertpair.first->second;
5771 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5776 mapped_debug_names::index_val::attr attr;
5777 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5779 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5781 if (attr.form == DW_FORM_implicit_const)
5783 attr.implicit_const = read_signed_leb128 (abfd, addr,
5787 if (attr.dw_idx == 0 && attr.form == 0)
5789 indexval.attr_vec.push_back (std::move (attr));
5792 if (addr != abbrev_table_start + abbrev_table_size)
5794 warning (_("Section .debug_names in %s has abbreviation_table "
5795 "of size %zu vs. written as %u, ignoring .debug_names."),
5796 filename, addr - abbrev_table_start, abbrev_table_size);
5799 map.entry_pool = addr;
5804 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5808 create_cus_from_debug_names_list (struct objfile *objfile,
5809 const mapped_debug_names &map,
5810 dwarf2_section_info §ion,
5811 bool is_dwz, int base_offset)
5813 sect_offset sect_off_prev;
5814 for (uint32_t i = 0; i <= map.cu_count; ++i)
5816 sect_offset sect_off_next;
5817 if (i < map.cu_count)
5820 = (sect_offset) (extract_unsigned_integer
5821 (map.cu_table_reordered + i * map.offset_size,
5823 map.dwarf5_byte_order));
5826 sect_off_next = (sect_offset) section.size;
5829 const ULONGEST length = sect_off_next - sect_off_prev;
5830 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
5831 = create_cu_from_index_list (objfile, §ion, is_dwz,
5832 sect_off_prev, length);
5834 sect_off_prev = sect_off_next;
5838 /* Read the CU list from the mapped index, and use it to create all
5839 the CU objects for this objfile. */
5842 create_cus_from_debug_names (struct objfile *objfile,
5843 const mapped_debug_names &map,
5844 const mapped_debug_names &dwz_map)
5847 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
5848 dwarf2_per_objfile->all_comp_units
5849 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
5850 dwarf2_per_objfile->n_comp_units);
5852 create_cus_from_debug_names_list (objfile, map, dwarf2_per_objfile->info,
5854 0 /* base_offset */);
5856 if (dwz_map.cu_count == 0)
5859 dwz_file *dwz = dwarf2_get_dwz_file ();
5860 create_cus_from_debug_names_list (objfile, dwz_map, dwz->info,
5862 map.cu_count /* base_offset */);
5865 /* Read .debug_names. If everything went ok, initialize the "quick"
5866 elements of all the CUs and return true. Otherwise, return false. */
5869 dwarf2_read_debug_names (struct objfile *objfile)
5871 mapped_debug_names local_map, dwz_map;
5873 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5874 &dwarf2_per_objfile->debug_names,
5878 /* Don't use the index if it's empty. */
5879 if (local_map.name_count == 0)
5882 /* If there is a .dwz file, read it so we can get its CU list as
5884 dwz_file *dwz = dwarf2_get_dwz_file ();
5887 if (!read_debug_names_from_section (objfile,
5888 bfd_get_filename (dwz->dwz_bfd),
5889 &dwz->debug_names, dwz_map))
5891 warning (_("could not read '.debug_names' section from %s; skipping"),
5892 bfd_get_filename (dwz->dwz_bfd));
5897 create_cus_from_debug_names (objfile, local_map, dwz_map);
5899 if (local_map.tu_count != 0)
5901 /* We can only handle a single .debug_types when we have an
5903 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5906 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5907 dwarf2_per_objfile->types, 0);
5909 create_signatured_type_table_from_debug_names
5910 (objfile, local_map, section, &dwarf2_per_objfile->abbrev);
5913 create_addrmap_from_aranges (objfile, &dwarf2_per_objfile->debug_aranges);
5915 dwarf2_per_objfile->debug_names_table.reset (new mapped_debug_names);
5916 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
5917 dwarf2_per_objfile->using_index = 1;
5918 dwarf2_per_objfile->quick_file_names_table =
5919 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
5924 /* Symbol name hashing function as specified by DWARF-5. */
5927 dwarf5_djb_hash (const char *str_)
5929 const unsigned char *str = (const unsigned char *) str_;
5931 /* Note: tolower here ignores UTF-8, which isn't fully compliant.
5932 See http://dwarfstd.org/ShowIssue.php?issue=161027.1. */
5934 uint32_t hash = 5381;
5935 while (int c = *str++)
5936 hash = hash * 33 + tolower (c);
5940 /* Type used to manage iterating over all CUs looking for a symbol for
5943 class dw2_debug_names_iterator
5946 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
5947 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
5948 dw2_debug_names_iterator (const mapped_debug_names &map,
5949 bool want_specific_block,
5950 block_enum block_index, domain_enum domain,
5952 : m_map (map), m_want_specific_block (want_specific_block),
5953 m_block_index (block_index), m_domain (domain),
5954 m_addr (find_vec_in_debug_names (map, name))
5957 dw2_debug_names_iterator (const mapped_debug_names &map,
5958 search_domain search, uint32_t namei)
5961 m_addr (find_vec_in_debug_names (map, namei))
5964 /* Return the next matching CU or NULL if there are no more. */
5965 dwarf2_per_cu_data *next ();
5968 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5970 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5973 /* The internalized form of .debug_names. */
5974 const mapped_debug_names &m_map;
5976 /* If true, only look for symbols that match BLOCK_INDEX. */
5977 const bool m_want_specific_block = false;
5979 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
5980 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
5982 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
5984 /* The kind of symbol we're looking for. */
5985 const domain_enum m_domain = UNDEF_DOMAIN;
5986 const search_domain m_search = ALL_DOMAIN;
5988 /* The list of CUs from the index entry of the symbol, or NULL if
5990 const gdb_byte *m_addr;
5994 mapped_debug_names::namei_to_name (uint32_t namei) const
5996 const ULONGEST namei_string_offs
5997 = extract_unsigned_integer ((name_table_string_offs_reordered
5998 + namei * offset_size),
6001 return read_indirect_string_at_offset
6002 (dwarf2_per_objfile->objfile->obfd, namei_string_offs);
6005 /* Find a slot in .debug_names for the object named NAME. If NAME is
6006 found, return pointer to its pool data. If NAME cannot be found,
6010 dw2_debug_names_iterator::find_vec_in_debug_names
6011 (const mapped_debug_names &map, const char *name)
6013 int (*cmp) (const char *, const char *);
6015 if (current_language->la_language == language_cplus
6016 || current_language->la_language == language_fortran
6017 || current_language->la_language == language_d)
6019 /* NAME is already canonical. Drop any qualifiers as
6020 .debug_names does not contain any. */
6022 if (strchr (name, '(') != NULL)
6024 gdb::unique_xmalloc_ptr<char> without_params
6025 = cp_remove_params (name);
6027 if (without_params != NULL)
6029 name = without_params.get();
6034 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
6036 const uint32_t full_hash = dwarf5_djb_hash (name);
6038 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6039 (map.bucket_table_reordered
6040 + (full_hash % map.bucket_count)), 4,
6041 map.dwarf5_byte_order);
6045 if (namei >= map.name_count)
6047 complaint (&symfile_complaints,
6048 _("Wrong .debug_names with name index %u but name_count=%u "
6050 namei, map.name_count,
6051 objfile_name (dwarf2_per_objfile->objfile));
6057 const uint32_t namei_full_hash
6058 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6059 (map.hash_table_reordered + namei), 4,
6060 map.dwarf5_byte_order);
6061 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
6064 if (full_hash == namei_full_hash)
6066 const char *const namei_string = map.namei_to_name (namei);
6068 #if 0 /* An expensive sanity check. */
6069 if (namei_full_hash != dwarf5_djb_hash (namei_string))
6071 complaint (&symfile_complaints,
6072 _("Wrong .debug_names hash for string at index %u "
6074 namei, objfile_name (dwarf2_per_objfile->objfile));
6079 if (cmp (namei_string, name) == 0)
6081 const ULONGEST namei_entry_offs
6082 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6083 + namei * map.offset_size),
6084 map.offset_size, map.dwarf5_byte_order);
6085 return map.entry_pool + namei_entry_offs;
6090 if (namei >= map.name_count)
6096 dw2_debug_names_iterator::find_vec_in_debug_names
6097 (const mapped_debug_names &map, uint32_t namei)
6099 if (namei >= map.name_count)
6101 complaint (&symfile_complaints,
6102 _("Wrong .debug_names with name index %u but name_count=%u "
6104 namei, map.name_count,
6105 objfile_name (dwarf2_per_objfile->objfile));
6109 const ULONGEST namei_entry_offs
6110 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6111 + namei * map.offset_size),
6112 map.offset_size, map.dwarf5_byte_order);
6113 return map.entry_pool + namei_entry_offs;
6116 /* See dw2_debug_names_iterator. */
6118 dwarf2_per_cu_data *
6119 dw2_debug_names_iterator::next ()
6124 bfd *const abfd = dwarf2_per_objfile->objfile->obfd;
6128 unsigned int bytes_read;
6129 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6130 m_addr += bytes_read;
6134 const auto indexval_it = m_map.abbrev_map.find (abbrev);
6135 if (indexval_it == m_map.abbrev_map.cend ())
6137 complaint (&symfile_complaints,
6138 _("Wrong .debug_names undefined abbrev code %s "
6140 pulongest (abbrev), objfile_name (dwarf2_per_objfile->objfile));
6143 const mapped_debug_names::index_val &indexval = indexval_it->second;
6144 bool have_is_static = false;
6146 dwarf2_per_cu_data *per_cu = NULL;
6147 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
6152 case DW_FORM_implicit_const:
6153 ull = attr.implicit_const;
6155 case DW_FORM_flag_present:
6159 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6160 m_addr += bytes_read;
6163 complaint (&symfile_complaints,
6164 _("Unsupported .debug_names form %s [in module %s]"),
6165 dwarf_form_name (attr.form),
6166 objfile_name (dwarf2_per_objfile->objfile));
6169 switch (attr.dw_idx)
6171 case DW_IDX_compile_unit:
6172 /* Don't crash on bad data. */
6173 if (ull >= (dwarf2_per_objfile->n_comp_units
6174 + dwarf2_per_objfile->n_type_units))
6176 complaint (&symfile_complaints,
6177 _(".debug_names entry has bad CU index %s"
6180 objfile_name (dwarf2_per_objfile->objfile));
6183 per_cu = dw2_get_cutu (ull);
6185 case DW_IDX_GNU_internal:
6186 if (!m_map.augmentation_is_gdb)
6188 have_is_static = true;
6191 case DW_IDX_GNU_external:
6192 if (!m_map.augmentation_is_gdb)
6194 have_is_static = true;
6200 /* Skip if already read in. */
6201 if (per_cu->v.quick->compunit_symtab)
6204 /* Check static vs global. */
6207 const bool want_static = m_block_index != GLOBAL_BLOCK;
6208 if (m_want_specific_block && want_static != is_static)
6212 /* Match dw2_symtab_iter_next, symbol_kind
6213 and debug_names::psymbol_tag. */
6217 switch (indexval.dwarf_tag)
6219 case DW_TAG_variable:
6220 case DW_TAG_subprogram:
6221 /* Some types are also in VAR_DOMAIN. */
6222 case DW_TAG_typedef:
6223 case DW_TAG_structure_type:
6230 switch (indexval.dwarf_tag)
6232 case DW_TAG_typedef:
6233 case DW_TAG_structure_type:
6240 switch (indexval.dwarf_tag)
6243 case DW_TAG_variable:
6253 /* Match dw2_expand_symtabs_matching, symbol_kind and
6254 debug_names::psymbol_tag. */
6257 case VARIABLES_DOMAIN:
6258 switch (indexval.dwarf_tag)
6260 case DW_TAG_variable:
6266 case FUNCTIONS_DOMAIN:
6267 switch (indexval.dwarf_tag)
6269 case DW_TAG_subprogram:
6276 switch (indexval.dwarf_tag)
6278 case DW_TAG_typedef:
6279 case DW_TAG_structure_type:
6292 static struct compunit_symtab *
6293 dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6294 const char *name, domain_enum domain)
6296 const block_enum block_index = static_cast<block_enum> (block_index_int);
6297 dw2_setup (objfile);
6299 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6302 /* index is NULL if OBJF_READNOW. */
6305 const auto &map = *mapp;
6307 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6308 block_index, domain, name);
6310 struct compunit_symtab *stab_best = NULL;
6311 struct dwarf2_per_cu_data *per_cu;
6312 while ((per_cu = iter.next ()) != NULL)
6314 struct symbol *sym, *with_opaque = NULL;
6315 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6316 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6317 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6319 sym = block_find_symbol (block, name, domain,
6320 block_find_non_opaque_type_preferred,
6323 /* Some caution must be observed with overloaded functions and
6324 methods, since the index will not contain any overload
6325 information (but NAME might contain it). */
6328 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6330 if (with_opaque != NULL
6331 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6334 /* Keep looking through other CUs. */
6340 /* This dumps minimal information about .debug_names. It is called
6341 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6342 uses this to verify that .debug_names has been loaded. */
6345 dw2_debug_names_dump (struct objfile *objfile)
6347 dw2_setup (objfile);
6348 gdb_assert (dwarf2_per_objfile->using_index);
6349 printf_filtered (".debug_names:");
6350 if (dwarf2_per_objfile->debug_names_table)
6351 printf_filtered (" exists\n");
6353 printf_filtered (" faked for \"readnow\"\n");
6354 printf_filtered ("\n");
6358 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6359 const char *func_name)
6361 dw2_setup (objfile);
6363 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6364 if (dwarf2_per_objfile->debug_names_table)
6366 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6368 /* Note: It doesn't matter what we pass for block_index here. */
6369 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6370 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
6372 struct dwarf2_per_cu_data *per_cu;
6373 while ((per_cu = iter.next ()) != NULL)
6374 dw2_instantiate_symtab (per_cu);
6379 dw2_debug_names_expand_symtabs_matching
6380 (struct objfile *objfile,
6381 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6382 const lookup_name_info &lookup_name,
6383 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6384 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6385 enum search_domain kind)
6387 dw2_setup (objfile);
6389 /* debug_names_table is NULL if OBJF_READNOW. */
6390 if (!dwarf2_per_objfile->debug_names_table)
6393 dw_expand_symtabs_matching_file_matcher (file_matcher);
6395 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6397 for (uint32_t namei = 0; namei < map.name_count; ++namei)
6401 const char *const namei_string = map.namei_to_name (namei);
6402 if (symbol_matcher != NULL && !symbol_matcher (namei_string))
6405 /* The name was matched, now expand corresponding CUs that were
6407 dw2_debug_names_iterator iter (map, kind, namei);
6409 struct dwarf2_per_cu_data *per_cu;
6410 while ((per_cu = iter.next ()) != NULL)
6411 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6416 const struct quick_symbol_functions dwarf2_debug_names_functions =
6419 dw2_find_last_source_symtab,
6420 dw2_forget_cached_source_info,
6421 dw2_map_symtabs_matching_filename,
6422 dw2_debug_names_lookup_symbol,
6424 dw2_debug_names_dump,
6426 dw2_debug_names_expand_symtabs_for_function,
6427 dw2_expand_all_symtabs,
6428 dw2_expand_symtabs_with_fullname,
6429 dw2_map_matching_symbols,
6430 dw2_debug_names_expand_symtabs_matching,
6431 dw2_find_pc_sect_compunit_symtab,
6433 dw2_map_symbol_filenames
6436 /* Initialize for reading DWARF for this objfile. Return 0 if this
6437 file will use psymtabs, or 1 if using the GNU index. */
6440 dwarf2_initialize_objfile (struct objfile *objfile)
6442 /* If we're about to read full symbols, don't bother with the
6443 indices. In this case we also don't care if some other debug
6444 format is making psymtabs, because they are all about to be
6446 if ((objfile->flags & OBJF_READNOW))
6450 dwarf2_per_objfile->using_index = 1;
6451 create_all_comp_units (objfile);
6452 create_all_type_units (objfile);
6453 dwarf2_per_objfile->quick_file_names_table =
6454 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6456 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
6457 + dwarf2_per_objfile->n_type_units); ++i)
6459 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6461 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6462 struct dwarf2_per_cu_quick_data);
6465 /* Return 1 so that gdb sees the "quick" functions. However,
6466 these functions will be no-ops because we will have expanded
6468 return elf_sym_fns_gdb_index;
6471 if (dwarf2_read_debug_names (objfile))
6472 return elf_sym_fns_debug_names;
6474 if (dwarf2_read_index (objfile))
6475 return elf_sym_fns_gdb_index;
6477 return elf_sym_fns_lazy_psyms;
6482 /* Build a partial symbol table. */
6485 dwarf2_build_psymtabs (struct objfile *objfile)
6488 if (objfile->global_psymbols.capacity () == 0
6489 && objfile->static_psymbols.capacity () == 0)
6490 init_psymbol_list (objfile, 1024);
6494 /* This isn't really ideal: all the data we allocate on the
6495 objfile's obstack is still uselessly kept around. However,
6496 freeing it seems unsafe. */
6497 psymtab_discarder psymtabs (objfile);
6498 dwarf2_build_psymtabs_hard (objfile);
6501 CATCH (except, RETURN_MASK_ERROR)
6503 exception_print (gdb_stderr, except);
6508 /* Return the total length of the CU described by HEADER. */
6511 get_cu_length (const struct comp_unit_head *header)
6513 return header->initial_length_size + header->length;
6516 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6519 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6521 sect_offset bottom = cu_header->sect_off;
6522 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6524 return sect_off >= bottom && sect_off < top;
6527 /* Find the base address of the compilation unit for range lists and
6528 location lists. It will normally be specified by DW_AT_low_pc.
6529 In DWARF-3 draft 4, the base address could be overridden by
6530 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6531 compilation units with discontinuous ranges. */
6534 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6536 struct attribute *attr;
6539 cu->base_address = 0;
6541 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6544 cu->base_address = attr_value_as_address (attr);
6549 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6552 cu->base_address = attr_value_as_address (attr);
6558 /* Read in the comp unit header information from the debug_info at info_ptr.
6559 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6560 NOTE: This leaves members offset, first_die_offset to be filled in
6563 static const gdb_byte *
6564 read_comp_unit_head (struct comp_unit_head *cu_header,
6565 const gdb_byte *info_ptr,
6566 struct dwarf2_section_info *section,
6567 rcuh_kind section_kind)
6570 unsigned int bytes_read;
6571 const char *filename = get_section_file_name (section);
6572 bfd *abfd = get_section_bfd_owner (section);
6574 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6575 cu_header->initial_length_size = bytes_read;
6576 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6577 info_ptr += bytes_read;
6578 cu_header->version = read_2_bytes (abfd, info_ptr);
6580 if (cu_header->version < 5)
6581 switch (section_kind)
6583 case rcuh_kind::COMPILE:
6584 cu_header->unit_type = DW_UT_compile;
6586 case rcuh_kind::TYPE:
6587 cu_header->unit_type = DW_UT_type;
6590 internal_error (__FILE__, __LINE__,
6591 _("read_comp_unit_head: invalid section_kind"));
6595 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6596 (read_1_byte (abfd, info_ptr));
6598 switch (cu_header->unit_type)
6601 if (section_kind != rcuh_kind::COMPILE)
6602 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6603 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6607 section_kind = rcuh_kind::TYPE;
6610 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6611 "(is %d, should be %d or %d) [in module %s]"),
6612 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6615 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6618 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6621 info_ptr += bytes_read;
6622 if (cu_header->version < 5)
6624 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6627 signed_addr = bfd_get_sign_extend_vma (abfd);
6628 if (signed_addr < 0)
6629 internal_error (__FILE__, __LINE__,
6630 _("read_comp_unit_head: dwarf from non elf file"));
6631 cu_header->signed_addr_p = signed_addr;
6633 if (section_kind == rcuh_kind::TYPE)
6635 LONGEST type_offset;
6637 cu_header->signature = read_8_bytes (abfd, info_ptr);
6640 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6641 info_ptr += bytes_read;
6642 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6643 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6644 error (_("Dwarf Error: Too big type_offset in compilation unit "
6645 "header (is %s) [in module %s]"), plongest (type_offset),
6652 /* Helper function that returns the proper abbrev section for
6655 static struct dwarf2_section_info *
6656 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6658 struct dwarf2_section_info *abbrev;
6660 if (this_cu->is_dwz)
6661 abbrev = &dwarf2_get_dwz_file ()->abbrev;
6663 abbrev = &dwarf2_per_objfile->abbrev;
6668 /* Subroutine of read_and_check_comp_unit_head and
6669 read_and_check_type_unit_head to simplify them.
6670 Perform various error checking on the header. */
6673 error_check_comp_unit_head (struct comp_unit_head *header,
6674 struct dwarf2_section_info *section,
6675 struct dwarf2_section_info *abbrev_section)
6677 const char *filename = get_section_file_name (section);
6679 if (header->version < 2 || header->version > 5)
6680 error (_("Dwarf Error: wrong version in compilation unit header "
6681 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
6684 if (to_underlying (header->abbrev_sect_off)
6685 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6686 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
6687 "(offset 0x%x + 6) [in module %s]"),
6688 to_underlying (header->abbrev_sect_off),
6689 to_underlying (header->sect_off),
6692 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6693 avoid potential 32-bit overflow. */
6694 if (((ULONGEST) header->sect_off + get_cu_length (header))
6696 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6697 "(offset 0x%x + 0) [in module %s]"),
6698 header->length, to_underlying (header->sect_off),
6702 /* Read in a CU/TU header and perform some basic error checking.
6703 The contents of the header are stored in HEADER.
6704 The result is a pointer to the start of the first DIE. */
6706 static const gdb_byte *
6707 read_and_check_comp_unit_head (struct comp_unit_head *header,
6708 struct dwarf2_section_info *section,
6709 struct dwarf2_section_info *abbrev_section,
6710 const gdb_byte *info_ptr,
6711 rcuh_kind section_kind)
6713 const gdb_byte *beg_of_comp_unit = info_ptr;
6715 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6717 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6719 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6721 error_check_comp_unit_head (header, section, abbrev_section);
6726 /* Fetch the abbreviation table offset from a comp or type unit header. */
6729 read_abbrev_offset (struct dwarf2_section_info *section,
6730 sect_offset sect_off)
6732 bfd *abfd = get_section_bfd_owner (section);
6733 const gdb_byte *info_ptr;
6734 unsigned int initial_length_size, offset_size;
6737 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6738 info_ptr = section->buffer + to_underlying (sect_off);
6739 read_initial_length (abfd, info_ptr, &initial_length_size);
6740 offset_size = initial_length_size == 4 ? 4 : 8;
6741 info_ptr += initial_length_size;
6743 version = read_2_bytes (abfd, info_ptr);
6747 /* Skip unit type and address size. */
6751 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6754 /* Allocate a new partial symtab for file named NAME and mark this new
6755 partial symtab as being an include of PST. */
6758 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6759 struct objfile *objfile)
6761 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6763 if (!IS_ABSOLUTE_PATH (subpst->filename))
6765 /* It shares objfile->objfile_obstack. */
6766 subpst->dirname = pst->dirname;
6769 subpst->textlow = 0;
6770 subpst->texthigh = 0;
6772 subpst->dependencies
6773 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
6774 subpst->dependencies[0] = pst;
6775 subpst->number_of_dependencies = 1;
6777 subpst->globals_offset = 0;
6778 subpst->n_global_syms = 0;
6779 subpst->statics_offset = 0;
6780 subpst->n_static_syms = 0;
6781 subpst->compunit_symtab = NULL;
6782 subpst->read_symtab = pst->read_symtab;
6785 /* No private part is necessary for include psymtabs. This property
6786 can be used to differentiate between such include psymtabs and
6787 the regular ones. */
6788 subpst->read_symtab_private = NULL;
6791 /* Read the Line Number Program data and extract the list of files
6792 included by the source file represented by PST. Build an include
6793 partial symtab for each of these included files. */
6796 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6797 struct die_info *die,
6798 struct partial_symtab *pst)
6801 struct attribute *attr;
6803 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6805 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6807 return; /* No linetable, so no includes. */
6809 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
6810 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
6814 hash_signatured_type (const void *item)
6816 const struct signatured_type *sig_type
6817 = (const struct signatured_type *) item;
6819 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6820 return sig_type->signature;
6824 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6826 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6827 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6829 return lhs->signature == rhs->signature;
6832 /* Allocate a hash table for signatured types. */
6835 allocate_signatured_type_table (struct objfile *objfile)
6837 return htab_create_alloc_ex (41,
6838 hash_signatured_type,
6841 &objfile->objfile_obstack,
6842 hashtab_obstack_allocate,
6843 dummy_obstack_deallocate);
6846 /* A helper function to add a signatured type CU to a table. */
6849 add_signatured_type_cu_to_table (void **slot, void *datum)
6851 struct signatured_type *sigt = (struct signatured_type *) *slot;
6852 struct signatured_type ***datap = (struct signatured_type ***) datum;
6860 /* A helper for create_debug_types_hash_table. Read types from SECTION
6861 and fill them into TYPES_HTAB. It will process only type units,
6862 therefore DW_UT_type. */
6865 create_debug_type_hash_table (struct dwo_file *dwo_file,
6866 dwarf2_section_info *section, htab_t &types_htab,
6867 rcuh_kind section_kind)
6869 struct objfile *objfile = dwarf2_per_objfile->objfile;
6870 struct dwarf2_section_info *abbrev_section;
6872 const gdb_byte *info_ptr, *end_ptr;
6874 abbrev_section = (dwo_file != NULL
6875 ? &dwo_file->sections.abbrev
6876 : &dwarf2_per_objfile->abbrev);
6878 if (dwarf_read_debug)
6879 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6880 get_section_name (section),
6881 get_section_file_name (abbrev_section));
6883 dwarf2_read_section (objfile, section);
6884 info_ptr = section->buffer;
6886 if (info_ptr == NULL)
6889 /* We can't set abfd until now because the section may be empty or
6890 not present, in which case the bfd is unknown. */
6891 abfd = get_section_bfd_owner (section);
6893 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6894 because we don't need to read any dies: the signature is in the
6897 end_ptr = info_ptr + section->size;
6898 while (info_ptr < end_ptr)
6900 struct signatured_type *sig_type;
6901 struct dwo_unit *dwo_tu;
6903 const gdb_byte *ptr = info_ptr;
6904 struct comp_unit_head header;
6905 unsigned int length;
6907 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6909 /* Initialize it due to a false compiler warning. */
6910 header.signature = -1;
6911 header.type_cu_offset_in_tu = (cu_offset) -1;
6913 /* We need to read the type's signature in order to build the hash
6914 table, but we don't need anything else just yet. */
6916 ptr = read_and_check_comp_unit_head (&header, section,
6917 abbrev_section, ptr, section_kind);
6919 length = get_cu_length (&header);
6921 /* Skip dummy type units. */
6922 if (ptr >= info_ptr + length
6923 || peek_abbrev_code (abfd, ptr) == 0
6924 || header.unit_type != DW_UT_type)
6930 if (types_htab == NULL)
6933 types_htab = allocate_dwo_unit_table (objfile);
6935 types_htab = allocate_signatured_type_table (objfile);
6941 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6943 dwo_tu->dwo_file = dwo_file;
6944 dwo_tu->signature = header.signature;
6945 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6946 dwo_tu->section = section;
6947 dwo_tu->sect_off = sect_off;
6948 dwo_tu->length = length;
6952 /* N.B.: type_offset is not usable if this type uses a DWO file.
6953 The real type_offset is in the DWO file. */
6955 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6956 struct signatured_type);
6957 sig_type->signature = header.signature;
6958 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6959 sig_type->per_cu.objfile = objfile;
6960 sig_type->per_cu.is_debug_types = 1;
6961 sig_type->per_cu.section = section;
6962 sig_type->per_cu.sect_off = sect_off;
6963 sig_type->per_cu.length = length;
6966 slot = htab_find_slot (types_htab,
6967 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6969 gdb_assert (slot != NULL);
6972 sect_offset dup_sect_off;
6976 const struct dwo_unit *dup_tu
6977 = (const struct dwo_unit *) *slot;
6979 dup_sect_off = dup_tu->sect_off;
6983 const struct signatured_type *dup_tu
6984 = (const struct signatured_type *) *slot;
6986 dup_sect_off = dup_tu->per_cu.sect_off;
6989 complaint (&symfile_complaints,
6990 _("debug type entry at offset 0x%x is duplicate to"
6991 " the entry at offset 0x%x, signature %s"),
6992 to_underlying (sect_off), to_underlying (dup_sect_off),
6993 hex_string (header.signature));
6995 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6997 if (dwarf_read_debug > 1)
6998 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
6999 to_underlying (sect_off),
7000 hex_string (header.signature));
7006 /* Create the hash table of all entries in the .debug_types
7007 (or .debug_types.dwo) section(s).
7008 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
7009 otherwise it is NULL.
7011 The result is a pointer to the hash table or NULL if there are no types.
7013 Note: This function processes DWO files only, not DWP files. */
7016 create_debug_types_hash_table (struct dwo_file *dwo_file,
7017 VEC (dwarf2_section_info_def) *types,
7021 struct dwarf2_section_info *section;
7023 if (VEC_empty (dwarf2_section_info_def, types))
7027 VEC_iterate (dwarf2_section_info_def, types, ix, section);
7029 create_debug_type_hash_table (dwo_file, section, types_htab,
7033 /* Create the hash table of all entries in the .debug_types section,
7034 and initialize all_type_units.
7035 The result is zero if there is an error (e.g. missing .debug_types section),
7036 otherwise non-zero. */
7039 create_all_type_units (struct objfile *objfile)
7041 htab_t types_htab = NULL;
7042 struct signatured_type **iter;
7044 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
7045 rcuh_kind::COMPILE);
7046 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
7047 if (types_htab == NULL)
7049 dwarf2_per_objfile->signatured_types = NULL;
7053 dwarf2_per_objfile->signatured_types = types_htab;
7055 dwarf2_per_objfile->n_type_units
7056 = dwarf2_per_objfile->n_allocated_type_units
7057 = htab_elements (types_htab);
7058 dwarf2_per_objfile->all_type_units =
7059 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
7060 iter = &dwarf2_per_objfile->all_type_units[0];
7061 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
7062 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
7063 == dwarf2_per_objfile->n_type_units);
7068 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
7069 If SLOT is non-NULL, it is the entry to use in the hash table.
7070 Otherwise we find one. */
7072 static struct signatured_type *
7073 add_type_unit (ULONGEST sig, void **slot)
7075 struct objfile *objfile = dwarf2_per_objfile->objfile;
7076 int n_type_units = dwarf2_per_objfile->n_type_units;
7077 struct signatured_type *sig_type;
7079 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
7081 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
7083 if (dwarf2_per_objfile->n_allocated_type_units == 0)
7084 dwarf2_per_objfile->n_allocated_type_units = 1;
7085 dwarf2_per_objfile->n_allocated_type_units *= 2;
7086 dwarf2_per_objfile->all_type_units
7087 = XRESIZEVEC (struct signatured_type *,
7088 dwarf2_per_objfile->all_type_units,
7089 dwarf2_per_objfile->n_allocated_type_units);
7090 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
7092 dwarf2_per_objfile->n_type_units = n_type_units;
7094 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7095 struct signatured_type);
7096 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
7097 sig_type->signature = sig;
7098 sig_type->per_cu.is_debug_types = 1;
7099 if (dwarf2_per_objfile->using_index)
7101 sig_type->per_cu.v.quick =
7102 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7103 struct dwarf2_per_cu_quick_data);
7108 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7111 gdb_assert (*slot == NULL);
7113 /* The rest of sig_type must be filled in by the caller. */
7117 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7118 Fill in SIG_ENTRY with DWO_ENTRY. */
7121 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
7122 struct signatured_type *sig_entry,
7123 struct dwo_unit *dwo_entry)
7125 /* Make sure we're not clobbering something we don't expect to. */
7126 gdb_assert (! sig_entry->per_cu.queued);
7127 gdb_assert (sig_entry->per_cu.cu == NULL);
7128 if (dwarf2_per_objfile->using_index)
7130 gdb_assert (sig_entry->per_cu.v.quick != NULL);
7131 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
7134 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7135 gdb_assert (sig_entry->signature == dwo_entry->signature);
7136 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7137 gdb_assert (sig_entry->type_unit_group == NULL);
7138 gdb_assert (sig_entry->dwo_unit == NULL);
7140 sig_entry->per_cu.section = dwo_entry->section;
7141 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7142 sig_entry->per_cu.length = dwo_entry->length;
7143 sig_entry->per_cu.reading_dwo_directly = 1;
7144 sig_entry->per_cu.objfile = objfile;
7145 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7146 sig_entry->dwo_unit = dwo_entry;
7149 /* Subroutine of lookup_signatured_type.
7150 If we haven't read the TU yet, create the signatured_type data structure
7151 for a TU to be read in directly from a DWO file, bypassing the stub.
7152 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7153 using .gdb_index, then when reading a CU we want to stay in the DWO file
7154 containing that CU. Otherwise we could end up reading several other DWO
7155 files (due to comdat folding) to process the transitive closure of all the
7156 mentioned TUs, and that can be slow. The current DWO file will have every
7157 type signature that it needs.
7158 We only do this for .gdb_index because in the psymtab case we already have
7159 to read all the DWOs to build the type unit groups. */
7161 static struct signatured_type *
7162 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7164 struct objfile *objfile = dwarf2_per_objfile->objfile;
7165 struct dwo_file *dwo_file;
7166 struct dwo_unit find_dwo_entry, *dwo_entry;
7167 struct signatured_type find_sig_entry, *sig_entry;
7170 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7172 /* If TU skeletons have been removed then we may not have read in any
7174 if (dwarf2_per_objfile->signatured_types == NULL)
7176 dwarf2_per_objfile->signatured_types
7177 = allocate_signatured_type_table (objfile);
7180 /* We only ever need to read in one copy of a signatured type.
7181 Use the global signatured_types array to do our own comdat-folding
7182 of types. If this is the first time we're reading this TU, and
7183 the TU has an entry in .gdb_index, replace the recorded data from
7184 .gdb_index with this TU. */
7186 find_sig_entry.signature = sig;
7187 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7188 &find_sig_entry, INSERT);
7189 sig_entry = (struct signatured_type *) *slot;
7191 /* We can get here with the TU already read, *or* in the process of being
7192 read. Don't reassign the global entry to point to this DWO if that's
7193 the case. Also note that if the TU is already being read, it may not
7194 have come from a DWO, the program may be a mix of Fission-compiled
7195 code and non-Fission-compiled code. */
7197 /* Have we already tried to read this TU?
7198 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7199 needn't exist in the global table yet). */
7200 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7203 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7204 dwo_unit of the TU itself. */
7205 dwo_file = cu->dwo_unit->dwo_file;
7207 /* Ok, this is the first time we're reading this TU. */
7208 if (dwo_file->tus == NULL)
7210 find_dwo_entry.signature = sig;
7211 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7212 if (dwo_entry == NULL)
7215 /* If the global table doesn't have an entry for this TU, add one. */
7216 if (sig_entry == NULL)
7217 sig_entry = add_type_unit (sig, slot);
7219 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
7220 sig_entry->per_cu.tu_read = 1;
7224 /* Subroutine of lookup_signatured_type.
7225 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7226 then try the DWP file. If the TU stub (skeleton) has been removed then
7227 it won't be in .gdb_index. */
7229 static struct signatured_type *
7230 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7232 struct objfile *objfile = dwarf2_per_objfile->objfile;
7233 struct dwp_file *dwp_file = get_dwp_file ();
7234 struct dwo_unit *dwo_entry;
7235 struct signatured_type find_sig_entry, *sig_entry;
7238 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7239 gdb_assert (dwp_file != NULL);
7241 /* If TU skeletons have been removed then we may not have read in any
7243 if (dwarf2_per_objfile->signatured_types == NULL)
7245 dwarf2_per_objfile->signatured_types
7246 = allocate_signatured_type_table (objfile);
7249 find_sig_entry.signature = sig;
7250 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7251 &find_sig_entry, INSERT);
7252 sig_entry = (struct signatured_type *) *slot;
7254 /* Have we already tried to read this TU?
7255 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7256 needn't exist in the global table yet). */
7257 if (sig_entry != NULL)
7260 if (dwp_file->tus == NULL)
7262 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
7263 sig, 1 /* is_debug_types */);
7264 if (dwo_entry == NULL)
7267 sig_entry = add_type_unit (sig, slot);
7268 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
7273 /* Lookup a signature based type for DW_FORM_ref_sig8.
7274 Returns NULL if signature SIG is not present in the table.
7275 It is up to the caller to complain about this. */
7277 static struct signatured_type *
7278 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7281 && dwarf2_per_objfile->using_index)
7283 /* We're in a DWO/DWP file, and we're using .gdb_index.
7284 These cases require special processing. */
7285 if (get_dwp_file () == NULL)
7286 return lookup_dwo_signatured_type (cu, sig);
7288 return lookup_dwp_signatured_type (cu, sig);
7292 struct signatured_type find_entry, *entry;
7294 if (dwarf2_per_objfile->signatured_types == NULL)
7296 find_entry.signature = sig;
7297 entry = ((struct signatured_type *)
7298 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7303 /* Low level DIE reading support. */
7305 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7308 init_cu_die_reader (struct die_reader_specs *reader,
7309 struct dwarf2_cu *cu,
7310 struct dwarf2_section_info *section,
7311 struct dwo_file *dwo_file)
7313 gdb_assert (section->readin && section->buffer != NULL);
7314 reader->abfd = get_section_bfd_owner (section);
7316 reader->dwo_file = dwo_file;
7317 reader->die_section = section;
7318 reader->buffer = section->buffer;
7319 reader->buffer_end = section->buffer + section->size;
7320 reader->comp_dir = NULL;
7323 /* Subroutine of init_cutu_and_read_dies to simplify it.
7324 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7325 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7328 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7329 from it to the DIE in the DWO. If NULL we are skipping the stub.
7330 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7331 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7332 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7333 STUB_COMP_DIR may be non-NULL.
7334 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7335 are filled in with the info of the DIE from the DWO file.
7336 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
7337 provided an abbrev table to use.
7338 The result is non-zero if a valid (non-dummy) DIE was found. */
7341 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7342 struct dwo_unit *dwo_unit,
7343 int abbrev_table_provided,
7344 struct die_info *stub_comp_unit_die,
7345 const char *stub_comp_dir,
7346 struct die_reader_specs *result_reader,
7347 const gdb_byte **result_info_ptr,
7348 struct die_info **result_comp_unit_die,
7349 int *result_has_children)
7351 struct objfile *objfile = dwarf2_per_objfile->objfile;
7352 struct dwarf2_cu *cu = this_cu->cu;
7353 struct dwarf2_section_info *section;
7355 const gdb_byte *begin_info_ptr, *info_ptr;
7356 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7357 int i,num_extra_attrs;
7358 struct dwarf2_section_info *dwo_abbrev_section;
7359 struct attribute *attr;
7360 struct die_info *comp_unit_die;
7362 /* At most one of these may be provided. */
7363 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7365 /* These attributes aren't processed until later:
7366 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7367 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7368 referenced later. However, these attributes are found in the stub
7369 which we won't have later. In order to not impose this complication
7370 on the rest of the code, we read them here and copy them to the
7379 if (stub_comp_unit_die != NULL)
7381 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7383 if (! this_cu->is_debug_types)
7384 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7385 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7386 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7387 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7388 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7390 /* There should be a DW_AT_addr_base attribute here (if needed).
7391 We need the value before we can process DW_FORM_GNU_addr_index. */
7393 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7395 cu->addr_base = DW_UNSND (attr);
7397 /* There should be a DW_AT_ranges_base attribute here (if needed).
7398 We need the value before we can process DW_AT_ranges. */
7399 cu->ranges_base = 0;
7400 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7402 cu->ranges_base = DW_UNSND (attr);
7404 else if (stub_comp_dir != NULL)
7406 /* Reconstruct the comp_dir attribute to simplify the code below. */
7407 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7408 comp_dir->name = DW_AT_comp_dir;
7409 comp_dir->form = DW_FORM_string;
7410 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7411 DW_STRING (comp_dir) = stub_comp_dir;
7414 /* Set up for reading the DWO CU/TU. */
7415 cu->dwo_unit = dwo_unit;
7416 section = dwo_unit->section;
7417 dwarf2_read_section (objfile, section);
7418 abfd = get_section_bfd_owner (section);
7419 begin_info_ptr = info_ptr = (section->buffer
7420 + to_underlying (dwo_unit->sect_off));
7421 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7422 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
7424 if (this_cu->is_debug_types)
7426 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7428 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
7430 info_ptr, rcuh_kind::TYPE);
7431 /* This is not an assert because it can be caused by bad debug info. */
7432 if (sig_type->signature != cu->header.signature)
7434 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7435 " TU at offset 0x%x [in module %s]"),
7436 hex_string (sig_type->signature),
7437 hex_string (cu->header.signature),
7438 to_underlying (dwo_unit->sect_off),
7439 bfd_get_filename (abfd));
7441 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7442 /* For DWOs coming from DWP files, we don't know the CU length
7443 nor the type's offset in the TU until now. */
7444 dwo_unit->length = get_cu_length (&cu->header);
7445 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7447 /* Establish the type offset that can be used to lookup the type.
7448 For DWO files, we don't know it until now. */
7449 sig_type->type_offset_in_section
7450 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7454 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
7456 info_ptr, rcuh_kind::COMPILE);
7457 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7458 /* For DWOs coming from DWP files, we don't know the CU length
7460 dwo_unit->length = get_cu_length (&cu->header);
7463 /* Replace the CU's original abbrev table with the DWO's.
7464 Reminder: We can't read the abbrev table until we've read the header. */
7465 if (abbrev_table_provided)
7467 /* Don't free the provided abbrev table, the caller of
7468 init_cutu_and_read_dies owns it. */
7469 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
7470 /* Ensure the DWO abbrev table gets freed. */
7471 make_cleanup (dwarf2_free_abbrev_table, cu);
7475 dwarf2_free_abbrev_table (cu);
7476 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
7477 /* Leave any existing abbrev table cleanup as is. */
7480 /* Read in the die, but leave space to copy over the attributes
7481 from the stub. This has the benefit of simplifying the rest of
7482 the code - all the work to maintain the illusion of a single
7483 DW_TAG_{compile,type}_unit DIE is done here. */
7484 num_extra_attrs = ((stmt_list != NULL)
7488 + (comp_dir != NULL));
7489 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7490 result_has_children, num_extra_attrs);
7492 /* Copy over the attributes from the stub to the DIE we just read in. */
7493 comp_unit_die = *result_comp_unit_die;
7494 i = comp_unit_die->num_attrs;
7495 if (stmt_list != NULL)
7496 comp_unit_die->attrs[i++] = *stmt_list;
7498 comp_unit_die->attrs[i++] = *low_pc;
7499 if (high_pc != NULL)
7500 comp_unit_die->attrs[i++] = *high_pc;
7502 comp_unit_die->attrs[i++] = *ranges;
7503 if (comp_dir != NULL)
7504 comp_unit_die->attrs[i++] = *comp_dir;
7505 comp_unit_die->num_attrs += num_extra_attrs;
7507 if (dwarf_die_debug)
7509 fprintf_unfiltered (gdb_stdlog,
7510 "Read die from %s@0x%x of %s:\n",
7511 get_section_name (section),
7512 (unsigned) (begin_info_ptr - section->buffer),
7513 bfd_get_filename (abfd));
7514 dump_die (comp_unit_die, dwarf_die_debug);
7517 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7518 TUs by skipping the stub and going directly to the entry in the DWO file.
7519 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7520 to get it via circuitous means. Blech. */
7521 if (comp_dir != NULL)
7522 result_reader->comp_dir = DW_STRING (comp_dir);
7524 /* Skip dummy compilation units. */
7525 if (info_ptr >= begin_info_ptr + dwo_unit->length
7526 || peek_abbrev_code (abfd, info_ptr) == 0)
7529 *result_info_ptr = info_ptr;
7533 /* Subroutine of init_cutu_and_read_dies to simplify it.
7534 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7535 Returns NULL if the specified DWO unit cannot be found. */
7537 static struct dwo_unit *
7538 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7539 struct die_info *comp_unit_die)
7541 struct dwarf2_cu *cu = this_cu->cu;
7543 struct dwo_unit *dwo_unit;
7544 const char *comp_dir, *dwo_name;
7546 gdb_assert (cu != NULL);
7548 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7549 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7550 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7552 if (this_cu->is_debug_types)
7554 struct signatured_type *sig_type;
7556 /* Since this_cu is the first member of struct signatured_type,
7557 we can go from a pointer to one to a pointer to the other. */
7558 sig_type = (struct signatured_type *) this_cu;
7559 signature = sig_type->signature;
7560 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7564 struct attribute *attr;
7566 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7568 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7570 dwo_name, objfile_name (this_cu->objfile));
7571 signature = DW_UNSND (attr);
7572 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7579 /* Subroutine of init_cutu_and_read_dies to simplify it.
7580 See it for a description of the parameters.
7581 Read a TU directly from a DWO file, bypassing the stub.
7583 Note: This function could be a little bit simpler if we shared cleanups
7584 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
7585 to do, so we keep this function self-contained. Or we could move this
7586 into our caller, but it's complex enough already. */
7589 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7590 int use_existing_cu, int keep,
7591 die_reader_func_ftype *die_reader_func,
7594 struct dwarf2_cu *cu;
7595 struct signatured_type *sig_type;
7596 struct cleanup *cleanups, *free_cu_cleanup = NULL;
7597 struct die_reader_specs reader;
7598 const gdb_byte *info_ptr;
7599 struct die_info *comp_unit_die;
7602 /* Verify we can do the following downcast, and that we have the
7604 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7605 sig_type = (struct signatured_type *) this_cu;
7606 gdb_assert (sig_type->dwo_unit != NULL);
7608 cleanups = make_cleanup (null_cleanup, NULL);
7610 if (use_existing_cu && this_cu->cu != NULL)
7612 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7614 /* There's no need to do the rereading_dwo_cu handling that
7615 init_cutu_and_read_dies does since we don't read the stub. */
7619 /* If !use_existing_cu, this_cu->cu must be NULL. */
7620 gdb_assert (this_cu->cu == NULL);
7621 cu = XNEW (struct dwarf2_cu);
7622 init_one_comp_unit (cu, this_cu);
7623 /* If an error occurs while loading, release our storage. */
7624 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
7627 /* A future optimization, if needed, would be to use an existing
7628 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7629 could share abbrev tables. */
7631 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7632 0 /* abbrev_table_provided */,
7633 NULL /* stub_comp_unit_die */,
7634 sig_type->dwo_unit->dwo_file->comp_dir,
7636 &comp_unit_die, &has_children) == 0)
7639 do_cleanups (cleanups);
7643 /* All the "real" work is done here. */
7644 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7646 /* This duplicates the code in init_cutu_and_read_dies,
7647 but the alternative is making the latter more complex.
7648 This function is only for the special case of using DWO files directly:
7649 no point in overly complicating the general case just to handle this. */
7650 if (free_cu_cleanup != NULL)
7654 /* We've successfully allocated this compilation unit. Let our
7655 caller clean it up when finished with it. */
7656 discard_cleanups (free_cu_cleanup);
7658 /* We can only discard free_cu_cleanup and all subsequent cleanups.
7659 So we have to manually free the abbrev table. */
7660 dwarf2_free_abbrev_table (cu);
7662 /* Link this CU into read_in_chain. */
7663 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7664 dwarf2_per_objfile->read_in_chain = this_cu;
7667 do_cleanups (free_cu_cleanup);
7670 do_cleanups (cleanups);
7673 /* Initialize a CU (or TU) and read its DIEs.
7674 If the CU defers to a DWO file, read the DWO file as well.
7676 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7677 Otherwise the table specified in the comp unit header is read in and used.
7678 This is an optimization for when we already have the abbrev table.
7680 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7681 Otherwise, a new CU is allocated with xmalloc.
7683 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7684 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7686 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7687 linker) then DIE_READER_FUNC will not get called. */
7690 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7691 struct abbrev_table *abbrev_table,
7692 int use_existing_cu, int keep,
7693 die_reader_func_ftype *die_reader_func,
7696 struct objfile *objfile = dwarf2_per_objfile->objfile;
7697 struct dwarf2_section_info *section = this_cu->section;
7698 bfd *abfd = get_section_bfd_owner (section);
7699 struct dwarf2_cu *cu;
7700 const gdb_byte *begin_info_ptr, *info_ptr;
7701 struct die_reader_specs reader;
7702 struct die_info *comp_unit_die;
7704 struct attribute *attr;
7705 struct cleanup *cleanups, *free_cu_cleanup = NULL;
7706 struct signatured_type *sig_type = NULL;
7707 struct dwarf2_section_info *abbrev_section;
7708 /* Non-zero if CU currently points to a DWO file and we need to
7709 reread it. When this happens we need to reread the skeleton die
7710 before we can reread the DWO file (this only applies to CUs, not TUs). */
7711 int rereading_dwo_cu = 0;
7713 if (dwarf_die_debug)
7714 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
7715 this_cu->is_debug_types ? "type" : "comp",
7716 to_underlying (this_cu->sect_off));
7718 if (use_existing_cu)
7721 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7722 file (instead of going through the stub), short-circuit all of this. */
7723 if (this_cu->reading_dwo_directly)
7725 /* Narrow down the scope of possibilities to have to understand. */
7726 gdb_assert (this_cu->is_debug_types);
7727 gdb_assert (abbrev_table == NULL);
7728 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7729 die_reader_func, data);
7733 cleanups = make_cleanup (null_cleanup, NULL);
7735 /* This is cheap if the section is already read in. */
7736 dwarf2_read_section (objfile, section);
7738 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7740 abbrev_section = get_abbrev_section_for_cu (this_cu);
7742 if (use_existing_cu && this_cu->cu != NULL)
7745 /* If this CU is from a DWO file we need to start over, we need to
7746 refetch the attributes from the skeleton CU.
7747 This could be optimized by retrieving those attributes from when we
7748 were here the first time: the previous comp_unit_die was stored in
7749 comp_unit_obstack. But there's no data yet that we need this
7751 if (cu->dwo_unit != NULL)
7752 rereading_dwo_cu = 1;
7756 /* If !use_existing_cu, this_cu->cu must be NULL. */
7757 gdb_assert (this_cu->cu == NULL);
7758 cu = XNEW (struct dwarf2_cu);
7759 init_one_comp_unit (cu, this_cu);
7760 /* If an error occurs while loading, release our storage. */
7761 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
7764 /* Get the header. */
7765 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7767 /* We already have the header, there's no need to read it in again. */
7768 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7772 if (this_cu->is_debug_types)
7774 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
7775 abbrev_section, info_ptr,
7778 /* Since per_cu is the first member of struct signatured_type,
7779 we can go from a pointer to one to a pointer to the other. */
7780 sig_type = (struct signatured_type *) this_cu;
7781 gdb_assert (sig_type->signature == cu->header.signature);
7782 gdb_assert (sig_type->type_offset_in_tu
7783 == cu->header.type_cu_offset_in_tu);
7784 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7786 /* LENGTH has not been set yet for type units if we're
7787 using .gdb_index. */
7788 this_cu->length = get_cu_length (&cu->header);
7790 /* Establish the type offset that can be used to lookup the type. */
7791 sig_type->type_offset_in_section =
7792 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7794 this_cu->dwarf_version = cu->header.version;
7798 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
7801 rcuh_kind::COMPILE);
7803 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7804 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7805 this_cu->dwarf_version = cu->header.version;
7809 /* Skip dummy compilation units. */
7810 if (info_ptr >= begin_info_ptr + this_cu->length
7811 || peek_abbrev_code (abfd, info_ptr) == 0)
7813 do_cleanups (cleanups);
7817 /* If we don't have them yet, read the abbrevs for this compilation unit.
7818 And if we need to read them now, make sure they're freed when we're
7819 done. Note that it's important that if the CU had an abbrev table
7820 on entry we don't free it when we're done: Somewhere up the call stack
7821 it may be in use. */
7822 if (abbrev_table != NULL)
7824 gdb_assert (cu->abbrev_table == NULL);
7825 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7826 cu->abbrev_table = abbrev_table;
7828 else if (cu->abbrev_table == NULL)
7830 dwarf2_read_abbrevs (cu, abbrev_section);
7831 make_cleanup (dwarf2_free_abbrev_table, cu);
7833 else if (rereading_dwo_cu)
7835 dwarf2_free_abbrev_table (cu);
7836 dwarf2_read_abbrevs (cu, abbrev_section);
7839 /* Read the top level CU/TU die. */
7840 init_cu_die_reader (&reader, cu, section, NULL);
7841 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7843 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7845 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7846 DWO CU, that this test will fail (the attribute will not be present). */
7847 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7850 struct dwo_unit *dwo_unit;
7851 struct die_info *dwo_comp_unit_die;
7855 complaint (&symfile_complaints,
7856 _("compilation unit with DW_AT_GNU_dwo_name"
7857 " has children (offset 0x%x) [in module %s]"),
7858 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
7860 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7861 if (dwo_unit != NULL)
7863 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7864 abbrev_table != NULL,
7865 comp_unit_die, NULL,
7867 &dwo_comp_unit_die, &has_children) == 0)
7870 do_cleanups (cleanups);
7873 comp_unit_die = dwo_comp_unit_die;
7877 /* Yikes, we couldn't find the rest of the DIE, we only have
7878 the stub. A complaint has already been logged. There's
7879 not much more we can do except pass on the stub DIE to
7880 die_reader_func. We don't want to throw an error on bad
7885 /* All of the above is setup for this call. Yikes. */
7886 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7888 /* Done, clean up. */
7889 if (free_cu_cleanup != NULL)
7893 /* We've successfully allocated this compilation unit. Let our
7894 caller clean it up when finished with it. */
7895 discard_cleanups (free_cu_cleanup);
7897 /* We can only discard free_cu_cleanup and all subsequent cleanups.
7898 So we have to manually free the abbrev table. */
7899 dwarf2_free_abbrev_table (cu);
7901 /* Link this CU into read_in_chain. */
7902 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7903 dwarf2_per_objfile->read_in_chain = this_cu;
7906 do_cleanups (free_cu_cleanup);
7909 do_cleanups (cleanups);
7912 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7913 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7914 to have already done the lookup to find the DWO file).
7916 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7917 THIS_CU->is_debug_types, but nothing else.
7919 We fill in THIS_CU->length.
7921 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7922 linker) then DIE_READER_FUNC will not get called.
7924 THIS_CU->cu is always freed when done.
7925 This is done in order to not leave THIS_CU->cu in a state where we have
7926 to care whether it refers to the "main" CU or the DWO CU. */
7929 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7930 struct dwo_file *dwo_file,
7931 die_reader_func_ftype *die_reader_func,
7934 struct objfile *objfile = dwarf2_per_objfile->objfile;
7935 struct dwarf2_section_info *section = this_cu->section;
7936 bfd *abfd = get_section_bfd_owner (section);
7937 struct dwarf2_section_info *abbrev_section;
7938 struct dwarf2_cu cu;
7939 const gdb_byte *begin_info_ptr, *info_ptr;
7940 struct die_reader_specs reader;
7941 struct cleanup *cleanups;
7942 struct die_info *comp_unit_die;
7945 if (dwarf_die_debug)
7946 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
7947 this_cu->is_debug_types ? "type" : "comp",
7948 to_underlying (this_cu->sect_off));
7950 gdb_assert (this_cu->cu == NULL);
7952 abbrev_section = (dwo_file != NULL
7953 ? &dwo_file->sections.abbrev
7954 : get_abbrev_section_for_cu (this_cu));
7956 /* This is cheap if the section is already read in. */
7957 dwarf2_read_section (objfile, section);
7959 init_one_comp_unit (&cu, this_cu);
7961 cleanups = make_cleanup (free_stack_comp_unit, &cu);
7963 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7964 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
7965 abbrev_section, info_ptr,
7966 (this_cu->is_debug_types
7968 : rcuh_kind::COMPILE));
7970 this_cu->length = get_cu_length (&cu.header);
7972 /* Skip dummy compilation units. */
7973 if (info_ptr >= begin_info_ptr + this_cu->length
7974 || peek_abbrev_code (abfd, info_ptr) == 0)
7976 do_cleanups (cleanups);
7980 dwarf2_read_abbrevs (&cu, abbrev_section);
7981 make_cleanup (dwarf2_free_abbrev_table, &cu);
7983 init_cu_die_reader (&reader, &cu, section, dwo_file);
7984 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7986 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7988 do_cleanups (cleanups);
7991 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7992 does not lookup the specified DWO file.
7993 This cannot be used to read DWO files.
7995 THIS_CU->cu is always freed when done.
7996 This is done in order to not leave THIS_CU->cu in a state where we have
7997 to care whether it refers to the "main" CU or the DWO CU.
7998 We can revisit this if the data shows there's a performance issue. */
8001 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
8002 die_reader_func_ftype *die_reader_func,
8005 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
8008 /* Type Unit Groups.
8010 Type Unit Groups are a way to collapse the set of all TUs (type units) into
8011 a more manageable set. The grouping is done by DW_AT_stmt_list entry
8012 so that all types coming from the same compilation (.o file) are grouped
8013 together. A future step could be to put the types in the same symtab as
8014 the CU the types ultimately came from. */
8017 hash_type_unit_group (const void *item)
8019 const struct type_unit_group *tu_group
8020 = (const struct type_unit_group *) item;
8022 return hash_stmt_list_entry (&tu_group->hash);
8026 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
8028 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
8029 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
8031 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
8034 /* Allocate a hash table for type unit groups. */
8037 allocate_type_unit_groups_table (void)
8039 return htab_create_alloc_ex (3,
8040 hash_type_unit_group,
8043 &dwarf2_per_objfile->objfile->objfile_obstack,
8044 hashtab_obstack_allocate,
8045 dummy_obstack_deallocate);
8048 /* Type units that don't have DW_AT_stmt_list are grouped into their own
8049 partial symtabs. We combine several TUs per psymtab to not let the size
8050 of any one psymtab grow too big. */
8051 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
8052 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
8054 /* Helper routine for get_type_unit_group.
8055 Create the type_unit_group object used to hold one or more TUs. */
8057 static struct type_unit_group *
8058 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
8060 struct objfile *objfile = dwarf2_per_objfile->objfile;
8061 struct dwarf2_per_cu_data *per_cu;
8062 struct type_unit_group *tu_group;
8064 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8065 struct type_unit_group);
8066 per_cu = &tu_group->per_cu;
8067 per_cu->objfile = objfile;
8069 if (dwarf2_per_objfile->using_index)
8071 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8072 struct dwarf2_per_cu_quick_data);
8076 unsigned int line_offset = to_underlying (line_offset_struct);
8077 struct partial_symtab *pst;
8080 /* Give the symtab a useful name for debug purposes. */
8081 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
8082 name = xstrprintf ("<type_units_%d>",
8083 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
8085 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
8087 pst = create_partial_symtab (per_cu, name);
8093 tu_group->hash.dwo_unit = cu->dwo_unit;
8094 tu_group->hash.line_sect_off = line_offset_struct;
8099 /* Look up the type_unit_group for type unit CU, and create it if necessary.
8100 STMT_LIST is a DW_AT_stmt_list attribute. */
8102 static struct type_unit_group *
8103 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
8105 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8106 struct type_unit_group *tu_group;
8108 unsigned int line_offset;
8109 struct type_unit_group type_unit_group_for_lookup;
8111 if (dwarf2_per_objfile->type_unit_groups == NULL)
8113 dwarf2_per_objfile->type_unit_groups =
8114 allocate_type_unit_groups_table ();
8117 /* Do we need to create a new group, or can we use an existing one? */
8121 line_offset = DW_UNSND (stmt_list);
8122 ++tu_stats->nr_symtab_sharers;
8126 /* Ugh, no stmt_list. Rare, but we have to handle it.
8127 We can do various things here like create one group per TU or
8128 spread them over multiple groups to split up the expansion work.
8129 To avoid worst case scenarios (too many groups or too large groups)
8130 we, umm, group them in bunches. */
8131 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
8132 | (tu_stats->nr_stmt_less_type_units
8133 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
8134 ++tu_stats->nr_stmt_less_type_units;
8137 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
8138 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
8139 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8140 &type_unit_group_for_lookup, INSERT);
8143 tu_group = (struct type_unit_group *) *slot;
8144 gdb_assert (tu_group != NULL);
8148 sect_offset line_offset_struct = (sect_offset) line_offset;
8149 tu_group = create_type_unit_group (cu, line_offset_struct);
8151 ++tu_stats->nr_symtabs;
8157 /* Partial symbol tables. */
8159 /* Create a psymtab named NAME and assign it to PER_CU.
8161 The caller must fill in the following details:
8162 dirname, textlow, texthigh. */
8164 static struct partial_symtab *
8165 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8167 struct objfile *objfile = per_cu->objfile;
8168 struct partial_symtab *pst;
8170 pst = start_psymtab_common (objfile, name, 0,
8171 objfile->global_psymbols,
8172 objfile->static_psymbols);
8174 pst->psymtabs_addrmap_supported = 1;
8176 /* This is the glue that links PST into GDB's symbol API. */
8177 pst->read_symtab_private = per_cu;
8178 pst->read_symtab = dwarf2_read_symtab;
8179 per_cu->v.psymtab = pst;
8184 /* The DATA object passed to process_psymtab_comp_unit_reader has this
8187 struct process_psymtab_comp_unit_data
8189 /* True if we are reading a DW_TAG_partial_unit. */
8191 int want_partial_unit;
8193 /* The "pretend" language that is used if the CU doesn't declare a
8196 enum language pretend_language;
8199 /* die_reader_func for process_psymtab_comp_unit. */
8202 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8203 const gdb_byte *info_ptr,
8204 struct die_info *comp_unit_die,
8208 struct dwarf2_cu *cu = reader->cu;
8209 struct objfile *objfile = cu->objfile;
8210 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8211 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8213 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8214 struct partial_symtab *pst;
8215 enum pc_bounds_kind cu_bounds_kind;
8216 const char *filename;
8217 struct process_psymtab_comp_unit_data *info
8218 = (struct process_psymtab_comp_unit_data *) data;
8220 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8223 gdb_assert (! per_cu->is_debug_types);
8225 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8227 cu->list_in_scope = &file_symbols;
8229 /* Allocate a new partial symbol table structure. */
8230 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8231 if (filename == NULL)
8234 pst = create_partial_symtab (per_cu, filename);
8236 /* This must be done before calling dwarf2_build_include_psymtabs. */
8237 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8239 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8241 dwarf2_find_base_address (comp_unit_die, cu);
8243 /* Possibly set the default values of LOWPC and HIGHPC from
8245 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8246 &best_highpc, cu, pst);
8247 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8248 /* Store the contiguous range if it is not empty; it can be empty for
8249 CUs with no code. */
8250 addrmap_set_empty (objfile->psymtabs_addrmap,
8251 gdbarch_adjust_dwarf2_addr (gdbarch,
8252 best_lowpc + baseaddr),
8253 gdbarch_adjust_dwarf2_addr (gdbarch,
8254 best_highpc + baseaddr) - 1,
8257 /* Check if comp unit has_children.
8258 If so, read the rest of the partial symbols from this comp unit.
8259 If not, there's no more debug_info for this comp unit. */
8262 struct partial_die_info *first_die;
8263 CORE_ADDR lowpc, highpc;
8265 lowpc = ((CORE_ADDR) -1);
8266 highpc = ((CORE_ADDR) 0);
8268 first_die = load_partial_dies (reader, info_ptr, 1);
8270 scan_partial_symbols (first_die, &lowpc, &highpc,
8271 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8273 /* If we didn't find a lowpc, set it to highpc to avoid
8274 complaints from `maint check'. */
8275 if (lowpc == ((CORE_ADDR) -1))
8278 /* If the compilation unit didn't have an explicit address range,
8279 then use the information extracted from its child dies. */
8280 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8283 best_highpc = highpc;
8286 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8287 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
8289 end_psymtab_common (objfile, pst);
8291 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8294 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8295 struct dwarf2_per_cu_data *iter;
8297 /* Fill in 'dependencies' here; we fill in 'users' in a
8299 pst->number_of_dependencies = len;
8301 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8303 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8306 pst->dependencies[i] = iter->v.psymtab;
8308 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8311 /* Get the list of files included in the current compilation unit,
8312 and build a psymtab for each of them. */
8313 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8315 if (dwarf_read_debug)
8317 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8319 fprintf_unfiltered (gdb_stdlog,
8320 "Psymtab for %s unit @0x%x: %s - %s"
8321 ", %d global, %d static syms\n",
8322 per_cu->is_debug_types ? "type" : "comp",
8323 to_underlying (per_cu->sect_off),
8324 paddress (gdbarch, pst->textlow),
8325 paddress (gdbarch, pst->texthigh),
8326 pst->n_global_syms, pst->n_static_syms);
8330 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8331 Process compilation unit THIS_CU for a psymtab. */
8334 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8335 int want_partial_unit,
8336 enum language pretend_language)
8338 /* If this compilation unit was already read in, free the
8339 cached copy in order to read it in again. This is
8340 necessary because we skipped some symbols when we first
8341 read in the compilation unit (see load_partial_dies).
8342 This problem could be avoided, but the benefit is unclear. */
8343 if (this_cu->cu != NULL)
8344 free_one_cached_comp_unit (this_cu);
8346 if (this_cu->is_debug_types)
8347 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8351 process_psymtab_comp_unit_data info;
8352 info.want_partial_unit = want_partial_unit;
8353 info.pretend_language = pretend_language;
8354 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8355 process_psymtab_comp_unit_reader, &info);
8358 /* Age out any secondary CUs. */
8359 age_cached_comp_units ();
8362 /* Reader function for build_type_psymtabs. */
8365 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8366 const gdb_byte *info_ptr,
8367 struct die_info *type_unit_die,
8371 struct objfile *objfile = dwarf2_per_objfile->objfile;
8372 struct dwarf2_cu *cu = reader->cu;
8373 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8374 struct signatured_type *sig_type;
8375 struct type_unit_group *tu_group;
8376 struct attribute *attr;
8377 struct partial_die_info *first_die;
8378 CORE_ADDR lowpc, highpc;
8379 struct partial_symtab *pst;
8381 gdb_assert (data == NULL);
8382 gdb_assert (per_cu->is_debug_types);
8383 sig_type = (struct signatured_type *) per_cu;
8388 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8389 tu_group = get_type_unit_group (cu, attr);
8391 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8393 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8394 cu->list_in_scope = &file_symbols;
8395 pst = create_partial_symtab (per_cu, "");
8398 first_die = load_partial_dies (reader, info_ptr, 1);
8400 lowpc = (CORE_ADDR) -1;
8401 highpc = (CORE_ADDR) 0;
8402 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8404 end_psymtab_common (objfile, pst);
8407 /* Struct used to sort TUs by their abbreviation table offset. */
8409 struct tu_abbrev_offset
8411 struct signatured_type *sig_type;
8412 sect_offset abbrev_offset;
8415 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
8418 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
8420 const struct tu_abbrev_offset * const *a
8421 = (const struct tu_abbrev_offset * const*) ap;
8422 const struct tu_abbrev_offset * const *b
8423 = (const struct tu_abbrev_offset * const*) bp;
8424 sect_offset aoff = (*a)->abbrev_offset;
8425 sect_offset boff = (*b)->abbrev_offset;
8427 return (aoff > boff) - (aoff < boff);
8430 /* Efficiently read all the type units.
8431 This does the bulk of the work for build_type_psymtabs.
8433 The efficiency is because we sort TUs by the abbrev table they use and
8434 only read each abbrev table once. In one program there are 200K TUs
8435 sharing 8K abbrev tables.
8437 The main purpose of this function is to support building the
8438 dwarf2_per_objfile->type_unit_groups table.
8439 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8440 can collapse the search space by grouping them by stmt_list.
8441 The savings can be significant, in the same program from above the 200K TUs
8442 share 8K stmt_list tables.
8444 FUNC is expected to call get_type_unit_group, which will create the
8445 struct type_unit_group if necessary and add it to
8446 dwarf2_per_objfile->type_unit_groups. */
8449 build_type_psymtabs_1 (void)
8451 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8452 struct cleanup *cleanups;
8453 struct abbrev_table *abbrev_table;
8454 sect_offset abbrev_offset;
8455 struct tu_abbrev_offset *sorted_by_abbrev;
8458 /* It's up to the caller to not call us multiple times. */
8459 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8461 if (dwarf2_per_objfile->n_type_units == 0)
8464 /* TUs typically share abbrev tables, and there can be way more TUs than
8465 abbrev tables. Sort by abbrev table to reduce the number of times we
8466 read each abbrev table in.
8467 Alternatives are to punt or to maintain a cache of abbrev tables.
8468 This is simpler and efficient enough for now.
8470 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8471 symtab to use). Typically TUs with the same abbrev offset have the same
8472 stmt_list value too so in practice this should work well.
8474 The basic algorithm here is:
8476 sort TUs by abbrev table
8477 for each TU with same abbrev table:
8478 read abbrev table if first user
8479 read TU top level DIE
8480 [IWBN if DWO skeletons had DW_AT_stmt_list]
8483 if (dwarf_read_debug)
8484 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8486 /* Sort in a separate table to maintain the order of all_type_units
8487 for .gdb_index: TU indices directly index all_type_units. */
8488 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
8489 dwarf2_per_objfile->n_type_units);
8490 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8492 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8494 sorted_by_abbrev[i].sig_type = sig_type;
8495 sorted_by_abbrev[i].abbrev_offset =
8496 read_abbrev_offset (sig_type->per_cu.section,
8497 sig_type->per_cu.sect_off);
8499 cleanups = make_cleanup (xfree, sorted_by_abbrev);
8500 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
8501 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
8503 abbrev_offset = (sect_offset) ~(unsigned) 0;
8504 abbrev_table = NULL;
8505 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
8507 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8509 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8511 /* Switch to the next abbrev table if necessary. */
8512 if (abbrev_table == NULL
8513 || tu->abbrev_offset != abbrev_offset)
8515 if (abbrev_table != NULL)
8517 abbrev_table_free (abbrev_table);
8518 /* Reset to NULL in case abbrev_table_read_table throws
8519 an error: abbrev_table_free_cleanup will get called. */
8520 abbrev_table = NULL;
8522 abbrev_offset = tu->abbrev_offset;
8524 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
8526 ++tu_stats->nr_uniq_abbrev_tables;
8529 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
8530 build_type_psymtabs_reader, NULL);
8533 do_cleanups (cleanups);
8536 /* Print collected type unit statistics. */
8539 print_tu_stats (void)
8541 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8543 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8544 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8545 dwarf2_per_objfile->n_type_units);
8546 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8547 tu_stats->nr_uniq_abbrev_tables);
8548 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8549 tu_stats->nr_symtabs);
8550 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8551 tu_stats->nr_symtab_sharers);
8552 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8553 tu_stats->nr_stmt_less_type_units);
8554 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8555 tu_stats->nr_all_type_units_reallocs);
8558 /* Traversal function for build_type_psymtabs. */
8561 build_type_psymtab_dependencies (void **slot, void *info)
8563 struct objfile *objfile = dwarf2_per_objfile->objfile;
8564 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8565 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8566 struct partial_symtab *pst = per_cu->v.psymtab;
8567 int len = VEC_length (sig_type_ptr, tu_group->tus);
8568 struct signatured_type *iter;
8571 gdb_assert (len > 0);
8572 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8574 pst->number_of_dependencies = len;
8576 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8578 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8581 gdb_assert (iter->per_cu.is_debug_types);
8582 pst->dependencies[i] = iter->per_cu.v.psymtab;
8583 iter->type_unit_group = tu_group;
8586 VEC_free (sig_type_ptr, tu_group->tus);
8591 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8592 Build partial symbol tables for the .debug_types comp-units. */
8595 build_type_psymtabs (struct objfile *objfile)
8597 if (! create_all_type_units (objfile))
8600 build_type_psymtabs_1 ();
8603 /* Traversal function for process_skeletonless_type_unit.
8604 Read a TU in a DWO file and build partial symbols for it. */
8607 process_skeletonless_type_unit (void **slot, void *info)
8609 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8610 struct objfile *objfile = (struct objfile *) info;
8611 struct signatured_type find_entry, *entry;
8613 /* If this TU doesn't exist in the global table, add it and read it in. */
8615 if (dwarf2_per_objfile->signatured_types == NULL)
8617 dwarf2_per_objfile->signatured_types
8618 = allocate_signatured_type_table (objfile);
8621 find_entry.signature = dwo_unit->signature;
8622 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8624 /* If we've already seen this type there's nothing to do. What's happening
8625 is we're doing our own version of comdat-folding here. */
8629 /* This does the job that create_all_type_units would have done for
8631 entry = add_type_unit (dwo_unit->signature, slot);
8632 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
8635 /* This does the job that build_type_psymtabs_1 would have done. */
8636 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8637 build_type_psymtabs_reader, NULL);
8642 /* Traversal function for process_skeletonless_type_units. */
8645 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8647 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8649 if (dwo_file->tus != NULL)
8651 htab_traverse_noresize (dwo_file->tus,
8652 process_skeletonless_type_unit, info);
8658 /* Scan all TUs of DWO files, verifying we've processed them.
8659 This is needed in case a TU was emitted without its skeleton.
8660 Note: This can't be done until we know what all the DWO files are. */
8663 process_skeletonless_type_units (struct objfile *objfile)
8665 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8666 if (get_dwp_file () == NULL
8667 && dwarf2_per_objfile->dwo_files != NULL)
8669 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8670 process_dwo_file_for_skeletonless_type_units,
8675 /* Compute the 'user' field for each psymtab in OBJFILE. */
8678 set_partial_user (struct objfile *objfile)
8682 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8684 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
8685 struct partial_symtab *pst = per_cu->v.psymtab;
8691 for (j = 0; j < pst->number_of_dependencies; ++j)
8693 /* Set the 'user' field only if it is not already set. */
8694 if (pst->dependencies[j]->user == NULL)
8695 pst->dependencies[j]->user = pst;
8700 /* Build the partial symbol table by doing a quick pass through the
8701 .debug_info and .debug_abbrev sections. */
8704 dwarf2_build_psymtabs_hard (struct objfile *objfile)
8706 struct cleanup *back_to;
8709 if (dwarf_read_debug)
8711 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8712 objfile_name (objfile));
8715 dwarf2_per_objfile->reading_partial_symbols = 1;
8717 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8719 /* Any cached compilation units will be linked by the per-objfile
8720 read_in_chain. Make sure to free them when we're done. */
8721 back_to = make_cleanup (free_cached_comp_units, NULL);
8723 build_type_psymtabs (objfile);
8725 create_all_comp_units (objfile);
8727 /* Create a temporary address map on a temporary obstack. We later
8728 copy this to the final obstack. */
8729 auto_obstack temp_obstack;
8731 scoped_restore save_psymtabs_addrmap
8732 = make_scoped_restore (&objfile->psymtabs_addrmap,
8733 addrmap_create_mutable (&temp_obstack));
8735 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8737 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
8739 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8742 /* This has to wait until we read the CUs, we need the list of DWOs. */
8743 process_skeletonless_type_units (objfile);
8745 /* Now that all TUs have been processed we can fill in the dependencies. */
8746 if (dwarf2_per_objfile->type_unit_groups != NULL)
8748 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8749 build_type_psymtab_dependencies, NULL);
8752 if (dwarf_read_debug)
8755 set_partial_user (objfile);
8757 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8758 &objfile->objfile_obstack);
8759 /* At this point we want to keep the address map. */
8760 save_psymtabs_addrmap.release ();
8762 do_cleanups (back_to);
8764 if (dwarf_read_debug)
8765 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8766 objfile_name (objfile));
8769 /* die_reader_func for load_partial_comp_unit. */
8772 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8773 const gdb_byte *info_ptr,
8774 struct die_info *comp_unit_die,
8778 struct dwarf2_cu *cu = reader->cu;
8780 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8782 /* Check if comp unit has_children.
8783 If so, read the rest of the partial symbols from this comp unit.
8784 If not, there's no more debug_info for this comp unit. */
8786 load_partial_dies (reader, info_ptr, 0);
8789 /* Load the partial DIEs for a secondary CU into memory.
8790 This is also used when rereading a primary CU with load_all_dies. */
8793 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8795 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8796 load_partial_comp_unit_reader, NULL);
8800 read_comp_units_from_section (struct objfile *objfile,
8801 struct dwarf2_section_info *section,
8802 struct dwarf2_section_info *abbrev_section,
8803 unsigned int is_dwz,
8806 struct dwarf2_per_cu_data ***all_comp_units)
8808 const gdb_byte *info_ptr;
8810 if (dwarf_read_debug)
8811 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8812 get_section_name (section),
8813 get_section_file_name (section));
8815 dwarf2_read_section (objfile, section);
8817 info_ptr = section->buffer;
8819 while (info_ptr < section->buffer + section->size)
8821 struct dwarf2_per_cu_data *this_cu;
8823 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8825 comp_unit_head cu_header;
8826 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
8827 info_ptr, rcuh_kind::COMPILE);
8829 /* Save the compilation unit for later lookup. */
8830 if (cu_header.unit_type != DW_UT_type)
8832 this_cu = XOBNEW (&objfile->objfile_obstack,
8833 struct dwarf2_per_cu_data);
8834 memset (this_cu, 0, sizeof (*this_cu));
8838 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8839 struct signatured_type);
8840 memset (sig_type, 0, sizeof (*sig_type));
8841 sig_type->signature = cu_header.signature;
8842 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8843 this_cu = &sig_type->per_cu;
8845 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8846 this_cu->sect_off = sect_off;
8847 this_cu->length = cu_header.length + cu_header.initial_length_size;
8848 this_cu->is_dwz = is_dwz;
8849 this_cu->objfile = objfile;
8850 this_cu->section = section;
8852 if (*n_comp_units == *n_allocated)
8855 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
8856 *all_comp_units, *n_allocated);
8858 (*all_comp_units)[*n_comp_units] = this_cu;
8861 info_ptr = info_ptr + this_cu->length;
8865 /* Create a list of all compilation units in OBJFILE.
8866 This is only done for -readnow and building partial symtabs. */
8869 create_all_comp_units (struct objfile *objfile)
8873 struct dwarf2_per_cu_data **all_comp_units;
8874 struct dwz_file *dwz;
8878 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
8880 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info,
8881 &dwarf2_per_objfile->abbrev, 0,
8882 &n_allocated, &n_comp_units, &all_comp_units);
8884 dwz = dwarf2_get_dwz_file ();
8886 read_comp_units_from_section (objfile, &dwz->info, &dwz->abbrev, 1,
8887 &n_allocated, &n_comp_units,
8890 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
8891 struct dwarf2_per_cu_data *,
8893 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
8894 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
8895 xfree (all_comp_units);
8896 dwarf2_per_objfile->n_comp_units = n_comp_units;
8899 /* Process all loaded DIEs for compilation unit CU, starting at
8900 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8901 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8902 DW_AT_ranges). See the comments of add_partial_subprogram on how
8903 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8906 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8907 CORE_ADDR *highpc, int set_addrmap,
8908 struct dwarf2_cu *cu)
8910 struct partial_die_info *pdi;
8912 /* Now, march along the PDI's, descending into ones which have
8913 interesting children but skipping the children of the other ones,
8914 until we reach the end of the compilation unit. */
8920 fixup_partial_die (pdi, cu);
8922 /* Anonymous namespaces or modules have no name but have interesting
8923 children, so we need to look at them. Ditto for anonymous
8926 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8927 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8928 || pdi->tag == DW_TAG_imported_unit)
8932 case DW_TAG_subprogram:
8933 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8935 case DW_TAG_constant:
8936 case DW_TAG_variable:
8937 case DW_TAG_typedef:
8938 case DW_TAG_union_type:
8939 if (!pdi->is_declaration)
8941 add_partial_symbol (pdi, cu);
8944 case DW_TAG_class_type:
8945 case DW_TAG_interface_type:
8946 case DW_TAG_structure_type:
8947 if (!pdi->is_declaration)
8949 add_partial_symbol (pdi, cu);
8951 if (cu->language == language_rust && pdi->has_children)
8952 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8955 case DW_TAG_enumeration_type:
8956 if (!pdi->is_declaration)
8957 add_partial_enumeration (pdi, cu);
8959 case DW_TAG_base_type:
8960 case DW_TAG_subrange_type:
8961 /* File scope base type definitions are added to the partial
8963 add_partial_symbol (pdi, cu);
8965 case DW_TAG_namespace:
8966 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8969 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8971 case DW_TAG_imported_unit:
8973 struct dwarf2_per_cu_data *per_cu;
8975 /* For now we don't handle imported units in type units. */
8976 if (cu->per_cu->is_debug_types)
8978 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8979 " supported in type units [in module %s]"),
8980 objfile_name (cu->objfile));
8983 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
8987 /* Go read the partial unit, if needed. */
8988 if (per_cu->v.psymtab == NULL)
8989 process_psymtab_comp_unit (per_cu, 1, cu->language);
8991 VEC_safe_push (dwarf2_per_cu_ptr,
8992 cu->per_cu->imported_symtabs, per_cu);
8995 case DW_TAG_imported_declaration:
8996 add_partial_symbol (pdi, cu);
9003 /* If the die has a sibling, skip to the sibling. */
9005 pdi = pdi->die_sibling;
9009 /* Functions used to compute the fully scoped name of a partial DIE.
9011 Normally, this is simple. For C++, the parent DIE's fully scoped
9012 name is concatenated with "::" and the partial DIE's name.
9013 Enumerators are an exception; they use the scope of their parent
9014 enumeration type, i.e. the name of the enumeration type is not
9015 prepended to the enumerator.
9017 There are two complexities. One is DW_AT_specification; in this
9018 case "parent" means the parent of the target of the specification,
9019 instead of the direct parent of the DIE. The other is compilers
9020 which do not emit DW_TAG_namespace; in this case we try to guess
9021 the fully qualified name of structure types from their members'
9022 linkage names. This must be done using the DIE's children rather
9023 than the children of any DW_AT_specification target. We only need
9024 to do this for structures at the top level, i.e. if the target of
9025 any DW_AT_specification (if any; otherwise the DIE itself) does not
9028 /* Compute the scope prefix associated with PDI's parent, in
9029 compilation unit CU. The result will be allocated on CU's
9030 comp_unit_obstack, or a copy of the already allocated PDI->NAME
9031 field. NULL is returned if no prefix is necessary. */
9033 partial_die_parent_scope (struct partial_die_info *pdi,
9034 struct dwarf2_cu *cu)
9036 const char *grandparent_scope;
9037 struct partial_die_info *parent, *real_pdi;
9039 /* We need to look at our parent DIE; if we have a DW_AT_specification,
9040 then this means the parent of the specification DIE. */
9043 while (real_pdi->has_specification)
9044 real_pdi = find_partial_die (real_pdi->spec_offset,
9045 real_pdi->spec_is_dwz, cu);
9047 parent = real_pdi->die_parent;
9051 if (parent->scope_set)
9052 return parent->scope;
9054 fixup_partial_die (parent, cu);
9056 grandparent_scope = partial_die_parent_scope (parent, cu);
9058 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9059 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9060 Work around this problem here. */
9061 if (cu->language == language_cplus
9062 && parent->tag == DW_TAG_namespace
9063 && strcmp (parent->name, "::") == 0
9064 && grandparent_scope == NULL)
9066 parent->scope = NULL;
9067 parent->scope_set = 1;
9071 if (pdi->tag == DW_TAG_enumerator)
9072 /* Enumerators should not get the name of the enumeration as a prefix. */
9073 parent->scope = grandparent_scope;
9074 else if (parent->tag == DW_TAG_namespace
9075 || parent->tag == DW_TAG_module
9076 || parent->tag == DW_TAG_structure_type
9077 || parent->tag == DW_TAG_class_type
9078 || parent->tag == DW_TAG_interface_type
9079 || parent->tag == DW_TAG_union_type
9080 || parent->tag == DW_TAG_enumeration_type)
9082 if (grandparent_scope == NULL)
9083 parent->scope = parent->name;
9085 parent->scope = typename_concat (&cu->comp_unit_obstack,
9087 parent->name, 0, cu);
9091 /* FIXME drow/2004-04-01: What should we be doing with
9092 function-local names? For partial symbols, we should probably be
9094 complaint (&symfile_complaints,
9095 _("unhandled containing DIE tag %d for DIE at %d"),
9096 parent->tag, to_underlying (pdi->sect_off));
9097 parent->scope = grandparent_scope;
9100 parent->scope_set = 1;
9101 return parent->scope;
9104 /* Return the fully scoped name associated with PDI, from compilation unit
9105 CU. The result will be allocated with malloc. */
9108 partial_die_full_name (struct partial_die_info *pdi,
9109 struct dwarf2_cu *cu)
9111 const char *parent_scope;
9113 /* If this is a template instantiation, we can not work out the
9114 template arguments from partial DIEs. So, unfortunately, we have
9115 to go through the full DIEs. At least any work we do building
9116 types here will be reused if full symbols are loaded later. */
9117 if (pdi->has_template_arguments)
9119 fixup_partial_die (pdi, cu);
9121 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
9123 struct die_info *die;
9124 struct attribute attr;
9125 struct dwarf2_cu *ref_cu = cu;
9127 /* DW_FORM_ref_addr is using section offset. */
9128 attr.name = (enum dwarf_attribute) 0;
9129 attr.form = DW_FORM_ref_addr;
9130 attr.u.unsnd = to_underlying (pdi->sect_off);
9131 die = follow_die_ref (NULL, &attr, &ref_cu);
9133 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
9137 parent_scope = partial_die_parent_scope (pdi, cu);
9138 if (parent_scope == NULL)
9141 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
9145 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
9147 struct objfile *objfile = cu->objfile;
9148 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9150 const char *actual_name = NULL;
9152 char *built_actual_name;
9154 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9156 built_actual_name = partial_die_full_name (pdi, cu);
9157 if (built_actual_name != NULL)
9158 actual_name = built_actual_name;
9160 if (actual_name == NULL)
9161 actual_name = pdi->name;
9165 case DW_TAG_subprogram:
9166 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
9167 if (pdi->is_external || cu->language == language_ada)
9169 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
9170 of the global scope. But in Ada, we want to be able to access
9171 nested procedures globally. So all Ada subprograms are stored
9172 in the global scope. */
9173 add_psymbol_to_list (actual_name, strlen (actual_name),
9174 built_actual_name != NULL,
9175 VAR_DOMAIN, LOC_BLOCK,
9176 &objfile->global_psymbols,
9177 addr, cu->language, objfile);
9181 add_psymbol_to_list (actual_name, strlen (actual_name),
9182 built_actual_name != NULL,
9183 VAR_DOMAIN, LOC_BLOCK,
9184 &objfile->static_psymbols,
9185 addr, cu->language, objfile);
9188 if (pdi->main_subprogram && actual_name != NULL)
9189 set_objfile_main_name (objfile, actual_name, cu->language);
9191 case DW_TAG_constant:
9193 std::vector<partial_symbol *> *list;
9195 if (pdi->is_external)
9196 list = &objfile->global_psymbols;
9198 list = &objfile->static_psymbols;
9199 add_psymbol_to_list (actual_name, strlen (actual_name),
9200 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9201 list, 0, cu->language, objfile);
9204 case DW_TAG_variable:
9206 addr = decode_locdesc (pdi->d.locdesc, cu);
9210 && !dwarf2_per_objfile->has_section_at_zero)
9212 /* A global or static variable may also have been stripped
9213 out by the linker if unused, in which case its address
9214 will be nullified; do not add such variables into partial
9215 symbol table then. */
9217 else if (pdi->is_external)
9220 Don't enter into the minimal symbol tables as there is
9221 a minimal symbol table entry from the ELF symbols already.
9222 Enter into partial symbol table if it has a location
9223 descriptor or a type.
9224 If the location descriptor is missing, new_symbol will create
9225 a LOC_UNRESOLVED symbol, the address of the variable will then
9226 be determined from the minimal symbol table whenever the variable
9228 The address for the partial symbol table entry is not
9229 used by GDB, but it comes in handy for debugging partial symbol
9232 if (pdi->d.locdesc || pdi->has_type)
9233 add_psymbol_to_list (actual_name, strlen (actual_name),
9234 built_actual_name != NULL,
9235 VAR_DOMAIN, LOC_STATIC,
9236 &objfile->global_psymbols,
9238 cu->language, objfile);
9242 int has_loc = pdi->d.locdesc != NULL;
9244 /* Static Variable. Skip symbols whose value we cannot know (those
9245 without location descriptors or constant values). */
9246 if (!has_loc && !pdi->has_const_value)
9248 xfree (built_actual_name);
9252 add_psymbol_to_list (actual_name, strlen (actual_name),
9253 built_actual_name != NULL,
9254 VAR_DOMAIN, LOC_STATIC,
9255 &objfile->static_psymbols,
9256 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
9257 cu->language, objfile);
9260 case DW_TAG_typedef:
9261 case DW_TAG_base_type:
9262 case DW_TAG_subrange_type:
9263 add_psymbol_to_list (actual_name, strlen (actual_name),
9264 built_actual_name != NULL,
9265 VAR_DOMAIN, LOC_TYPEDEF,
9266 &objfile->static_psymbols,
9267 0, cu->language, objfile);
9269 case DW_TAG_imported_declaration:
9270 case DW_TAG_namespace:
9271 add_psymbol_to_list (actual_name, strlen (actual_name),
9272 built_actual_name != NULL,
9273 VAR_DOMAIN, LOC_TYPEDEF,
9274 &objfile->global_psymbols,
9275 0, cu->language, objfile);
9278 add_psymbol_to_list (actual_name, strlen (actual_name),
9279 built_actual_name != NULL,
9280 MODULE_DOMAIN, LOC_TYPEDEF,
9281 &objfile->global_psymbols,
9282 0, cu->language, objfile);
9284 case DW_TAG_class_type:
9285 case DW_TAG_interface_type:
9286 case DW_TAG_structure_type:
9287 case DW_TAG_union_type:
9288 case DW_TAG_enumeration_type:
9289 /* Skip external references. The DWARF standard says in the section
9290 about "Structure, Union, and Class Type Entries": "An incomplete
9291 structure, union or class type is represented by a structure,
9292 union or class entry that does not have a byte size attribute
9293 and that has a DW_AT_declaration attribute." */
9294 if (!pdi->has_byte_size && pdi->is_declaration)
9296 xfree (built_actual_name);
9300 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9301 static vs. global. */
9302 add_psymbol_to_list (actual_name, strlen (actual_name),
9303 built_actual_name != NULL,
9304 STRUCT_DOMAIN, LOC_TYPEDEF,
9305 cu->language == language_cplus
9306 ? &objfile->global_psymbols
9307 : &objfile->static_psymbols,
9308 0, cu->language, objfile);
9311 case DW_TAG_enumerator:
9312 add_psymbol_to_list (actual_name, strlen (actual_name),
9313 built_actual_name != NULL,
9314 VAR_DOMAIN, LOC_CONST,
9315 cu->language == language_cplus
9316 ? &objfile->global_psymbols
9317 : &objfile->static_psymbols,
9318 0, cu->language, objfile);
9324 xfree (built_actual_name);
9327 /* Read a partial die corresponding to a namespace; also, add a symbol
9328 corresponding to that namespace to the symbol table. NAMESPACE is
9329 the name of the enclosing namespace. */
9332 add_partial_namespace (struct partial_die_info *pdi,
9333 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9334 int set_addrmap, struct dwarf2_cu *cu)
9336 /* Add a symbol for the namespace. */
9338 add_partial_symbol (pdi, cu);
9340 /* Now scan partial symbols in that namespace. */
9342 if (pdi->has_children)
9343 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9346 /* Read a partial die corresponding to a Fortran module. */
9349 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9350 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9352 /* Add a symbol for the namespace. */
9354 add_partial_symbol (pdi, cu);
9356 /* Now scan partial symbols in that module. */
9358 if (pdi->has_children)
9359 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9362 /* Read a partial die corresponding to a subprogram and create a partial
9363 symbol for that subprogram. When the CU language allows it, this
9364 routine also defines a partial symbol for each nested subprogram
9365 that this subprogram contains. If SET_ADDRMAP is true, record the
9366 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
9367 and highest PC values found in PDI.
9369 PDI may also be a lexical block, in which case we simply search
9370 recursively for subprograms defined inside that lexical block.
9371 Again, this is only performed when the CU language allows this
9372 type of definitions. */
9375 add_partial_subprogram (struct partial_die_info *pdi,
9376 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9377 int set_addrmap, struct dwarf2_cu *cu)
9379 if (pdi->tag == DW_TAG_subprogram)
9381 if (pdi->has_pc_info)
9383 if (pdi->lowpc < *lowpc)
9384 *lowpc = pdi->lowpc;
9385 if (pdi->highpc > *highpc)
9386 *highpc = pdi->highpc;
9389 struct objfile *objfile = cu->objfile;
9390 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9395 baseaddr = ANOFFSET (objfile->section_offsets,
9396 SECT_OFF_TEXT (objfile));
9397 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9398 pdi->lowpc + baseaddr);
9399 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9400 pdi->highpc + baseaddr);
9401 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9402 cu->per_cu->v.psymtab);
9406 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9408 if (!pdi->is_declaration)
9409 /* Ignore subprogram DIEs that do not have a name, they are
9410 illegal. Do not emit a complaint at this point, we will
9411 do so when we convert this psymtab into a symtab. */
9413 add_partial_symbol (pdi, cu);
9417 if (! pdi->has_children)
9420 if (cu->language == language_ada)
9422 pdi = pdi->die_child;
9425 fixup_partial_die (pdi, cu);
9426 if (pdi->tag == DW_TAG_subprogram
9427 || pdi->tag == DW_TAG_lexical_block)
9428 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9429 pdi = pdi->die_sibling;
9434 /* Read a partial die corresponding to an enumeration type. */
9437 add_partial_enumeration (struct partial_die_info *enum_pdi,
9438 struct dwarf2_cu *cu)
9440 struct partial_die_info *pdi;
9442 if (enum_pdi->name != NULL)
9443 add_partial_symbol (enum_pdi, cu);
9445 pdi = enum_pdi->die_child;
9448 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9449 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
9451 add_partial_symbol (pdi, cu);
9452 pdi = pdi->die_sibling;
9456 /* Return the initial uleb128 in the die at INFO_PTR. */
9459 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9461 unsigned int bytes_read;
9463 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9466 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
9467 Return the corresponding abbrev, or NULL if the number is zero (indicating
9468 an empty DIE). In either case *BYTES_READ will be set to the length of
9469 the initial number. */
9471 static struct abbrev_info *
9472 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
9473 struct dwarf2_cu *cu)
9475 bfd *abfd = cu->objfile->obfd;
9476 unsigned int abbrev_number;
9477 struct abbrev_info *abbrev;
9479 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9481 if (abbrev_number == 0)
9484 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
9487 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9488 " at offset 0x%x [in module %s]"),
9489 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9490 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
9496 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9497 Returns a pointer to the end of a series of DIEs, terminated by an empty
9498 DIE. Any children of the skipped DIEs will also be skipped. */
9500 static const gdb_byte *
9501 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9503 struct dwarf2_cu *cu = reader->cu;
9504 struct abbrev_info *abbrev;
9505 unsigned int bytes_read;
9509 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9511 return info_ptr + bytes_read;
9513 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9517 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9518 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9519 abbrev corresponding to that skipped uleb128 should be passed in
9520 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9523 static const gdb_byte *
9524 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9525 struct abbrev_info *abbrev)
9527 unsigned int bytes_read;
9528 struct attribute attr;
9529 bfd *abfd = reader->abfd;
9530 struct dwarf2_cu *cu = reader->cu;
9531 const gdb_byte *buffer = reader->buffer;
9532 const gdb_byte *buffer_end = reader->buffer_end;
9533 unsigned int form, i;
9535 for (i = 0; i < abbrev->num_attrs; i++)
9537 /* The only abbrev we care about is DW_AT_sibling. */
9538 if (abbrev->attrs[i].name == DW_AT_sibling)
9540 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9541 if (attr.form == DW_FORM_ref_addr)
9542 complaint (&symfile_complaints,
9543 _("ignoring absolute DW_AT_sibling"));
9546 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9547 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9549 if (sibling_ptr < info_ptr)
9550 complaint (&symfile_complaints,
9551 _("DW_AT_sibling points backwards"));
9552 else if (sibling_ptr > reader->buffer_end)
9553 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9559 /* If it isn't DW_AT_sibling, skip this attribute. */
9560 form = abbrev->attrs[i].form;
9564 case DW_FORM_ref_addr:
9565 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9566 and later it is offset sized. */
9567 if (cu->header.version == 2)
9568 info_ptr += cu->header.addr_size;
9570 info_ptr += cu->header.offset_size;
9572 case DW_FORM_GNU_ref_alt:
9573 info_ptr += cu->header.offset_size;
9576 info_ptr += cu->header.addr_size;
9583 case DW_FORM_flag_present:
9584 case DW_FORM_implicit_const:
9596 case DW_FORM_ref_sig8:
9599 case DW_FORM_data16:
9602 case DW_FORM_string:
9603 read_direct_string (abfd, info_ptr, &bytes_read);
9604 info_ptr += bytes_read;
9606 case DW_FORM_sec_offset:
9608 case DW_FORM_GNU_strp_alt:
9609 info_ptr += cu->header.offset_size;
9611 case DW_FORM_exprloc:
9613 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9614 info_ptr += bytes_read;
9616 case DW_FORM_block1:
9617 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9619 case DW_FORM_block2:
9620 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9622 case DW_FORM_block4:
9623 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9627 case DW_FORM_ref_udata:
9628 case DW_FORM_GNU_addr_index:
9629 case DW_FORM_GNU_str_index:
9630 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9632 case DW_FORM_indirect:
9633 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9634 info_ptr += bytes_read;
9635 /* We need to continue parsing from here, so just go back to
9637 goto skip_attribute;
9640 error (_("Dwarf Error: Cannot handle %s "
9641 "in DWARF reader [in module %s]"),
9642 dwarf_form_name (form),
9643 bfd_get_filename (abfd));
9647 if (abbrev->has_children)
9648 return skip_children (reader, info_ptr);
9653 /* Locate ORIG_PDI's sibling.
9654 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9656 static const gdb_byte *
9657 locate_pdi_sibling (const struct die_reader_specs *reader,
9658 struct partial_die_info *orig_pdi,
9659 const gdb_byte *info_ptr)
9661 /* Do we know the sibling already? */
9663 if (orig_pdi->sibling)
9664 return orig_pdi->sibling;
9666 /* Are there any children to deal with? */
9668 if (!orig_pdi->has_children)
9671 /* Skip the children the long way. */
9673 return skip_children (reader, info_ptr);
9676 /* Expand this partial symbol table into a full symbol table. SELF is
9680 dwarf2_read_symtab (struct partial_symtab *self,
9681 struct objfile *objfile)
9685 warning (_("bug: psymtab for %s is already read in."),
9692 printf_filtered (_("Reading in symbols for %s..."),
9694 gdb_flush (gdb_stdout);
9697 /* Restore our global data. */
9699 = (struct dwarf2_per_objfile *) objfile_data (objfile,
9700 dwarf2_objfile_data_key);
9702 /* If this psymtab is constructed from a debug-only objfile, the
9703 has_section_at_zero flag will not necessarily be correct. We
9704 can get the correct value for this flag by looking at the data
9705 associated with the (presumably stripped) associated objfile. */
9706 if (objfile->separate_debug_objfile_backlink)
9708 struct dwarf2_per_objfile *dpo_backlink
9709 = ((struct dwarf2_per_objfile *)
9710 objfile_data (objfile->separate_debug_objfile_backlink,
9711 dwarf2_objfile_data_key));
9713 dwarf2_per_objfile->has_section_at_zero
9714 = dpo_backlink->has_section_at_zero;
9717 dwarf2_per_objfile->reading_partial_symbols = 0;
9719 psymtab_to_symtab_1 (self);
9721 /* Finish up the debug error message. */
9723 printf_filtered (_("done.\n"));
9726 process_cu_includes ();
9729 /* Reading in full CUs. */
9731 /* Add PER_CU to the queue. */
9734 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9735 enum language pretend_language)
9737 struct dwarf2_queue_item *item;
9740 item = XNEW (struct dwarf2_queue_item);
9741 item->per_cu = per_cu;
9742 item->pretend_language = pretend_language;
9745 if (dwarf2_queue == NULL)
9746 dwarf2_queue = item;
9748 dwarf2_queue_tail->next = item;
9750 dwarf2_queue_tail = item;
9753 /* If PER_CU is not yet queued, add it to the queue.
9754 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9756 The result is non-zero if PER_CU was queued, otherwise the result is zero
9757 meaning either PER_CU is already queued or it is already loaded.
9759 N.B. There is an invariant here that if a CU is queued then it is loaded.
9760 The caller is required to load PER_CU if we return non-zero. */
9763 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9764 struct dwarf2_per_cu_data *per_cu,
9765 enum language pretend_language)
9767 /* We may arrive here during partial symbol reading, if we need full
9768 DIEs to process an unusual case (e.g. template arguments). Do
9769 not queue PER_CU, just tell our caller to load its DIEs. */
9770 if (dwarf2_per_objfile->reading_partial_symbols)
9772 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9777 /* Mark the dependence relation so that we don't flush PER_CU
9779 if (dependent_cu != NULL)
9780 dwarf2_add_dependence (dependent_cu, per_cu);
9782 /* If it's already on the queue, we have nothing to do. */
9786 /* If the compilation unit is already loaded, just mark it as
9788 if (per_cu->cu != NULL)
9790 per_cu->cu->last_used = 0;
9794 /* Add it to the queue. */
9795 queue_comp_unit (per_cu, pretend_language);
9800 /* Process the queue. */
9803 process_queue (void)
9805 struct dwarf2_queue_item *item, *next_item;
9807 if (dwarf_read_debug)
9809 fprintf_unfiltered (gdb_stdlog,
9810 "Expanding one or more symtabs of objfile %s ...\n",
9811 objfile_name (dwarf2_per_objfile->objfile));
9814 /* The queue starts out with one item, but following a DIE reference
9815 may load a new CU, adding it to the end of the queue. */
9816 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9818 if ((dwarf2_per_objfile->using_index
9819 ? !item->per_cu->v.quick->compunit_symtab
9820 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9821 /* Skip dummy CUs. */
9822 && item->per_cu->cu != NULL)
9824 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9825 unsigned int debug_print_threshold;
9828 if (per_cu->is_debug_types)
9830 struct signatured_type *sig_type =
9831 (struct signatured_type *) per_cu;
9833 sprintf (buf, "TU %s at offset 0x%x",
9834 hex_string (sig_type->signature),
9835 to_underlying (per_cu->sect_off));
9836 /* There can be 100s of TUs.
9837 Only print them in verbose mode. */
9838 debug_print_threshold = 2;
9842 sprintf (buf, "CU at offset 0x%x",
9843 to_underlying (per_cu->sect_off));
9844 debug_print_threshold = 1;
9847 if (dwarf_read_debug >= debug_print_threshold)
9848 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9850 if (per_cu->is_debug_types)
9851 process_full_type_unit (per_cu, item->pretend_language);
9853 process_full_comp_unit (per_cu, item->pretend_language);
9855 if (dwarf_read_debug >= debug_print_threshold)
9856 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9859 item->per_cu->queued = 0;
9860 next_item = item->next;
9864 dwarf2_queue_tail = NULL;
9866 if (dwarf_read_debug)
9868 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9869 objfile_name (dwarf2_per_objfile->objfile));
9873 /* Free all allocated queue entries. This function only releases anything if
9874 an error was thrown; if the queue was processed then it would have been
9875 freed as we went along. */
9878 dwarf2_release_queue (void *dummy)
9880 struct dwarf2_queue_item *item, *last;
9882 item = dwarf2_queue;
9885 /* Anything still marked queued is likely to be in an
9886 inconsistent state, so discard it. */
9887 if (item->per_cu->queued)
9889 if (item->per_cu->cu != NULL)
9890 free_one_cached_comp_unit (item->per_cu);
9891 item->per_cu->queued = 0;
9899 dwarf2_queue = dwarf2_queue_tail = NULL;
9902 /* Read in full symbols for PST, and anything it depends on. */
9905 psymtab_to_symtab_1 (struct partial_symtab *pst)
9907 struct dwarf2_per_cu_data *per_cu;
9913 for (i = 0; i < pst->number_of_dependencies; i++)
9914 if (!pst->dependencies[i]->readin
9915 && pst->dependencies[i]->user == NULL)
9917 /* Inform about additional files that need to be read in. */
9920 /* FIXME: i18n: Need to make this a single string. */
9921 fputs_filtered (" ", gdb_stdout);
9923 fputs_filtered ("and ", gdb_stdout);
9925 printf_filtered ("%s...", pst->dependencies[i]->filename);
9926 wrap_here (""); /* Flush output. */
9927 gdb_flush (gdb_stdout);
9929 psymtab_to_symtab_1 (pst->dependencies[i]);
9932 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9936 /* It's an include file, no symbols to read for it.
9937 Everything is in the parent symtab. */
9942 dw2_do_instantiate_symtab (per_cu);
9945 /* Trivial hash function for die_info: the hash value of a DIE
9946 is its offset in .debug_info for this objfile. */
9949 die_hash (const void *item)
9951 const struct die_info *die = (const struct die_info *) item;
9953 return to_underlying (die->sect_off);
9956 /* Trivial comparison function for die_info structures: two DIEs
9957 are equal if they have the same offset. */
9960 die_eq (const void *item_lhs, const void *item_rhs)
9962 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9963 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9965 return die_lhs->sect_off == die_rhs->sect_off;
9968 /* die_reader_func for load_full_comp_unit.
9969 This is identical to read_signatured_type_reader,
9970 but is kept separate for now. */
9973 load_full_comp_unit_reader (const struct die_reader_specs *reader,
9974 const gdb_byte *info_ptr,
9975 struct die_info *comp_unit_die,
9979 struct dwarf2_cu *cu = reader->cu;
9980 enum language *language_ptr = (enum language *) data;
9982 gdb_assert (cu->die_hash == NULL);
9984 htab_create_alloc_ex (cu->header.length / 12,
9988 &cu->comp_unit_obstack,
9989 hashtab_obstack_allocate,
9990 dummy_obstack_deallocate);
9993 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9994 &info_ptr, comp_unit_die);
9995 cu->dies = comp_unit_die;
9996 /* comp_unit_die is not stored in die_hash, no need. */
9998 /* We try not to read any attributes in this function, because not
9999 all CUs needed for references have been loaded yet, and symbol
10000 table processing isn't initialized. But we have to set the CU language,
10001 or we won't be able to build types correctly.
10002 Similarly, if we do not read the producer, we can not apply
10003 producer-specific interpretation. */
10004 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
10007 /* Load the DIEs associated with PER_CU into memory. */
10010 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
10011 enum language pretend_language)
10013 gdb_assert (! this_cu->is_debug_types);
10015 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
10016 load_full_comp_unit_reader, &pretend_language);
10019 /* Add a DIE to the delayed physname list. */
10022 add_to_method_list (struct type *type, int fnfield_index, int index,
10023 const char *name, struct die_info *die,
10024 struct dwarf2_cu *cu)
10026 struct delayed_method_info mi;
10028 mi.fnfield_index = fnfield_index;
10032 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
10035 /* A cleanup for freeing the delayed method list. */
10038 free_delayed_list (void *ptr)
10040 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
10041 if (cu->method_list != NULL)
10043 VEC_free (delayed_method_info, cu->method_list);
10044 cu->method_list = NULL;
10048 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
10049 "const" / "volatile". If so, decrements LEN by the length of the
10050 modifier and return true. Otherwise return false. */
10054 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
10056 size_t mod_len = sizeof (mod) - 1;
10057 if (len > mod_len && startswith (physname + (len - mod_len), mod))
10065 /* Compute the physnames of any methods on the CU's method list.
10067 The computation of method physnames is delayed in order to avoid the
10068 (bad) condition that one of the method's formal parameters is of an as yet
10069 incomplete type. */
10072 compute_delayed_physnames (struct dwarf2_cu *cu)
10075 struct delayed_method_info *mi;
10077 /* Only C++ delays computing physnames. */
10078 if (VEC_empty (delayed_method_info, cu->method_list))
10080 gdb_assert (cu->language == language_cplus);
10082 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
10084 const char *physname;
10085 struct fn_fieldlist *fn_flp
10086 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
10087 physname = dwarf2_physname (mi->name, mi->die, cu);
10088 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
10089 = physname ? physname : "";
10091 /* Since there's no tag to indicate whether a method is a
10092 const/volatile overload, extract that information out of the
10094 if (physname != NULL)
10096 size_t len = strlen (physname);
10100 if (physname[len] == ')') /* shortcut */
10102 else if (check_modifier (physname, len, " const"))
10103 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi->index) = 1;
10104 else if (check_modifier (physname, len, " volatile"))
10105 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi->index) = 1;
10113 /* Go objects should be embedded in a DW_TAG_module DIE,
10114 and it's not clear if/how imported objects will appear.
10115 To keep Go support simple until that's worked out,
10116 go back through what we've read and create something usable.
10117 We could do this while processing each DIE, and feels kinda cleaner,
10118 but that way is more invasive.
10119 This is to, for example, allow the user to type "p var" or "b main"
10120 without having to specify the package name, and allow lookups
10121 of module.object to work in contexts that use the expression
10125 fixup_go_packaging (struct dwarf2_cu *cu)
10127 char *package_name = NULL;
10128 struct pending *list;
10131 for (list = global_symbols; list != NULL; list = list->next)
10133 for (i = 0; i < list->nsyms; ++i)
10135 struct symbol *sym = list->symbol[i];
10137 if (SYMBOL_LANGUAGE (sym) == language_go
10138 && SYMBOL_CLASS (sym) == LOC_BLOCK)
10140 char *this_package_name = go_symbol_package_name (sym);
10142 if (this_package_name == NULL)
10144 if (package_name == NULL)
10145 package_name = this_package_name;
10148 if (strcmp (package_name, this_package_name) != 0)
10149 complaint (&symfile_complaints,
10150 _("Symtab %s has objects from two different Go packages: %s and %s"),
10151 (symbol_symtab (sym) != NULL
10152 ? symtab_to_filename_for_display
10153 (symbol_symtab (sym))
10154 : objfile_name (cu->objfile)),
10155 this_package_name, package_name);
10156 xfree (this_package_name);
10162 if (package_name != NULL)
10164 struct objfile *objfile = cu->objfile;
10165 const char *saved_package_name
10166 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
10168 strlen (package_name));
10169 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
10170 saved_package_name);
10171 struct symbol *sym;
10173 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10175 sym = allocate_symbol (objfile);
10176 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
10177 SYMBOL_SET_NAMES (sym, saved_package_name,
10178 strlen (saved_package_name), 0, objfile);
10179 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
10180 e.g., "main" finds the "main" module and not C's main(). */
10181 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10182 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
10183 SYMBOL_TYPE (sym) = type;
10185 add_symbol_to_list (sym, &global_symbols);
10187 xfree (package_name);
10191 /* Return the symtab for PER_CU. This works properly regardless of
10192 whether we're using the index or psymtabs. */
10194 static struct compunit_symtab *
10195 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10197 return (dwarf2_per_objfile->using_index
10198 ? per_cu->v.quick->compunit_symtab
10199 : per_cu->v.psymtab->compunit_symtab);
10202 /* A helper function for computing the list of all symbol tables
10203 included by PER_CU. */
10206 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
10207 htab_t all_children, htab_t all_type_symtabs,
10208 struct dwarf2_per_cu_data *per_cu,
10209 struct compunit_symtab *immediate_parent)
10213 struct compunit_symtab *cust;
10214 struct dwarf2_per_cu_data *iter;
10216 slot = htab_find_slot (all_children, per_cu, INSERT);
10219 /* This inclusion and its children have been processed. */
10224 /* Only add a CU if it has a symbol table. */
10225 cust = get_compunit_symtab (per_cu);
10228 /* If this is a type unit only add its symbol table if we haven't
10229 seen it yet (type unit per_cu's can share symtabs). */
10230 if (per_cu->is_debug_types)
10232 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10236 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10237 if (cust->user == NULL)
10238 cust->user = immediate_parent;
10243 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10244 if (cust->user == NULL)
10245 cust->user = immediate_parent;
10250 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10253 recursively_compute_inclusions (result, all_children,
10254 all_type_symtabs, iter, cust);
10258 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10262 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10264 gdb_assert (! per_cu->is_debug_types);
10266 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10269 struct dwarf2_per_cu_data *per_cu_iter;
10270 struct compunit_symtab *compunit_symtab_iter;
10271 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
10272 htab_t all_children, all_type_symtabs;
10273 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10275 /* If we don't have a symtab, we can just skip this case. */
10279 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10280 NULL, xcalloc, xfree);
10281 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10282 NULL, xcalloc, xfree);
10285 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10289 recursively_compute_inclusions (&result_symtabs, all_children,
10290 all_type_symtabs, per_cu_iter,
10294 /* Now we have a transitive closure of all the included symtabs. */
10295 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10297 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
10298 struct compunit_symtab *, len + 1);
10300 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10301 compunit_symtab_iter);
10303 cust->includes[ix] = compunit_symtab_iter;
10304 cust->includes[len] = NULL;
10306 VEC_free (compunit_symtab_ptr, result_symtabs);
10307 htab_delete (all_children);
10308 htab_delete (all_type_symtabs);
10312 /* Compute the 'includes' field for the symtabs of all the CUs we just
10316 process_cu_includes (void)
10319 struct dwarf2_per_cu_data *iter;
10322 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10326 if (! iter->is_debug_types)
10327 compute_compunit_symtab_includes (iter);
10330 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10333 /* Generate full symbol information for PER_CU, whose DIEs have
10334 already been loaded into memory. */
10337 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10338 enum language pretend_language)
10340 struct dwarf2_cu *cu = per_cu->cu;
10341 struct objfile *objfile = per_cu->objfile;
10342 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10343 CORE_ADDR lowpc, highpc;
10344 struct compunit_symtab *cust;
10345 struct cleanup *delayed_list_cleanup;
10346 CORE_ADDR baseaddr;
10347 struct block *static_block;
10350 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10353 scoped_free_pendings free_pending;
10354 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10356 cu->list_in_scope = &file_symbols;
10358 cu->language = pretend_language;
10359 cu->language_defn = language_def (cu->language);
10361 /* Do line number decoding in read_file_scope () */
10362 process_die (cu->dies, cu);
10364 /* For now fudge the Go package. */
10365 if (cu->language == language_go)
10366 fixup_go_packaging (cu);
10368 /* Now that we have processed all the DIEs in the CU, all the types
10369 should be complete, and it should now be safe to compute all of the
10371 compute_delayed_physnames (cu);
10372 do_cleanups (delayed_list_cleanup);
10374 /* Some compilers don't define a DW_AT_high_pc attribute for the
10375 compilation unit. If the DW_AT_high_pc is missing, synthesize
10376 it, by scanning the DIE's below the compilation unit. */
10377 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10379 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10380 static_block = end_symtab_get_static_block (addr, 0, 1);
10382 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10383 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10384 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10385 addrmap to help ensure it has an accurate map of pc values belonging to
10387 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10389 cust = end_symtab_from_static_block (static_block,
10390 SECT_OFF_TEXT (objfile), 0);
10394 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10396 /* Set symtab language to language from DW_AT_language. If the
10397 compilation is from a C file generated by language preprocessors, do
10398 not set the language if it was already deduced by start_subfile. */
10399 if (!(cu->language == language_c
10400 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10401 COMPUNIT_FILETABS (cust)->language = cu->language;
10403 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10404 produce DW_AT_location with location lists but it can be possibly
10405 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10406 there were bugs in prologue debug info, fixed later in GCC-4.5
10407 by "unwind info for epilogues" patch (which is not directly related).
10409 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10410 needed, it would be wrong due to missing DW_AT_producer there.
10412 Still one can confuse GDB by using non-standard GCC compilation
10413 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10415 if (cu->has_loclist && gcc_4_minor >= 5)
10416 cust->locations_valid = 1;
10418 if (gcc_4_minor >= 5)
10419 cust->epilogue_unwind_valid = 1;
10421 cust->call_site_htab = cu->call_site_htab;
10424 if (dwarf2_per_objfile->using_index)
10425 per_cu->v.quick->compunit_symtab = cust;
10428 struct partial_symtab *pst = per_cu->v.psymtab;
10429 pst->compunit_symtab = cust;
10433 /* Push it for inclusion processing later. */
10434 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
10437 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10438 already been loaded into memory. */
10441 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10442 enum language pretend_language)
10444 struct dwarf2_cu *cu = per_cu->cu;
10445 struct objfile *objfile = per_cu->objfile;
10446 struct compunit_symtab *cust;
10447 struct cleanup *delayed_list_cleanup;
10448 struct signatured_type *sig_type;
10450 gdb_assert (per_cu->is_debug_types);
10451 sig_type = (struct signatured_type *) per_cu;
10454 scoped_free_pendings free_pending;
10455 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10457 cu->list_in_scope = &file_symbols;
10459 cu->language = pretend_language;
10460 cu->language_defn = language_def (cu->language);
10462 /* The symbol tables are set up in read_type_unit_scope. */
10463 process_die (cu->dies, cu);
10465 /* For now fudge the Go package. */
10466 if (cu->language == language_go)
10467 fixup_go_packaging (cu);
10469 /* Now that we have processed all the DIEs in the CU, all the types
10470 should be complete, and it should now be safe to compute all of the
10472 compute_delayed_physnames (cu);
10473 do_cleanups (delayed_list_cleanup);
10475 /* TUs share symbol tables.
10476 If this is the first TU to use this symtab, complete the construction
10477 of it with end_expandable_symtab. Otherwise, complete the addition of
10478 this TU's symbols to the existing symtab. */
10479 if (sig_type->type_unit_group->compunit_symtab == NULL)
10481 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10482 sig_type->type_unit_group->compunit_symtab = cust;
10486 /* Set symtab language to language from DW_AT_language. If the
10487 compilation is from a C file generated by language preprocessors,
10488 do not set the language if it was already deduced by
10490 if (!(cu->language == language_c
10491 && COMPUNIT_FILETABS (cust)->language != language_c))
10492 COMPUNIT_FILETABS (cust)->language = cu->language;
10497 augment_type_symtab ();
10498 cust = sig_type->type_unit_group->compunit_symtab;
10501 if (dwarf2_per_objfile->using_index)
10502 per_cu->v.quick->compunit_symtab = cust;
10505 struct partial_symtab *pst = per_cu->v.psymtab;
10506 pst->compunit_symtab = cust;
10511 /* Process an imported unit DIE. */
10514 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10516 struct attribute *attr;
10518 /* For now we don't handle imported units in type units. */
10519 if (cu->per_cu->is_debug_types)
10521 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10522 " supported in type units [in module %s]"),
10523 objfile_name (cu->objfile));
10526 attr = dwarf2_attr (die, DW_AT_import, cu);
10529 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10530 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10531 dwarf2_per_cu_data *per_cu
10532 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
10534 /* If necessary, add it to the queue and load its DIEs. */
10535 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10536 load_full_comp_unit (per_cu, cu->language);
10538 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10543 /* RAII object that represents a process_die scope: i.e.,
10544 starts/finishes processing a DIE. */
10545 class process_die_scope
10548 process_die_scope (die_info *die, dwarf2_cu *cu)
10549 : m_die (die), m_cu (cu)
10551 /* We should only be processing DIEs not already in process. */
10552 gdb_assert (!m_die->in_process);
10553 m_die->in_process = true;
10556 ~process_die_scope ()
10558 m_die->in_process = false;
10560 /* If we're done processing the DIE for the CU that owns the line
10561 header, we don't need the line header anymore. */
10562 if (m_cu->line_header_die_owner == m_die)
10564 delete m_cu->line_header;
10565 m_cu->line_header = NULL;
10566 m_cu->line_header_die_owner = NULL;
10575 /* Process a die and its children. */
10578 process_die (struct die_info *die, struct dwarf2_cu *cu)
10580 process_die_scope scope (die, cu);
10584 case DW_TAG_padding:
10586 case DW_TAG_compile_unit:
10587 case DW_TAG_partial_unit:
10588 read_file_scope (die, cu);
10590 case DW_TAG_type_unit:
10591 read_type_unit_scope (die, cu);
10593 case DW_TAG_subprogram:
10594 case DW_TAG_inlined_subroutine:
10595 read_func_scope (die, cu);
10597 case DW_TAG_lexical_block:
10598 case DW_TAG_try_block:
10599 case DW_TAG_catch_block:
10600 read_lexical_block_scope (die, cu);
10602 case DW_TAG_call_site:
10603 case DW_TAG_GNU_call_site:
10604 read_call_site_scope (die, cu);
10606 case DW_TAG_class_type:
10607 case DW_TAG_interface_type:
10608 case DW_TAG_structure_type:
10609 case DW_TAG_union_type:
10610 process_structure_scope (die, cu);
10612 case DW_TAG_enumeration_type:
10613 process_enumeration_scope (die, cu);
10616 /* These dies have a type, but processing them does not create
10617 a symbol or recurse to process the children. Therefore we can
10618 read them on-demand through read_type_die. */
10619 case DW_TAG_subroutine_type:
10620 case DW_TAG_set_type:
10621 case DW_TAG_array_type:
10622 case DW_TAG_pointer_type:
10623 case DW_TAG_ptr_to_member_type:
10624 case DW_TAG_reference_type:
10625 case DW_TAG_rvalue_reference_type:
10626 case DW_TAG_string_type:
10629 case DW_TAG_base_type:
10630 case DW_TAG_subrange_type:
10631 case DW_TAG_typedef:
10632 /* Add a typedef symbol for the type definition, if it has a
10634 new_symbol (die, read_type_die (die, cu), cu);
10636 case DW_TAG_common_block:
10637 read_common_block (die, cu);
10639 case DW_TAG_common_inclusion:
10641 case DW_TAG_namespace:
10642 cu->processing_has_namespace_info = 1;
10643 read_namespace (die, cu);
10645 case DW_TAG_module:
10646 cu->processing_has_namespace_info = 1;
10647 read_module (die, cu);
10649 case DW_TAG_imported_declaration:
10650 cu->processing_has_namespace_info = 1;
10651 if (read_namespace_alias (die, cu))
10653 /* The declaration is not a global namespace alias: fall through. */
10654 case DW_TAG_imported_module:
10655 cu->processing_has_namespace_info = 1;
10656 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10657 || cu->language != language_fortran))
10658 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10659 dwarf_tag_name (die->tag));
10660 read_import_statement (die, cu);
10663 case DW_TAG_imported_unit:
10664 process_imported_unit_die (die, cu);
10667 case DW_TAG_variable:
10668 read_variable (die, cu);
10672 new_symbol (die, NULL, cu);
10677 /* DWARF name computation. */
10679 /* A helper function for dwarf2_compute_name which determines whether DIE
10680 needs to have the name of the scope prepended to the name listed in the
10684 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10686 struct attribute *attr;
10690 case DW_TAG_namespace:
10691 case DW_TAG_typedef:
10692 case DW_TAG_class_type:
10693 case DW_TAG_interface_type:
10694 case DW_TAG_structure_type:
10695 case DW_TAG_union_type:
10696 case DW_TAG_enumeration_type:
10697 case DW_TAG_enumerator:
10698 case DW_TAG_subprogram:
10699 case DW_TAG_inlined_subroutine:
10700 case DW_TAG_member:
10701 case DW_TAG_imported_declaration:
10704 case DW_TAG_variable:
10705 case DW_TAG_constant:
10706 /* We only need to prefix "globally" visible variables. These include
10707 any variable marked with DW_AT_external or any variable that
10708 lives in a namespace. [Variables in anonymous namespaces
10709 require prefixing, but they are not DW_AT_external.] */
10711 if (dwarf2_attr (die, DW_AT_specification, cu))
10713 struct dwarf2_cu *spec_cu = cu;
10715 return die_needs_namespace (die_specification (die, &spec_cu),
10719 attr = dwarf2_attr (die, DW_AT_external, cu);
10720 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10721 && die->parent->tag != DW_TAG_module)
10723 /* A variable in a lexical block of some kind does not need a
10724 namespace, even though in C++ such variables may be external
10725 and have a mangled name. */
10726 if (die->parent->tag == DW_TAG_lexical_block
10727 || die->parent->tag == DW_TAG_try_block
10728 || die->parent->tag == DW_TAG_catch_block
10729 || die->parent->tag == DW_TAG_subprogram)
10738 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10739 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10740 defined for the given DIE. */
10742 static struct attribute *
10743 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10745 struct attribute *attr;
10747 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10749 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10754 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10755 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10756 defined for the given DIE. */
10758 static const char *
10759 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10761 const char *linkage_name;
10763 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10764 if (linkage_name == NULL)
10765 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10767 return linkage_name;
10770 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10771 compute the physname for the object, which include a method's:
10772 - formal parameters (C++),
10773 - receiver type (Go),
10775 The term "physname" is a bit confusing.
10776 For C++, for example, it is the demangled name.
10777 For Go, for example, it's the mangled name.
10779 For Ada, return the DIE's linkage name rather than the fully qualified
10780 name. PHYSNAME is ignored..
10782 The result is allocated on the objfile_obstack and canonicalized. */
10784 static const char *
10785 dwarf2_compute_name (const char *name,
10786 struct die_info *die, struct dwarf2_cu *cu,
10789 struct objfile *objfile = cu->objfile;
10792 name = dwarf2_name (die, cu);
10794 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10795 but otherwise compute it by typename_concat inside GDB.
10796 FIXME: Actually this is not really true, or at least not always true.
10797 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10798 Fortran names because there is no mangling standard. So new_symbol_full
10799 will set the demangled name to the result of dwarf2_full_name, and it is
10800 the demangled name that GDB uses if it exists. */
10801 if (cu->language == language_ada
10802 || (cu->language == language_fortran && physname))
10804 /* For Ada unit, we prefer the linkage name over the name, as
10805 the former contains the exported name, which the user expects
10806 to be able to reference. Ideally, we want the user to be able
10807 to reference this entity using either natural or linkage name,
10808 but we haven't started looking at this enhancement yet. */
10809 const char *linkage_name = dw2_linkage_name (die, cu);
10811 if (linkage_name != NULL)
10812 return linkage_name;
10815 /* These are the only languages we know how to qualify names in. */
10817 && (cu->language == language_cplus
10818 || cu->language == language_fortran || cu->language == language_d
10819 || cu->language == language_rust))
10821 if (die_needs_namespace (die, cu))
10823 const char *prefix;
10824 const char *canonical_name = NULL;
10828 prefix = determine_prefix (die, cu);
10829 if (*prefix != '\0')
10831 char *prefixed_name = typename_concat (NULL, prefix, name,
10834 buf.puts (prefixed_name);
10835 xfree (prefixed_name);
10840 /* Template parameters may be specified in the DIE's DW_AT_name, or
10841 as children with DW_TAG_template_type_param or
10842 DW_TAG_value_type_param. If the latter, add them to the name
10843 here. If the name already has template parameters, then
10844 skip this step; some versions of GCC emit both, and
10845 it is more efficient to use the pre-computed name.
10847 Something to keep in mind about this process: it is very
10848 unlikely, or in some cases downright impossible, to produce
10849 something that will match the mangled name of a function.
10850 If the definition of the function has the same debug info,
10851 we should be able to match up with it anyway. But fallbacks
10852 using the minimal symbol, for instance to find a method
10853 implemented in a stripped copy of libstdc++, will not work.
10854 If we do not have debug info for the definition, we will have to
10855 match them up some other way.
10857 When we do name matching there is a related problem with function
10858 templates; two instantiated function templates are allowed to
10859 differ only by their return types, which we do not add here. */
10861 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10863 struct attribute *attr;
10864 struct die_info *child;
10867 die->building_fullname = 1;
10869 for (child = die->child; child != NULL; child = child->sibling)
10873 const gdb_byte *bytes;
10874 struct dwarf2_locexpr_baton *baton;
10877 if (child->tag != DW_TAG_template_type_param
10878 && child->tag != DW_TAG_template_value_param)
10889 attr = dwarf2_attr (child, DW_AT_type, cu);
10892 complaint (&symfile_complaints,
10893 _("template parameter missing DW_AT_type"));
10894 buf.puts ("UNKNOWN_TYPE");
10897 type = die_type (child, cu);
10899 if (child->tag == DW_TAG_template_type_param)
10901 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
10905 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10908 complaint (&symfile_complaints,
10909 _("template parameter missing "
10910 "DW_AT_const_value"));
10911 buf.puts ("UNKNOWN_VALUE");
10915 dwarf2_const_value_attr (attr, type, name,
10916 &cu->comp_unit_obstack, cu,
10917 &value, &bytes, &baton);
10919 if (TYPE_NOSIGN (type))
10920 /* GDB prints characters as NUMBER 'CHAR'. If that's
10921 changed, this can use value_print instead. */
10922 c_printchar (value, type, &buf);
10925 struct value_print_options opts;
10928 v = dwarf2_evaluate_loc_desc (type, NULL,
10932 else if (bytes != NULL)
10934 v = allocate_value (type);
10935 memcpy (value_contents_writeable (v), bytes,
10936 TYPE_LENGTH (type));
10939 v = value_from_longest (type, value);
10941 /* Specify decimal so that we do not depend on
10943 get_formatted_print_options (&opts, 'd');
10945 value_print (v, &buf, &opts);
10951 die->building_fullname = 0;
10955 /* Close the argument list, with a space if necessary
10956 (nested templates). */
10957 if (!buf.empty () && buf.string ().back () == '>')
10964 /* For C++ methods, append formal parameter type
10965 information, if PHYSNAME. */
10967 if (physname && die->tag == DW_TAG_subprogram
10968 && cu->language == language_cplus)
10970 struct type *type = read_type_die (die, cu);
10972 c_type_print_args (type, &buf, 1, cu->language,
10973 &type_print_raw_options);
10975 if (cu->language == language_cplus)
10977 /* Assume that an artificial first parameter is
10978 "this", but do not crash if it is not. RealView
10979 marks unnamed (and thus unused) parameters as
10980 artificial; there is no way to differentiate
10982 if (TYPE_NFIELDS (type) > 0
10983 && TYPE_FIELD_ARTIFICIAL (type, 0)
10984 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10985 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10987 buf.puts (" const");
10991 const std::string &intermediate_name = buf.string ();
10993 if (cu->language == language_cplus)
10995 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10996 &objfile->per_bfd->storage_obstack);
10998 /* If we only computed INTERMEDIATE_NAME, or if
10999 INTERMEDIATE_NAME is already canonical, then we need to
11000 copy it to the appropriate obstack. */
11001 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11002 name = ((const char *)
11003 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11004 intermediate_name.c_str (),
11005 intermediate_name.length ()));
11007 name = canonical_name;
11014 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11015 If scope qualifiers are appropriate they will be added. The result
11016 will be allocated on the storage_obstack, or NULL if the DIE does
11017 not have a name. NAME may either be from a previous call to
11018 dwarf2_name or NULL.
11020 The output string will be canonicalized (if C++). */
11022 static const char *
11023 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11025 return dwarf2_compute_name (name, die, cu, 0);
11028 /* Construct a physname for the given DIE in CU. NAME may either be
11029 from a previous call to dwarf2_name or NULL. The result will be
11030 allocated on the objfile_objstack or NULL if the DIE does not have a
11033 The output string will be canonicalized (if C++). */
11035 static const char *
11036 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11038 struct objfile *objfile = cu->objfile;
11039 const char *retval, *mangled = NULL, *canon = NULL;
11042 /* In this case dwarf2_compute_name is just a shortcut not building anything
11044 if (!die_needs_namespace (die, cu))
11045 return dwarf2_compute_name (name, die, cu, 1);
11047 mangled = dw2_linkage_name (die, cu);
11049 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11050 See https://github.com/rust-lang/rust/issues/32925. */
11051 if (cu->language == language_rust && mangled != NULL
11052 && strchr (mangled, '{') != NULL)
11055 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11057 gdb::unique_xmalloc_ptr<char> demangled;
11058 if (mangled != NULL)
11060 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
11061 type. It is easier for GDB users to search for such functions as
11062 `name(params)' than `long name(params)'. In such case the minimal
11063 symbol names do not match the full symbol names but for template
11064 functions there is never a need to look up their definition from their
11065 declaration so the only disadvantage remains the minimal symbol
11066 variant `long name(params)' does not have the proper inferior type.
11069 if (cu->language == language_go)
11071 /* This is a lie, but we already lie to the caller new_symbol_full.
11072 new_symbol_full assumes we return the mangled name.
11073 This just undoes that lie until things are cleaned up. */
11077 demangled.reset (gdb_demangle (mangled,
11078 (DMGL_PARAMS | DMGL_ANSI
11079 | DMGL_RET_DROP)));
11082 canon = demangled.get ();
11090 if (canon == NULL || check_physname)
11092 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11094 if (canon != NULL && strcmp (physname, canon) != 0)
11096 /* It may not mean a bug in GDB. The compiler could also
11097 compute DW_AT_linkage_name incorrectly. But in such case
11098 GDB would need to be bug-to-bug compatible. */
11100 complaint (&symfile_complaints,
11101 _("Computed physname <%s> does not match demangled <%s> "
11102 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
11103 physname, canon, mangled, to_underlying (die->sect_off),
11104 objfile_name (objfile));
11106 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11107 is available here - over computed PHYSNAME. It is safer
11108 against both buggy GDB and buggy compilers. */
11122 retval = ((const char *)
11123 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11124 retval, strlen (retval)));
11129 /* Inspect DIE in CU for a namespace alias. If one exists, record
11130 a new symbol for it.
11132 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11135 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11137 struct attribute *attr;
11139 /* If the die does not have a name, this is not a namespace
11141 attr = dwarf2_attr (die, DW_AT_name, cu);
11145 struct die_info *d = die;
11146 struct dwarf2_cu *imported_cu = cu;
11148 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11149 keep inspecting DIEs until we hit the underlying import. */
11150 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11151 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11153 attr = dwarf2_attr (d, DW_AT_import, cu);
11157 d = follow_die_ref (d, attr, &imported_cu);
11158 if (d->tag != DW_TAG_imported_declaration)
11162 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11164 complaint (&symfile_complaints,
11165 _("DIE at 0x%x has too many recursively imported "
11166 "declarations"), to_underlying (d->sect_off));
11173 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11175 type = get_die_type_at_offset (sect_off, cu->per_cu);
11176 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11178 /* This declaration is a global namespace alias. Add
11179 a symbol for it whose type is the aliased namespace. */
11180 new_symbol (die, type, cu);
11189 /* Return the using directives repository (global or local?) to use in the
11190 current context for LANGUAGE.
11192 For Ada, imported declarations can materialize renamings, which *may* be
11193 global. However it is impossible (for now?) in DWARF to distinguish
11194 "external" imported declarations and "static" ones. As all imported
11195 declarations seem to be static in all other languages, make them all CU-wide
11196 global only in Ada. */
11198 static struct using_direct **
11199 using_directives (enum language language)
11201 if (language == language_ada && context_stack_depth == 0)
11202 return &global_using_directives;
11204 return &local_using_directives;
11207 /* Read the import statement specified by the given die and record it. */
11210 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11212 struct objfile *objfile = cu->objfile;
11213 struct attribute *import_attr;
11214 struct die_info *imported_die, *child_die;
11215 struct dwarf2_cu *imported_cu;
11216 const char *imported_name;
11217 const char *imported_name_prefix;
11218 const char *canonical_name;
11219 const char *import_alias;
11220 const char *imported_declaration = NULL;
11221 const char *import_prefix;
11222 std::vector<const char *> excludes;
11224 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11225 if (import_attr == NULL)
11227 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11228 dwarf_tag_name (die->tag));
11233 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11234 imported_name = dwarf2_name (imported_die, imported_cu);
11235 if (imported_name == NULL)
11237 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11239 The import in the following code:
11253 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11254 <52> DW_AT_decl_file : 1
11255 <53> DW_AT_decl_line : 6
11256 <54> DW_AT_import : <0x75>
11257 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11258 <59> DW_AT_name : B
11259 <5b> DW_AT_decl_file : 1
11260 <5c> DW_AT_decl_line : 2
11261 <5d> DW_AT_type : <0x6e>
11263 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11264 <76> DW_AT_byte_size : 4
11265 <77> DW_AT_encoding : 5 (signed)
11267 imports the wrong die ( 0x75 instead of 0x58 ).
11268 This case will be ignored until the gcc bug is fixed. */
11272 /* Figure out the local name after import. */
11273 import_alias = dwarf2_name (die, cu);
11275 /* Figure out where the statement is being imported to. */
11276 import_prefix = determine_prefix (die, cu);
11278 /* Figure out what the scope of the imported die is and prepend it
11279 to the name of the imported die. */
11280 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11282 if (imported_die->tag != DW_TAG_namespace
11283 && imported_die->tag != DW_TAG_module)
11285 imported_declaration = imported_name;
11286 canonical_name = imported_name_prefix;
11288 else if (strlen (imported_name_prefix) > 0)
11289 canonical_name = obconcat (&objfile->objfile_obstack,
11290 imported_name_prefix,
11291 (cu->language == language_d ? "." : "::"),
11292 imported_name, (char *) NULL);
11294 canonical_name = imported_name;
11296 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11297 for (child_die = die->child; child_die && child_die->tag;
11298 child_die = sibling_die (child_die))
11300 /* DWARF-4: A Fortran use statement with a “rename list” may be
11301 represented by an imported module entry with an import attribute
11302 referring to the module and owned entries corresponding to those
11303 entities that are renamed as part of being imported. */
11305 if (child_die->tag != DW_TAG_imported_declaration)
11307 complaint (&symfile_complaints,
11308 _("child DW_TAG_imported_declaration expected "
11309 "- DIE at 0x%x [in module %s]"),
11310 to_underlying (child_die->sect_off), objfile_name (objfile));
11314 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11315 if (import_attr == NULL)
11317 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11318 dwarf_tag_name (child_die->tag));
11323 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11325 imported_name = dwarf2_name (imported_die, imported_cu);
11326 if (imported_name == NULL)
11328 complaint (&symfile_complaints,
11329 _("child DW_TAG_imported_declaration has unknown "
11330 "imported name - DIE at 0x%x [in module %s]"),
11331 to_underlying (child_die->sect_off), objfile_name (objfile));
11335 excludes.push_back (imported_name);
11337 process_die (child_die, cu);
11340 add_using_directive (using_directives (cu->language),
11344 imported_declaration,
11347 &objfile->objfile_obstack);
11350 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11351 types, but gives them a size of zero. Starting with version 14,
11352 ICC is compatible with GCC. */
11355 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11357 if (!cu->checked_producer)
11358 check_producer (cu);
11360 return cu->producer_is_icc_lt_14;
11363 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11364 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11365 this, it was first present in GCC release 4.3.0. */
11368 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11370 if (!cu->checked_producer)
11371 check_producer (cu);
11373 return cu->producer_is_gcc_lt_4_3;
11376 static file_and_directory
11377 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11379 file_and_directory res;
11381 /* Find the filename. Do not use dwarf2_name here, since the filename
11382 is not a source language identifier. */
11383 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11384 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11386 if (res.comp_dir == NULL
11387 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11388 && IS_ABSOLUTE_PATH (res.name))
11390 res.comp_dir_storage = ldirname (res.name);
11391 if (!res.comp_dir_storage.empty ())
11392 res.comp_dir = res.comp_dir_storage.c_str ();
11394 if (res.comp_dir != NULL)
11396 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11397 directory, get rid of it. */
11398 const char *cp = strchr (res.comp_dir, ':');
11400 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11401 res.comp_dir = cp + 1;
11404 if (res.name == NULL)
11405 res.name = "<unknown>";
11410 /* Handle DW_AT_stmt_list for a compilation unit.
11411 DIE is the DW_TAG_compile_unit die for CU.
11412 COMP_DIR is the compilation directory. LOWPC is passed to
11413 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11416 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11417 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11419 struct objfile *objfile = dwarf2_per_objfile->objfile;
11420 struct attribute *attr;
11421 struct line_header line_header_local;
11422 hashval_t line_header_local_hash;
11424 int decode_mapping;
11426 gdb_assert (! cu->per_cu->is_debug_types);
11428 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11432 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11434 /* The line header hash table is only created if needed (it exists to
11435 prevent redundant reading of the line table for partial_units).
11436 If we're given a partial_unit, we'll need it. If we're given a
11437 compile_unit, then use the line header hash table if it's already
11438 created, but don't create one just yet. */
11440 if (dwarf2_per_objfile->line_header_hash == NULL
11441 && die->tag == DW_TAG_partial_unit)
11443 dwarf2_per_objfile->line_header_hash
11444 = htab_create_alloc_ex (127, line_header_hash_voidp,
11445 line_header_eq_voidp,
11446 free_line_header_voidp,
11447 &objfile->objfile_obstack,
11448 hashtab_obstack_allocate,
11449 dummy_obstack_deallocate);
11452 line_header_local.sect_off = line_offset;
11453 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11454 line_header_local_hash = line_header_hash (&line_header_local);
11455 if (dwarf2_per_objfile->line_header_hash != NULL)
11457 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11458 &line_header_local,
11459 line_header_local_hash, NO_INSERT);
11461 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11462 is not present in *SLOT (since if there is something in *SLOT then
11463 it will be for a partial_unit). */
11464 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11466 gdb_assert (*slot != NULL);
11467 cu->line_header = (struct line_header *) *slot;
11472 /* dwarf_decode_line_header does not yet provide sufficient information.
11473 We always have to call also dwarf_decode_lines for it. */
11474 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11478 cu->line_header = lh.release ();
11479 cu->line_header_die_owner = die;
11481 if (dwarf2_per_objfile->line_header_hash == NULL)
11485 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11486 &line_header_local,
11487 line_header_local_hash, INSERT);
11488 gdb_assert (slot != NULL);
11490 if (slot != NULL && *slot == NULL)
11492 /* This newly decoded line number information unit will be owned
11493 by line_header_hash hash table. */
11494 *slot = cu->line_header;
11495 cu->line_header_die_owner = NULL;
11499 /* We cannot free any current entry in (*slot) as that struct line_header
11500 may be already used by multiple CUs. Create only temporary decoded
11501 line_header for this CU - it may happen at most once for each line
11502 number information unit. And if we're not using line_header_hash
11503 then this is what we want as well. */
11504 gdb_assert (die->tag != DW_TAG_partial_unit);
11506 decode_mapping = (die->tag != DW_TAG_partial_unit);
11507 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11512 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11515 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11517 struct objfile *objfile = dwarf2_per_objfile->objfile;
11518 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11519 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11520 CORE_ADDR highpc = ((CORE_ADDR) 0);
11521 struct attribute *attr;
11522 struct die_info *child_die;
11523 CORE_ADDR baseaddr;
11525 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11527 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11529 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11530 from finish_block. */
11531 if (lowpc == ((CORE_ADDR) -1))
11533 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11535 file_and_directory fnd = find_file_and_directory (die, cu);
11537 prepare_one_comp_unit (cu, die, cu->language);
11539 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11540 standardised yet. As a workaround for the language detection we fall
11541 back to the DW_AT_producer string. */
11542 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11543 cu->language = language_opencl;
11545 /* Similar hack for Go. */
11546 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11547 set_cu_language (DW_LANG_Go, cu);
11549 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
11551 /* Decode line number information if present. We do this before
11552 processing child DIEs, so that the line header table is available
11553 for DW_AT_decl_file. */
11554 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11556 /* Process all dies in compilation unit. */
11557 if (die->child != NULL)
11559 child_die = die->child;
11560 while (child_die && child_die->tag)
11562 process_die (child_die, cu);
11563 child_die = sibling_die (child_die);
11567 /* Decode macro information, if present. Dwarf 2 macro information
11568 refers to information in the line number info statement program
11569 header, so we can only read it if we've read the header
11571 attr = dwarf2_attr (die, DW_AT_macros, cu);
11573 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11574 if (attr && cu->line_header)
11576 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11577 complaint (&symfile_complaints,
11578 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11580 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11584 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11585 if (attr && cu->line_header)
11587 unsigned int macro_offset = DW_UNSND (attr);
11589 dwarf_decode_macros (cu, macro_offset, 0);
11594 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11595 Create the set of symtabs used by this TU, or if this TU is sharing
11596 symtabs with another TU and the symtabs have already been created
11597 then restore those symtabs in the line header.
11598 We don't need the pc/line-number mapping for type units. */
11601 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
11603 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11604 struct type_unit_group *tu_group;
11606 struct attribute *attr;
11608 struct signatured_type *sig_type;
11610 gdb_assert (per_cu->is_debug_types);
11611 sig_type = (struct signatured_type *) per_cu;
11613 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11615 /* If we're using .gdb_index (includes -readnow) then
11616 per_cu->type_unit_group may not have been set up yet. */
11617 if (sig_type->type_unit_group == NULL)
11618 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11619 tu_group = sig_type->type_unit_group;
11621 /* If we've already processed this stmt_list there's no real need to
11622 do it again, we could fake it and just recreate the part we need
11623 (file name,index -> symtab mapping). If data shows this optimization
11624 is useful we can do it then. */
11625 first_time = tu_group->compunit_symtab == NULL;
11627 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11632 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11633 lh = dwarf_decode_line_header (line_offset, cu);
11638 dwarf2_start_symtab (cu, "", NULL, 0);
11641 gdb_assert (tu_group->symtabs == NULL);
11642 restart_symtab (tu_group->compunit_symtab, "", 0);
11647 cu->line_header = lh.release ();
11648 cu->line_header_die_owner = die;
11652 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
11654 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11655 still initializing it, and our caller (a few levels up)
11656 process_full_type_unit still needs to know if this is the first
11659 tu_group->num_symtabs = cu->line_header->file_names.size ();
11660 tu_group->symtabs = XNEWVEC (struct symtab *,
11661 cu->line_header->file_names.size ());
11663 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11665 file_entry &fe = cu->line_header->file_names[i];
11667 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
11669 if (current_subfile->symtab == NULL)
11671 /* NOTE: start_subfile will recognize when it's been
11672 passed a file it has already seen. So we can't
11673 assume there's a simple mapping from
11674 cu->line_header->file_names to subfiles, plus
11675 cu->line_header->file_names may contain dups. */
11676 current_subfile->symtab
11677 = allocate_symtab (cust, current_subfile->name);
11680 fe.symtab = current_subfile->symtab;
11681 tu_group->symtabs[i] = fe.symtab;
11686 restart_symtab (tu_group->compunit_symtab, "", 0);
11688 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11690 file_entry &fe = cu->line_header->file_names[i];
11692 fe.symtab = tu_group->symtabs[i];
11696 /* The main symtab is allocated last. Type units don't have DW_AT_name
11697 so they don't have a "real" (so to speak) symtab anyway.
11698 There is later code that will assign the main symtab to all symbols
11699 that don't have one. We need to handle the case of a symbol with a
11700 missing symtab (DW_AT_decl_file) anyway. */
11703 /* Process DW_TAG_type_unit.
11704 For TUs we want to skip the first top level sibling if it's not the
11705 actual type being defined by this TU. In this case the first top
11706 level sibling is there to provide context only. */
11709 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11711 struct die_info *child_die;
11713 prepare_one_comp_unit (cu, die, language_minimal);
11715 /* Initialize (or reinitialize) the machinery for building symtabs.
11716 We do this before processing child DIEs, so that the line header table
11717 is available for DW_AT_decl_file. */
11718 setup_type_unit_groups (die, cu);
11720 if (die->child != NULL)
11722 child_die = die->child;
11723 while (child_die && child_die->tag)
11725 process_die (child_die, cu);
11726 child_die = sibling_die (child_die);
11733 http://gcc.gnu.org/wiki/DebugFission
11734 http://gcc.gnu.org/wiki/DebugFissionDWP
11736 To simplify handling of both DWO files ("object" files with the DWARF info)
11737 and DWP files (a file with the DWOs packaged up into one file), we treat
11738 DWP files as having a collection of virtual DWO files. */
11741 hash_dwo_file (const void *item)
11743 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11746 hash = htab_hash_string (dwo_file->dwo_name);
11747 if (dwo_file->comp_dir != NULL)
11748 hash += htab_hash_string (dwo_file->comp_dir);
11753 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11755 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11756 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11758 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11760 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11761 return lhs->comp_dir == rhs->comp_dir;
11762 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11765 /* Allocate a hash table for DWO files. */
11768 allocate_dwo_file_hash_table (void)
11770 struct objfile *objfile = dwarf2_per_objfile->objfile;
11772 return htab_create_alloc_ex (41,
11776 &objfile->objfile_obstack,
11777 hashtab_obstack_allocate,
11778 dummy_obstack_deallocate);
11781 /* Lookup DWO file DWO_NAME. */
11784 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
11786 struct dwo_file find_entry;
11789 if (dwarf2_per_objfile->dwo_files == NULL)
11790 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
11792 memset (&find_entry, 0, sizeof (find_entry));
11793 find_entry.dwo_name = dwo_name;
11794 find_entry.comp_dir = comp_dir;
11795 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11801 hash_dwo_unit (const void *item)
11803 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11805 /* This drops the top 32 bits of the id, but is ok for a hash. */
11806 return dwo_unit->signature;
11810 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11812 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11813 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11815 /* The signature is assumed to be unique within the DWO file.
11816 So while object file CU dwo_id's always have the value zero,
11817 that's OK, assuming each object file DWO file has only one CU,
11818 and that's the rule for now. */
11819 return lhs->signature == rhs->signature;
11822 /* Allocate a hash table for DWO CUs,TUs.
11823 There is one of these tables for each of CUs,TUs for each DWO file. */
11826 allocate_dwo_unit_table (struct objfile *objfile)
11828 /* Start out with a pretty small number.
11829 Generally DWO files contain only one CU and maybe some TUs. */
11830 return htab_create_alloc_ex (3,
11834 &objfile->objfile_obstack,
11835 hashtab_obstack_allocate,
11836 dummy_obstack_deallocate);
11839 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11841 struct create_dwo_cu_data
11843 struct dwo_file *dwo_file;
11844 struct dwo_unit dwo_unit;
11847 /* die_reader_func for create_dwo_cu. */
11850 create_dwo_cu_reader (const struct die_reader_specs *reader,
11851 const gdb_byte *info_ptr,
11852 struct die_info *comp_unit_die,
11856 struct dwarf2_cu *cu = reader->cu;
11857 sect_offset sect_off = cu->per_cu->sect_off;
11858 struct dwarf2_section_info *section = cu->per_cu->section;
11859 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11860 struct dwo_file *dwo_file = data->dwo_file;
11861 struct dwo_unit *dwo_unit = &data->dwo_unit;
11862 struct attribute *attr;
11864 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11867 complaint (&symfile_complaints,
11868 _("Dwarf Error: debug entry at offset 0x%x is missing"
11869 " its dwo_id [in module %s]"),
11870 to_underlying (sect_off), dwo_file->dwo_name);
11874 dwo_unit->dwo_file = dwo_file;
11875 dwo_unit->signature = DW_UNSND (attr);
11876 dwo_unit->section = section;
11877 dwo_unit->sect_off = sect_off;
11878 dwo_unit->length = cu->per_cu->length;
11880 if (dwarf_read_debug)
11881 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
11882 to_underlying (sect_off),
11883 hex_string (dwo_unit->signature));
11886 /* Create the dwo_units for the CUs in a DWO_FILE.
11887 Note: This function processes DWO files only, not DWP files. */
11890 create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info §ion,
11893 struct objfile *objfile = dwarf2_per_objfile->objfile;
11894 const gdb_byte *info_ptr, *end_ptr;
11896 dwarf2_read_section (objfile, §ion);
11897 info_ptr = section.buffer;
11899 if (info_ptr == NULL)
11902 if (dwarf_read_debug)
11904 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11905 get_section_name (§ion),
11906 get_section_file_name (§ion));
11909 end_ptr = info_ptr + section.size;
11910 while (info_ptr < end_ptr)
11912 struct dwarf2_per_cu_data per_cu;
11913 struct create_dwo_cu_data create_dwo_cu_data;
11914 struct dwo_unit *dwo_unit;
11916 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11918 memset (&create_dwo_cu_data.dwo_unit, 0,
11919 sizeof (create_dwo_cu_data.dwo_unit));
11920 memset (&per_cu, 0, sizeof (per_cu));
11921 per_cu.objfile = objfile;
11922 per_cu.is_debug_types = 0;
11923 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11924 per_cu.section = §ion;
11925 create_dwo_cu_data.dwo_file = &dwo_file;
11927 init_cutu_and_read_dies_no_follow (
11928 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11929 info_ptr += per_cu.length;
11931 // If the unit could not be parsed, skip it.
11932 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11935 if (cus_htab == NULL)
11936 cus_htab = allocate_dwo_unit_table (objfile);
11938 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11939 *dwo_unit = create_dwo_cu_data.dwo_unit;
11940 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11941 gdb_assert (slot != NULL);
11944 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11945 sect_offset dup_sect_off = dup_cu->sect_off;
11947 complaint (&symfile_complaints,
11948 _("debug cu entry at offset 0x%x is duplicate to"
11949 " the entry at offset 0x%x, signature %s"),
11950 to_underlying (sect_off), to_underlying (dup_sect_off),
11951 hex_string (dwo_unit->signature));
11953 *slot = (void *)dwo_unit;
11957 /* DWP file .debug_{cu,tu}_index section format:
11958 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11962 Both index sections have the same format, and serve to map a 64-bit
11963 signature to a set of section numbers. Each section begins with a header,
11964 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11965 indexes, and a pool of 32-bit section numbers. The index sections will be
11966 aligned at 8-byte boundaries in the file.
11968 The index section header consists of:
11970 V, 32 bit version number
11972 N, 32 bit number of compilation units or type units in the index
11973 M, 32 bit number of slots in the hash table
11975 Numbers are recorded using the byte order of the application binary.
11977 The hash table begins at offset 16 in the section, and consists of an array
11978 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11979 order of the application binary). Unused slots in the hash table are 0.
11980 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11982 The parallel table begins immediately after the hash table
11983 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11984 array of 32-bit indexes (using the byte order of the application binary),
11985 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11986 table contains a 32-bit index into the pool of section numbers. For unused
11987 hash table slots, the corresponding entry in the parallel table will be 0.
11989 The pool of section numbers begins immediately following the hash table
11990 (at offset 16 + 12 * M from the beginning of the section). The pool of
11991 section numbers consists of an array of 32-bit words (using the byte order
11992 of the application binary). Each item in the array is indexed starting
11993 from 0. The hash table entry provides the index of the first section
11994 number in the set. Additional section numbers in the set follow, and the
11995 set is terminated by a 0 entry (section number 0 is not used in ELF).
11997 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11998 section must be the first entry in the set, and the .debug_abbrev.dwo must
11999 be the second entry. Other members of the set may follow in any order.
12005 DWP Version 2 combines all the .debug_info, etc. sections into one,
12006 and the entries in the index tables are now offsets into these sections.
12007 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12010 Index Section Contents:
12012 Hash Table of Signatures dwp_hash_table.hash_table
12013 Parallel Table of Indices dwp_hash_table.unit_table
12014 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12015 Table of Section Sizes dwp_hash_table.v2.sizes
12017 The index section header consists of:
12019 V, 32 bit version number
12020 L, 32 bit number of columns in the table of section offsets
12021 N, 32 bit number of compilation units or type units in the index
12022 M, 32 bit number of slots in the hash table
12024 Numbers are recorded using the byte order of the application binary.
12026 The hash table has the same format as version 1.
12027 The parallel table of indices has the same format as version 1,
12028 except that the entries are origin-1 indices into the table of sections
12029 offsets and the table of section sizes.
12031 The table of offsets begins immediately following the parallel table
12032 (at offset 16 + 12 * M from the beginning of the section). The table is
12033 a two-dimensional array of 32-bit words (using the byte order of the
12034 application binary), with L columns and N+1 rows, in row-major order.
12035 Each row in the array is indexed starting from 0. The first row provides
12036 a key to the remaining rows: each column in this row provides an identifier
12037 for a debug section, and the offsets in the same column of subsequent rows
12038 refer to that section. The section identifiers are:
12040 DW_SECT_INFO 1 .debug_info.dwo
12041 DW_SECT_TYPES 2 .debug_types.dwo
12042 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12043 DW_SECT_LINE 4 .debug_line.dwo
12044 DW_SECT_LOC 5 .debug_loc.dwo
12045 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12046 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12047 DW_SECT_MACRO 8 .debug_macro.dwo
12049 The offsets provided by the CU and TU index sections are the base offsets
12050 for the contributions made by each CU or TU to the corresponding section
12051 in the package file. Each CU and TU header contains an abbrev_offset
12052 field, used to find the abbreviations table for that CU or TU within the
12053 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12054 be interpreted as relative to the base offset given in the index section.
12055 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12056 should be interpreted as relative to the base offset for .debug_line.dwo,
12057 and offsets into other debug sections obtained from DWARF attributes should
12058 also be interpreted as relative to the corresponding base offset.
12060 The table of sizes begins immediately following the table of offsets.
12061 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12062 with L columns and N rows, in row-major order. Each row in the array is
12063 indexed starting from 1 (row 0 is shared by the two tables).
12067 Hash table lookup is handled the same in version 1 and 2:
12069 We assume that N and M will not exceed 2^32 - 1.
12070 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12072 Given a 64-bit compilation unit signature or a type signature S, an entry
12073 in the hash table is located as follows:
12075 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12076 the low-order k bits all set to 1.
12078 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12080 3) If the hash table entry at index H matches the signature, use that
12081 entry. If the hash table entry at index H is unused (all zeroes),
12082 terminate the search: the signature is not present in the table.
12084 4) Let H = (H + H') modulo M. Repeat at Step 3.
12086 Because M > N and H' and M are relatively prime, the search is guaranteed
12087 to stop at an unused slot or find the match. */
12089 /* Create a hash table to map DWO IDs to their CU/TU entry in
12090 .debug_{info,types}.dwo in DWP_FILE.
12091 Returns NULL if there isn't one.
12092 Note: This function processes DWP files only, not DWO files. */
12094 static struct dwp_hash_table *
12095 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
12097 struct objfile *objfile = dwarf2_per_objfile->objfile;
12098 bfd *dbfd = dwp_file->dbfd;
12099 const gdb_byte *index_ptr, *index_end;
12100 struct dwarf2_section_info *index;
12101 uint32_t version, nr_columns, nr_units, nr_slots;
12102 struct dwp_hash_table *htab;
12104 if (is_debug_types)
12105 index = &dwp_file->sections.tu_index;
12107 index = &dwp_file->sections.cu_index;
12109 if (dwarf2_section_empty_p (index))
12111 dwarf2_read_section (objfile, index);
12113 index_ptr = index->buffer;
12114 index_end = index_ptr + index->size;
12116 version = read_4_bytes (dbfd, index_ptr);
12119 nr_columns = read_4_bytes (dbfd, index_ptr);
12123 nr_units = read_4_bytes (dbfd, index_ptr);
12125 nr_slots = read_4_bytes (dbfd, index_ptr);
12128 if (version != 1 && version != 2)
12130 error (_("Dwarf Error: unsupported DWP file version (%s)"
12131 " [in module %s]"),
12132 pulongest (version), dwp_file->name);
12134 if (nr_slots != (nr_slots & -nr_slots))
12136 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12137 " is not power of 2 [in module %s]"),
12138 pulongest (nr_slots), dwp_file->name);
12141 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12142 htab->version = version;
12143 htab->nr_columns = nr_columns;
12144 htab->nr_units = nr_units;
12145 htab->nr_slots = nr_slots;
12146 htab->hash_table = index_ptr;
12147 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12149 /* Exit early if the table is empty. */
12150 if (nr_slots == 0 || nr_units == 0
12151 || (version == 2 && nr_columns == 0))
12153 /* All must be zero. */
12154 if (nr_slots != 0 || nr_units != 0
12155 || (version == 2 && nr_columns != 0))
12157 complaint (&symfile_complaints,
12158 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12159 " all zero [in modules %s]"),
12167 htab->section_pool.v1.indices =
12168 htab->unit_table + sizeof (uint32_t) * nr_slots;
12169 /* It's harder to decide whether the section is too small in v1.
12170 V1 is deprecated anyway so we punt. */
12174 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12175 int *ids = htab->section_pool.v2.section_ids;
12176 /* Reverse map for error checking. */
12177 int ids_seen[DW_SECT_MAX + 1];
12180 if (nr_columns < 2)
12182 error (_("Dwarf Error: bad DWP hash table, too few columns"
12183 " in section table [in module %s]"),
12186 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12188 error (_("Dwarf Error: bad DWP hash table, too many columns"
12189 " in section table [in module %s]"),
12192 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12193 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12194 for (i = 0; i < nr_columns; ++i)
12196 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12198 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12200 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12201 " in section table [in module %s]"),
12202 id, dwp_file->name);
12204 if (ids_seen[id] != -1)
12206 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12207 " id %d in section table [in module %s]"),
12208 id, dwp_file->name);
12213 /* Must have exactly one info or types section. */
12214 if (((ids_seen[DW_SECT_INFO] != -1)
12215 + (ids_seen[DW_SECT_TYPES] != -1))
12218 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12219 " DWO info/types section [in module %s]"),
12222 /* Must have an abbrev section. */
12223 if (ids_seen[DW_SECT_ABBREV] == -1)
12225 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12226 " section [in module %s]"),
12229 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12230 htab->section_pool.v2.sizes =
12231 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12232 * nr_units * nr_columns);
12233 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12234 * nr_units * nr_columns))
12237 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12238 " [in module %s]"),
12246 /* Update SECTIONS with the data from SECTP.
12248 This function is like the other "locate" section routines that are
12249 passed to bfd_map_over_sections, but in this context the sections to
12250 read comes from the DWP V1 hash table, not the full ELF section table.
12252 The result is non-zero for success, or zero if an error was found. */
12255 locate_v1_virtual_dwo_sections (asection *sectp,
12256 struct virtual_v1_dwo_sections *sections)
12258 const struct dwop_section_names *names = &dwop_section_names;
12260 if (section_is_p (sectp->name, &names->abbrev_dwo))
12262 /* There can be only one. */
12263 if (sections->abbrev.s.section != NULL)
12265 sections->abbrev.s.section = sectp;
12266 sections->abbrev.size = bfd_get_section_size (sectp);
12268 else if (section_is_p (sectp->name, &names->info_dwo)
12269 || section_is_p (sectp->name, &names->types_dwo))
12271 /* There can be only one. */
12272 if (sections->info_or_types.s.section != NULL)
12274 sections->info_or_types.s.section = sectp;
12275 sections->info_or_types.size = bfd_get_section_size (sectp);
12277 else if (section_is_p (sectp->name, &names->line_dwo))
12279 /* There can be only one. */
12280 if (sections->line.s.section != NULL)
12282 sections->line.s.section = sectp;
12283 sections->line.size = bfd_get_section_size (sectp);
12285 else if (section_is_p (sectp->name, &names->loc_dwo))
12287 /* There can be only one. */
12288 if (sections->loc.s.section != NULL)
12290 sections->loc.s.section = sectp;
12291 sections->loc.size = bfd_get_section_size (sectp);
12293 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12295 /* There can be only one. */
12296 if (sections->macinfo.s.section != NULL)
12298 sections->macinfo.s.section = sectp;
12299 sections->macinfo.size = bfd_get_section_size (sectp);
12301 else if (section_is_p (sectp->name, &names->macro_dwo))
12303 /* There can be only one. */
12304 if (sections->macro.s.section != NULL)
12306 sections->macro.s.section = sectp;
12307 sections->macro.size = bfd_get_section_size (sectp);
12309 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12311 /* There can be only one. */
12312 if (sections->str_offsets.s.section != NULL)
12314 sections->str_offsets.s.section = sectp;
12315 sections->str_offsets.size = bfd_get_section_size (sectp);
12319 /* No other kind of section is valid. */
12326 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12327 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12328 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12329 This is for DWP version 1 files. */
12331 static struct dwo_unit *
12332 create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
12333 uint32_t unit_index,
12334 const char *comp_dir,
12335 ULONGEST signature, int is_debug_types)
12337 struct objfile *objfile = dwarf2_per_objfile->objfile;
12338 const struct dwp_hash_table *dwp_htab =
12339 is_debug_types ? dwp_file->tus : dwp_file->cus;
12340 bfd *dbfd = dwp_file->dbfd;
12341 const char *kind = is_debug_types ? "TU" : "CU";
12342 struct dwo_file *dwo_file;
12343 struct dwo_unit *dwo_unit;
12344 struct virtual_v1_dwo_sections sections;
12345 void **dwo_file_slot;
12348 gdb_assert (dwp_file->version == 1);
12350 if (dwarf_read_debug)
12352 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12354 pulongest (unit_index), hex_string (signature),
12358 /* Fetch the sections of this DWO unit.
12359 Put a limit on the number of sections we look for so that bad data
12360 doesn't cause us to loop forever. */
12362 #define MAX_NR_V1_DWO_SECTIONS \
12363 (1 /* .debug_info or .debug_types */ \
12364 + 1 /* .debug_abbrev */ \
12365 + 1 /* .debug_line */ \
12366 + 1 /* .debug_loc */ \
12367 + 1 /* .debug_str_offsets */ \
12368 + 1 /* .debug_macro or .debug_macinfo */ \
12369 + 1 /* trailing zero */)
12371 memset (§ions, 0, sizeof (sections));
12373 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12376 uint32_t section_nr =
12377 read_4_bytes (dbfd,
12378 dwp_htab->section_pool.v1.indices
12379 + (unit_index + i) * sizeof (uint32_t));
12381 if (section_nr == 0)
12383 if (section_nr >= dwp_file->num_sections)
12385 error (_("Dwarf Error: bad DWP hash table, section number too large"
12386 " [in module %s]"),
12390 sectp = dwp_file->elf_sections[section_nr];
12391 if (! locate_v1_virtual_dwo_sections (sectp, §ions))
12393 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12394 " [in module %s]"),
12400 || dwarf2_section_empty_p (§ions.info_or_types)
12401 || dwarf2_section_empty_p (§ions.abbrev))
12403 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12404 " [in module %s]"),
12407 if (i == MAX_NR_V1_DWO_SECTIONS)
12409 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12410 " [in module %s]"),
12414 /* It's easier for the rest of the code if we fake a struct dwo_file and
12415 have dwo_unit "live" in that. At least for now.
12417 The DWP file can be made up of a random collection of CUs and TUs.
12418 However, for each CU + set of TUs that came from the same original DWO
12419 file, we can combine them back into a virtual DWO file to save space
12420 (fewer struct dwo_file objects to allocate). Remember that for really
12421 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12423 std::string virtual_dwo_name =
12424 string_printf ("virtual-dwo/%d-%d-%d-%d",
12425 get_section_id (§ions.abbrev),
12426 get_section_id (§ions.line),
12427 get_section_id (§ions.loc),
12428 get_section_id (§ions.str_offsets));
12429 /* Can we use an existing virtual DWO file? */
12430 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
12431 /* Create one if necessary. */
12432 if (*dwo_file_slot == NULL)
12434 if (dwarf_read_debug)
12436 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12437 virtual_dwo_name.c_str ());
12439 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12441 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12442 virtual_dwo_name.c_str (),
12443 virtual_dwo_name.size ());
12444 dwo_file->comp_dir = comp_dir;
12445 dwo_file->sections.abbrev = sections.abbrev;
12446 dwo_file->sections.line = sections.line;
12447 dwo_file->sections.loc = sections.loc;
12448 dwo_file->sections.macinfo = sections.macinfo;
12449 dwo_file->sections.macro = sections.macro;
12450 dwo_file->sections.str_offsets = sections.str_offsets;
12451 /* The "str" section is global to the entire DWP file. */
12452 dwo_file->sections.str = dwp_file->sections.str;
12453 /* The info or types section is assigned below to dwo_unit,
12454 there's no need to record it in dwo_file.
12455 Also, we can't simply record type sections in dwo_file because
12456 we record a pointer into the vector in dwo_unit. As we collect more
12457 types we'll grow the vector and eventually have to reallocate space
12458 for it, invalidating all copies of pointers into the previous
12460 *dwo_file_slot = dwo_file;
12464 if (dwarf_read_debug)
12466 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12467 virtual_dwo_name.c_str ());
12469 dwo_file = (struct dwo_file *) *dwo_file_slot;
12472 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12473 dwo_unit->dwo_file = dwo_file;
12474 dwo_unit->signature = signature;
12475 dwo_unit->section =
12476 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12477 *dwo_unit->section = sections.info_or_types;
12478 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12483 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12484 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12485 piece within that section used by a TU/CU, return a virtual section
12486 of just that piece. */
12488 static struct dwarf2_section_info
12489 create_dwp_v2_section (struct dwarf2_section_info *section,
12490 bfd_size_type offset, bfd_size_type size)
12492 struct dwarf2_section_info result;
12495 gdb_assert (section != NULL);
12496 gdb_assert (!section->is_virtual);
12498 memset (&result, 0, sizeof (result));
12499 result.s.containing_section = section;
12500 result.is_virtual = 1;
12505 sectp = get_section_bfd_section (section);
12507 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12508 bounds of the real section. This is a pretty-rare event, so just
12509 flag an error (easier) instead of a warning and trying to cope. */
12511 || offset + size > bfd_get_section_size (sectp))
12513 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12514 " in section %s [in module %s]"),
12515 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12516 objfile_name (dwarf2_per_objfile->objfile));
12519 result.virtual_offset = offset;
12520 result.size = size;
12524 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12525 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12526 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12527 This is for DWP version 2 files. */
12529 static struct dwo_unit *
12530 create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
12531 uint32_t unit_index,
12532 const char *comp_dir,
12533 ULONGEST signature, int is_debug_types)
12535 struct objfile *objfile = dwarf2_per_objfile->objfile;
12536 const struct dwp_hash_table *dwp_htab =
12537 is_debug_types ? dwp_file->tus : dwp_file->cus;
12538 bfd *dbfd = dwp_file->dbfd;
12539 const char *kind = is_debug_types ? "TU" : "CU";
12540 struct dwo_file *dwo_file;
12541 struct dwo_unit *dwo_unit;
12542 struct virtual_v2_dwo_sections sections;
12543 void **dwo_file_slot;
12546 gdb_assert (dwp_file->version == 2);
12548 if (dwarf_read_debug)
12550 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12552 pulongest (unit_index), hex_string (signature),
12556 /* Fetch the section offsets of this DWO unit. */
12558 memset (§ions, 0, sizeof (sections));
12560 for (i = 0; i < dwp_htab->nr_columns; ++i)
12562 uint32_t offset = read_4_bytes (dbfd,
12563 dwp_htab->section_pool.v2.offsets
12564 + (((unit_index - 1) * dwp_htab->nr_columns
12566 * sizeof (uint32_t)));
12567 uint32_t size = read_4_bytes (dbfd,
12568 dwp_htab->section_pool.v2.sizes
12569 + (((unit_index - 1) * dwp_htab->nr_columns
12571 * sizeof (uint32_t)));
12573 switch (dwp_htab->section_pool.v2.section_ids[i])
12576 case DW_SECT_TYPES:
12577 sections.info_or_types_offset = offset;
12578 sections.info_or_types_size = size;
12580 case DW_SECT_ABBREV:
12581 sections.abbrev_offset = offset;
12582 sections.abbrev_size = size;
12585 sections.line_offset = offset;
12586 sections.line_size = size;
12589 sections.loc_offset = offset;
12590 sections.loc_size = size;
12592 case DW_SECT_STR_OFFSETS:
12593 sections.str_offsets_offset = offset;
12594 sections.str_offsets_size = size;
12596 case DW_SECT_MACINFO:
12597 sections.macinfo_offset = offset;
12598 sections.macinfo_size = size;
12600 case DW_SECT_MACRO:
12601 sections.macro_offset = offset;
12602 sections.macro_size = size;
12607 /* It's easier for the rest of the code if we fake a struct dwo_file and
12608 have dwo_unit "live" in that. At least for now.
12610 The DWP file can be made up of a random collection of CUs and TUs.
12611 However, for each CU + set of TUs that came from the same original DWO
12612 file, we can combine them back into a virtual DWO file to save space
12613 (fewer struct dwo_file objects to allocate). Remember that for really
12614 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12616 std::string virtual_dwo_name =
12617 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12618 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12619 (long) (sections.line_size ? sections.line_offset : 0),
12620 (long) (sections.loc_size ? sections.loc_offset : 0),
12621 (long) (sections.str_offsets_size
12622 ? sections.str_offsets_offset : 0));
12623 /* Can we use an existing virtual DWO file? */
12624 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
12625 /* Create one if necessary. */
12626 if (*dwo_file_slot == NULL)
12628 if (dwarf_read_debug)
12630 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12631 virtual_dwo_name.c_str ());
12633 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12635 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12636 virtual_dwo_name.c_str (),
12637 virtual_dwo_name.size ());
12638 dwo_file->comp_dir = comp_dir;
12639 dwo_file->sections.abbrev =
12640 create_dwp_v2_section (&dwp_file->sections.abbrev,
12641 sections.abbrev_offset, sections.abbrev_size);
12642 dwo_file->sections.line =
12643 create_dwp_v2_section (&dwp_file->sections.line,
12644 sections.line_offset, sections.line_size);
12645 dwo_file->sections.loc =
12646 create_dwp_v2_section (&dwp_file->sections.loc,
12647 sections.loc_offset, sections.loc_size);
12648 dwo_file->sections.macinfo =
12649 create_dwp_v2_section (&dwp_file->sections.macinfo,
12650 sections.macinfo_offset, sections.macinfo_size);
12651 dwo_file->sections.macro =
12652 create_dwp_v2_section (&dwp_file->sections.macro,
12653 sections.macro_offset, sections.macro_size);
12654 dwo_file->sections.str_offsets =
12655 create_dwp_v2_section (&dwp_file->sections.str_offsets,
12656 sections.str_offsets_offset,
12657 sections.str_offsets_size);
12658 /* The "str" section is global to the entire DWP file. */
12659 dwo_file->sections.str = dwp_file->sections.str;
12660 /* The info or types section is assigned below to dwo_unit,
12661 there's no need to record it in dwo_file.
12662 Also, we can't simply record type sections in dwo_file because
12663 we record a pointer into the vector in dwo_unit. As we collect more
12664 types we'll grow the vector and eventually have to reallocate space
12665 for it, invalidating all copies of pointers into the previous
12667 *dwo_file_slot = dwo_file;
12671 if (dwarf_read_debug)
12673 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12674 virtual_dwo_name.c_str ());
12676 dwo_file = (struct dwo_file *) *dwo_file_slot;
12679 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12680 dwo_unit->dwo_file = dwo_file;
12681 dwo_unit->signature = signature;
12682 dwo_unit->section =
12683 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12684 *dwo_unit->section = create_dwp_v2_section (is_debug_types
12685 ? &dwp_file->sections.types
12686 : &dwp_file->sections.info,
12687 sections.info_or_types_offset,
12688 sections.info_or_types_size);
12689 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12694 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12695 Returns NULL if the signature isn't found. */
12697 static struct dwo_unit *
12698 lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
12699 ULONGEST signature, int is_debug_types)
12701 const struct dwp_hash_table *dwp_htab =
12702 is_debug_types ? dwp_file->tus : dwp_file->cus;
12703 bfd *dbfd = dwp_file->dbfd;
12704 uint32_t mask = dwp_htab->nr_slots - 1;
12705 uint32_t hash = signature & mask;
12706 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12709 struct dwo_unit find_dwo_cu;
12711 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12712 find_dwo_cu.signature = signature;
12713 slot = htab_find_slot (is_debug_types
12714 ? dwp_file->loaded_tus
12715 : dwp_file->loaded_cus,
12716 &find_dwo_cu, INSERT);
12719 return (struct dwo_unit *) *slot;
12721 /* Use a for loop so that we don't loop forever on bad debug info. */
12722 for (i = 0; i < dwp_htab->nr_slots; ++i)
12724 ULONGEST signature_in_table;
12726 signature_in_table =
12727 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12728 if (signature_in_table == signature)
12730 uint32_t unit_index =
12731 read_4_bytes (dbfd,
12732 dwp_htab->unit_table + hash * sizeof (uint32_t));
12734 if (dwp_file->version == 1)
12736 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
12737 comp_dir, signature,
12742 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
12743 comp_dir, signature,
12746 return (struct dwo_unit *) *slot;
12748 if (signature_in_table == 0)
12750 hash = (hash + hash2) & mask;
12753 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12754 " [in module %s]"),
12758 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12759 Open the file specified by FILE_NAME and hand it off to BFD for
12760 preliminary analysis. Return a newly initialized bfd *, which
12761 includes a canonicalized copy of FILE_NAME.
12762 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12763 SEARCH_CWD is true if the current directory is to be searched.
12764 It will be searched before debug-file-directory.
12765 If successful, the file is added to the bfd include table of the
12766 objfile's bfd (see gdb_bfd_record_inclusion).
12767 If unable to find/open the file, return NULL.
12768 NOTE: This function is derived from symfile_bfd_open. */
12770 static gdb_bfd_ref_ptr
12771 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
12774 char *absolute_name;
12775 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12776 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12777 to debug_file_directory. */
12779 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12783 if (*debug_file_directory != '\0')
12784 search_path = concat (".", dirname_separator_string,
12785 debug_file_directory, (char *) NULL);
12787 search_path = xstrdup (".");
12790 search_path = xstrdup (debug_file_directory);
12792 flags = OPF_RETURN_REALPATH;
12794 flags |= OPF_SEARCH_IN_PATH;
12795 desc = openp (search_path, flags, file_name,
12796 O_RDONLY | O_BINARY, &absolute_name);
12797 xfree (search_path);
12801 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
12802 xfree (absolute_name);
12803 if (sym_bfd == NULL)
12805 bfd_set_cacheable (sym_bfd.get (), 1);
12807 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12810 /* Success. Record the bfd as having been included by the objfile's bfd.
12811 This is important because things like demangled_names_hash lives in the
12812 objfile's per_bfd space and may have references to things like symbol
12813 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12814 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12819 /* Try to open DWO file FILE_NAME.
12820 COMP_DIR is the DW_AT_comp_dir attribute.
12821 The result is the bfd handle of the file.
12822 If there is a problem finding or opening the file, return NULL.
12823 Upon success, the canonicalized path of the file is stored in the bfd,
12824 same as symfile_bfd_open. */
12826 static gdb_bfd_ref_ptr
12827 open_dwo_file (const char *file_name, const char *comp_dir)
12829 if (IS_ABSOLUTE_PATH (file_name))
12830 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
12832 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12834 if (comp_dir != NULL)
12836 char *path_to_try = concat (comp_dir, SLASH_STRING,
12837 file_name, (char *) NULL);
12839 /* NOTE: If comp_dir is a relative path, this will also try the
12840 search path, which seems useful. */
12841 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
12842 1 /*search_cwd*/));
12843 xfree (path_to_try);
12848 /* That didn't work, try debug-file-directory, which, despite its name,
12849 is a list of paths. */
12851 if (*debug_file_directory == '\0')
12854 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
12857 /* This function is mapped across the sections and remembers the offset and
12858 size of each of the DWO debugging sections we are interested in. */
12861 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12863 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12864 const struct dwop_section_names *names = &dwop_section_names;
12866 if (section_is_p (sectp->name, &names->abbrev_dwo))
12868 dwo_sections->abbrev.s.section = sectp;
12869 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12871 else if (section_is_p (sectp->name, &names->info_dwo))
12873 dwo_sections->info.s.section = sectp;
12874 dwo_sections->info.size = bfd_get_section_size (sectp);
12876 else if (section_is_p (sectp->name, &names->line_dwo))
12878 dwo_sections->line.s.section = sectp;
12879 dwo_sections->line.size = bfd_get_section_size (sectp);
12881 else if (section_is_p (sectp->name, &names->loc_dwo))
12883 dwo_sections->loc.s.section = sectp;
12884 dwo_sections->loc.size = bfd_get_section_size (sectp);
12886 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12888 dwo_sections->macinfo.s.section = sectp;
12889 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12891 else if (section_is_p (sectp->name, &names->macro_dwo))
12893 dwo_sections->macro.s.section = sectp;
12894 dwo_sections->macro.size = bfd_get_section_size (sectp);
12896 else if (section_is_p (sectp->name, &names->str_dwo))
12898 dwo_sections->str.s.section = sectp;
12899 dwo_sections->str.size = bfd_get_section_size (sectp);
12901 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12903 dwo_sections->str_offsets.s.section = sectp;
12904 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12906 else if (section_is_p (sectp->name, &names->types_dwo))
12908 struct dwarf2_section_info type_section;
12910 memset (&type_section, 0, sizeof (type_section));
12911 type_section.s.section = sectp;
12912 type_section.size = bfd_get_section_size (sectp);
12913 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
12918 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12919 by PER_CU. This is for the non-DWP case.
12920 The result is NULL if DWO_NAME can't be found. */
12922 static struct dwo_file *
12923 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12924 const char *dwo_name, const char *comp_dir)
12926 struct objfile *objfile = dwarf2_per_objfile->objfile;
12927 struct dwo_file *dwo_file;
12928 struct cleanup *cleanups;
12930 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
12933 if (dwarf_read_debug)
12934 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12937 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12938 dwo_file->dwo_name = dwo_name;
12939 dwo_file->comp_dir = comp_dir;
12940 dwo_file->dbfd = dbfd.release ();
12942 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
12944 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
12945 &dwo_file->sections);
12947 create_cus_hash_table (*dwo_file, dwo_file->sections.info, dwo_file->cus);
12949 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
12952 discard_cleanups (cleanups);
12954 if (dwarf_read_debug)
12955 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12960 /* This function is mapped across the sections and remembers the offset and
12961 size of each of the DWP debugging sections common to version 1 and 2 that
12962 we are interested in. */
12965 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12966 void *dwp_file_ptr)
12968 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12969 const struct dwop_section_names *names = &dwop_section_names;
12970 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12972 /* Record the ELF section number for later lookup: this is what the
12973 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12974 gdb_assert (elf_section_nr < dwp_file->num_sections);
12975 dwp_file->elf_sections[elf_section_nr] = sectp;
12977 /* Look for specific sections that we need. */
12978 if (section_is_p (sectp->name, &names->str_dwo))
12980 dwp_file->sections.str.s.section = sectp;
12981 dwp_file->sections.str.size = bfd_get_section_size (sectp);
12983 else if (section_is_p (sectp->name, &names->cu_index))
12985 dwp_file->sections.cu_index.s.section = sectp;
12986 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
12988 else if (section_is_p (sectp->name, &names->tu_index))
12990 dwp_file->sections.tu_index.s.section = sectp;
12991 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
12995 /* This function is mapped across the sections and remembers the offset and
12996 size of each of the DWP version 2 debugging sections that we are interested
12997 in. This is split into a separate function because we don't know if we
12998 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13001 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13003 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13004 const struct dwop_section_names *names = &dwop_section_names;
13005 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13007 /* Record the ELF section number for later lookup: this is what the
13008 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13009 gdb_assert (elf_section_nr < dwp_file->num_sections);
13010 dwp_file->elf_sections[elf_section_nr] = sectp;
13012 /* Look for specific sections that we need. */
13013 if (section_is_p (sectp->name, &names->abbrev_dwo))
13015 dwp_file->sections.abbrev.s.section = sectp;
13016 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13018 else if (section_is_p (sectp->name, &names->info_dwo))
13020 dwp_file->sections.info.s.section = sectp;
13021 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13023 else if (section_is_p (sectp->name, &names->line_dwo))
13025 dwp_file->sections.line.s.section = sectp;
13026 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13028 else if (section_is_p (sectp->name, &names->loc_dwo))
13030 dwp_file->sections.loc.s.section = sectp;
13031 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13033 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13035 dwp_file->sections.macinfo.s.section = sectp;
13036 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13038 else if (section_is_p (sectp->name, &names->macro_dwo))
13040 dwp_file->sections.macro.s.section = sectp;
13041 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13043 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13045 dwp_file->sections.str_offsets.s.section = sectp;
13046 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13048 else if (section_is_p (sectp->name, &names->types_dwo))
13050 dwp_file->sections.types.s.section = sectp;
13051 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13055 /* Hash function for dwp_file loaded CUs/TUs. */
13058 hash_dwp_loaded_cutus (const void *item)
13060 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13062 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13063 return dwo_unit->signature;
13066 /* Equality function for dwp_file loaded CUs/TUs. */
13069 eq_dwp_loaded_cutus (const void *a, const void *b)
13071 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13072 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13074 return dua->signature == dub->signature;
13077 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13080 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13082 return htab_create_alloc_ex (3,
13083 hash_dwp_loaded_cutus,
13084 eq_dwp_loaded_cutus,
13086 &objfile->objfile_obstack,
13087 hashtab_obstack_allocate,
13088 dummy_obstack_deallocate);
13091 /* Try to open DWP file FILE_NAME.
13092 The result is the bfd handle of the file.
13093 If there is a problem finding or opening the file, return NULL.
13094 Upon success, the canonicalized path of the file is stored in the bfd,
13095 same as symfile_bfd_open. */
13097 static gdb_bfd_ref_ptr
13098 open_dwp_file (const char *file_name)
13100 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
13101 1 /*search_cwd*/));
13105 /* Work around upstream bug 15652.
13106 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13107 [Whether that's a "bug" is debatable, but it is getting in our way.]
13108 We have no real idea where the dwp file is, because gdb's realpath-ing
13109 of the executable's path may have discarded the needed info.
13110 [IWBN if the dwp file name was recorded in the executable, akin to
13111 .gnu_debuglink, but that doesn't exist yet.]
13112 Strip the directory from FILE_NAME and search again. */
13113 if (*debug_file_directory != '\0')
13115 /* Don't implicitly search the current directory here.
13116 If the user wants to search "." to handle this case,
13117 it must be added to debug-file-directory. */
13118 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
13125 /* Initialize the use of the DWP file for the current objfile.
13126 By convention the name of the DWP file is ${objfile}.dwp.
13127 The result is NULL if it can't be found. */
13129 static struct dwp_file *
13130 open_and_init_dwp_file (void)
13132 struct objfile *objfile = dwarf2_per_objfile->objfile;
13133 struct dwp_file *dwp_file;
13135 /* Try to find first .dwp for the binary file before any symbolic links
13138 /* If the objfile is a debug file, find the name of the real binary
13139 file and get the name of dwp file from there. */
13140 std::string dwp_name;
13141 if (objfile->separate_debug_objfile_backlink != NULL)
13143 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13144 const char *backlink_basename = lbasename (backlink->original_name);
13146 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13149 dwp_name = objfile->original_name;
13151 dwp_name += ".dwp";
13153 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
13155 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13157 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13158 dwp_name = objfile_name (objfile);
13159 dwp_name += ".dwp";
13160 dbfd = open_dwp_file (dwp_name.c_str ());
13165 if (dwarf_read_debug)
13166 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13169 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
13170 dwp_file->name = bfd_get_filename (dbfd.get ());
13171 dwp_file->dbfd = dbfd.release ();
13173 /* +1: section 0 is unused */
13174 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
13175 dwp_file->elf_sections =
13176 OBSTACK_CALLOC (&objfile->objfile_obstack,
13177 dwp_file->num_sections, asection *);
13179 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13182 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
13184 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
13186 /* The DWP file version is stored in the hash table. Oh well. */
13187 if (dwp_file->cus && dwp_file->tus
13188 && dwp_file->cus->version != dwp_file->tus->version)
13190 /* Technically speaking, we should try to limp along, but this is
13191 pretty bizarre. We use pulongest here because that's the established
13192 portability solution (e.g, we cannot use %u for uint32_t). */
13193 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13194 " TU version %s [in DWP file %s]"),
13195 pulongest (dwp_file->cus->version),
13196 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13200 dwp_file->version = dwp_file->cus->version;
13201 else if (dwp_file->tus)
13202 dwp_file->version = dwp_file->tus->version;
13204 dwp_file->version = 2;
13206 if (dwp_file->version == 2)
13207 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13210 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13211 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13213 if (dwarf_read_debug)
13215 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13216 fprintf_unfiltered (gdb_stdlog,
13217 " %s CUs, %s TUs\n",
13218 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13219 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13225 /* Wrapper around open_and_init_dwp_file, only open it once. */
13227 static struct dwp_file *
13228 get_dwp_file (void)
13230 if (! dwarf2_per_objfile->dwp_checked)
13232 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
13233 dwarf2_per_objfile->dwp_checked = 1;
13235 return dwarf2_per_objfile->dwp_file;
13238 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13239 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13240 or in the DWP file for the objfile, referenced by THIS_UNIT.
13241 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13242 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13244 This is called, for example, when wanting to read a variable with a
13245 complex location. Therefore we don't want to do file i/o for every call.
13246 Therefore we don't want to look for a DWO file on every call.
13247 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13248 then we check if we've already seen DWO_NAME, and only THEN do we check
13251 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13252 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13254 static struct dwo_unit *
13255 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13256 const char *dwo_name, const char *comp_dir,
13257 ULONGEST signature, int is_debug_types)
13259 struct objfile *objfile = dwarf2_per_objfile->objfile;
13260 const char *kind = is_debug_types ? "TU" : "CU";
13261 void **dwo_file_slot;
13262 struct dwo_file *dwo_file;
13263 struct dwp_file *dwp_file;
13265 /* First see if there's a DWP file.
13266 If we have a DWP file but didn't find the DWO inside it, don't
13267 look for the original DWO file. It makes gdb behave differently
13268 depending on whether one is debugging in the build tree. */
13270 dwp_file = get_dwp_file ();
13271 if (dwp_file != NULL)
13273 const struct dwp_hash_table *dwp_htab =
13274 is_debug_types ? dwp_file->tus : dwp_file->cus;
13276 if (dwp_htab != NULL)
13278 struct dwo_unit *dwo_cutu =
13279 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
13280 signature, is_debug_types);
13282 if (dwo_cutu != NULL)
13284 if (dwarf_read_debug)
13286 fprintf_unfiltered (gdb_stdlog,
13287 "Virtual DWO %s %s found: @%s\n",
13288 kind, hex_string (signature),
13289 host_address_to_string (dwo_cutu));
13297 /* No DWP file, look for the DWO file. */
13299 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
13300 if (*dwo_file_slot == NULL)
13302 /* Read in the file and build a table of the CUs/TUs it contains. */
13303 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13305 /* NOTE: This will be NULL if unable to open the file. */
13306 dwo_file = (struct dwo_file *) *dwo_file_slot;
13308 if (dwo_file != NULL)
13310 struct dwo_unit *dwo_cutu = NULL;
13312 if (is_debug_types && dwo_file->tus)
13314 struct dwo_unit find_dwo_cutu;
13316 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13317 find_dwo_cutu.signature = signature;
13319 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13321 else if (!is_debug_types && dwo_file->cus)
13323 struct dwo_unit find_dwo_cutu;
13325 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13326 find_dwo_cutu.signature = signature;
13327 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13331 if (dwo_cutu != NULL)
13333 if (dwarf_read_debug)
13335 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13336 kind, dwo_name, hex_string (signature),
13337 host_address_to_string (dwo_cutu));
13344 /* We didn't find it. This could mean a dwo_id mismatch, or
13345 someone deleted the DWO/DWP file, or the search path isn't set up
13346 correctly to find the file. */
13348 if (dwarf_read_debug)
13350 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13351 kind, dwo_name, hex_string (signature));
13354 /* This is a warning and not a complaint because it can be caused by
13355 pilot error (e.g., user accidentally deleting the DWO). */
13357 /* Print the name of the DWP file if we looked there, helps the user
13358 better diagnose the problem. */
13359 std::string dwp_text;
13361 if (dwp_file != NULL)
13362 dwp_text = string_printf (" [in DWP file %s]",
13363 lbasename (dwp_file->name));
13365 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
13366 " [in module %s]"),
13367 kind, dwo_name, hex_string (signature),
13369 this_unit->is_debug_types ? "TU" : "CU",
13370 to_underlying (this_unit->sect_off), objfile_name (objfile));
13375 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13376 See lookup_dwo_cutu_unit for details. */
13378 static struct dwo_unit *
13379 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13380 const char *dwo_name, const char *comp_dir,
13381 ULONGEST signature)
13383 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13386 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13387 See lookup_dwo_cutu_unit for details. */
13389 static struct dwo_unit *
13390 lookup_dwo_type_unit (struct signatured_type *this_tu,
13391 const char *dwo_name, const char *comp_dir)
13393 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13396 /* Traversal function for queue_and_load_all_dwo_tus. */
13399 queue_and_load_dwo_tu (void **slot, void *info)
13401 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13402 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13403 ULONGEST signature = dwo_unit->signature;
13404 struct signatured_type *sig_type =
13405 lookup_dwo_signatured_type (per_cu->cu, signature);
13407 if (sig_type != NULL)
13409 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13411 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13412 a real dependency of PER_CU on SIG_TYPE. That is detected later
13413 while processing PER_CU. */
13414 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13415 load_full_type_unit (sig_cu);
13416 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13422 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13423 The DWO may have the only definition of the type, though it may not be
13424 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13425 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13428 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13430 struct dwo_unit *dwo_unit;
13431 struct dwo_file *dwo_file;
13433 gdb_assert (!per_cu->is_debug_types);
13434 gdb_assert (get_dwp_file () == NULL);
13435 gdb_assert (per_cu->cu != NULL);
13437 dwo_unit = per_cu->cu->dwo_unit;
13438 gdb_assert (dwo_unit != NULL);
13440 dwo_file = dwo_unit->dwo_file;
13441 if (dwo_file->tus != NULL)
13442 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13445 /* Free all resources associated with DWO_FILE.
13446 Close the DWO file and munmap the sections.
13447 All memory should be on the objfile obstack. */
13450 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
13453 /* Note: dbfd is NULL for virtual DWO files. */
13454 gdb_bfd_unref (dwo_file->dbfd);
13456 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13459 /* Wrapper for free_dwo_file for use in cleanups. */
13462 free_dwo_file_cleanup (void *arg)
13464 struct dwo_file *dwo_file = (struct dwo_file *) arg;
13465 struct objfile *objfile = dwarf2_per_objfile->objfile;
13467 free_dwo_file (dwo_file, objfile);
13470 /* Traversal function for free_dwo_files. */
13473 free_dwo_file_from_slot (void **slot, void *info)
13475 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13476 struct objfile *objfile = (struct objfile *) info;
13478 free_dwo_file (dwo_file, objfile);
13483 /* Free all resources associated with DWO_FILES. */
13486 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13488 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
13491 /* Read in various DIEs. */
13493 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13494 Inherit only the children of the DW_AT_abstract_origin DIE not being
13495 already referenced by DW_AT_abstract_origin from the children of the
13499 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13501 struct die_info *child_die;
13502 sect_offset *offsetp;
13503 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13504 struct die_info *origin_die;
13505 /* Iterator of the ORIGIN_DIE children. */
13506 struct die_info *origin_child_die;
13507 struct attribute *attr;
13508 struct dwarf2_cu *origin_cu;
13509 struct pending **origin_previous_list_in_scope;
13511 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13515 /* Note that following die references may follow to a die in a
13519 origin_die = follow_die_ref (die, attr, &origin_cu);
13521 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13523 origin_previous_list_in_scope = origin_cu->list_in_scope;
13524 origin_cu->list_in_scope = cu->list_in_scope;
13526 if (die->tag != origin_die->tag
13527 && !(die->tag == DW_TAG_inlined_subroutine
13528 && origin_die->tag == DW_TAG_subprogram))
13529 complaint (&symfile_complaints,
13530 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
13531 to_underlying (die->sect_off),
13532 to_underlying (origin_die->sect_off));
13534 std::vector<sect_offset> offsets;
13536 for (child_die = die->child;
13537 child_die && child_die->tag;
13538 child_die = sibling_die (child_die))
13540 struct die_info *child_origin_die;
13541 struct dwarf2_cu *child_origin_cu;
13543 /* We are trying to process concrete instance entries:
13544 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13545 it's not relevant to our analysis here. i.e. detecting DIEs that are
13546 present in the abstract instance but not referenced in the concrete
13548 if (child_die->tag == DW_TAG_call_site
13549 || child_die->tag == DW_TAG_GNU_call_site)
13552 /* For each CHILD_DIE, find the corresponding child of
13553 ORIGIN_DIE. If there is more than one layer of
13554 DW_AT_abstract_origin, follow them all; there shouldn't be,
13555 but GCC versions at least through 4.4 generate this (GCC PR
13557 child_origin_die = child_die;
13558 child_origin_cu = cu;
13561 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13565 child_origin_die = follow_die_ref (child_origin_die, attr,
13569 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13570 counterpart may exist. */
13571 if (child_origin_die != child_die)
13573 if (child_die->tag != child_origin_die->tag
13574 && !(child_die->tag == DW_TAG_inlined_subroutine
13575 && child_origin_die->tag == DW_TAG_subprogram))
13576 complaint (&symfile_complaints,
13577 _("Child DIE 0x%x and its abstract origin 0x%x have "
13579 to_underlying (child_die->sect_off),
13580 to_underlying (child_origin_die->sect_off));
13581 if (child_origin_die->parent != origin_die)
13582 complaint (&symfile_complaints,
13583 _("Child DIE 0x%x and its abstract origin 0x%x have "
13584 "different parents"),
13585 to_underlying (child_die->sect_off),
13586 to_underlying (child_origin_die->sect_off));
13588 offsets.push_back (child_origin_die->sect_off);
13591 std::sort (offsets.begin (), offsets.end ());
13592 sect_offset *offsets_end = offsets.data () + offsets.size ();
13593 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13594 if (offsetp[-1] == *offsetp)
13595 complaint (&symfile_complaints,
13596 _("Multiple children of DIE 0x%x refer "
13597 "to DIE 0x%x as their abstract origin"),
13598 to_underlying (die->sect_off), to_underlying (*offsetp));
13600 offsetp = offsets.data ();
13601 origin_child_die = origin_die->child;
13602 while (origin_child_die && origin_child_die->tag)
13604 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13605 while (offsetp < offsets_end
13606 && *offsetp < origin_child_die->sect_off)
13608 if (offsetp >= offsets_end
13609 || *offsetp > origin_child_die->sect_off)
13611 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13612 Check whether we're already processing ORIGIN_CHILD_DIE.
13613 This can happen with mutually referenced abstract_origins.
13615 if (!origin_child_die->in_process)
13616 process_die (origin_child_die, origin_cu);
13618 origin_child_die = sibling_die (origin_child_die);
13620 origin_cu->list_in_scope = origin_previous_list_in_scope;
13624 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13626 struct objfile *objfile = cu->objfile;
13627 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13628 struct context_stack *newobj;
13631 struct die_info *child_die;
13632 struct attribute *attr, *call_line, *call_file;
13634 CORE_ADDR baseaddr;
13635 struct block *block;
13636 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13637 std::vector<struct symbol *> template_args;
13638 struct template_symbol *templ_func = NULL;
13642 /* If we do not have call site information, we can't show the
13643 caller of this inlined function. That's too confusing, so
13644 only use the scope for local variables. */
13645 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13646 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13647 if (call_line == NULL || call_file == NULL)
13649 read_lexical_block_scope (die, cu);
13654 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13656 name = dwarf2_name (die, cu);
13658 /* Ignore functions with missing or empty names. These are actually
13659 illegal according to the DWARF standard. */
13662 complaint (&symfile_complaints,
13663 _("missing name for subprogram DIE at %d"),
13664 to_underlying (die->sect_off));
13668 /* Ignore functions with missing or invalid low and high pc attributes. */
13669 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13670 <= PC_BOUNDS_INVALID)
13672 attr = dwarf2_attr (die, DW_AT_external, cu);
13673 if (!attr || !DW_UNSND (attr))
13674 complaint (&symfile_complaints,
13675 _("cannot get low and high bounds "
13676 "for subprogram DIE at %d"),
13677 to_underlying (die->sect_off));
13681 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13682 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13684 /* If we have any template arguments, then we must allocate a
13685 different sort of symbol. */
13686 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13688 if (child_die->tag == DW_TAG_template_type_param
13689 || child_die->tag == DW_TAG_template_value_param)
13691 templ_func = allocate_template_symbol (objfile);
13692 templ_func->subclass = SYMBOL_TEMPLATE;
13697 newobj = push_context (0, lowpc);
13698 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
13699 (struct symbol *) templ_func);
13701 /* If there is a location expression for DW_AT_frame_base, record
13703 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13705 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13707 /* If there is a location for the static link, record it. */
13708 newobj->static_link = NULL;
13709 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13712 newobj->static_link
13713 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13714 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13717 cu->list_in_scope = &local_symbols;
13719 if (die->child != NULL)
13721 child_die = die->child;
13722 while (child_die && child_die->tag)
13724 if (child_die->tag == DW_TAG_template_type_param
13725 || child_die->tag == DW_TAG_template_value_param)
13727 struct symbol *arg = new_symbol (child_die, NULL, cu);
13730 template_args.push_back (arg);
13733 process_die (child_die, cu);
13734 child_die = sibling_die (child_die);
13738 inherit_abstract_dies (die, cu);
13740 /* If we have a DW_AT_specification, we might need to import using
13741 directives from the context of the specification DIE. See the
13742 comment in determine_prefix. */
13743 if (cu->language == language_cplus
13744 && dwarf2_attr (die, DW_AT_specification, cu))
13746 struct dwarf2_cu *spec_cu = cu;
13747 struct die_info *spec_die = die_specification (die, &spec_cu);
13751 child_die = spec_die->child;
13752 while (child_die && child_die->tag)
13754 if (child_die->tag == DW_TAG_imported_module)
13755 process_die (child_die, spec_cu);
13756 child_die = sibling_die (child_die);
13759 /* In some cases, GCC generates specification DIEs that
13760 themselves contain DW_AT_specification attributes. */
13761 spec_die = die_specification (spec_die, &spec_cu);
13765 newobj = pop_context ();
13766 /* Make a block for the local symbols within. */
13767 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
13768 newobj->static_link, lowpc, highpc);
13770 /* For C++, set the block's scope. */
13771 if ((cu->language == language_cplus
13772 || cu->language == language_fortran
13773 || cu->language == language_d
13774 || cu->language == language_rust)
13775 && cu->processing_has_namespace_info)
13776 block_set_scope (block, determine_prefix (die, cu),
13777 &objfile->objfile_obstack);
13779 /* If we have address ranges, record them. */
13780 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13782 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
13784 /* Attach template arguments to function. */
13785 if (!template_args.empty ())
13787 gdb_assert (templ_func != NULL);
13789 templ_func->n_template_arguments = template_args.size ();
13790 templ_func->template_arguments
13791 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13792 templ_func->n_template_arguments);
13793 memcpy (templ_func->template_arguments,
13794 template_args.data (),
13795 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13798 /* In C++, we can have functions nested inside functions (e.g., when
13799 a function declares a class that has methods). This means that
13800 when we finish processing a function scope, we may need to go
13801 back to building a containing block's symbol lists. */
13802 local_symbols = newobj->locals;
13803 local_using_directives = newobj->local_using_directives;
13805 /* If we've finished processing a top-level function, subsequent
13806 symbols go in the file symbol list. */
13807 if (outermost_context_p ())
13808 cu->list_in_scope = &file_symbols;
13811 /* Process all the DIES contained within a lexical block scope. Start
13812 a new scope, process the dies, and then close the scope. */
13815 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13817 struct objfile *objfile = cu->objfile;
13818 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13819 struct context_stack *newobj;
13820 CORE_ADDR lowpc, highpc;
13821 struct die_info *child_die;
13822 CORE_ADDR baseaddr;
13824 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13826 /* Ignore blocks with missing or invalid low and high pc attributes. */
13827 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13828 as multiple lexical blocks? Handling children in a sane way would
13829 be nasty. Might be easier to properly extend generic blocks to
13830 describe ranges. */
13831 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13833 case PC_BOUNDS_NOT_PRESENT:
13834 /* DW_TAG_lexical_block has no attributes, process its children as if
13835 there was no wrapping by that DW_TAG_lexical_block.
13836 GCC does no longer produces such DWARF since GCC r224161. */
13837 for (child_die = die->child;
13838 child_die != NULL && child_die->tag;
13839 child_die = sibling_die (child_die))
13840 process_die (child_die, cu);
13842 case PC_BOUNDS_INVALID:
13845 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13846 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13848 push_context (0, lowpc);
13849 if (die->child != NULL)
13851 child_die = die->child;
13852 while (child_die && child_die->tag)
13854 process_die (child_die, cu);
13855 child_die = sibling_die (child_die);
13858 inherit_abstract_dies (die, cu);
13859 newobj = pop_context ();
13861 if (local_symbols != NULL || local_using_directives != NULL)
13863 struct block *block
13864 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
13865 newobj->start_addr, highpc);
13867 /* Note that recording ranges after traversing children, as we
13868 do here, means that recording a parent's ranges entails
13869 walking across all its children's ranges as they appear in
13870 the address map, which is quadratic behavior.
13872 It would be nicer to record the parent's ranges before
13873 traversing its children, simply overriding whatever you find
13874 there. But since we don't even decide whether to create a
13875 block until after we've traversed its children, that's hard
13877 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13879 local_symbols = newobj->locals;
13880 local_using_directives = newobj->local_using_directives;
13883 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13886 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13888 struct objfile *objfile = cu->objfile;
13889 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13890 CORE_ADDR pc, baseaddr;
13891 struct attribute *attr;
13892 struct call_site *call_site, call_site_local;
13895 struct die_info *child_die;
13897 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13899 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13902 /* This was a pre-DWARF-5 GNU extension alias
13903 for DW_AT_call_return_pc. */
13904 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13908 complaint (&symfile_complaints,
13909 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
13910 "DIE 0x%x [in module %s]"),
13911 to_underlying (die->sect_off), objfile_name (objfile));
13914 pc = attr_value_as_address (attr) + baseaddr;
13915 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13917 if (cu->call_site_htab == NULL)
13918 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13919 NULL, &objfile->objfile_obstack,
13920 hashtab_obstack_allocate, NULL);
13921 call_site_local.pc = pc;
13922 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13925 complaint (&symfile_complaints,
13926 _("Duplicate PC %s for DW_TAG_call_site "
13927 "DIE 0x%x [in module %s]"),
13928 paddress (gdbarch, pc), to_underlying (die->sect_off),
13929 objfile_name (objfile));
13933 /* Count parameters at the caller. */
13936 for (child_die = die->child; child_die && child_die->tag;
13937 child_die = sibling_die (child_die))
13939 if (child_die->tag != DW_TAG_call_site_parameter
13940 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13942 complaint (&symfile_complaints,
13943 _("Tag %d is not DW_TAG_call_site_parameter in "
13944 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
13945 child_die->tag, to_underlying (child_die->sect_off),
13946 objfile_name (objfile));
13954 = ((struct call_site *)
13955 obstack_alloc (&objfile->objfile_obstack,
13956 sizeof (*call_site)
13957 + (sizeof (*call_site->parameter) * (nparams - 1))));
13959 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13960 call_site->pc = pc;
13962 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13963 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13965 struct die_info *func_die;
13967 /* Skip also over DW_TAG_inlined_subroutine. */
13968 for (func_die = die->parent;
13969 func_die && func_die->tag != DW_TAG_subprogram
13970 && func_die->tag != DW_TAG_subroutine_type;
13971 func_die = func_die->parent);
13973 /* DW_AT_call_all_calls is a superset
13974 of DW_AT_call_all_tail_calls. */
13976 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13977 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13978 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13979 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13981 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13982 not complete. But keep CALL_SITE for look ups via call_site_htab,
13983 both the initial caller containing the real return address PC and
13984 the final callee containing the current PC of a chain of tail
13985 calls do not need to have the tail call list complete. But any
13986 function candidate for a virtual tail call frame searched via
13987 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13988 determined unambiguously. */
13992 struct type *func_type = NULL;
13995 func_type = get_die_type (func_die, cu);
13996 if (func_type != NULL)
13998 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14000 /* Enlist this call site to the function. */
14001 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14002 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14005 complaint (&symfile_complaints,
14006 _("Cannot find function owning DW_TAG_call_site "
14007 "DIE 0x%x [in module %s]"),
14008 to_underlying (die->sect_off), objfile_name (objfile));
14012 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14014 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14016 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14019 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14020 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14022 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14023 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14024 /* Keep NULL DWARF_BLOCK. */;
14025 else if (attr_form_is_block (attr))
14027 struct dwarf2_locexpr_baton *dlbaton;
14029 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14030 dlbaton->data = DW_BLOCK (attr)->data;
14031 dlbaton->size = DW_BLOCK (attr)->size;
14032 dlbaton->per_cu = cu->per_cu;
14034 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14036 else if (attr_form_is_ref (attr))
14038 struct dwarf2_cu *target_cu = cu;
14039 struct die_info *target_die;
14041 target_die = follow_die_ref (die, attr, &target_cu);
14042 gdb_assert (target_cu->objfile == objfile);
14043 if (die_is_declaration (target_die, target_cu))
14045 const char *target_physname;
14047 /* Prefer the mangled name; otherwise compute the demangled one. */
14048 target_physname = dw2_linkage_name (target_die, target_cu);
14049 if (target_physname == NULL)
14050 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14051 if (target_physname == NULL)
14052 complaint (&symfile_complaints,
14053 _("DW_AT_call_target target DIE has invalid "
14054 "physname, for referencing DIE 0x%x [in module %s]"),
14055 to_underlying (die->sect_off), objfile_name (objfile));
14057 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14063 /* DW_AT_entry_pc should be preferred. */
14064 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14065 <= PC_BOUNDS_INVALID)
14066 complaint (&symfile_complaints,
14067 _("DW_AT_call_target target DIE has invalid "
14068 "low pc, for referencing DIE 0x%x [in module %s]"),
14069 to_underlying (die->sect_off), objfile_name (objfile));
14072 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14073 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14078 complaint (&symfile_complaints,
14079 _("DW_TAG_call_site DW_AT_call_target is neither "
14080 "block nor reference, for DIE 0x%x [in module %s]"),
14081 to_underlying (die->sect_off), objfile_name (objfile));
14083 call_site->per_cu = cu->per_cu;
14085 for (child_die = die->child;
14086 child_die && child_die->tag;
14087 child_die = sibling_die (child_die))
14089 struct call_site_parameter *parameter;
14090 struct attribute *loc, *origin;
14092 if (child_die->tag != DW_TAG_call_site_parameter
14093 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14095 /* Already printed the complaint above. */
14099 gdb_assert (call_site->parameter_count < nparams);
14100 parameter = &call_site->parameter[call_site->parameter_count];
14102 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14103 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14104 register is contained in DW_AT_call_value. */
14106 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14107 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14108 if (origin == NULL)
14110 /* This was a pre-DWARF-5 GNU extension alias
14111 for DW_AT_call_parameter. */
14112 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14114 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14116 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14118 sect_offset sect_off
14119 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14120 if (!offset_in_cu_p (&cu->header, sect_off))
14122 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14123 binding can be done only inside one CU. Such referenced DIE
14124 therefore cannot be even moved to DW_TAG_partial_unit. */
14125 complaint (&symfile_complaints,
14126 _("DW_AT_call_parameter offset is not in CU for "
14127 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14128 to_underlying (child_die->sect_off),
14129 objfile_name (objfile));
14132 parameter->u.param_cu_off
14133 = (cu_offset) (sect_off - cu->header.sect_off);
14135 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14137 complaint (&symfile_complaints,
14138 _("No DW_FORM_block* DW_AT_location for "
14139 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14140 to_underlying (child_die->sect_off), objfile_name (objfile));
14145 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14146 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14147 if (parameter->u.dwarf_reg != -1)
14148 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14149 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14150 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14151 ¶meter->u.fb_offset))
14152 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14155 complaint (&symfile_complaints,
14156 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14157 "for DW_FORM_block* DW_AT_location is supported for "
14158 "DW_TAG_call_site child DIE 0x%x "
14160 to_underlying (child_die->sect_off),
14161 objfile_name (objfile));
14166 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14168 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14169 if (!attr_form_is_block (attr))
14171 complaint (&symfile_complaints,
14172 _("No DW_FORM_block* DW_AT_call_value for "
14173 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14174 to_underlying (child_die->sect_off),
14175 objfile_name (objfile));
14178 parameter->value = DW_BLOCK (attr)->data;
14179 parameter->value_size = DW_BLOCK (attr)->size;
14181 /* Parameters are not pre-cleared by memset above. */
14182 parameter->data_value = NULL;
14183 parameter->data_value_size = 0;
14184 call_site->parameter_count++;
14186 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14188 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14191 if (!attr_form_is_block (attr))
14192 complaint (&symfile_complaints,
14193 _("No DW_FORM_block* DW_AT_call_data_value for "
14194 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14195 to_underlying (child_die->sect_off),
14196 objfile_name (objfile));
14199 parameter->data_value = DW_BLOCK (attr)->data;
14200 parameter->data_value_size = DW_BLOCK (attr)->size;
14206 /* Helper function for read_variable. If DIE represents a virtual
14207 table, then return the type of the concrete object that is
14208 associated with the virtual table. Otherwise, return NULL. */
14210 static struct type *
14211 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14213 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14217 /* Find the type DIE. */
14218 struct die_info *type_die = NULL;
14219 struct dwarf2_cu *type_cu = cu;
14221 if (attr_form_is_ref (attr))
14222 type_die = follow_die_ref (die, attr, &type_cu);
14223 if (type_die == NULL)
14226 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14228 return die_containing_type (type_die, type_cu);
14231 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14234 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14236 struct rust_vtable_symbol *storage = NULL;
14238 if (cu->language == language_rust)
14240 struct type *containing_type = rust_containing_type (die, cu);
14242 if (containing_type != NULL)
14244 struct objfile *objfile = cu->objfile;
14246 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14247 struct rust_vtable_symbol);
14248 initialize_objfile_symbol (storage);
14249 storage->concrete_type = containing_type;
14250 storage->subclass = SYMBOL_RUST_VTABLE;
14254 new_symbol_full (die, NULL, cu, storage);
14257 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14258 reading .debug_rnglists.
14259 Callback's type should be:
14260 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14261 Return true if the attributes are present and valid, otherwise,
14264 template <typename Callback>
14266 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14267 Callback &&callback)
14269 struct objfile *objfile = cu->objfile;
14270 bfd *obfd = objfile->obfd;
14271 /* Base address selection entry. */
14274 const gdb_byte *buffer;
14275 CORE_ADDR baseaddr;
14276 bool overflow = false;
14278 found_base = cu->base_known;
14279 base = cu->base_address;
14281 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14282 if (offset >= dwarf2_per_objfile->rnglists.size)
14284 complaint (&symfile_complaints,
14285 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14289 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14291 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14295 /* Initialize it due to a false compiler warning. */
14296 CORE_ADDR range_beginning = 0, range_end = 0;
14297 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14298 + dwarf2_per_objfile->rnglists.size);
14299 unsigned int bytes_read;
14301 if (buffer == buf_end)
14306 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14309 case DW_RLE_end_of_list:
14311 case DW_RLE_base_address:
14312 if (buffer + cu->header.addr_size > buf_end)
14317 base = read_address (obfd, buffer, cu, &bytes_read);
14319 buffer += bytes_read;
14321 case DW_RLE_start_length:
14322 if (buffer + cu->header.addr_size > buf_end)
14327 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14328 buffer += bytes_read;
14329 range_end = (range_beginning
14330 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14331 buffer += bytes_read;
14332 if (buffer > buf_end)
14338 case DW_RLE_offset_pair:
14339 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14340 buffer += bytes_read;
14341 if (buffer > buf_end)
14346 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14347 buffer += bytes_read;
14348 if (buffer > buf_end)
14354 case DW_RLE_start_end:
14355 if (buffer + 2 * cu->header.addr_size > buf_end)
14360 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14361 buffer += bytes_read;
14362 range_end = read_address (obfd, buffer, cu, &bytes_read);
14363 buffer += bytes_read;
14366 complaint (&symfile_complaints,
14367 _("Invalid .debug_rnglists data (no base address)"));
14370 if (rlet == DW_RLE_end_of_list || overflow)
14372 if (rlet == DW_RLE_base_address)
14377 /* We have no valid base address for the ranges
14379 complaint (&symfile_complaints,
14380 _("Invalid .debug_rnglists data (no base address)"));
14384 if (range_beginning > range_end)
14386 /* Inverted range entries are invalid. */
14387 complaint (&symfile_complaints,
14388 _("Invalid .debug_rnglists data (inverted range)"));
14392 /* Empty range entries have no effect. */
14393 if (range_beginning == range_end)
14396 range_beginning += base;
14399 /* A not-uncommon case of bad debug info.
14400 Don't pollute the addrmap with bad data. */
14401 if (range_beginning + baseaddr == 0
14402 && !dwarf2_per_objfile->has_section_at_zero)
14404 complaint (&symfile_complaints,
14405 _(".debug_rnglists entry has start address of zero"
14406 " [in module %s]"), objfile_name (objfile));
14410 callback (range_beginning, range_end);
14415 complaint (&symfile_complaints,
14416 _("Offset %d is not terminated "
14417 "for DW_AT_ranges attribute"),
14425 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14426 Callback's type should be:
14427 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14428 Return 1 if the attributes are present and valid, otherwise, return 0. */
14430 template <typename Callback>
14432 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14433 Callback &&callback)
14435 struct objfile *objfile = cu->objfile;
14436 struct comp_unit_head *cu_header = &cu->header;
14437 bfd *obfd = objfile->obfd;
14438 unsigned int addr_size = cu_header->addr_size;
14439 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14440 /* Base address selection entry. */
14443 unsigned int dummy;
14444 const gdb_byte *buffer;
14445 CORE_ADDR baseaddr;
14447 if (cu_header->version >= 5)
14448 return dwarf2_rnglists_process (offset, cu, callback);
14450 found_base = cu->base_known;
14451 base = cu->base_address;
14453 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14454 if (offset >= dwarf2_per_objfile->ranges.size)
14456 complaint (&symfile_complaints,
14457 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14461 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14463 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14467 CORE_ADDR range_beginning, range_end;
14469 range_beginning = read_address (obfd, buffer, cu, &dummy);
14470 buffer += addr_size;
14471 range_end = read_address (obfd, buffer, cu, &dummy);
14472 buffer += addr_size;
14473 offset += 2 * addr_size;
14475 /* An end of list marker is a pair of zero addresses. */
14476 if (range_beginning == 0 && range_end == 0)
14477 /* Found the end of list entry. */
14480 /* Each base address selection entry is a pair of 2 values.
14481 The first is the largest possible address, the second is
14482 the base address. Check for a base address here. */
14483 if ((range_beginning & mask) == mask)
14485 /* If we found the largest possible address, then we already
14486 have the base address in range_end. */
14494 /* We have no valid base address for the ranges
14496 complaint (&symfile_complaints,
14497 _("Invalid .debug_ranges data (no base address)"));
14501 if (range_beginning > range_end)
14503 /* Inverted range entries are invalid. */
14504 complaint (&symfile_complaints,
14505 _("Invalid .debug_ranges data (inverted range)"));
14509 /* Empty range entries have no effect. */
14510 if (range_beginning == range_end)
14513 range_beginning += base;
14516 /* A not-uncommon case of bad debug info.
14517 Don't pollute the addrmap with bad data. */
14518 if (range_beginning + baseaddr == 0
14519 && !dwarf2_per_objfile->has_section_at_zero)
14521 complaint (&symfile_complaints,
14522 _(".debug_ranges entry has start address of zero"
14523 " [in module %s]"), objfile_name (objfile));
14527 callback (range_beginning, range_end);
14533 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14534 Return 1 if the attributes are present and valid, otherwise, return 0.
14535 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14538 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14539 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14540 struct partial_symtab *ranges_pst)
14542 struct objfile *objfile = cu->objfile;
14543 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14544 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14545 SECT_OFF_TEXT (objfile));
14548 CORE_ADDR high = 0;
14551 retval = dwarf2_ranges_process (offset, cu,
14552 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14554 if (ranges_pst != NULL)
14559 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14560 range_beginning + baseaddr);
14561 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14562 range_end + baseaddr);
14563 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14567 /* FIXME: This is recording everything as a low-high
14568 segment of consecutive addresses. We should have a
14569 data structure for discontiguous block ranges
14573 low = range_beginning;
14579 if (range_beginning < low)
14580 low = range_beginning;
14581 if (range_end > high)
14589 /* If the first entry is an end-of-list marker, the range
14590 describes an empty scope, i.e. no instructions. */
14596 *high_return = high;
14600 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14601 definition for the return value. *LOWPC and *HIGHPC are set iff
14602 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14604 static enum pc_bounds_kind
14605 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14606 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14607 struct partial_symtab *pst)
14609 struct attribute *attr;
14610 struct attribute *attr_high;
14612 CORE_ADDR high = 0;
14613 enum pc_bounds_kind ret;
14615 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14618 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14621 low = attr_value_as_address (attr);
14622 high = attr_value_as_address (attr_high);
14623 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14627 /* Found high w/o low attribute. */
14628 return PC_BOUNDS_INVALID;
14630 /* Found consecutive range of addresses. */
14631 ret = PC_BOUNDS_HIGH_LOW;
14635 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14638 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14639 We take advantage of the fact that DW_AT_ranges does not appear
14640 in DW_TAG_compile_unit of DWO files. */
14641 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14642 unsigned int ranges_offset = (DW_UNSND (attr)
14643 + (need_ranges_base
14647 /* Value of the DW_AT_ranges attribute is the offset in the
14648 .debug_ranges section. */
14649 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14650 return PC_BOUNDS_INVALID;
14651 /* Found discontinuous range of addresses. */
14652 ret = PC_BOUNDS_RANGES;
14655 return PC_BOUNDS_NOT_PRESENT;
14658 /* read_partial_die has also the strict LOW < HIGH requirement. */
14660 return PC_BOUNDS_INVALID;
14662 /* When using the GNU linker, .gnu.linkonce. sections are used to
14663 eliminate duplicate copies of functions and vtables and such.
14664 The linker will arbitrarily choose one and discard the others.
14665 The AT_*_pc values for such functions refer to local labels in
14666 these sections. If the section from that file was discarded, the
14667 labels are not in the output, so the relocs get a value of 0.
14668 If this is a discarded function, mark the pc bounds as invalid,
14669 so that GDB will ignore it. */
14670 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14671 return PC_BOUNDS_INVALID;
14679 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14680 its low and high PC addresses. Do nothing if these addresses could not
14681 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14682 and HIGHPC to the high address if greater than HIGHPC. */
14685 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14686 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14687 struct dwarf2_cu *cu)
14689 CORE_ADDR low, high;
14690 struct die_info *child = die->child;
14692 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14694 *lowpc = std::min (*lowpc, low);
14695 *highpc = std::max (*highpc, high);
14698 /* If the language does not allow nested subprograms (either inside
14699 subprograms or lexical blocks), we're done. */
14700 if (cu->language != language_ada)
14703 /* Check all the children of the given DIE. If it contains nested
14704 subprograms, then check their pc bounds. Likewise, we need to
14705 check lexical blocks as well, as they may also contain subprogram
14707 while (child && child->tag)
14709 if (child->tag == DW_TAG_subprogram
14710 || child->tag == DW_TAG_lexical_block)
14711 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14712 child = sibling_die (child);
14716 /* Get the low and high pc's represented by the scope DIE, and store
14717 them in *LOWPC and *HIGHPC. If the correct values can't be
14718 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14721 get_scope_pc_bounds (struct die_info *die,
14722 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14723 struct dwarf2_cu *cu)
14725 CORE_ADDR best_low = (CORE_ADDR) -1;
14726 CORE_ADDR best_high = (CORE_ADDR) 0;
14727 CORE_ADDR current_low, current_high;
14729 if (dwarf2_get_pc_bounds (die, ¤t_low, ¤t_high, cu, NULL)
14730 >= PC_BOUNDS_RANGES)
14732 best_low = current_low;
14733 best_high = current_high;
14737 struct die_info *child = die->child;
14739 while (child && child->tag)
14741 switch (child->tag) {
14742 case DW_TAG_subprogram:
14743 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14745 case DW_TAG_namespace:
14746 case DW_TAG_module:
14747 /* FIXME: carlton/2004-01-16: Should we do this for
14748 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14749 that current GCC's always emit the DIEs corresponding
14750 to definitions of methods of classes as children of a
14751 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14752 the DIEs giving the declarations, which could be
14753 anywhere). But I don't see any reason why the
14754 standards says that they have to be there. */
14755 get_scope_pc_bounds (child, ¤t_low, ¤t_high, cu);
14757 if (current_low != ((CORE_ADDR) -1))
14759 best_low = std::min (best_low, current_low);
14760 best_high = std::max (best_high, current_high);
14768 child = sibling_die (child);
14773 *highpc = best_high;
14776 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14780 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14781 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14783 struct objfile *objfile = cu->objfile;
14784 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14785 struct attribute *attr;
14786 struct attribute *attr_high;
14788 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14791 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14794 CORE_ADDR low = attr_value_as_address (attr);
14795 CORE_ADDR high = attr_value_as_address (attr_high);
14797 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14800 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14801 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14802 record_block_range (block, low, high - 1);
14806 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14809 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14810 We take advantage of the fact that DW_AT_ranges does not appear
14811 in DW_TAG_compile_unit of DWO files. */
14812 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14814 /* The value of the DW_AT_ranges attribute is the offset of the
14815 address range list in the .debug_ranges section. */
14816 unsigned long offset = (DW_UNSND (attr)
14817 + (need_ranges_base ? cu->ranges_base : 0));
14818 const gdb_byte *buffer;
14820 /* For some target architectures, but not others, the
14821 read_address function sign-extends the addresses it returns.
14822 To recognize base address selection entries, we need a
14824 unsigned int addr_size = cu->header.addr_size;
14825 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14827 /* The base address, to which the next pair is relative. Note
14828 that this 'base' is a DWARF concept: most entries in a range
14829 list are relative, to reduce the number of relocs against the
14830 debugging information. This is separate from this function's
14831 'baseaddr' argument, which GDB uses to relocate debugging
14832 information from a shared library based on the address at
14833 which the library was loaded. */
14834 CORE_ADDR base = cu->base_address;
14835 int base_known = cu->base_known;
14837 dwarf2_ranges_process (offset, cu,
14838 [&] (CORE_ADDR start, CORE_ADDR end)
14842 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14843 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14844 record_block_range (block, start, end - 1);
14849 /* Check whether the producer field indicates either of GCC < 4.6, or the
14850 Intel C/C++ compiler, and cache the result in CU. */
14853 check_producer (struct dwarf2_cu *cu)
14857 if (cu->producer == NULL)
14859 /* For unknown compilers expect their behavior is DWARF version
14862 GCC started to support .debug_types sections by -gdwarf-4 since
14863 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14864 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14865 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14866 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14868 else if (producer_is_gcc (cu->producer, &major, &minor))
14870 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14871 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14873 else if (producer_is_icc (cu->producer, &major, &minor))
14874 cu->producer_is_icc_lt_14 = major < 14;
14877 /* For other non-GCC compilers, expect their behavior is DWARF version
14881 cu->checked_producer = 1;
14884 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14885 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14886 during 4.6.0 experimental. */
14889 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14891 if (!cu->checked_producer)
14892 check_producer (cu);
14894 return cu->producer_is_gxx_lt_4_6;
14897 /* Return the default accessibility type if it is not overriden by
14898 DW_AT_accessibility. */
14900 static enum dwarf_access_attribute
14901 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14903 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14905 /* The default DWARF 2 accessibility for members is public, the default
14906 accessibility for inheritance is private. */
14908 if (die->tag != DW_TAG_inheritance)
14909 return DW_ACCESS_public;
14911 return DW_ACCESS_private;
14915 /* DWARF 3+ defines the default accessibility a different way. The same
14916 rules apply now for DW_TAG_inheritance as for the members and it only
14917 depends on the container kind. */
14919 if (die->parent->tag == DW_TAG_class_type)
14920 return DW_ACCESS_private;
14922 return DW_ACCESS_public;
14926 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14927 offset. If the attribute was not found return 0, otherwise return
14928 1. If it was found but could not properly be handled, set *OFFSET
14932 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14935 struct attribute *attr;
14937 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14942 /* Note that we do not check for a section offset first here.
14943 This is because DW_AT_data_member_location is new in DWARF 4,
14944 so if we see it, we can assume that a constant form is really
14945 a constant and not a section offset. */
14946 if (attr_form_is_constant (attr))
14947 *offset = dwarf2_get_attr_constant_value (attr, 0);
14948 else if (attr_form_is_section_offset (attr))
14949 dwarf2_complex_location_expr_complaint ();
14950 else if (attr_form_is_block (attr))
14951 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14953 dwarf2_complex_location_expr_complaint ();
14961 /* Add an aggregate field to the field list. */
14964 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14965 struct dwarf2_cu *cu)
14967 struct objfile *objfile = cu->objfile;
14968 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14969 struct nextfield *new_field;
14970 struct attribute *attr;
14972 const char *fieldname = "";
14974 /* Allocate a new field list entry and link it in. */
14975 new_field = XNEW (struct nextfield);
14976 make_cleanup (xfree, new_field);
14977 memset (new_field, 0, sizeof (struct nextfield));
14979 if (die->tag == DW_TAG_inheritance)
14981 new_field->next = fip->baseclasses;
14982 fip->baseclasses = new_field;
14986 new_field->next = fip->fields;
14987 fip->fields = new_field;
14991 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14993 new_field->accessibility = DW_UNSND (attr);
14995 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14996 if (new_field->accessibility != DW_ACCESS_public)
14997 fip->non_public_fields = 1;
14999 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15001 new_field->virtuality = DW_UNSND (attr);
15003 new_field->virtuality = DW_VIRTUALITY_none;
15005 fp = &new_field->field;
15007 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15011 /* Data member other than a C++ static data member. */
15013 /* Get type of field. */
15014 fp->type = die_type (die, cu);
15016 SET_FIELD_BITPOS (*fp, 0);
15018 /* Get bit size of field (zero if none). */
15019 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15022 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15026 FIELD_BITSIZE (*fp) = 0;
15029 /* Get bit offset of field. */
15030 if (handle_data_member_location (die, cu, &offset))
15031 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15032 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15035 if (gdbarch_bits_big_endian (gdbarch))
15037 /* For big endian bits, the DW_AT_bit_offset gives the
15038 additional bit offset from the MSB of the containing
15039 anonymous object to the MSB of the field. We don't
15040 have to do anything special since we don't need to
15041 know the size of the anonymous object. */
15042 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15046 /* For little endian bits, compute the bit offset to the
15047 MSB of the anonymous object, subtract off the number of
15048 bits from the MSB of the field to the MSB of the
15049 object, and then subtract off the number of bits of
15050 the field itself. The result is the bit offset of
15051 the LSB of the field. */
15052 int anonymous_size;
15053 int bit_offset = DW_UNSND (attr);
15055 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15058 /* The size of the anonymous object containing
15059 the bit field is explicit, so use the
15060 indicated size (in bytes). */
15061 anonymous_size = DW_UNSND (attr);
15065 /* The size of the anonymous object containing
15066 the bit field must be inferred from the type
15067 attribute of the data member containing the
15069 anonymous_size = TYPE_LENGTH (fp->type);
15071 SET_FIELD_BITPOS (*fp,
15072 (FIELD_BITPOS (*fp)
15073 + anonymous_size * bits_per_byte
15074 - bit_offset - FIELD_BITSIZE (*fp)));
15077 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15079 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15080 + dwarf2_get_attr_constant_value (attr, 0)));
15082 /* Get name of field. */
15083 fieldname = dwarf2_name (die, cu);
15084 if (fieldname == NULL)
15087 /* The name is already allocated along with this objfile, so we don't
15088 need to duplicate it for the type. */
15089 fp->name = fieldname;
15091 /* Change accessibility for artificial fields (e.g. virtual table
15092 pointer or virtual base class pointer) to private. */
15093 if (dwarf2_attr (die, DW_AT_artificial, cu))
15095 FIELD_ARTIFICIAL (*fp) = 1;
15096 new_field->accessibility = DW_ACCESS_private;
15097 fip->non_public_fields = 1;
15100 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15102 /* C++ static member. */
15104 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15105 is a declaration, but all versions of G++ as of this writing
15106 (so through at least 3.2.1) incorrectly generate
15107 DW_TAG_variable tags. */
15109 const char *physname;
15111 /* Get name of field. */
15112 fieldname = dwarf2_name (die, cu);
15113 if (fieldname == NULL)
15116 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15118 /* Only create a symbol if this is an external value.
15119 new_symbol checks this and puts the value in the global symbol
15120 table, which we want. If it is not external, new_symbol
15121 will try to put the value in cu->list_in_scope which is wrong. */
15122 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15124 /* A static const member, not much different than an enum as far as
15125 we're concerned, except that we can support more types. */
15126 new_symbol (die, NULL, cu);
15129 /* Get physical name. */
15130 physname = dwarf2_physname (fieldname, die, cu);
15132 /* The name is already allocated along with this objfile, so we don't
15133 need to duplicate it for the type. */
15134 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15135 FIELD_TYPE (*fp) = die_type (die, cu);
15136 FIELD_NAME (*fp) = fieldname;
15138 else if (die->tag == DW_TAG_inheritance)
15142 /* C++ base class field. */
15143 if (handle_data_member_location (die, cu, &offset))
15144 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15145 FIELD_BITSIZE (*fp) = 0;
15146 FIELD_TYPE (*fp) = die_type (die, cu);
15147 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15148 fip->nbaseclasses++;
15152 /* Can the type given by DIE define another type? */
15155 type_can_define_types (const struct die_info *die)
15159 case DW_TAG_typedef:
15160 case DW_TAG_class_type:
15161 case DW_TAG_structure_type:
15162 case DW_TAG_union_type:
15163 case DW_TAG_enumeration_type:
15171 /* Add a type definition defined in the scope of the FIP's class. */
15174 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15175 struct dwarf2_cu *cu)
15177 struct decl_field_list *new_field;
15178 struct decl_field *fp;
15180 /* Allocate a new field list entry and link it in. */
15181 new_field = XCNEW (struct decl_field_list);
15182 make_cleanup (xfree, new_field);
15184 gdb_assert (type_can_define_types (die));
15186 fp = &new_field->field;
15188 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15189 fp->name = dwarf2_name (die, cu);
15190 fp->type = read_type_die (die, cu);
15192 /* Save accessibility. */
15193 enum dwarf_access_attribute accessibility;
15194 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15196 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15198 accessibility = dwarf2_default_access_attribute (die, cu);
15199 switch (accessibility)
15201 case DW_ACCESS_public:
15202 /* The assumed value if neither private nor protected. */
15204 case DW_ACCESS_private:
15205 fp->is_private = 1;
15207 case DW_ACCESS_protected:
15208 fp->is_protected = 1;
15211 complaint (&symfile_complaints,
15212 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15215 if (die->tag == DW_TAG_typedef)
15217 new_field->next = fip->typedef_field_list;
15218 fip->typedef_field_list = new_field;
15219 fip->typedef_field_list_count++;
15223 new_field->next = fip->nested_types_list;
15224 fip->nested_types_list = new_field;
15225 fip->nested_types_list_count++;
15229 /* Create the vector of fields, and attach it to the type. */
15232 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15233 struct dwarf2_cu *cu)
15235 int nfields = fip->nfields;
15237 /* Record the field count, allocate space for the array of fields,
15238 and create blank accessibility bitfields if necessary. */
15239 TYPE_NFIELDS (type) = nfields;
15240 TYPE_FIELDS (type) = (struct field *)
15241 TYPE_ALLOC (type, sizeof (struct field) * nfields);
15242 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
15244 if (fip->non_public_fields && cu->language != language_ada)
15246 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15248 TYPE_FIELD_PRIVATE_BITS (type) =
15249 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15250 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15252 TYPE_FIELD_PROTECTED_BITS (type) =
15253 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15254 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15256 TYPE_FIELD_IGNORE_BITS (type) =
15257 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15258 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15261 /* If the type has baseclasses, allocate and clear a bit vector for
15262 TYPE_FIELD_VIRTUAL_BITS. */
15263 if (fip->nbaseclasses && cu->language != language_ada)
15265 int num_bytes = B_BYTES (fip->nbaseclasses);
15266 unsigned char *pointer;
15268 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15269 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15270 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15271 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
15272 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
15275 /* Copy the saved-up fields into the field vector. Start from the head of
15276 the list, adding to the tail of the field array, so that they end up in
15277 the same order in the array in which they were added to the list. */
15278 while (nfields-- > 0)
15280 struct nextfield *fieldp;
15284 fieldp = fip->fields;
15285 fip->fields = fieldp->next;
15289 fieldp = fip->baseclasses;
15290 fip->baseclasses = fieldp->next;
15293 TYPE_FIELD (type, nfields) = fieldp->field;
15294 switch (fieldp->accessibility)
15296 case DW_ACCESS_private:
15297 if (cu->language != language_ada)
15298 SET_TYPE_FIELD_PRIVATE (type, nfields);
15301 case DW_ACCESS_protected:
15302 if (cu->language != language_ada)
15303 SET_TYPE_FIELD_PROTECTED (type, nfields);
15306 case DW_ACCESS_public:
15310 /* Unknown accessibility. Complain and treat it as public. */
15312 complaint (&symfile_complaints, _("unsupported accessibility %d"),
15313 fieldp->accessibility);
15317 if (nfields < fip->nbaseclasses)
15319 switch (fieldp->virtuality)
15321 case DW_VIRTUALITY_virtual:
15322 case DW_VIRTUALITY_pure_virtual:
15323 if (cu->language == language_ada)
15324 error (_("unexpected virtuality in component of Ada type"));
15325 SET_TYPE_FIELD_VIRTUAL (type, nfields);
15332 /* Return true if this member function is a constructor, false
15336 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15338 const char *fieldname;
15339 const char *type_name;
15342 if (die->parent == NULL)
15345 if (die->parent->tag != DW_TAG_structure_type
15346 && die->parent->tag != DW_TAG_union_type
15347 && die->parent->tag != DW_TAG_class_type)
15350 fieldname = dwarf2_name (die, cu);
15351 type_name = dwarf2_name (die->parent, cu);
15352 if (fieldname == NULL || type_name == NULL)
15355 len = strlen (fieldname);
15356 return (strncmp (fieldname, type_name, len) == 0
15357 && (type_name[len] == '\0' || type_name[len] == '<'));
15360 /* Add a member function to the proper fieldlist. */
15363 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15364 struct type *type, struct dwarf2_cu *cu)
15366 struct objfile *objfile = cu->objfile;
15367 struct attribute *attr;
15368 struct fnfieldlist *flp;
15370 struct fn_field *fnp;
15371 const char *fieldname;
15372 struct nextfnfield *new_fnfield;
15373 struct type *this_type;
15374 enum dwarf_access_attribute accessibility;
15376 if (cu->language == language_ada)
15377 error (_("unexpected member function in Ada type"));
15379 /* Get name of member function. */
15380 fieldname = dwarf2_name (die, cu);
15381 if (fieldname == NULL)
15384 /* Look up member function name in fieldlist. */
15385 for (i = 0; i < fip->nfnfields; i++)
15387 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15391 /* Create new list element if necessary. */
15392 if (i < fip->nfnfields)
15393 flp = &fip->fnfieldlists[i];
15396 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
15398 fip->fnfieldlists = (struct fnfieldlist *)
15399 xrealloc (fip->fnfieldlists,
15400 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
15401 * sizeof (struct fnfieldlist));
15402 if (fip->nfnfields == 0)
15403 make_cleanup (free_current_contents, &fip->fnfieldlists);
15405 flp = &fip->fnfieldlists[fip->nfnfields];
15406 flp->name = fieldname;
15409 i = fip->nfnfields++;
15412 /* Create a new member function field and chain it to the field list
15414 new_fnfield = XNEW (struct nextfnfield);
15415 make_cleanup (xfree, new_fnfield);
15416 memset (new_fnfield, 0, sizeof (struct nextfnfield));
15417 new_fnfield->next = flp->head;
15418 flp->head = new_fnfield;
15421 /* Fill in the member function field info. */
15422 fnp = &new_fnfield->fnfield;
15424 /* Delay processing of the physname until later. */
15425 if (cu->language == language_cplus)
15427 add_to_method_list (type, i, flp->length - 1, fieldname,
15432 const char *physname = dwarf2_physname (fieldname, die, cu);
15433 fnp->physname = physname ? physname : "";
15436 fnp->type = alloc_type (objfile);
15437 this_type = read_type_die (die, cu);
15438 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15440 int nparams = TYPE_NFIELDS (this_type);
15442 /* TYPE is the domain of this method, and THIS_TYPE is the type
15443 of the method itself (TYPE_CODE_METHOD). */
15444 smash_to_method_type (fnp->type, type,
15445 TYPE_TARGET_TYPE (this_type),
15446 TYPE_FIELDS (this_type),
15447 TYPE_NFIELDS (this_type),
15448 TYPE_VARARGS (this_type));
15450 /* Handle static member functions.
15451 Dwarf2 has no clean way to discern C++ static and non-static
15452 member functions. G++ helps GDB by marking the first
15453 parameter for non-static member functions (which is the this
15454 pointer) as artificial. We obtain this information from
15455 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15456 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15457 fnp->voffset = VOFFSET_STATIC;
15460 complaint (&symfile_complaints, _("member function type missing for '%s'"),
15461 dwarf2_full_name (fieldname, die, cu));
15463 /* Get fcontext from DW_AT_containing_type if present. */
15464 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15465 fnp->fcontext = die_containing_type (die, cu);
15467 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15468 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15470 /* Get accessibility. */
15471 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15473 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15475 accessibility = dwarf2_default_access_attribute (die, cu);
15476 switch (accessibility)
15478 case DW_ACCESS_private:
15479 fnp->is_private = 1;
15481 case DW_ACCESS_protected:
15482 fnp->is_protected = 1;
15486 /* Check for artificial methods. */
15487 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15488 if (attr && DW_UNSND (attr) != 0)
15489 fnp->is_artificial = 1;
15491 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15493 /* Get index in virtual function table if it is a virtual member
15494 function. For older versions of GCC, this is an offset in the
15495 appropriate virtual table, as specified by DW_AT_containing_type.
15496 For everyone else, it is an expression to be evaluated relative
15497 to the object address. */
15499 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15502 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15504 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15506 /* Old-style GCC. */
15507 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15509 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15510 || (DW_BLOCK (attr)->size > 1
15511 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15512 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15514 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15515 if ((fnp->voffset % cu->header.addr_size) != 0)
15516 dwarf2_complex_location_expr_complaint ();
15518 fnp->voffset /= cu->header.addr_size;
15522 dwarf2_complex_location_expr_complaint ();
15524 if (!fnp->fcontext)
15526 /* If there is no `this' field and no DW_AT_containing_type,
15527 we cannot actually find a base class context for the
15529 if (TYPE_NFIELDS (this_type) == 0
15530 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15532 complaint (&symfile_complaints,
15533 _("cannot determine context for virtual member "
15534 "function \"%s\" (offset %d)"),
15535 fieldname, to_underlying (die->sect_off));
15540 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15544 else if (attr_form_is_section_offset (attr))
15546 dwarf2_complex_location_expr_complaint ();
15550 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15556 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15557 if (attr && DW_UNSND (attr))
15559 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15560 complaint (&symfile_complaints,
15561 _("Member function \"%s\" (offset %d) is virtual "
15562 "but the vtable offset is not specified"),
15563 fieldname, to_underlying (die->sect_off));
15564 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15565 TYPE_CPLUS_DYNAMIC (type) = 1;
15570 /* Create the vector of member function fields, and attach it to the type. */
15573 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15574 struct dwarf2_cu *cu)
15576 struct fnfieldlist *flp;
15579 if (cu->language == language_ada)
15580 error (_("unexpected member functions in Ada type"));
15582 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15583 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15584 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
15586 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
15588 struct nextfnfield *nfp = flp->head;
15589 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15592 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
15593 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
15594 fn_flp->fn_fields = (struct fn_field *)
15595 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
15596 for (k = flp->length; (k--, nfp); nfp = nfp->next)
15597 fn_flp->fn_fields[k] = nfp->fnfield;
15600 TYPE_NFN_FIELDS (type) = fip->nfnfields;
15603 /* Returns non-zero if NAME is the name of a vtable member in CU's
15604 language, zero otherwise. */
15606 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15608 static const char vptr[] = "_vptr";
15610 /* Look for the C++ form of the vtable. */
15611 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15617 /* GCC outputs unnamed structures that are really pointers to member
15618 functions, with the ABI-specified layout. If TYPE describes
15619 such a structure, smash it into a member function type.
15621 GCC shouldn't do this; it should just output pointer to member DIEs.
15622 This is GCC PR debug/28767. */
15625 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15627 struct type *pfn_type, *self_type, *new_type;
15629 /* Check for a structure with no name and two children. */
15630 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15633 /* Check for __pfn and __delta members. */
15634 if (TYPE_FIELD_NAME (type, 0) == NULL
15635 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15636 || TYPE_FIELD_NAME (type, 1) == NULL
15637 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15640 /* Find the type of the method. */
15641 pfn_type = TYPE_FIELD_TYPE (type, 0);
15642 if (pfn_type == NULL
15643 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15644 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15647 /* Look for the "this" argument. */
15648 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15649 if (TYPE_NFIELDS (pfn_type) == 0
15650 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15651 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15654 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15655 new_type = alloc_type (objfile);
15656 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15657 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15658 TYPE_VARARGS (pfn_type));
15659 smash_to_methodptr_type (type, new_type);
15663 /* Called when we find the DIE that starts a structure or union scope
15664 (definition) to create a type for the structure or union. Fill in
15665 the type's name and general properties; the members will not be
15666 processed until process_structure_scope. A symbol table entry for
15667 the type will also not be done until process_structure_scope (assuming
15668 the type has a name).
15670 NOTE: we need to call these functions regardless of whether or not the
15671 DIE has a DW_AT_name attribute, since it might be an anonymous
15672 structure or union. This gets the type entered into our set of
15673 user defined types. */
15675 static struct type *
15676 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15678 struct objfile *objfile = cu->objfile;
15680 struct attribute *attr;
15683 /* If the definition of this type lives in .debug_types, read that type.
15684 Don't follow DW_AT_specification though, that will take us back up
15685 the chain and we want to go down. */
15686 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15689 type = get_DW_AT_signature_type (die, attr, cu);
15691 /* The type's CU may not be the same as CU.
15692 Ensure TYPE is recorded with CU in die_type_hash. */
15693 return set_die_type (die, type, cu);
15696 type = alloc_type (objfile);
15697 INIT_CPLUS_SPECIFIC (type);
15699 name = dwarf2_name (die, cu);
15702 if (cu->language == language_cplus
15703 || cu->language == language_d
15704 || cu->language == language_rust)
15706 const char *full_name = dwarf2_full_name (name, die, cu);
15708 /* dwarf2_full_name might have already finished building the DIE's
15709 type. If so, there is no need to continue. */
15710 if (get_die_type (die, cu) != NULL)
15711 return get_die_type (die, cu);
15713 TYPE_TAG_NAME (type) = full_name;
15714 if (die->tag == DW_TAG_structure_type
15715 || die->tag == DW_TAG_class_type)
15716 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15720 /* The name is already allocated along with this objfile, so
15721 we don't need to duplicate it for the type. */
15722 TYPE_TAG_NAME (type) = name;
15723 if (die->tag == DW_TAG_class_type)
15724 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15728 if (die->tag == DW_TAG_structure_type)
15730 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15732 else if (die->tag == DW_TAG_union_type)
15734 TYPE_CODE (type) = TYPE_CODE_UNION;
15738 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15741 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15742 TYPE_DECLARED_CLASS (type) = 1;
15744 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15747 if (attr_form_is_constant (attr))
15748 TYPE_LENGTH (type) = DW_UNSND (attr);
15751 /* For the moment, dynamic type sizes are not supported
15752 by GDB's struct type. The actual size is determined
15753 on-demand when resolving the type of a given object,
15754 so set the type's length to zero for now. Otherwise,
15755 we record an expression as the length, and that expression
15756 could lead to a very large value, which could eventually
15757 lead to us trying to allocate that much memory when creating
15758 a value of that type. */
15759 TYPE_LENGTH (type) = 0;
15764 TYPE_LENGTH (type) = 0;
15767 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15769 /* ICC<14 does not output the required DW_AT_declaration on
15770 incomplete types, but gives them a size of zero. */
15771 TYPE_STUB (type) = 1;
15774 TYPE_STUB_SUPPORTED (type) = 1;
15776 if (die_is_declaration (die, cu))
15777 TYPE_STUB (type) = 1;
15778 else if (attr == NULL && die->child == NULL
15779 && producer_is_realview (cu->producer))
15780 /* RealView does not output the required DW_AT_declaration
15781 on incomplete types. */
15782 TYPE_STUB (type) = 1;
15784 /* We need to add the type field to the die immediately so we don't
15785 infinitely recurse when dealing with pointers to the structure
15786 type within the structure itself. */
15787 set_die_type (die, type, cu);
15789 /* set_die_type should be already done. */
15790 set_descriptive_type (type, die, cu);
15795 /* Finish creating a structure or union type, including filling in
15796 its members and creating a symbol for it. */
15799 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15801 struct objfile *objfile = cu->objfile;
15802 struct die_info *child_die;
15805 type = get_die_type (die, cu);
15807 type = read_structure_type (die, cu);
15809 if (die->child != NULL && ! die_is_declaration (die, cu))
15811 struct field_info fi;
15812 std::vector<struct symbol *> template_args;
15813 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
15815 memset (&fi, 0, sizeof (struct field_info));
15817 child_die = die->child;
15819 while (child_die && child_die->tag)
15821 if (child_die->tag == DW_TAG_member
15822 || child_die->tag == DW_TAG_variable)
15824 /* NOTE: carlton/2002-11-05: A C++ static data member
15825 should be a DW_TAG_member that is a declaration, but
15826 all versions of G++ as of this writing (so through at
15827 least 3.2.1) incorrectly generate DW_TAG_variable
15828 tags for them instead. */
15829 dwarf2_add_field (&fi, child_die, cu);
15831 else if (child_die->tag == DW_TAG_subprogram)
15833 /* Rust doesn't have member functions in the C++ sense.
15834 However, it does emit ordinary functions as children
15835 of a struct DIE. */
15836 if (cu->language == language_rust)
15837 read_func_scope (child_die, cu);
15840 /* C++ member function. */
15841 dwarf2_add_member_fn (&fi, child_die, type, cu);
15844 else if (child_die->tag == DW_TAG_inheritance)
15846 /* C++ base class field. */
15847 dwarf2_add_field (&fi, child_die, cu);
15849 else if (type_can_define_types (child_die))
15850 dwarf2_add_type_defn (&fi, child_die, cu);
15851 else if (child_die->tag == DW_TAG_template_type_param
15852 || child_die->tag == DW_TAG_template_value_param)
15854 struct symbol *arg = new_symbol (child_die, NULL, cu);
15857 template_args.push_back (arg);
15860 child_die = sibling_die (child_die);
15863 /* Attach template arguments to type. */
15864 if (!template_args.empty ())
15866 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15867 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15868 TYPE_TEMPLATE_ARGUMENTS (type)
15869 = XOBNEWVEC (&objfile->objfile_obstack,
15871 TYPE_N_TEMPLATE_ARGUMENTS (type));
15872 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15873 template_args.data (),
15874 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15875 * sizeof (struct symbol *)));
15878 /* Attach fields and member functions to the type. */
15880 dwarf2_attach_fields_to_type (&fi, type, cu);
15883 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15885 /* Get the type which refers to the base class (possibly this
15886 class itself) which contains the vtable pointer for the current
15887 class from the DW_AT_containing_type attribute. This use of
15888 DW_AT_containing_type is a GNU extension. */
15890 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15892 struct type *t = die_containing_type (die, cu);
15894 set_type_vptr_basetype (type, t);
15899 /* Our own class provides vtbl ptr. */
15900 for (i = TYPE_NFIELDS (t) - 1;
15901 i >= TYPE_N_BASECLASSES (t);
15904 const char *fieldname = TYPE_FIELD_NAME (t, i);
15906 if (is_vtable_name (fieldname, cu))
15908 set_type_vptr_fieldno (type, i);
15913 /* Complain if virtual function table field not found. */
15914 if (i < TYPE_N_BASECLASSES (t))
15915 complaint (&symfile_complaints,
15916 _("virtual function table pointer "
15917 "not found when defining class '%s'"),
15918 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
15923 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15926 else if (cu->producer
15927 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15929 /* The IBM XLC compiler does not provide direct indication
15930 of the containing type, but the vtable pointer is
15931 always named __vfp. */
15935 for (i = TYPE_NFIELDS (type) - 1;
15936 i >= TYPE_N_BASECLASSES (type);
15939 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15941 set_type_vptr_fieldno (type, i);
15942 set_type_vptr_basetype (type, type);
15949 /* Copy fi.typedef_field_list linked list elements content into the
15950 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15951 if (fi.typedef_field_list)
15953 int i = fi.typedef_field_list_count;
15955 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15956 TYPE_TYPEDEF_FIELD_ARRAY (type)
15957 = ((struct decl_field *)
15958 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
15959 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
15961 /* Reverse the list order to keep the debug info elements order. */
15964 struct decl_field *dest, *src;
15966 dest = &TYPE_TYPEDEF_FIELD (type, i);
15967 src = &fi.typedef_field_list->field;
15968 fi.typedef_field_list = fi.typedef_field_list->next;
15973 /* Copy fi.nested_types_list linked list elements content into the
15974 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15975 if (fi.nested_types_list != NULL && cu->language != language_ada)
15977 int i = fi.nested_types_list_count;
15979 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15980 TYPE_NESTED_TYPES_ARRAY (type)
15981 = ((struct decl_field *)
15982 TYPE_ALLOC (type, sizeof (struct decl_field) * i));
15983 TYPE_NESTED_TYPES_COUNT (type) = i;
15985 /* Reverse the list order to keep the debug info elements order. */
15988 struct decl_field *dest, *src;
15990 dest = &TYPE_NESTED_TYPES_FIELD (type, i);
15991 src = &fi.nested_types_list->field;
15992 fi.nested_types_list = fi.nested_types_list->next;
15997 do_cleanups (back_to);
16000 quirk_gcc_member_function_pointer (type, objfile);
16002 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16003 snapshots) has been known to create a die giving a declaration
16004 for a class that has, as a child, a die giving a definition for a
16005 nested class. So we have to process our children even if the
16006 current die is a declaration. Normally, of course, a declaration
16007 won't have any children at all. */
16009 child_die = die->child;
16011 while (child_die != NULL && child_die->tag)
16013 if (child_die->tag == DW_TAG_member
16014 || child_die->tag == DW_TAG_variable
16015 || child_die->tag == DW_TAG_inheritance
16016 || child_die->tag == DW_TAG_template_value_param
16017 || child_die->tag == DW_TAG_template_type_param)
16022 process_die (child_die, cu);
16024 child_die = sibling_die (child_die);
16027 /* Do not consider external references. According to the DWARF standard,
16028 these DIEs are identified by the fact that they have no byte_size
16029 attribute, and a declaration attribute. */
16030 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16031 || !die_is_declaration (die, cu))
16032 new_symbol (die, type, cu);
16035 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16036 update TYPE using some information only available in DIE's children. */
16039 update_enumeration_type_from_children (struct die_info *die,
16041 struct dwarf2_cu *cu)
16043 struct die_info *child_die;
16044 int unsigned_enum = 1;
16048 auto_obstack obstack;
16050 for (child_die = die->child;
16051 child_die != NULL && child_die->tag;
16052 child_die = sibling_die (child_die))
16054 struct attribute *attr;
16056 const gdb_byte *bytes;
16057 struct dwarf2_locexpr_baton *baton;
16060 if (child_die->tag != DW_TAG_enumerator)
16063 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16067 name = dwarf2_name (child_die, cu);
16069 name = "<anonymous enumerator>";
16071 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16072 &value, &bytes, &baton);
16078 else if ((mask & value) != 0)
16083 /* If we already know that the enum type is neither unsigned, nor
16084 a flag type, no need to look at the rest of the enumerates. */
16085 if (!unsigned_enum && !flag_enum)
16090 TYPE_UNSIGNED (type) = 1;
16092 TYPE_FLAG_ENUM (type) = 1;
16095 /* Given a DW_AT_enumeration_type die, set its type. We do not
16096 complete the type's fields yet, or create any symbols. */
16098 static struct type *
16099 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16101 struct objfile *objfile = cu->objfile;
16103 struct attribute *attr;
16106 /* If the definition of this type lives in .debug_types, read that type.
16107 Don't follow DW_AT_specification though, that will take us back up
16108 the chain and we want to go down. */
16109 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16112 type = get_DW_AT_signature_type (die, attr, cu);
16114 /* The type's CU may not be the same as CU.
16115 Ensure TYPE is recorded with CU in die_type_hash. */
16116 return set_die_type (die, type, cu);
16119 type = alloc_type (objfile);
16121 TYPE_CODE (type) = TYPE_CODE_ENUM;
16122 name = dwarf2_full_name (NULL, die, cu);
16124 TYPE_TAG_NAME (type) = name;
16126 attr = dwarf2_attr (die, DW_AT_type, cu);
16129 struct type *underlying_type = die_type (die, cu);
16131 TYPE_TARGET_TYPE (type) = underlying_type;
16134 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16137 TYPE_LENGTH (type) = DW_UNSND (attr);
16141 TYPE_LENGTH (type) = 0;
16144 /* The enumeration DIE can be incomplete. In Ada, any type can be
16145 declared as private in the package spec, and then defined only
16146 inside the package body. Such types are known as Taft Amendment
16147 Types. When another package uses such a type, an incomplete DIE
16148 may be generated by the compiler. */
16149 if (die_is_declaration (die, cu))
16150 TYPE_STUB (type) = 1;
16152 /* Finish the creation of this type by using the enum's children.
16153 We must call this even when the underlying type has been provided
16154 so that we can determine if we're looking at a "flag" enum. */
16155 update_enumeration_type_from_children (die, type, cu);
16157 /* If this type has an underlying type that is not a stub, then we
16158 may use its attributes. We always use the "unsigned" attribute
16159 in this situation, because ordinarily we guess whether the type
16160 is unsigned -- but the guess can be wrong and the underlying type
16161 can tell us the reality. However, we defer to a local size
16162 attribute if one exists, because this lets the compiler override
16163 the underlying type if needed. */
16164 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16166 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16167 if (TYPE_LENGTH (type) == 0)
16168 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16171 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16173 return set_die_type (die, type, cu);
16176 /* Given a pointer to a die which begins an enumeration, process all
16177 the dies that define the members of the enumeration, and create the
16178 symbol for the enumeration type.
16180 NOTE: We reverse the order of the element list. */
16183 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16185 struct type *this_type;
16187 this_type = get_die_type (die, cu);
16188 if (this_type == NULL)
16189 this_type = read_enumeration_type (die, cu);
16191 if (die->child != NULL)
16193 struct die_info *child_die;
16194 struct symbol *sym;
16195 struct field *fields = NULL;
16196 int num_fields = 0;
16199 child_die = die->child;
16200 while (child_die && child_die->tag)
16202 if (child_die->tag != DW_TAG_enumerator)
16204 process_die (child_die, cu);
16208 name = dwarf2_name (child_die, cu);
16211 sym = new_symbol (child_die, this_type, cu);
16213 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16215 fields = (struct field *)
16217 (num_fields + DW_FIELD_ALLOC_CHUNK)
16218 * sizeof (struct field));
16221 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16222 FIELD_TYPE (fields[num_fields]) = NULL;
16223 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16224 FIELD_BITSIZE (fields[num_fields]) = 0;
16230 child_die = sibling_die (child_die);
16235 TYPE_NFIELDS (this_type) = num_fields;
16236 TYPE_FIELDS (this_type) = (struct field *)
16237 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16238 memcpy (TYPE_FIELDS (this_type), fields,
16239 sizeof (struct field) * num_fields);
16244 /* If we are reading an enum from a .debug_types unit, and the enum
16245 is a declaration, and the enum is not the signatured type in the
16246 unit, then we do not want to add a symbol for it. Adding a
16247 symbol would in some cases obscure the true definition of the
16248 enum, giving users an incomplete type when the definition is
16249 actually available. Note that we do not want to do this for all
16250 enums which are just declarations, because C++0x allows forward
16251 enum declarations. */
16252 if (cu->per_cu->is_debug_types
16253 && die_is_declaration (die, cu))
16255 struct signatured_type *sig_type;
16257 sig_type = (struct signatured_type *) cu->per_cu;
16258 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16259 if (sig_type->type_offset_in_section != die->sect_off)
16263 new_symbol (die, this_type, cu);
16266 /* Extract all information from a DW_TAG_array_type DIE and put it in
16267 the DIE's type field. For now, this only handles one dimensional
16270 static struct type *
16271 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16273 struct objfile *objfile = cu->objfile;
16274 struct die_info *child_die;
16276 struct type *element_type, *range_type, *index_type;
16277 struct attribute *attr;
16279 unsigned int bit_stride = 0;
16281 element_type = die_type (die, cu);
16283 /* The die_type call above may have already set the type for this DIE. */
16284 type = get_die_type (die, cu);
16288 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16290 bit_stride = DW_UNSND (attr) * 8;
16292 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16294 bit_stride = DW_UNSND (attr);
16296 /* Irix 6.2 native cc creates array types without children for
16297 arrays with unspecified length. */
16298 if (die->child == NULL)
16300 index_type = objfile_type (objfile)->builtin_int;
16301 range_type = create_static_range_type (NULL, index_type, 0, -1);
16302 type = create_array_type_with_stride (NULL, element_type, range_type,
16304 return set_die_type (die, type, cu);
16307 std::vector<struct type *> range_types;
16308 child_die = die->child;
16309 while (child_die && child_die->tag)
16311 if (child_die->tag == DW_TAG_subrange_type)
16313 struct type *child_type = read_type_die (child_die, cu);
16315 if (child_type != NULL)
16317 /* The range type was succesfully read. Save it for the
16318 array type creation. */
16319 range_types.push_back (child_type);
16322 child_die = sibling_die (child_die);
16325 /* Dwarf2 dimensions are output from left to right, create the
16326 necessary array types in backwards order. */
16328 type = element_type;
16330 if (read_array_order (die, cu) == DW_ORD_col_major)
16334 while (i < range_types.size ())
16335 type = create_array_type_with_stride (NULL, type, range_types[i++],
16340 size_t ndim = range_types.size ();
16342 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16346 /* Understand Dwarf2 support for vector types (like they occur on
16347 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16348 array type. This is not part of the Dwarf2/3 standard yet, but a
16349 custom vendor extension. The main difference between a regular
16350 array and the vector variant is that vectors are passed by value
16352 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16354 make_vector_type (type);
16356 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16357 implementation may choose to implement triple vectors using this
16359 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16362 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16363 TYPE_LENGTH (type) = DW_UNSND (attr);
16365 complaint (&symfile_complaints,
16366 _("DW_AT_byte_size for array type smaller "
16367 "than the total size of elements"));
16370 name = dwarf2_name (die, cu);
16372 TYPE_NAME (type) = name;
16374 /* Install the type in the die. */
16375 set_die_type (die, type, cu);
16377 /* set_die_type should be already done. */
16378 set_descriptive_type (type, die, cu);
16383 static enum dwarf_array_dim_ordering
16384 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16386 struct attribute *attr;
16388 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16391 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16393 /* GNU F77 is a special case, as at 08/2004 array type info is the
16394 opposite order to the dwarf2 specification, but data is still
16395 laid out as per normal fortran.
16397 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16398 version checking. */
16400 if (cu->language == language_fortran
16401 && cu->producer && strstr (cu->producer, "GNU F77"))
16403 return DW_ORD_row_major;
16406 switch (cu->language_defn->la_array_ordering)
16408 case array_column_major:
16409 return DW_ORD_col_major;
16410 case array_row_major:
16412 return DW_ORD_row_major;
16416 /* Extract all information from a DW_TAG_set_type DIE and put it in
16417 the DIE's type field. */
16419 static struct type *
16420 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16422 struct type *domain_type, *set_type;
16423 struct attribute *attr;
16425 domain_type = die_type (die, cu);
16427 /* The die_type call above may have already set the type for this DIE. */
16428 set_type = get_die_type (die, cu);
16432 set_type = create_set_type (NULL, domain_type);
16434 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16436 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16438 return set_die_type (die, set_type, cu);
16441 /* A helper for read_common_block that creates a locexpr baton.
16442 SYM is the symbol which we are marking as computed.
16443 COMMON_DIE is the DIE for the common block.
16444 COMMON_LOC is the location expression attribute for the common
16446 MEMBER_LOC is the location expression attribute for the particular
16447 member of the common block that we are processing.
16448 CU is the CU from which the above come. */
16451 mark_common_block_symbol_computed (struct symbol *sym,
16452 struct die_info *common_die,
16453 struct attribute *common_loc,
16454 struct attribute *member_loc,
16455 struct dwarf2_cu *cu)
16457 struct objfile *objfile = dwarf2_per_objfile->objfile;
16458 struct dwarf2_locexpr_baton *baton;
16460 unsigned int cu_off;
16461 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16462 LONGEST offset = 0;
16464 gdb_assert (common_loc && member_loc);
16465 gdb_assert (attr_form_is_block (common_loc));
16466 gdb_assert (attr_form_is_block (member_loc)
16467 || attr_form_is_constant (member_loc));
16469 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16470 baton->per_cu = cu->per_cu;
16471 gdb_assert (baton->per_cu);
16473 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16475 if (attr_form_is_constant (member_loc))
16477 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16478 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16481 baton->size += DW_BLOCK (member_loc)->size;
16483 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16486 *ptr++ = DW_OP_call4;
16487 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16488 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16491 if (attr_form_is_constant (member_loc))
16493 *ptr++ = DW_OP_addr;
16494 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16495 ptr += cu->header.addr_size;
16499 /* We have to copy the data here, because DW_OP_call4 will only
16500 use a DW_AT_location attribute. */
16501 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16502 ptr += DW_BLOCK (member_loc)->size;
16505 *ptr++ = DW_OP_plus;
16506 gdb_assert (ptr - baton->data == baton->size);
16508 SYMBOL_LOCATION_BATON (sym) = baton;
16509 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16512 /* Create appropriate locally-scoped variables for all the
16513 DW_TAG_common_block entries. Also create a struct common_block
16514 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16515 is used to sepate the common blocks name namespace from regular
16519 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16521 struct attribute *attr;
16523 attr = dwarf2_attr (die, DW_AT_location, cu);
16526 /* Support the .debug_loc offsets. */
16527 if (attr_form_is_block (attr))
16531 else if (attr_form_is_section_offset (attr))
16533 dwarf2_complex_location_expr_complaint ();
16538 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16539 "common block member");
16544 if (die->child != NULL)
16546 struct objfile *objfile = cu->objfile;
16547 struct die_info *child_die;
16548 size_t n_entries = 0, size;
16549 struct common_block *common_block;
16550 struct symbol *sym;
16552 for (child_die = die->child;
16553 child_die && child_die->tag;
16554 child_die = sibling_die (child_die))
16557 size = (sizeof (struct common_block)
16558 + (n_entries - 1) * sizeof (struct symbol *));
16560 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16562 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16563 common_block->n_entries = 0;
16565 for (child_die = die->child;
16566 child_die && child_die->tag;
16567 child_die = sibling_die (child_die))
16569 /* Create the symbol in the DW_TAG_common_block block in the current
16571 sym = new_symbol (child_die, NULL, cu);
16574 struct attribute *member_loc;
16576 common_block->contents[common_block->n_entries++] = sym;
16578 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16582 /* GDB has handled this for a long time, but it is
16583 not specified by DWARF. It seems to have been
16584 emitted by gfortran at least as recently as:
16585 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16586 complaint (&symfile_complaints,
16587 _("Variable in common block has "
16588 "DW_AT_data_member_location "
16589 "- DIE at 0x%x [in module %s]"),
16590 to_underlying (child_die->sect_off),
16591 objfile_name (cu->objfile));
16593 if (attr_form_is_section_offset (member_loc))
16594 dwarf2_complex_location_expr_complaint ();
16595 else if (attr_form_is_constant (member_loc)
16596 || attr_form_is_block (member_loc))
16599 mark_common_block_symbol_computed (sym, die, attr,
16603 dwarf2_complex_location_expr_complaint ();
16608 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16609 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16613 /* Create a type for a C++ namespace. */
16615 static struct type *
16616 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16618 struct objfile *objfile = cu->objfile;
16619 const char *previous_prefix, *name;
16623 /* For extensions, reuse the type of the original namespace. */
16624 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16626 struct die_info *ext_die;
16627 struct dwarf2_cu *ext_cu = cu;
16629 ext_die = dwarf2_extension (die, &ext_cu);
16630 type = read_type_die (ext_die, ext_cu);
16632 /* EXT_CU may not be the same as CU.
16633 Ensure TYPE is recorded with CU in die_type_hash. */
16634 return set_die_type (die, type, cu);
16637 name = namespace_name (die, &is_anonymous, cu);
16639 /* Now build the name of the current namespace. */
16641 previous_prefix = determine_prefix (die, cu);
16642 if (previous_prefix[0] != '\0')
16643 name = typename_concat (&objfile->objfile_obstack,
16644 previous_prefix, name, 0, cu);
16646 /* Create the type. */
16647 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16648 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16650 return set_die_type (die, type, cu);
16653 /* Read a namespace scope. */
16656 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16658 struct objfile *objfile = cu->objfile;
16661 /* Add a symbol associated to this if we haven't seen the namespace
16662 before. Also, add a using directive if it's an anonymous
16665 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16669 type = read_type_die (die, cu);
16670 new_symbol (die, type, cu);
16672 namespace_name (die, &is_anonymous, cu);
16675 const char *previous_prefix = determine_prefix (die, cu);
16677 std::vector<const char *> excludes;
16678 add_using_directive (using_directives (cu->language),
16679 previous_prefix, TYPE_NAME (type), NULL,
16680 NULL, excludes, 0, &objfile->objfile_obstack);
16684 if (die->child != NULL)
16686 struct die_info *child_die = die->child;
16688 while (child_die && child_die->tag)
16690 process_die (child_die, cu);
16691 child_die = sibling_die (child_die);
16696 /* Read a Fortran module as type. This DIE can be only a declaration used for
16697 imported module. Still we need that type as local Fortran "use ... only"
16698 declaration imports depend on the created type in determine_prefix. */
16700 static struct type *
16701 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16703 struct objfile *objfile = cu->objfile;
16704 const char *module_name;
16707 module_name = dwarf2_name (die, cu);
16709 complaint (&symfile_complaints,
16710 _("DW_TAG_module has no name, offset 0x%x"),
16711 to_underlying (die->sect_off));
16712 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16714 /* determine_prefix uses TYPE_TAG_NAME. */
16715 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16717 return set_die_type (die, type, cu);
16720 /* Read a Fortran module. */
16723 read_module (struct die_info *die, struct dwarf2_cu *cu)
16725 struct die_info *child_die = die->child;
16728 type = read_type_die (die, cu);
16729 new_symbol (die, type, cu);
16731 while (child_die && child_die->tag)
16733 process_die (child_die, cu);
16734 child_die = sibling_die (child_die);
16738 /* Return the name of the namespace represented by DIE. Set
16739 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16742 static const char *
16743 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16745 struct die_info *current_die;
16746 const char *name = NULL;
16748 /* Loop through the extensions until we find a name. */
16750 for (current_die = die;
16751 current_die != NULL;
16752 current_die = dwarf2_extension (die, &cu))
16754 /* We don't use dwarf2_name here so that we can detect the absence
16755 of a name -> anonymous namespace. */
16756 name = dwarf2_string_attr (die, DW_AT_name, cu);
16762 /* Is it an anonymous namespace? */
16764 *is_anonymous = (name == NULL);
16766 name = CP_ANONYMOUS_NAMESPACE_STR;
16771 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16772 the user defined type vector. */
16774 static struct type *
16775 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16777 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
16778 struct comp_unit_head *cu_header = &cu->header;
16780 struct attribute *attr_byte_size;
16781 struct attribute *attr_address_class;
16782 int byte_size, addr_class;
16783 struct type *target_type;
16785 target_type = die_type (die, cu);
16787 /* The die_type call above may have already set the type for this DIE. */
16788 type = get_die_type (die, cu);
16792 type = lookup_pointer_type (target_type);
16794 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16795 if (attr_byte_size)
16796 byte_size = DW_UNSND (attr_byte_size);
16798 byte_size = cu_header->addr_size;
16800 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16801 if (attr_address_class)
16802 addr_class = DW_UNSND (attr_address_class);
16804 addr_class = DW_ADDR_none;
16806 /* If the pointer size or address class is different than the
16807 default, create a type variant marked as such and set the
16808 length accordingly. */
16809 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
16811 if (gdbarch_address_class_type_flags_p (gdbarch))
16815 type_flags = gdbarch_address_class_type_flags
16816 (gdbarch, byte_size, addr_class);
16817 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16819 type = make_type_with_address_space (type, type_flags);
16821 else if (TYPE_LENGTH (type) != byte_size)
16823 complaint (&symfile_complaints,
16824 _("invalid pointer size %d"), byte_size);
16828 /* Should we also complain about unhandled address classes? */
16832 TYPE_LENGTH (type) = byte_size;
16833 return set_die_type (die, type, cu);
16836 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16837 the user defined type vector. */
16839 static struct type *
16840 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16843 struct type *to_type;
16844 struct type *domain;
16846 to_type = die_type (die, cu);
16847 domain = die_containing_type (die, cu);
16849 /* The calls above may have already set the type for this DIE. */
16850 type = get_die_type (die, cu);
16854 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16855 type = lookup_methodptr_type (to_type);
16856 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16858 struct type *new_type = alloc_type (cu->objfile);
16860 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16861 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16862 TYPE_VARARGS (to_type));
16863 type = lookup_methodptr_type (new_type);
16866 type = lookup_memberptr_type (to_type, domain);
16868 return set_die_type (die, type, cu);
16871 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16872 the user defined type vector. */
16874 static struct type *
16875 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16876 enum type_code refcode)
16878 struct comp_unit_head *cu_header = &cu->header;
16879 struct type *type, *target_type;
16880 struct attribute *attr;
16882 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16884 target_type = die_type (die, cu);
16886 /* The die_type call above may have already set the type for this DIE. */
16887 type = get_die_type (die, cu);
16891 type = lookup_reference_type (target_type, refcode);
16892 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16895 TYPE_LENGTH (type) = DW_UNSND (attr);
16899 TYPE_LENGTH (type) = cu_header->addr_size;
16901 return set_die_type (die, type, cu);
16904 /* Add the given cv-qualifiers to the element type of the array. GCC
16905 outputs DWARF type qualifiers that apply to an array, not the
16906 element type. But GDB relies on the array element type to carry
16907 the cv-qualifiers. This mimics section 6.7.3 of the C99
16910 static struct type *
16911 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16912 struct type *base_type, int cnst, int voltl)
16914 struct type *el_type, *inner_array;
16916 base_type = copy_type (base_type);
16917 inner_array = base_type;
16919 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16921 TYPE_TARGET_TYPE (inner_array) =
16922 copy_type (TYPE_TARGET_TYPE (inner_array));
16923 inner_array = TYPE_TARGET_TYPE (inner_array);
16926 el_type = TYPE_TARGET_TYPE (inner_array);
16927 cnst |= TYPE_CONST (el_type);
16928 voltl |= TYPE_VOLATILE (el_type);
16929 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16931 return set_die_type (die, base_type, cu);
16934 static struct type *
16935 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16937 struct type *base_type, *cv_type;
16939 base_type = die_type (die, cu);
16941 /* The die_type call above may have already set the type for this DIE. */
16942 cv_type = get_die_type (die, cu);
16946 /* In case the const qualifier is applied to an array type, the element type
16947 is so qualified, not the array type (section 6.7.3 of C99). */
16948 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16949 return add_array_cv_type (die, cu, base_type, 1, 0);
16951 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16952 return set_die_type (die, cv_type, cu);
16955 static struct type *
16956 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16958 struct type *base_type, *cv_type;
16960 base_type = die_type (die, cu);
16962 /* The die_type call above may have already set the type for this DIE. */
16963 cv_type = get_die_type (die, cu);
16967 /* In case the volatile qualifier is applied to an array type, the
16968 element type is so qualified, not the array type (section 6.7.3
16970 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16971 return add_array_cv_type (die, cu, base_type, 0, 1);
16973 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16974 return set_die_type (die, cv_type, cu);
16977 /* Handle DW_TAG_restrict_type. */
16979 static struct type *
16980 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16982 struct type *base_type, *cv_type;
16984 base_type = die_type (die, cu);
16986 /* The die_type call above may have already set the type for this DIE. */
16987 cv_type = get_die_type (die, cu);
16991 cv_type = make_restrict_type (base_type);
16992 return set_die_type (die, cv_type, cu);
16995 /* Handle DW_TAG_atomic_type. */
16997 static struct type *
16998 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17000 struct type *base_type, *cv_type;
17002 base_type = die_type (die, cu);
17004 /* The die_type call above may have already set the type for this DIE. */
17005 cv_type = get_die_type (die, cu);
17009 cv_type = make_atomic_type (base_type);
17010 return set_die_type (die, cv_type, cu);
17013 /* Extract all information from a DW_TAG_string_type DIE and add to
17014 the user defined type vector. It isn't really a user defined type,
17015 but it behaves like one, with other DIE's using an AT_user_def_type
17016 attribute to reference it. */
17018 static struct type *
17019 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17021 struct objfile *objfile = cu->objfile;
17022 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17023 struct type *type, *range_type, *index_type, *char_type;
17024 struct attribute *attr;
17025 unsigned int length;
17027 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17030 length = DW_UNSND (attr);
17034 /* Check for the DW_AT_byte_size attribute. */
17035 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17038 length = DW_UNSND (attr);
17046 index_type = objfile_type (objfile)->builtin_int;
17047 range_type = create_static_range_type (NULL, index_type, 1, length);
17048 char_type = language_string_char_type (cu->language_defn, gdbarch);
17049 type = create_string_type (NULL, char_type, range_type);
17051 return set_die_type (die, type, cu);
17054 /* Assuming that DIE corresponds to a function, returns nonzero
17055 if the function is prototyped. */
17058 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17060 struct attribute *attr;
17062 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17063 if (attr && (DW_UNSND (attr) != 0))
17066 /* The DWARF standard implies that the DW_AT_prototyped attribute
17067 is only meaninful for C, but the concept also extends to other
17068 languages that allow unprototyped functions (Eg: Objective C).
17069 For all other languages, assume that functions are always
17071 if (cu->language != language_c
17072 && cu->language != language_objc
17073 && cu->language != language_opencl)
17076 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17077 prototyped and unprototyped functions; default to prototyped,
17078 since that is more common in modern code (and RealView warns
17079 about unprototyped functions). */
17080 if (producer_is_realview (cu->producer))
17086 /* Handle DIES due to C code like:
17090 int (*funcp)(int a, long l);
17094 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17096 static struct type *
17097 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17099 struct objfile *objfile = cu->objfile;
17100 struct type *type; /* Type that this function returns. */
17101 struct type *ftype; /* Function that returns above type. */
17102 struct attribute *attr;
17104 type = die_type (die, cu);
17106 /* The die_type call above may have already set the type for this DIE. */
17107 ftype = get_die_type (die, cu);
17111 ftype = lookup_function_type (type);
17113 if (prototyped_function_p (die, cu))
17114 TYPE_PROTOTYPED (ftype) = 1;
17116 /* Store the calling convention in the type if it's available in
17117 the subroutine die. Otherwise set the calling convention to
17118 the default value DW_CC_normal. */
17119 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17121 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17122 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17123 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17125 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17127 /* Record whether the function returns normally to its caller or not
17128 if the DWARF producer set that information. */
17129 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17130 if (attr && (DW_UNSND (attr) != 0))
17131 TYPE_NO_RETURN (ftype) = 1;
17133 /* We need to add the subroutine type to the die immediately so
17134 we don't infinitely recurse when dealing with parameters
17135 declared as the same subroutine type. */
17136 set_die_type (die, ftype, cu);
17138 if (die->child != NULL)
17140 struct type *void_type = objfile_type (objfile)->builtin_void;
17141 struct die_info *child_die;
17142 int nparams, iparams;
17144 /* Count the number of parameters.
17145 FIXME: GDB currently ignores vararg functions, but knows about
17146 vararg member functions. */
17148 child_die = die->child;
17149 while (child_die && child_die->tag)
17151 if (child_die->tag == DW_TAG_formal_parameter)
17153 else if (child_die->tag == DW_TAG_unspecified_parameters)
17154 TYPE_VARARGS (ftype) = 1;
17155 child_die = sibling_die (child_die);
17158 /* Allocate storage for parameters and fill them in. */
17159 TYPE_NFIELDS (ftype) = nparams;
17160 TYPE_FIELDS (ftype) = (struct field *)
17161 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17163 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17164 even if we error out during the parameters reading below. */
17165 for (iparams = 0; iparams < nparams; iparams++)
17166 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17169 child_die = die->child;
17170 while (child_die && child_die->tag)
17172 if (child_die->tag == DW_TAG_formal_parameter)
17174 struct type *arg_type;
17176 /* DWARF version 2 has no clean way to discern C++
17177 static and non-static member functions. G++ helps
17178 GDB by marking the first parameter for non-static
17179 member functions (which is the this pointer) as
17180 artificial. We pass this information to
17181 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17183 DWARF version 3 added DW_AT_object_pointer, which GCC
17184 4.5 does not yet generate. */
17185 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17187 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17189 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17190 arg_type = die_type (child_die, cu);
17192 /* RealView does not mark THIS as const, which the testsuite
17193 expects. GCC marks THIS as const in method definitions,
17194 but not in the class specifications (GCC PR 43053). */
17195 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17196 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17199 struct dwarf2_cu *arg_cu = cu;
17200 const char *name = dwarf2_name (child_die, cu);
17202 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17205 /* If the compiler emits this, use it. */
17206 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17209 else if (name && strcmp (name, "this") == 0)
17210 /* Function definitions will have the argument names. */
17212 else if (name == NULL && iparams == 0)
17213 /* Declarations may not have the names, so like
17214 elsewhere in GDB, assume an artificial first
17215 argument is "this". */
17219 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17223 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17226 child_die = sibling_die (child_die);
17233 static struct type *
17234 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17236 struct objfile *objfile = cu->objfile;
17237 const char *name = NULL;
17238 struct type *this_type, *target_type;
17240 name = dwarf2_full_name (NULL, die, cu);
17241 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17242 TYPE_TARGET_STUB (this_type) = 1;
17243 set_die_type (die, this_type, cu);
17244 target_type = die_type (die, cu);
17245 if (target_type != this_type)
17246 TYPE_TARGET_TYPE (this_type) = target_type;
17249 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17250 spec and cause infinite loops in GDB. */
17251 complaint (&symfile_complaints,
17252 _("Self-referential DW_TAG_typedef "
17253 "- DIE at 0x%x [in module %s]"),
17254 to_underlying (die->sect_off), objfile_name (objfile));
17255 TYPE_TARGET_TYPE (this_type) = NULL;
17260 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17261 (which may be different from NAME) to the architecture back-end to allow
17262 it to guess the correct format if necessary. */
17264 static struct type *
17265 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17266 const char *name_hint)
17268 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17269 const struct floatformat **format;
17272 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17274 type = init_float_type (objfile, bits, name, format);
17276 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17281 /* Find a representation of a given base type and install
17282 it in the TYPE field of the die. */
17284 static struct type *
17285 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17287 struct objfile *objfile = cu->objfile;
17289 struct attribute *attr;
17290 int encoding = 0, bits = 0;
17293 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17296 encoding = DW_UNSND (attr);
17298 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17301 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17303 name = dwarf2_name (die, cu);
17306 complaint (&symfile_complaints,
17307 _("DW_AT_name missing from DW_TAG_base_type"));
17312 case DW_ATE_address:
17313 /* Turn DW_ATE_address into a void * pointer. */
17314 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17315 type = init_pointer_type (objfile, bits, name, type);
17317 case DW_ATE_boolean:
17318 type = init_boolean_type (objfile, bits, 1, name);
17320 case DW_ATE_complex_float:
17321 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
17322 type = init_complex_type (objfile, name, type);
17324 case DW_ATE_decimal_float:
17325 type = init_decfloat_type (objfile, bits, name);
17328 type = dwarf2_init_float_type (objfile, bits, name, name);
17330 case DW_ATE_signed:
17331 type = init_integer_type (objfile, bits, 0, name);
17333 case DW_ATE_unsigned:
17334 if (cu->language == language_fortran
17336 && startswith (name, "character("))
17337 type = init_character_type (objfile, bits, 1, name);
17339 type = init_integer_type (objfile, bits, 1, name);
17341 case DW_ATE_signed_char:
17342 if (cu->language == language_ada || cu->language == language_m2
17343 || cu->language == language_pascal
17344 || cu->language == language_fortran)
17345 type = init_character_type (objfile, bits, 0, name);
17347 type = init_integer_type (objfile, bits, 0, name);
17349 case DW_ATE_unsigned_char:
17350 if (cu->language == language_ada || cu->language == language_m2
17351 || cu->language == language_pascal
17352 || cu->language == language_fortran
17353 || cu->language == language_rust)
17354 type = init_character_type (objfile, bits, 1, name);
17356 type = init_integer_type (objfile, bits, 1, name);
17360 gdbarch *arch = get_objfile_arch (objfile);
17363 type = builtin_type (arch)->builtin_char16;
17364 else if (bits == 32)
17365 type = builtin_type (arch)->builtin_char32;
17368 complaint (&symfile_complaints,
17369 _("unsupported DW_ATE_UTF bit size: '%d'"),
17371 type = init_integer_type (objfile, bits, 1, name);
17373 return set_die_type (die, type, cu);
17378 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17379 dwarf_type_encoding_name (encoding));
17380 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17384 if (name && strcmp (name, "char") == 0)
17385 TYPE_NOSIGN (type) = 1;
17387 return set_die_type (die, type, cu);
17390 /* Parse dwarf attribute if it's a block, reference or constant and put the
17391 resulting value of the attribute into struct bound_prop.
17392 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17395 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17396 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17398 struct dwarf2_property_baton *baton;
17399 struct obstack *obstack = &cu->objfile->objfile_obstack;
17401 if (attr == NULL || prop == NULL)
17404 if (attr_form_is_block (attr))
17406 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17407 baton->referenced_type = NULL;
17408 baton->locexpr.per_cu = cu->per_cu;
17409 baton->locexpr.size = DW_BLOCK (attr)->size;
17410 baton->locexpr.data = DW_BLOCK (attr)->data;
17411 prop->data.baton = baton;
17412 prop->kind = PROP_LOCEXPR;
17413 gdb_assert (prop->data.baton != NULL);
17415 else if (attr_form_is_ref (attr))
17417 struct dwarf2_cu *target_cu = cu;
17418 struct die_info *target_die;
17419 struct attribute *target_attr;
17421 target_die = follow_die_ref (die, attr, &target_cu);
17422 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17423 if (target_attr == NULL)
17424 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17426 if (target_attr == NULL)
17429 switch (target_attr->name)
17431 case DW_AT_location:
17432 if (attr_form_is_section_offset (target_attr))
17434 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17435 baton->referenced_type = die_type (target_die, target_cu);
17436 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17437 prop->data.baton = baton;
17438 prop->kind = PROP_LOCLIST;
17439 gdb_assert (prop->data.baton != NULL);
17441 else if (attr_form_is_block (target_attr))
17443 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17444 baton->referenced_type = die_type (target_die, target_cu);
17445 baton->locexpr.per_cu = cu->per_cu;
17446 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17447 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17448 prop->data.baton = baton;
17449 prop->kind = PROP_LOCEXPR;
17450 gdb_assert (prop->data.baton != NULL);
17454 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17455 "dynamic property");
17459 case DW_AT_data_member_location:
17463 if (!handle_data_member_location (target_die, target_cu,
17467 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17468 baton->referenced_type = read_type_die (target_die->parent,
17470 baton->offset_info.offset = offset;
17471 baton->offset_info.type = die_type (target_die, target_cu);
17472 prop->data.baton = baton;
17473 prop->kind = PROP_ADDR_OFFSET;
17478 else if (attr_form_is_constant (attr))
17480 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17481 prop->kind = PROP_CONST;
17485 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17486 dwarf2_name (die, cu));
17493 /* Read the given DW_AT_subrange DIE. */
17495 static struct type *
17496 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17498 struct type *base_type, *orig_base_type;
17499 struct type *range_type;
17500 struct attribute *attr;
17501 struct dynamic_prop low, high;
17502 int low_default_is_valid;
17503 int high_bound_is_count = 0;
17505 LONGEST negative_mask;
17507 orig_base_type = die_type (die, cu);
17508 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17509 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17510 creating the range type, but we use the result of check_typedef
17511 when examining properties of the type. */
17512 base_type = check_typedef (orig_base_type);
17514 /* The die_type call above may have already set the type for this DIE. */
17515 range_type = get_die_type (die, cu);
17519 low.kind = PROP_CONST;
17520 high.kind = PROP_CONST;
17521 high.data.const_val = 0;
17523 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17524 omitting DW_AT_lower_bound. */
17525 switch (cu->language)
17528 case language_cplus:
17529 low.data.const_val = 0;
17530 low_default_is_valid = 1;
17532 case language_fortran:
17533 low.data.const_val = 1;
17534 low_default_is_valid = 1;
17537 case language_objc:
17538 case language_rust:
17539 low.data.const_val = 0;
17540 low_default_is_valid = (cu->header.version >= 4);
17544 case language_pascal:
17545 low.data.const_val = 1;
17546 low_default_is_valid = (cu->header.version >= 4);
17549 low.data.const_val = 0;
17550 low_default_is_valid = 0;
17554 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17556 attr_to_dynamic_prop (attr, die, cu, &low);
17557 else if (!low_default_is_valid)
17558 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
17559 "- DIE at 0x%x [in module %s]"),
17560 to_underlying (die->sect_off), objfile_name (cu->objfile));
17562 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
17563 if (!attr_to_dynamic_prop (attr, die, cu, &high))
17565 attr = dwarf2_attr (die, DW_AT_count, cu);
17566 if (attr_to_dynamic_prop (attr, die, cu, &high))
17568 /* If bounds are constant do the final calculation here. */
17569 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17570 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17572 high_bound_is_count = 1;
17576 /* Dwarf-2 specifications explicitly allows to create subrange types
17577 without specifying a base type.
17578 In that case, the base type must be set to the type of
17579 the lower bound, upper bound or count, in that order, if any of these
17580 three attributes references an object that has a type.
17581 If no base type is found, the Dwarf-2 specifications say that
17582 a signed integer type of size equal to the size of an address should
17584 For the following C code: `extern char gdb_int [];'
17585 GCC produces an empty range DIE.
17586 FIXME: muller/2010-05-28: Possible references to object for low bound,
17587 high bound or count are not yet handled by this code. */
17588 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17590 struct objfile *objfile = cu->objfile;
17591 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17592 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17593 struct type *int_type = objfile_type (objfile)->builtin_int;
17595 /* Test "int", "long int", and "long long int" objfile types,
17596 and select the first one having a size above or equal to the
17597 architecture address size. */
17598 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17599 base_type = int_type;
17602 int_type = objfile_type (objfile)->builtin_long;
17603 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17604 base_type = int_type;
17607 int_type = objfile_type (objfile)->builtin_long_long;
17608 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17609 base_type = int_type;
17614 /* Normally, the DWARF producers are expected to use a signed
17615 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17616 But this is unfortunately not always the case, as witnessed
17617 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17618 is used instead. To work around that ambiguity, we treat
17619 the bounds as signed, and thus sign-extend their values, when
17620 the base type is signed. */
17622 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17623 if (low.kind == PROP_CONST
17624 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17625 low.data.const_val |= negative_mask;
17626 if (high.kind == PROP_CONST
17627 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17628 high.data.const_val |= negative_mask;
17630 range_type = create_range_type (NULL, orig_base_type, &low, &high);
17632 if (high_bound_is_count)
17633 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17635 /* Ada expects an empty array on no boundary attributes. */
17636 if (attr == NULL && cu->language != language_ada)
17637 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17639 name = dwarf2_name (die, cu);
17641 TYPE_NAME (range_type) = name;
17643 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17645 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17647 set_die_type (die, range_type, cu);
17649 /* set_die_type should be already done. */
17650 set_descriptive_type (range_type, die, cu);
17655 static struct type *
17656 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17660 /* For now, we only support the C meaning of an unspecified type: void. */
17662 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
17663 TYPE_NAME (type) = dwarf2_name (die, cu);
17665 return set_die_type (die, type, cu);
17668 /* Read a single die and all its descendents. Set the die's sibling
17669 field to NULL; set other fields in the die correctly, and set all
17670 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17671 location of the info_ptr after reading all of those dies. PARENT
17672 is the parent of the die in question. */
17674 static struct die_info *
17675 read_die_and_children (const struct die_reader_specs *reader,
17676 const gdb_byte *info_ptr,
17677 const gdb_byte **new_info_ptr,
17678 struct die_info *parent)
17680 struct die_info *die;
17681 const gdb_byte *cur_ptr;
17684 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
17687 *new_info_ptr = cur_ptr;
17690 store_in_ref_table (die, reader->cu);
17693 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17697 *new_info_ptr = cur_ptr;
17700 die->sibling = NULL;
17701 die->parent = parent;
17705 /* Read a die, all of its descendents, and all of its siblings; set
17706 all of the fields of all of the dies correctly. Arguments are as
17707 in read_die_and_children. */
17709 static struct die_info *
17710 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17711 const gdb_byte *info_ptr,
17712 const gdb_byte **new_info_ptr,
17713 struct die_info *parent)
17715 struct die_info *first_die, *last_sibling;
17716 const gdb_byte *cur_ptr;
17718 cur_ptr = info_ptr;
17719 first_die = last_sibling = NULL;
17723 struct die_info *die
17724 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17728 *new_info_ptr = cur_ptr;
17735 last_sibling->sibling = die;
17737 last_sibling = die;
17741 /* Read a die, all of its descendents, and all of its siblings; set
17742 all of the fields of all of the dies correctly. Arguments are as
17743 in read_die_and_children.
17744 This the main entry point for reading a DIE and all its children. */
17746 static struct die_info *
17747 read_die_and_siblings (const struct die_reader_specs *reader,
17748 const gdb_byte *info_ptr,
17749 const gdb_byte **new_info_ptr,
17750 struct die_info *parent)
17752 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17753 new_info_ptr, parent);
17755 if (dwarf_die_debug)
17757 fprintf_unfiltered (gdb_stdlog,
17758 "Read die from %s@0x%x of %s:\n",
17759 get_section_name (reader->die_section),
17760 (unsigned) (info_ptr - reader->die_section->buffer),
17761 bfd_get_filename (reader->abfd));
17762 dump_die (die, dwarf_die_debug);
17768 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17770 The caller is responsible for filling in the extra attributes
17771 and updating (*DIEP)->num_attrs.
17772 Set DIEP to point to a newly allocated die with its information,
17773 except for its child, sibling, and parent fields.
17774 Set HAS_CHILDREN to tell whether the die has children or not. */
17776 static const gdb_byte *
17777 read_full_die_1 (const struct die_reader_specs *reader,
17778 struct die_info **diep, const gdb_byte *info_ptr,
17779 int *has_children, int num_extra_attrs)
17781 unsigned int abbrev_number, bytes_read, i;
17782 struct abbrev_info *abbrev;
17783 struct die_info *die;
17784 struct dwarf2_cu *cu = reader->cu;
17785 bfd *abfd = reader->abfd;
17787 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17788 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17789 info_ptr += bytes_read;
17790 if (!abbrev_number)
17797 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
17799 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17801 bfd_get_filename (abfd));
17803 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17804 die->sect_off = sect_off;
17805 die->tag = abbrev->tag;
17806 die->abbrev = abbrev_number;
17808 /* Make the result usable.
17809 The caller needs to update num_attrs after adding the extra
17811 die->num_attrs = abbrev->num_attrs;
17813 for (i = 0; i < abbrev->num_attrs; ++i)
17814 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17818 *has_children = abbrev->has_children;
17822 /* Read a die and all its attributes.
17823 Set DIEP to point to a newly allocated die with its information,
17824 except for its child, sibling, and parent fields.
17825 Set HAS_CHILDREN to tell whether the die has children or not. */
17827 static const gdb_byte *
17828 read_full_die (const struct die_reader_specs *reader,
17829 struct die_info **diep, const gdb_byte *info_ptr,
17832 const gdb_byte *result;
17834 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
17836 if (dwarf_die_debug)
17838 fprintf_unfiltered (gdb_stdlog,
17839 "Read die from %s@0x%x of %s:\n",
17840 get_section_name (reader->die_section),
17841 (unsigned) (info_ptr - reader->die_section->buffer),
17842 bfd_get_filename (reader->abfd));
17843 dump_die (*diep, dwarf_die_debug);
17849 /* Abbreviation tables.
17851 In DWARF version 2, the description of the debugging information is
17852 stored in a separate .debug_abbrev section. Before we read any
17853 dies from a section we read in all abbreviations and install them
17854 in a hash table. */
17856 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
17858 static struct abbrev_info *
17859 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
17861 struct abbrev_info *abbrev;
17863 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
17864 memset (abbrev, 0, sizeof (struct abbrev_info));
17869 /* Add an abbreviation to the table. */
17872 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
17873 unsigned int abbrev_number,
17874 struct abbrev_info *abbrev)
17876 unsigned int hash_number;
17878 hash_number = abbrev_number % ABBREV_HASH_SIZE;
17879 abbrev->next = abbrev_table->abbrevs[hash_number];
17880 abbrev_table->abbrevs[hash_number] = abbrev;
17883 /* Look up an abbrev in the table.
17884 Returns NULL if the abbrev is not found. */
17886 static struct abbrev_info *
17887 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
17888 unsigned int abbrev_number)
17890 unsigned int hash_number;
17891 struct abbrev_info *abbrev;
17893 hash_number = abbrev_number % ABBREV_HASH_SIZE;
17894 abbrev = abbrev_table->abbrevs[hash_number];
17898 if (abbrev->number == abbrev_number)
17900 abbrev = abbrev->next;
17905 /* Read in an abbrev table. */
17907 static struct abbrev_table *
17908 abbrev_table_read_table (struct dwarf2_section_info *section,
17909 sect_offset sect_off)
17911 struct objfile *objfile = dwarf2_per_objfile->objfile;
17912 bfd *abfd = get_section_bfd_owner (section);
17913 struct abbrev_table *abbrev_table;
17914 const gdb_byte *abbrev_ptr;
17915 struct abbrev_info *cur_abbrev;
17916 unsigned int abbrev_number, bytes_read, abbrev_name;
17917 unsigned int abbrev_form;
17918 struct attr_abbrev *cur_attrs;
17919 unsigned int allocated_attrs;
17921 abbrev_table = XNEW (struct abbrev_table);
17922 abbrev_table->sect_off = sect_off;
17923 obstack_init (&abbrev_table->abbrev_obstack);
17924 abbrev_table->abbrevs =
17925 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
17927 memset (abbrev_table->abbrevs, 0,
17928 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
17930 dwarf2_read_section (objfile, section);
17931 abbrev_ptr = section->buffer + to_underlying (sect_off);
17932 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17933 abbrev_ptr += bytes_read;
17935 allocated_attrs = ATTR_ALLOC_CHUNK;
17936 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
17938 /* Loop until we reach an abbrev number of 0. */
17939 while (abbrev_number)
17941 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
17943 /* read in abbrev header */
17944 cur_abbrev->number = abbrev_number;
17946 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17947 abbrev_ptr += bytes_read;
17948 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
17951 /* now read in declarations */
17954 LONGEST implicit_const;
17956 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17957 abbrev_ptr += bytes_read;
17958 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17959 abbrev_ptr += bytes_read;
17960 if (abbrev_form == DW_FORM_implicit_const)
17962 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
17964 abbrev_ptr += bytes_read;
17968 /* Initialize it due to a false compiler warning. */
17969 implicit_const = -1;
17972 if (abbrev_name == 0)
17975 if (cur_abbrev->num_attrs == allocated_attrs)
17977 allocated_attrs += ATTR_ALLOC_CHUNK;
17979 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
17982 cur_attrs[cur_abbrev->num_attrs].name
17983 = (enum dwarf_attribute) abbrev_name;
17984 cur_attrs[cur_abbrev->num_attrs].form
17985 = (enum dwarf_form) abbrev_form;
17986 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
17987 ++cur_abbrev->num_attrs;
17990 cur_abbrev->attrs =
17991 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
17992 cur_abbrev->num_attrs);
17993 memcpy (cur_abbrev->attrs, cur_attrs,
17994 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
17996 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
17998 /* Get next abbreviation.
17999 Under Irix6 the abbreviations for a compilation unit are not
18000 always properly terminated with an abbrev number of 0.
18001 Exit loop if we encounter an abbreviation which we have
18002 already read (which means we are about to read the abbreviations
18003 for the next compile unit) or if the end of the abbreviation
18004 table is reached. */
18005 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18007 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18008 abbrev_ptr += bytes_read;
18009 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
18014 return abbrev_table;
18017 /* Free the resources held by ABBREV_TABLE. */
18020 abbrev_table_free (struct abbrev_table *abbrev_table)
18022 obstack_free (&abbrev_table->abbrev_obstack, NULL);
18023 xfree (abbrev_table);
18026 /* Same as abbrev_table_free but as a cleanup.
18027 We pass in a pointer to the pointer to the table so that we can
18028 set the pointer to NULL when we're done. It also simplifies
18029 build_type_psymtabs_1. */
18032 abbrev_table_free_cleanup (void *table_ptr)
18034 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
18036 if (*abbrev_table_ptr != NULL)
18037 abbrev_table_free (*abbrev_table_ptr);
18038 *abbrev_table_ptr = NULL;
18041 /* Read the abbrev table for CU from ABBREV_SECTION. */
18044 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
18045 struct dwarf2_section_info *abbrev_section)
18048 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
18051 /* Release the memory used by the abbrev table for a compilation unit. */
18054 dwarf2_free_abbrev_table (void *ptr_to_cu)
18056 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
18058 if (cu->abbrev_table != NULL)
18059 abbrev_table_free (cu->abbrev_table);
18060 /* Set this to NULL so that we SEGV if we try to read it later,
18061 and also because free_comp_unit verifies this is NULL. */
18062 cu->abbrev_table = NULL;
18065 /* Returns nonzero if TAG represents a type that we might generate a partial
18069 is_type_tag_for_partial (int tag)
18074 /* Some types that would be reasonable to generate partial symbols for,
18075 that we don't at present. */
18076 case DW_TAG_array_type:
18077 case DW_TAG_file_type:
18078 case DW_TAG_ptr_to_member_type:
18079 case DW_TAG_set_type:
18080 case DW_TAG_string_type:
18081 case DW_TAG_subroutine_type:
18083 case DW_TAG_base_type:
18084 case DW_TAG_class_type:
18085 case DW_TAG_interface_type:
18086 case DW_TAG_enumeration_type:
18087 case DW_TAG_structure_type:
18088 case DW_TAG_subrange_type:
18089 case DW_TAG_typedef:
18090 case DW_TAG_union_type:
18097 /* Load all DIEs that are interesting for partial symbols into memory. */
18099 static struct partial_die_info *
18100 load_partial_dies (const struct die_reader_specs *reader,
18101 const gdb_byte *info_ptr, int building_psymtab)
18103 struct dwarf2_cu *cu = reader->cu;
18104 struct objfile *objfile = cu->objfile;
18105 struct partial_die_info *part_die;
18106 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18107 struct abbrev_info *abbrev;
18108 unsigned int bytes_read;
18109 unsigned int load_all = 0;
18110 int nesting_level = 1;
18115 gdb_assert (cu->per_cu != NULL);
18116 if (cu->per_cu->load_all_dies)
18120 = htab_create_alloc_ex (cu->header.length / 12,
18124 &cu->comp_unit_obstack,
18125 hashtab_obstack_allocate,
18126 dummy_obstack_deallocate);
18128 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
18132 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
18134 /* A NULL abbrev means the end of a series of children. */
18135 if (abbrev == NULL)
18137 if (--nesting_level == 0)
18139 /* PART_DIE was probably the last thing allocated on the
18140 comp_unit_obstack, so we could call obstack_free
18141 here. We don't do that because the waste is small,
18142 and will be cleaned up when we're done with this
18143 compilation unit. This way, we're also more robust
18144 against other users of the comp_unit_obstack. */
18147 info_ptr += bytes_read;
18148 last_die = parent_die;
18149 parent_die = parent_die->die_parent;
18153 /* Check for template arguments. We never save these; if
18154 they're seen, we just mark the parent, and go on our way. */
18155 if (parent_die != NULL
18156 && cu->language == language_cplus
18157 && (abbrev->tag == DW_TAG_template_type_param
18158 || abbrev->tag == DW_TAG_template_value_param))
18160 parent_die->has_template_arguments = 1;
18164 /* We don't need a partial DIE for the template argument. */
18165 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18170 /* We only recurse into c++ subprograms looking for template arguments.
18171 Skip their other children. */
18173 && cu->language == language_cplus
18174 && parent_die != NULL
18175 && parent_die->tag == DW_TAG_subprogram)
18177 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18181 /* Check whether this DIE is interesting enough to save. Normally
18182 we would not be interested in members here, but there may be
18183 later variables referencing them via DW_AT_specification (for
18184 static members). */
18186 && !is_type_tag_for_partial (abbrev->tag)
18187 && abbrev->tag != DW_TAG_constant
18188 && abbrev->tag != DW_TAG_enumerator
18189 && abbrev->tag != DW_TAG_subprogram
18190 && abbrev->tag != DW_TAG_lexical_block
18191 && abbrev->tag != DW_TAG_variable
18192 && abbrev->tag != DW_TAG_namespace
18193 && abbrev->tag != DW_TAG_module
18194 && abbrev->tag != DW_TAG_member
18195 && abbrev->tag != DW_TAG_imported_unit
18196 && abbrev->tag != DW_TAG_imported_declaration)
18198 /* Otherwise we skip to the next sibling, if any. */
18199 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18203 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
18206 /* This two-pass algorithm for processing partial symbols has a
18207 high cost in cache pressure. Thus, handle some simple cases
18208 here which cover the majority of C partial symbols. DIEs
18209 which neither have specification tags in them, nor could have
18210 specification tags elsewhere pointing at them, can simply be
18211 processed and discarded.
18213 This segment is also optional; scan_partial_symbols and
18214 add_partial_symbol will handle these DIEs if we chain
18215 them in normally. When compilers which do not emit large
18216 quantities of duplicate debug information are more common,
18217 this code can probably be removed. */
18219 /* Any complete simple types at the top level (pretty much all
18220 of them, for a language without namespaces), can be processed
18222 if (parent_die == NULL
18223 && part_die->has_specification == 0
18224 && part_die->is_declaration == 0
18225 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
18226 || part_die->tag == DW_TAG_base_type
18227 || part_die->tag == DW_TAG_subrange_type))
18229 if (building_psymtab && part_die->name != NULL)
18230 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
18231 VAR_DOMAIN, LOC_TYPEDEF,
18232 &objfile->static_psymbols,
18233 0, cu->language, objfile);
18234 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
18238 /* The exception for DW_TAG_typedef with has_children above is
18239 a workaround of GCC PR debug/47510. In the case of this complaint
18240 type_name_no_tag_or_error will error on such types later.
18242 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18243 it could not find the child DIEs referenced later, this is checked
18244 above. In correct DWARF DW_TAG_typedef should have no children. */
18246 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
18247 complaint (&symfile_complaints,
18248 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18249 "- DIE at 0x%x [in module %s]"),
18250 to_underlying (part_die->sect_off), objfile_name (objfile));
18252 /* If we're at the second level, and we're an enumerator, and
18253 our parent has no specification (meaning possibly lives in a
18254 namespace elsewhere), then we can add the partial symbol now
18255 instead of queueing it. */
18256 if (part_die->tag == DW_TAG_enumerator
18257 && parent_die != NULL
18258 && parent_die->die_parent == NULL
18259 && parent_die->tag == DW_TAG_enumeration_type
18260 && parent_die->has_specification == 0)
18262 if (part_die->name == NULL)
18263 complaint (&symfile_complaints,
18264 _("malformed enumerator DIE ignored"));
18265 else if (building_psymtab)
18266 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
18267 VAR_DOMAIN, LOC_CONST,
18268 cu->language == language_cplus
18269 ? &objfile->global_psymbols
18270 : &objfile->static_psymbols,
18271 0, cu->language, objfile);
18273 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
18277 /* We'll save this DIE so link it in. */
18278 part_die->die_parent = parent_die;
18279 part_die->die_sibling = NULL;
18280 part_die->die_child = NULL;
18282 if (last_die && last_die == parent_die)
18283 last_die->die_child = part_die;
18285 last_die->die_sibling = part_die;
18287 last_die = part_die;
18289 if (first_die == NULL)
18290 first_die = part_die;
18292 /* Maybe add the DIE to the hash table. Not all DIEs that we
18293 find interesting need to be in the hash table, because we
18294 also have the parent/sibling/child chains; only those that we
18295 might refer to by offset later during partial symbol reading.
18297 For now this means things that might have be the target of a
18298 DW_AT_specification, DW_AT_abstract_origin, or
18299 DW_AT_extension. DW_AT_extension will refer only to
18300 namespaces; DW_AT_abstract_origin refers to functions (and
18301 many things under the function DIE, but we do not recurse
18302 into function DIEs during partial symbol reading) and
18303 possibly variables as well; DW_AT_specification refers to
18304 declarations. Declarations ought to have the DW_AT_declaration
18305 flag. It happens that GCC forgets to put it in sometimes, but
18306 only for functions, not for types.
18308 Adding more things than necessary to the hash table is harmless
18309 except for the performance cost. Adding too few will result in
18310 wasted time in find_partial_die, when we reread the compilation
18311 unit with load_all_dies set. */
18314 || abbrev->tag == DW_TAG_constant
18315 || abbrev->tag == DW_TAG_subprogram
18316 || abbrev->tag == DW_TAG_variable
18317 || abbrev->tag == DW_TAG_namespace
18318 || part_die->is_declaration)
18322 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18323 to_underlying (part_die->sect_off),
18328 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
18330 /* For some DIEs we want to follow their children (if any). For C
18331 we have no reason to follow the children of structures; for other
18332 languages we have to, so that we can get at method physnames
18333 to infer fully qualified class names, for DW_AT_specification,
18334 and for C++ template arguments. For C++, we also look one level
18335 inside functions to find template arguments (if the name of the
18336 function does not already contain the template arguments).
18338 For Ada, we need to scan the children of subprograms and lexical
18339 blocks as well because Ada allows the definition of nested
18340 entities that could be interesting for the debugger, such as
18341 nested subprograms for instance. */
18342 if (last_die->has_children
18344 || last_die->tag == DW_TAG_namespace
18345 || last_die->tag == DW_TAG_module
18346 || last_die->tag == DW_TAG_enumeration_type
18347 || (cu->language == language_cplus
18348 && last_die->tag == DW_TAG_subprogram
18349 && (last_die->name == NULL
18350 || strchr (last_die->name, '<') == NULL))
18351 || (cu->language != language_c
18352 && (last_die->tag == DW_TAG_class_type
18353 || last_die->tag == DW_TAG_interface_type
18354 || last_die->tag == DW_TAG_structure_type
18355 || last_die->tag == DW_TAG_union_type))
18356 || (cu->language == language_ada
18357 && (last_die->tag == DW_TAG_subprogram
18358 || last_die->tag == DW_TAG_lexical_block))))
18361 parent_die = last_die;
18365 /* Otherwise we skip to the next sibling, if any. */
18366 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18368 /* Back to the top, do it again. */
18372 /* Read a minimal amount of information into the minimal die structure. */
18374 static const gdb_byte *
18375 read_partial_die (const struct die_reader_specs *reader,
18376 struct partial_die_info *part_die,
18377 struct abbrev_info *abbrev, unsigned int abbrev_len,
18378 const gdb_byte *info_ptr)
18380 struct dwarf2_cu *cu = reader->cu;
18381 struct objfile *objfile = cu->objfile;
18382 const gdb_byte *buffer = reader->buffer;
18384 struct attribute attr;
18385 int has_low_pc_attr = 0;
18386 int has_high_pc_attr = 0;
18387 int high_pc_relative = 0;
18389 memset (part_die, 0, sizeof (struct partial_die_info));
18391 part_die->sect_off = (sect_offset) (info_ptr - buffer);
18393 info_ptr += abbrev_len;
18395 if (abbrev == NULL)
18398 part_die->tag = abbrev->tag;
18399 part_die->has_children = abbrev->has_children;
18401 for (i = 0; i < abbrev->num_attrs; ++i)
18403 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
18405 /* Store the data if it is of an attribute we want to keep in a
18406 partial symbol table. */
18410 switch (part_die->tag)
18412 case DW_TAG_compile_unit:
18413 case DW_TAG_partial_unit:
18414 case DW_TAG_type_unit:
18415 /* Compilation units have a DW_AT_name that is a filename, not
18416 a source language identifier. */
18417 case DW_TAG_enumeration_type:
18418 case DW_TAG_enumerator:
18419 /* These tags always have simple identifiers already; no need
18420 to canonicalize them. */
18421 part_die->name = DW_STRING (&attr);
18425 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18426 &objfile->per_bfd->storage_obstack);
18430 case DW_AT_linkage_name:
18431 case DW_AT_MIPS_linkage_name:
18432 /* Note that both forms of linkage name might appear. We
18433 assume they will be the same, and we only store the last
18435 if (cu->language == language_ada)
18436 part_die->name = DW_STRING (&attr);
18437 part_die->linkage_name = DW_STRING (&attr);
18440 has_low_pc_attr = 1;
18441 part_die->lowpc = attr_value_as_address (&attr);
18443 case DW_AT_high_pc:
18444 has_high_pc_attr = 1;
18445 part_die->highpc = attr_value_as_address (&attr);
18446 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18447 high_pc_relative = 1;
18449 case DW_AT_location:
18450 /* Support the .debug_loc offsets. */
18451 if (attr_form_is_block (&attr))
18453 part_die->d.locdesc = DW_BLOCK (&attr);
18455 else if (attr_form_is_section_offset (&attr))
18457 dwarf2_complex_location_expr_complaint ();
18461 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18462 "partial symbol information");
18465 case DW_AT_external:
18466 part_die->is_external = DW_UNSND (&attr);
18468 case DW_AT_declaration:
18469 part_die->is_declaration = DW_UNSND (&attr);
18472 part_die->has_type = 1;
18474 case DW_AT_abstract_origin:
18475 case DW_AT_specification:
18476 case DW_AT_extension:
18477 part_die->has_specification = 1;
18478 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
18479 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18480 || cu->per_cu->is_dwz);
18482 case DW_AT_sibling:
18483 /* Ignore absolute siblings, they might point outside of
18484 the current compile unit. */
18485 if (attr.form == DW_FORM_ref_addr)
18486 complaint (&symfile_complaints,
18487 _("ignoring absolute DW_AT_sibling"));
18490 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18491 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18493 if (sibling_ptr < info_ptr)
18494 complaint (&symfile_complaints,
18495 _("DW_AT_sibling points backwards"));
18496 else if (sibling_ptr > reader->buffer_end)
18497 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18499 part_die->sibling = sibling_ptr;
18502 case DW_AT_byte_size:
18503 part_die->has_byte_size = 1;
18505 case DW_AT_const_value:
18506 part_die->has_const_value = 1;
18508 case DW_AT_calling_convention:
18509 /* DWARF doesn't provide a way to identify a program's source-level
18510 entry point. DW_AT_calling_convention attributes are only meant
18511 to describe functions' calling conventions.
18513 However, because it's a necessary piece of information in
18514 Fortran, and before DWARF 4 DW_CC_program was the only
18515 piece of debugging information whose definition refers to
18516 a 'main program' at all, several compilers marked Fortran
18517 main programs with DW_CC_program --- even when those
18518 functions use the standard calling conventions.
18520 Although DWARF now specifies a way to provide this
18521 information, we support this practice for backward
18523 if (DW_UNSND (&attr) == DW_CC_program
18524 && cu->language == language_fortran)
18525 part_die->main_subprogram = 1;
18528 if (DW_UNSND (&attr) == DW_INL_inlined
18529 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18530 part_die->may_be_inlined = 1;
18534 if (part_die->tag == DW_TAG_imported_unit)
18536 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
18537 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18538 || cu->per_cu->is_dwz);
18542 case DW_AT_main_subprogram:
18543 part_die->main_subprogram = DW_UNSND (&attr);
18551 if (high_pc_relative)
18552 part_die->highpc += part_die->lowpc;
18554 if (has_low_pc_attr && has_high_pc_attr)
18556 /* When using the GNU linker, .gnu.linkonce. sections are used to
18557 eliminate duplicate copies of functions and vtables and such.
18558 The linker will arbitrarily choose one and discard the others.
18559 The AT_*_pc values for such functions refer to local labels in
18560 these sections. If the section from that file was discarded, the
18561 labels are not in the output, so the relocs get a value of 0.
18562 If this is a discarded function, mark the pc bounds as invalid,
18563 so that GDB will ignore it. */
18564 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18566 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18568 complaint (&symfile_complaints,
18569 _("DW_AT_low_pc %s is zero "
18570 "for DIE at 0x%x [in module %s]"),
18571 paddress (gdbarch, part_die->lowpc),
18572 to_underlying (part_die->sect_off), objfile_name (objfile));
18574 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18575 else if (part_die->lowpc >= part_die->highpc)
18577 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18579 complaint (&symfile_complaints,
18580 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18581 "for DIE at 0x%x [in module %s]"),
18582 paddress (gdbarch, part_die->lowpc),
18583 paddress (gdbarch, part_die->highpc),
18584 to_underlying (part_die->sect_off),
18585 objfile_name (objfile));
18588 part_die->has_pc_info = 1;
18594 /* Find a cached partial DIE at OFFSET in CU. */
18596 static struct partial_die_info *
18597 find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
18599 struct partial_die_info *lookup_die = NULL;
18600 struct partial_die_info part_die;
18602 part_die.sect_off = sect_off;
18603 lookup_die = ((struct partial_die_info *)
18604 htab_find_with_hash (cu->partial_dies, &part_die,
18605 to_underlying (sect_off)));
18610 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18611 except in the case of .debug_types DIEs which do not reference
18612 outside their CU (they do however referencing other types via
18613 DW_FORM_ref_sig8). */
18615 static struct partial_die_info *
18616 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18618 struct objfile *objfile = cu->objfile;
18619 struct dwarf2_per_cu_data *per_cu = NULL;
18620 struct partial_die_info *pd = NULL;
18622 if (offset_in_dwz == cu->per_cu->is_dwz
18623 && offset_in_cu_p (&cu->header, sect_off))
18625 pd = find_partial_die_in_comp_unit (sect_off, cu);
18628 /* We missed recording what we needed.
18629 Load all dies and try again. */
18630 per_cu = cu->per_cu;
18634 /* TUs don't reference other CUs/TUs (except via type signatures). */
18635 if (cu->per_cu->is_debug_types)
18637 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
18638 " external reference to offset 0x%x [in module %s].\n"),
18639 to_underlying (cu->header.sect_off), to_underlying (sect_off),
18640 bfd_get_filename (objfile->obfd));
18642 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18645 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18646 load_partial_comp_unit (per_cu);
18648 per_cu->cu->last_used = 0;
18649 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
18652 /* If we didn't find it, and not all dies have been loaded,
18653 load them all and try again. */
18655 if (pd == NULL && per_cu->load_all_dies == 0)
18657 per_cu->load_all_dies = 1;
18659 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18660 THIS_CU->cu may already be in use. So we can't just free it and
18661 replace its DIEs with the ones we read in. Instead, we leave those
18662 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18663 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18665 load_partial_comp_unit (per_cu);
18667 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
18671 internal_error (__FILE__, __LINE__,
18672 _("could not find partial DIE 0x%x "
18673 "in cache [from module %s]\n"),
18674 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
18678 /* See if we can figure out if the class lives in a namespace. We do
18679 this by looking for a member function; its demangled name will
18680 contain namespace info, if there is any. */
18683 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18684 struct dwarf2_cu *cu)
18686 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18687 what template types look like, because the demangler
18688 frequently doesn't give the same name as the debug info. We
18689 could fix this by only using the demangled name to get the
18690 prefix (but see comment in read_structure_type). */
18692 struct partial_die_info *real_pdi;
18693 struct partial_die_info *child_pdi;
18695 /* If this DIE (this DIE's specification, if any) has a parent, then
18696 we should not do this. We'll prepend the parent's fully qualified
18697 name when we create the partial symbol. */
18699 real_pdi = struct_pdi;
18700 while (real_pdi->has_specification)
18701 real_pdi = find_partial_die (real_pdi->spec_offset,
18702 real_pdi->spec_is_dwz, cu);
18704 if (real_pdi->die_parent != NULL)
18707 for (child_pdi = struct_pdi->die_child;
18709 child_pdi = child_pdi->die_sibling)
18711 if (child_pdi->tag == DW_TAG_subprogram
18712 && child_pdi->linkage_name != NULL)
18714 char *actual_class_name
18715 = language_class_name_from_physname (cu->language_defn,
18716 child_pdi->linkage_name);
18717 if (actual_class_name != NULL)
18721 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
18723 strlen (actual_class_name)));
18724 xfree (actual_class_name);
18731 /* Adjust PART_DIE before generating a symbol for it. This function
18732 may set the is_external flag or change the DIE's name. */
18735 fixup_partial_die (struct partial_die_info *part_die,
18736 struct dwarf2_cu *cu)
18738 /* Once we've fixed up a die, there's no point in doing so again.
18739 This also avoids a memory leak if we were to call
18740 guess_partial_die_structure_name multiple times. */
18741 if (part_die->fixup_called)
18744 /* If we found a reference attribute and the DIE has no name, try
18745 to find a name in the referred to DIE. */
18747 if (part_die->name == NULL && part_die->has_specification)
18749 struct partial_die_info *spec_die;
18751 spec_die = find_partial_die (part_die->spec_offset,
18752 part_die->spec_is_dwz, cu);
18754 fixup_partial_die (spec_die, cu);
18756 if (spec_die->name)
18758 part_die->name = spec_die->name;
18760 /* Copy DW_AT_external attribute if it is set. */
18761 if (spec_die->is_external)
18762 part_die->is_external = spec_die->is_external;
18766 /* Set default names for some unnamed DIEs. */
18768 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
18769 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
18771 /* If there is no parent die to provide a namespace, and there are
18772 children, see if we can determine the namespace from their linkage
18774 if (cu->language == language_cplus
18775 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
18776 && part_die->die_parent == NULL
18777 && part_die->has_children
18778 && (part_die->tag == DW_TAG_class_type
18779 || part_die->tag == DW_TAG_structure_type
18780 || part_die->tag == DW_TAG_union_type))
18781 guess_partial_die_structure_name (part_die, cu);
18783 /* GCC might emit a nameless struct or union that has a linkage
18784 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18785 if (part_die->name == NULL
18786 && (part_die->tag == DW_TAG_class_type
18787 || part_die->tag == DW_TAG_interface_type
18788 || part_die->tag == DW_TAG_structure_type
18789 || part_die->tag == DW_TAG_union_type)
18790 && part_die->linkage_name != NULL)
18794 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
18799 /* Strip any leading namespaces/classes, keep only the base name.
18800 DW_AT_name for named DIEs does not contain the prefixes. */
18801 base = strrchr (demangled, ':');
18802 if (base && base > demangled && base[-1] == ':')
18809 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
18810 base, strlen (base)));
18815 part_die->fixup_called = 1;
18818 /* Read an attribute value described by an attribute form. */
18820 static const gdb_byte *
18821 read_attribute_value (const struct die_reader_specs *reader,
18822 struct attribute *attr, unsigned form,
18823 LONGEST implicit_const, const gdb_byte *info_ptr)
18825 struct dwarf2_cu *cu = reader->cu;
18826 struct objfile *objfile = cu->objfile;
18827 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18828 bfd *abfd = reader->abfd;
18829 struct comp_unit_head *cu_header = &cu->header;
18830 unsigned int bytes_read;
18831 struct dwarf_block *blk;
18833 attr->form = (enum dwarf_form) form;
18836 case DW_FORM_ref_addr:
18837 if (cu->header.version == 2)
18838 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18840 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18841 &cu->header, &bytes_read);
18842 info_ptr += bytes_read;
18844 case DW_FORM_GNU_ref_alt:
18845 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18846 info_ptr += bytes_read;
18849 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18850 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18851 info_ptr += bytes_read;
18853 case DW_FORM_block2:
18854 blk = dwarf_alloc_block (cu);
18855 blk->size = read_2_bytes (abfd, info_ptr);
18857 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18858 info_ptr += blk->size;
18859 DW_BLOCK (attr) = blk;
18861 case DW_FORM_block4:
18862 blk = dwarf_alloc_block (cu);
18863 blk->size = read_4_bytes (abfd, info_ptr);
18865 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18866 info_ptr += blk->size;
18867 DW_BLOCK (attr) = blk;
18869 case DW_FORM_data2:
18870 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18873 case DW_FORM_data4:
18874 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18877 case DW_FORM_data8:
18878 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18881 case DW_FORM_data16:
18882 blk = dwarf_alloc_block (cu);
18884 blk->data = read_n_bytes (abfd, info_ptr, 16);
18886 DW_BLOCK (attr) = blk;
18888 case DW_FORM_sec_offset:
18889 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18890 info_ptr += bytes_read;
18892 case DW_FORM_string:
18893 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18894 DW_STRING_IS_CANONICAL (attr) = 0;
18895 info_ptr += bytes_read;
18898 if (!cu->per_cu->is_dwz)
18900 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
18902 DW_STRING_IS_CANONICAL (attr) = 0;
18903 info_ptr += bytes_read;
18907 case DW_FORM_line_strp:
18908 if (!cu->per_cu->is_dwz)
18910 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
18911 cu_header, &bytes_read);
18912 DW_STRING_IS_CANONICAL (attr) = 0;
18913 info_ptr += bytes_read;
18917 case DW_FORM_GNU_strp_alt:
18919 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18920 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
18923 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
18924 DW_STRING_IS_CANONICAL (attr) = 0;
18925 info_ptr += bytes_read;
18928 case DW_FORM_exprloc:
18929 case DW_FORM_block:
18930 blk = dwarf_alloc_block (cu);
18931 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18932 info_ptr += bytes_read;
18933 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18934 info_ptr += blk->size;
18935 DW_BLOCK (attr) = blk;
18937 case DW_FORM_block1:
18938 blk = dwarf_alloc_block (cu);
18939 blk->size = read_1_byte (abfd, info_ptr);
18941 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18942 info_ptr += blk->size;
18943 DW_BLOCK (attr) = blk;
18945 case DW_FORM_data1:
18946 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18950 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18953 case DW_FORM_flag_present:
18954 DW_UNSND (attr) = 1;
18956 case DW_FORM_sdata:
18957 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18958 info_ptr += bytes_read;
18960 case DW_FORM_udata:
18961 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18962 info_ptr += bytes_read;
18965 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18966 + read_1_byte (abfd, info_ptr));
18970 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18971 + read_2_bytes (abfd, info_ptr));
18975 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18976 + read_4_bytes (abfd, info_ptr));
18980 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18981 + read_8_bytes (abfd, info_ptr));
18984 case DW_FORM_ref_sig8:
18985 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18988 case DW_FORM_ref_udata:
18989 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18990 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18991 info_ptr += bytes_read;
18993 case DW_FORM_indirect:
18994 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18995 info_ptr += bytes_read;
18996 if (form == DW_FORM_implicit_const)
18998 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18999 info_ptr += bytes_read;
19001 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19004 case DW_FORM_implicit_const:
19005 DW_SND (attr) = implicit_const;
19007 case DW_FORM_GNU_addr_index:
19008 if (reader->dwo_file == NULL)
19010 /* For now flag a hard error.
19011 Later we can turn this into a complaint. */
19012 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19013 dwarf_form_name (form),
19014 bfd_get_filename (abfd));
19016 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19017 info_ptr += bytes_read;
19019 case DW_FORM_GNU_str_index:
19020 if (reader->dwo_file == NULL)
19022 /* For now flag a hard error.
19023 Later we can turn this into a complaint if warranted. */
19024 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19025 dwarf_form_name (form),
19026 bfd_get_filename (abfd));
19029 ULONGEST str_index =
19030 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19032 DW_STRING (attr) = read_str_index (reader, str_index);
19033 DW_STRING_IS_CANONICAL (attr) = 0;
19034 info_ptr += bytes_read;
19038 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19039 dwarf_form_name (form),
19040 bfd_get_filename (abfd));
19044 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19045 attr->form = DW_FORM_GNU_ref_alt;
19047 /* We have seen instances where the compiler tried to emit a byte
19048 size attribute of -1 which ended up being encoded as an unsigned
19049 0xffffffff. Although 0xffffffff is technically a valid size value,
19050 an object of this size seems pretty unlikely so we can relatively
19051 safely treat these cases as if the size attribute was invalid and
19052 treat them as zero by default. */
19053 if (attr->name == DW_AT_byte_size
19054 && form == DW_FORM_data4
19055 && DW_UNSND (attr) >= 0xffffffff)
19058 (&symfile_complaints,
19059 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19060 hex_string (DW_UNSND (attr)));
19061 DW_UNSND (attr) = 0;
19067 /* Read an attribute described by an abbreviated attribute. */
19069 static const gdb_byte *
19070 read_attribute (const struct die_reader_specs *reader,
19071 struct attribute *attr, struct attr_abbrev *abbrev,
19072 const gdb_byte *info_ptr)
19074 attr->name = abbrev->name;
19075 return read_attribute_value (reader, attr, abbrev->form,
19076 abbrev->implicit_const, info_ptr);
19079 /* Read dwarf information from a buffer. */
19081 static unsigned int
19082 read_1_byte (bfd *abfd, const gdb_byte *buf)
19084 return bfd_get_8 (abfd, buf);
19088 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19090 return bfd_get_signed_8 (abfd, buf);
19093 static unsigned int
19094 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19096 return bfd_get_16 (abfd, buf);
19100 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19102 return bfd_get_signed_16 (abfd, buf);
19105 static unsigned int
19106 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19108 return bfd_get_32 (abfd, buf);
19112 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19114 return bfd_get_signed_32 (abfd, buf);
19118 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19120 return bfd_get_64 (abfd, buf);
19124 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19125 unsigned int *bytes_read)
19127 struct comp_unit_head *cu_header = &cu->header;
19128 CORE_ADDR retval = 0;
19130 if (cu_header->signed_addr_p)
19132 switch (cu_header->addr_size)
19135 retval = bfd_get_signed_16 (abfd, buf);
19138 retval = bfd_get_signed_32 (abfd, buf);
19141 retval = bfd_get_signed_64 (abfd, buf);
19144 internal_error (__FILE__, __LINE__,
19145 _("read_address: bad switch, signed [in module %s]"),
19146 bfd_get_filename (abfd));
19151 switch (cu_header->addr_size)
19154 retval = bfd_get_16 (abfd, buf);
19157 retval = bfd_get_32 (abfd, buf);
19160 retval = bfd_get_64 (abfd, buf);
19163 internal_error (__FILE__, __LINE__,
19164 _("read_address: bad switch, "
19165 "unsigned [in module %s]"),
19166 bfd_get_filename (abfd));
19170 *bytes_read = cu_header->addr_size;
19174 /* Read the initial length from a section. The (draft) DWARF 3
19175 specification allows the initial length to take up either 4 bytes
19176 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19177 bytes describe the length and all offsets will be 8 bytes in length
19180 An older, non-standard 64-bit format is also handled by this
19181 function. The older format in question stores the initial length
19182 as an 8-byte quantity without an escape value. Lengths greater
19183 than 2^32 aren't very common which means that the initial 4 bytes
19184 is almost always zero. Since a length value of zero doesn't make
19185 sense for the 32-bit format, this initial zero can be considered to
19186 be an escape value which indicates the presence of the older 64-bit
19187 format. As written, the code can't detect (old format) lengths
19188 greater than 4GB. If it becomes necessary to handle lengths
19189 somewhat larger than 4GB, we could allow other small values (such
19190 as the non-sensical values of 1, 2, and 3) to also be used as
19191 escape values indicating the presence of the old format.
19193 The value returned via bytes_read should be used to increment the
19194 relevant pointer after calling read_initial_length().
19196 [ Note: read_initial_length() and read_offset() are based on the
19197 document entitled "DWARF Debugging Information Format", revision
19198 3, draft 8, dated November 19, 2001. This document was obtained
19201 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19203 This document is only a draft and is subject to change. (So beware.)
19205 Details regarding the older, non-standard 64-bit format were
19206 determined empirically by examining 64-bit ELF files produced by
19207 the SGI toolchain on an IRIX 6.5 machine.
19209 - Kevin, July 16, 2002
19213 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19215 LONGEST length = bfd_get_32 (abfd, buf);
19217 if (length == 0xffffffff)
19219 length = bfd_get_64 (abfd, buf + 4);
19222 else if (length == 0)
19224 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19225 length = bfd_get_64 (abfd, buf);
19236 /* Cover function for read_initial_length.
19237 Returns the length of the object at BUF, and stores the size of the
19238 initial length in *BYTES_READ and stores the size that offsets will be in
19240 If the initial length size is not equivalent to that specified in
19241 CU_HEADER then issue a complaint.
19242 This is useful when reading non-comp-unit headers. */
19245 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19246 const struct comp_unit_head *cu_header,
19247 unsigned int *bytes_read,
19248 unsigned int *offset_size)
19250 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19252 gdb_assert (cu_header->initial_length_size == 4
19253 || cu_header->initial_length_size == 8
19254 || cu_header->initial_length_size == 12);
19256 if (cu_header->initial_length_size != *bytes_read)
19257 complaint (&symfile_complaints,
19258 _("intermixed 32-bit and 64-bit DWARF sections"));
19260 *offset_size = (*bytes_read == 4) ? 4 : 8;
19264 /* Read an offset from the data stream. The size of the offset is
19265 given by cu_header->offset_size. */
19268 read_offset (bfd *abfd, const gdb_byte *buf,
19269 const struct comp_unit_head *cu_header,
19270 unsigned int *bytes_read)
19272 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19274 *bytes_read = cu_header->offset_size;
19278 /* Read an offset from the data stream. */
19281 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19283 LONGEST retval = 0;
19285 switch (offset_size)
19288 retval = bfd_get_32 (abfd, buf);
19291 retval = bfd_get_64 (abfd, buf);
19294 internal_error (__FILE__, __LINE__,
19295 _("read_offset_1: bad switch [in module %s]"),
19296 bfd_get_filename (abfd));
19302 static const gdb_byte *
19303 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19305 /* If the size of a host char is 8 bits, we can return a pointer
19306 to the buffer, otherwise we have to copy the data to a buffer
19307 allocated on the temporary obstack. */
19308 gdb_assert (HOST_CHAR_BIT == 8);
19312 static const char *
19313 read_direct_string (bfd *abfd, const gdb_byte *buf,
19314 unsigned int *bytes_read_ptr)
19316 /* If the size of a host char is 8 bits, we can return a pointer
19317 to the string, otherwise we have to copy the string to a buffer
19318 allocated on the temporary obstack. */
19319 gdb_assert (HOST_CHAR_BIT == 8);
19322 *bytes_read_ptr = 1;
19325 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19326 return (const char *) buf;
19329 /* Return pointer to string at section SECT offset STR_OFFSET with error
19330 reporting strings FORM_NAME and SECT_NAME. */
19332 static const char *
19333 read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
19334 struct dwarf2_section_info *sect,
19335 const char *form_name,
19336 const char *sect_name)
19338 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
19339 if (sect->buffer == NULL)
19340 error (_("%s used without %s section [in module %s]"),
19341 form_name, sect_name, bfd_get_filename (abfd));
19342 if (str_offset >= sect->size)
19343 error (_("%s pointing outside of %s section [in module %s]"),
19344 form_name, sect_name, bfd_get_filename (abfd));
19345 gdb_assert (HOST_CHAR_BIT == 8);
19346 if (sect->buffer[str_offset] == '\0')
19348 return (const char *) (sect->buffer + str_offset);
19351 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19353 static const char *
19354 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
19356 return read_indirect_string_at_offset_from (abfd, str_offset,
19357 &dwarf2_per_objfile->str,
19358 "DW_FORM_strp", ".debug_str");
19361 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19363 static const char *
19364 read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
19366 return read_indirect_string_at_offset_from (abfd, str_offset,
19367 &dwarf2_per_objfile->line_str,
19368 "DW_FORM_line_strp",
19369 ".debug_line_str");
19372 /* Read a string at offset STR_OFFSET in the .debug_str section from
19373 the .dwz file DWZ. Throw an error if the offset is too large. If
19374 the string consists of a single NUL byte, return NULL; otherwise
19375 return a pointer to the string. */
19377 static const char *
19378 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
19380 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
19382 if (dwz->str.buffer == NULL)
19383 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19384 "section [in module %s]"),
19385 bfd_get_filename (dwz->dwz_bfd));
19386 if (str_offset >= dwz->str.size)
19387 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19388 ".debug_str section [in module %s]"),
19389 bfd_get_filename (dwz->dwz_bfd));
19390 gdb_assert (HOST_CHAR_BIT == 8);
19391 if (dwz->str.buffer[str_offset] == '\0')
19393 return (const char *) (dwz->str.buffer + str_offset);
19396 /* Return pointer to string at .debug_str offset as read from BUF.
19397 BUF is assumed to be in a compilation unit described by CU_HEADER.
19398 Return *BYTES_READ_PTR count of bytes read from BUF. */
19400 static const char *
19401 read_indirect_string (bfd *abfd, const gdb_byte *buf,
19402 const struct comp_unit_head *cu_header,
19403 unsigned int *bytes_read_ptr)
19405 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19407 return read_indirect_string_at_offset (abfd, str_offset);
19410 /* Return pointer to string at .debug_line_str offset as read from BUF.
19411 BUF is assumed to be in a compilation unit described by CU_HEADER.
19412 Return *BYTES_READ_PTR count of bytes read from BUF. */
19414 static const char *
19415 read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
19416 const struct comp_unit_head *cu_header,
19417 unsigned int *bytes_read_ptr)
19419 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19421 return read_indirect_line_string_at_offset (abfd, str_offset);
19425 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19426 unsigned int *bytes_read_ptr)
19429 unsigned int num_read;
19431 unsigned char byte;
19438 byte = bfd_get_8 (abfd, buf);
19441 result |= ((ULONGEST) (byte & 127) << shift);
19442 if ((byte & 128) == 0)
19448 *bytes_read_ptr = num_read;
19453 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19454 unsigned int *bytes_read_ptr)
19457 int shift, num_read;
19458 unsigned char byte;
19465 byte = bfd_get_8 (abfd, buf);
19468 result |= ((LONGEST) (byte & 127) << shift);
19470 if ((byte & 128) == 0)
19475 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19476 result |= -(((LONGEST) 1) << shift);
19477 *bytes_read_ptr = num_read;
19481 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19482 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19483 ADDR_SIZE is the size of addresses from the CU header. */
19486 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
19488 struct objfile *objfile = dwarf2_per_objfile->objfile;
19489 bfd *abfd = objfile->obfd;
19490 const gdb_byte *info_ptr;
19492 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19493 if (dwarf2_per_objfile->addr.buffer == NULL)
19494 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19495 objfile_name (objfile));
19496 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19497 error (_("DW_FORM_addr_index pointing outside of "
19498 ".debug_addr section [in module %s]"),
19499 objfile_name (objfile));
19500 info_ptr = (dwarf2_per_objfile->addr.buffer
19501 + addr_base + addr_index * addr_size);
19502 if (addr_size == 4)
19503 return bfd_get_32 (abfd, info_ptr);
19505 return bfd_get_64 (abfd, info_ptr);
19508 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19511 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19513 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
19516 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19519 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19520 unsigned int *bytes_read)
19522 bfd *abfd = cu->objfile->obfd;
19523 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19525 return read_addr_index (cu, addr_index);
19528 /* Data structure to pass results from dwarf2_read_addr_index_reader
19529 back to dwarf2_read_addr_index. */
19531 struct dwarf2_read_addr_index_data
19533 ULONGEST addr_base;
19537 /* die_reader_func for dwarf2_read_addr_index. */
19540 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19541 const gdb_byte *info_ptr,
19542 struct die_info *comp_unit_die,
19546 struct dwarf2_cu *cu = reader->cu;
19547 struct dwarf2_read_addr_index_data *aidata =
19548 (struct dwarf2_read_addr_index_data *) data;
19550 aidata->addr_base = cu->addr_base;
19551 aidata->addr_size = cu->header.addr_size;
19554 /* Given an index in .debug_addr, fetch the value.
19555 NOTE: This can be called during dwarf expression evaluation,
19556 long after the debug information has been read, and thus per_cu->cu
19557 may no longer exist. */
19560 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19561 unsigned int addr_index)
19563 struct objfile *objfile = per_cu->objfile;
19564 struct dwarf2_cu *cu = per_cu->cu;
19565 ULONGEST addr_base;
19568 /* This is intended to be called from outside this file. */
19569 dw2_setup (objfile);
19571 /* We need addr_base and addr_size.
19572 If we don't have PER_CU->cu, we have to get it.
19573 Nasty, but the alternative is storing the needed info in PER_CU,
19574 which at this point doesn't seem justified: it's not clear how frequently
19575 it would get used and it would increase the size of every PER_CU.
19576 Entry points like dwarf2_per_cu_addr_size do a similar thing
19577 so we're not in uncharted territory here.
19578 Alas we need to be a bit more complicated as addr_base is contained
19581 We don't need to read the entire CU(/TU).
19582 We just need the header and top level die.
19584 IWBN to use the aging mechanism to let us lazily later discard the CU.
19585 For now we skip this optimization. */
19589 addr_base = cu->addr_base;
19590 addr_size = cu->header.addr_size;
19594 struct dwarf2_read_addr_index_data aidata;
19596 /* Note: We can't use init_cutu_and_read_dies_simple here,
19597 we need addr_base. */
19598 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
19599 dwarf2_read_addr_index_reader, &aidata);
19600 addr_base = aidata.addr_base;
19601 addr_size = aidata.addr_size;
19604 return read_addr_index_1 (addr_index, addr_base, addr_size);
19607 /* Given a DW_FORM_GNU_str_index, fetch the string.
19608 This is only used by the Fission support. */
19610 static const char *
19611 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19613 struct objfile *objfile = dwarf2_per_objfile->objfile;
19614 const char *objf_name = objfile_name (objfile);
19615 bfd *abfd = objfile->obfd;
19616 struct dwarf2_cu *cu = reader->cu;
19617 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19618 struct dwarf2_section_info *str_offsets_section =
19619 &reader->dwo_file->sections.str_offsets;
19620 const gdb_byte *info_ptr;
19621 ULONGEST str_offset;
19622 static const char form_name[] = "DW_FORM_GNU_str_index";
19624 dwarf2_read_section (objfile, str_section);
19625 dwarf2_read_section (objfile, str_offsets_section);
19626 if (str_section->buffer == NULL)
19627 error (_("%s used without .debug_str.dwo section"
19628 " in CU at offset 0x%x [in module %s]"),
19629 form_name, to_underlying (cu->header.sect_off), objf_name);
19630 if (str_offsets_section->buffer == NULL)
19631 error (_("%s used without .debug_str_offsets.dwo section"
19632 " in CU at offset 0x%x [in module %s]"),
19633 form_name, to_underlying (cu->header.sect_off), objf_name);
19634 if (str_index * cu->header.offset_size >= str_offsets_section->size)
19635 error (_("%s pointing outside of .debug_str_offsets.dwo"
19636 " section in CU at offset 0x%x [in module %s]"),
19637 form_name, to_underlying (cu->header.sect_off), objf_name);
19638 info_ptr = (str_offsets_section->buffer
19639 + str_index * cu->header.offset_size);
19640 if (cu->header.offset_size == 4)
19641 str_offset = bfd_get_32 (abfd, info_ptr);
19643 str_offset = bfd_get_64 (abfd, info_ptr);
19644 if (str_offset >= str_section->size)
19645 error (_("Offset from %s pointing outside of"
19646 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
19647 form_name, to_underlying (cu->header.sect_off), objf_name);
19648 return (const char *) (str_section->buffer + str_offset);
19651 /* Return the length of an LEB128 number in BUF. */
19654 leb128_size (const gdb_byte *buf)
19656 const gdb_byte *begin = buf;
19662 if ((byte & 128) == 0)
19663 return buf - begin;
19668 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19677 cu->language = language_c;
19680 case DW_LANG_C_plus_plus:
19681 case DW_LANG_C_plus_plus_11:
19682 case DW_LANG_C_plus_plus_14:
19683 cu->language = language_cplus;
19686 cu->language = language_d;
19688 case DW_LANG_Fortran77:
19689 case DW_LANG_Fortran90:
19690 case DW_LANG_Fortran95:
19691 case DW_LANG_Fortran03:
19692 case DW_LANG_Fortran08:
19693 cu->language = language_fortran;
19696 cu->language = language_go;
19698 case DW_LANG_Mips_Assembler:
19699 cu->language = language_asm;
19701 case DW_LANG_Ada83:
19702 case DW_LANG_Ada95:
19703 cu->language = language_ada;
19705 case DW_LANG_Modula2:
19706 cu->language = language_m2;
19708 case DW_LANG_Pascal83:
19709 cu->language = language_pascal;
19712 cu->language = language_objc;
19715 case DW_LANG_Rust_old:
19716 cu->language = language_rust;
19718 case DW_LANG_Cobol74:
19719 case DW_LANG_Cobol85:
19721 cu->language = language_minimal;
19724 cu->language_defn = language_def (cu->language);
19727 /* Return the named attribute or NULL if not there. */
19729 static struct attribute *
19730 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19735 struct attribute *spec = NULL;
19737 for (i = 0; i < die->num_attrs; ++i)
19739 if (die->attrs[i].name == name)
19740 return &die->attrs[i];
19741 if (die->attrs[i].name == DW_AT_specification
19742 || die->attrs[i].name == DW_AT_abstract_origin)
19743 spec = &die->attrs[i];
19749 die = follow_die_ref (die, spec, &cu);
19755 /* Return the named attribute or NULL if not there,
19756 but do not follow DW_AT_specification, etc.
19757 This is for use in contexts where we're reading .debug_types dies.
19758 Following DW_AT_specification, DW_AT_abstract_origin will take us
19759 back up the chain, and we want to go down. */
19761 static struct attribute *
19762 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19766 for (i = 0; i < die->num_attrs; ++i)
19767 if (die->attrs[i].name == name)
19768 return &die->attrs[i];
19773 /* Return the string associated with a string-typed attribute, or NULL if it
19774 is either not found or is of an incorrect type. */
19776 static const char *
19777 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19779 struct attribute *attr;
19780 const char *str = NULL;
19782 attr = dwarf2_attr (die, name, cu);
19786 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19787 || attr->form == DW_FORM_string
19788 || attr->form == DW_FORM_GNU_str_index
19789 || attr->form == DW_FORM_GNU_strp_alt)
19790 str = DW_STRING (attr);
19792 complaint (&symfile_complaints,
19793 _("string type expected for attribute %s for "
19794 "DIE at 0x%x in module %s"),
19795 dwarf_attr_name (name), to_underlying (die->sect_off),
19796 objfile_name (cu->objfile));
19802 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19803 and holds a non-zero value. This function should only be used for
19804 DW_FORM_flag or DW_FORM_flag_present attributes. */
19807 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19809 struct attribute *attr = dwarf2_attr (die, name, cu);
19811 return (attr && DW_UNSND (attr));
19815 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19817 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19818 which value is non-zero. However, we have to be careful with
19819 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19820 (via dwarf2_flag_true_p) follows this attribute. So we may
19821 end up accidently finding a declaration attribute that belongs
19822 to a different DIE referenced by the specification attribute,
19823 even though the given DIE does not have a declaration attribute. */
19824 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19825 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19828 /* Return the die giving the specification for DIE, if there is
19829 one. *SPEC_CU is the CU containing DIE on input, and the CU
19830 containing the return value on output. If there is no
19831 specification, but there is an abstract origin, that is
19834 static struct die_info *
19835 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19837 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19840 if (spec_attr == NULL)
19841 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19843 if (spec_attr == NULL)
19846 return follow_die_ref (die, spec_attr, spec_cu);
19849 /* Stub for free_line_header to match void * callback types. */
19852 free_line_header_voidp (void *arg)
19854 struct line_header *lh = (struct line_header *) arg;
19860 line_header::add_include_dir (const char *include_dir)
19862 if (dwarf_line_debug >= 2)
19863 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19864 include_dirs.size () + 1, include_dir);
19866 include_dirs.push_back (include_dir);
19870 line_header::add_file_name (const char *name,
19872 unsigned int mod_time,
19873 unsigned int length)
19875 if (dwarf_line_debug >= 2)
19876 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
19877 (unsigned) file_names.size () + 1, name);
19879 file_names.emplace_back (name, d_index, mod_time, length);
19882 /* A convenience function to find the proper .debug_line section for a CU. */
19884 static struct dwarf2_section_info *
19885 get_debug_line_section (struct dwarf2_cu *cu)
19887 struct dwarf2_section_info *section;
19889 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19891 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19892 section = &cu->dwo_unit->dwo_file->sections.line;
19893 else if (cu->per_cu->is_dwz)
19895 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19897 section = &dwz->line;
19900 section = &dwarf2_per_objfile->line;
19905 /* Read directory or file name entry format, starting with byte of
19906 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19907 entries count and the entries themselves in the described entry
19911 read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
19912 struct line_header *lh,
19913 const struct comp_unit_head *cu_header,
19914 void (*callback) (struct line_header *lh,
19917 unsigned int mod_time,
19918 unsigned int length))
19920 gdb_byte format_count, formati;
19921 ULONGEST data_count, datai;
19922 const gdb_byte *buf = *bufp;
19923 const gdb_byte *format_header_data;
19924 unsigned int bytes_read;
19926 format_count = read_1_byte (abfd, buf);
19928 format_header_data = buf;
19929 for (formati = 0; formati < format_count; formati++)
19931 read_unsigned_leb128 (abfd, buf, &bytes_read);
19933 read_unsigned_leb128 (abfd, buf, &bytes_read);
19937 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19939 for (datai = 0; datai < data_count; datai++)
19941 const gdb_byte *format = format_header_data;
19942 struct file_entry fe;
19944 for (formati = 0; formati < format_count; formati++)
19946 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
19947 format += bytes_read;
19949 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
19950 format += bytes_read;
19952 gdb::optional<const char *> string;
19953 gdb::optional<unsigned int> uint;
19957 case DW_FORM_string:
19958 string.emplace (read_direct_string (abfd, buf, &bytes_read));
19962 case DW_FORM_line_strp:
19963 string.emplace (read_indirect_line_string (abfd, buf,
19969 case DW_FORM_data1:
19970 uint.emplace (read_1_byte (abfd, buf));
19974 case DW_FORM_data2:
19975 uint.emplace (read_2_bytes (abfd, buf));
19979 case DW_FORM_data4:
19980 uint.emplace (read_4_bytes (abfd, buf));
19984 case DW_FORM_data8:
19985 uint.emplace (read_8_bytes (abfd, buf));
19989 case DW_FORM_udata:
19990 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
19994 case DW_FORM_block:
19995 /* It is valid only for DW_LNCT_timestamp which is ignored by
20000 switch (content_type)
20003 if (string.has_value ())
20006 case DW_LNCT_directory_index:
20007 if (uint.has_value ())
20008 fe.d_index = (dir_index) *uint;
20010 case DW_LNCT_timestamp:
20011 if (uint.has_value ())
20012 fe.mod_time = *uint;
20015 if (uint.has_value ())
20021 complaint (&symfile_complaints,
20022 _("Unknown format content type %s"),
20023 pulongest (content_type));
20027 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20033 /* Read the statement program header starting at OFFSET in
20034 .debug_line, or .debug_line.dwo. Return a pointer
20035 to a struct line_header, allocated using xmalloc.
20036 Returns NULL if there is a problem reading the header, e.g., if it
20037 has a version we don't understand.
20039 NOTE: the strings in the include directory and file name tables of
20040 the returned object point into the dwarf line section buffer,
20041 and must not be freed. */
20043 static line_header_up
20044 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20046 const gdb_byte *line_ptr;
20047 unsigned int bytes_read, offset_size;
20049 const char *cur_dir, *cur_file;
20050 struct dwarf2_section_info *section;
20053 section = get_debug_line_section (cu);
20054 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20055 if (section->buffer == NULL)
20057 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20058 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20060 complaint (&symfile_complaints, _("missing .debug_line section"));
20064 /* We can't do this until we know the section is non-empty.
20065 Only then do we know we have such a section. */
20066 abfd = get_section_bfd_owner (section);
20068 /* Make sure that at least there's room for the total_length field.
20069 That could be 12 bytes long, but we're just going to fudge that. */
20070 if (to_underlying (sect_off) + 4 >= section->size)
20072 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20076 line_header_up lh (new line_header ());
20078 lh->sect_off = sect_off;
20079 lh->offset_in_dwz = cu->per_cu->is_dwz;
20081 line_ptr = section->buffer + to_underlying (sect_off);
20083 /* Read in the header. */
20085 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20086 &bytes_read, &offset_size);
20087 line_ptr += bytes_read;
20088 if (line_ptr + lh->total_length > (section->buffer + section->size))
20090 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20093 lh->statement_program_end = line_ptr + lh->total_length;
20094 lh->version = read_2_bytes (abfd, line_ptr);
20096 if (lh->version > 5)
20098 /* This is a version we don't understand. The format could have
20099 changed in ways we don't handle properly so just punt. */
20100 complaint (&symfile_complaints,
20101 _("unsupported version in .debug_line section"));
20104 if (lh->version >= 5)
20106 gdb_byte segment_selector_size;
20108 /* Skip address size. */
20109 read_1_byte (abfd, line_ptr);
20112 segment_selector_size = read_1_byte (abfd, line_ptr);
20114 if (segment_selector_size != 0)
20116 complaint (&symfile_complaints,
20117 _("unsupported segment selector size %u "
20118 "in .debug_line section"),
20119 segment_selector_size);
20123 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20124 line_ptr += offset_size;
20125 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20127 if (lh->version >= 4)
20129 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20133 lh->maximum_ops_per_instruction = 1;
20135 if (lh->maximum_ops_per_instruction == 0)
20137 lh->maximum_ops_per_instruction = 1;
20138 complaint (&symfile_complaints,
20139 _("invalid maximum_ops_per_instruction "
20140 "in `.debug_line' section"));
20143 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20145 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20147 lh->line_range = read_1_byte (abfd, line_ptr);
20149 lh->opcode_base = read_1_byte (abfd, line_ptr);
20151 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20153 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20154 for (i = 1; i < lh->opcode_base; ++i)
20156 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20160 if (lh->version >= 5)
20162 /* Read directory table. */
20163 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
20164 [] (struct line_header *lh, const char *name,
20165 dir_index d_index, unsigned int mod_time,
20166 unsigned int length)
20168 lh->add_include_dir (name);
20171 /* Read file name table. */
20172 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
20173 [] (struct line_header *lh, const char *name,
20174 dir_index d_index, unsigned int mod_time,
20175 unsigned int length)
20177 lh->add_file_name (name, d_index, mod_time, length);
20182 /* Read directory table. */
20183 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20185 line_ptr += bytes_read;
20186 lh->add_include_dir (cur_dir);
20188 line_ptr += bytes_read;
20190 /* Read file name table. */
20191 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20193 unsigned int mod_time, length;
20196 line_ptr += bytes_read;
20197 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20198 line_ptr += bytes_read;
20199 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20200 line_ptr += bytes_read;
20201 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20202 line_ptr += bytes_read;
20204 lh->add_file_name (cur_file, d_index, mod_time, length);
20206 line_ptr += bytes_read;
20208 lh->statement_program_start = line_ptr;
20210 if (line_ptr > (section->buffer + section->size))
20211 complaint (&symfile_complaints,
20212 _("line number info header doesn't "
20213 "fit in `.debug_line' section"));
20218 /* Subroutine of dwarf_decode_lines to simplify it.
20219 Return the file name of the psymtab for included file FILE_INDEX
20220 in line header LH of PST.
20221 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20222 If space for the result is malloc'd, it will be freed by a cleanup.
20223 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
20225 The function creates dangling cleanup registration. */
20227 static const char *
20228 psymtab_include_file_name (const struct line_header *lh, int file_index,
20229 const struct partial_symtab *pst,
20230 const char *comp_dir)
20232 const file_entry &fe = lh->file_names[file_index];
20233 const char *include_name = fe.name;
20234 const char *include_name_to_compare = include_name;
20235 const char *pst_filename;
20236 char *copied_name = NULL;
20239 const char *dir_name = fe.include_dir (lh);
20241 if (!IS_ABSOLUTE_PATH (include_name)
20242 && (dir_name != NULL || comp_dir != NULL))
20244 /* Avoid creating a duplicate psymtab for PST.
20245 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20246 Before we do the comparison, however, we need to account
20247 for DIR_NAME and COMP_DIR.
20248 First prepend dir_name (if non-NULL). If we still don't
20249 have an absolute path prepend comp_dir (if non-NULL).
20250 However, the directory we record in the include-file's
20251 psymtab does not contain COMP_DIR (to match the
20252 corresponding symtab(s)).
20257 bash$ gcc -g ./hello.c
20258 include_name = "hello.c"
20260 DW_AT_comp_dir = comp_dir = "/tmp"
20261 DW_AT_name = "./hello.c"
20265 if (dir_name != NULL)
20267 char *tem = concat (dir_name, SLASH_STRING,
20268 include_name, (char *)NULL);
20270 make_cleanup (xfree, tem);
20271 include_name = tem;
20272 include_name_to_compare = include_name;
20274 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20276 char *tem = concat (comp_dir, SLASH_STRING,
20277 include_name, (char *)NULL);
20279 make_cleanup (xfree, tem);
20280 include_name_to_compare = tem;
20284 pst_filename = pst->filename;
20285 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20287 copied_name = concat (pst->dirname, SLASH_STRING,
20288 pst_filename, (char *)NULL);
20289 pst_filename = copied_name;
20292 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20294 if (copied_name != NULL)
20295 xfree (copied_name);
20299 return include_name;
20302 /* State machine to track the state of the line number program. */
20304 class lnp_state_machine
20307 /* Initialize a machine state for the start of a line number
20309 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20311 file_entry *current_file ()
20313 /* lh->file_names is 0-based, but the file name numbers in the
20314 statement program are 1-based. */
20315 return m_line_header->file_name_at (m_file);
20318 /* Record the line in the state machine. END_SEQUENCE is true if
20319 we're processing the end of a sequence. */
20320 void record_line (bool end_sequence);
20322 /* Check address and if invalid nop-out the rest of the lines in this
20324 void check_line_address (struct dwarf2_cu *cu,
20325 const gdb_byte *line_ptr,
20326 CORE_ADDR lowpc, CORE_ADDR address);
20328 void handle_set_discriminator (unsigned int discriminator)
20330 m_discriminator = discriminator;
20331 m_line_has_non_zero_discriminator |= discriminator != 0;
20334 /* Handle DW_LNE_set_address. */
20335 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20338 address += baseaddr;
20339 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20342 /* Handle DW_LNS_advance_pc. */
20343 void handle_advance_pc (CORE_ADDR adjust);
20345 /* Handle a special opcode. */
20346 void handle_special_opcode (unsigned char op_code);
20348 /* Handle DW_LNS_advance_line. */
20349 void handle_advance_line (int line_delta)
20351 advance_line (line_delta);
20354 /* Handle DW_LNS_set_file. */
20355 void handle_set_file (file_name_index file);
20357 /* Handle DW_LNS_negate_stmt. */
20358 void handle_negate_stmt ()
20360 m_is_stmt = !m_is_stmt;
20363 /* Handle DW_LNS_const_add_pc. */
20364 void handle_const_add_pc ();
20366 /* Handle DW_LNS_fixed_advance_pc. */
20367 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20369 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20373 /* Handle DW_LNS_copy. */
20374 void handle_copy ()
20376 record_line (false);
20377 m_discriminator = 0;
20380 /* Handle DW_LNE_end_sequence. */
20381 void handle_end_sequence ()
20383 m_record_line_callback = ::record_line;
20387 /* Advance the line by LINE_DELTA. */
20388 void advance_line (int line_delta)
20390 m_line += line_delta;
20392 if (line_delta != 0)
20393 m_line_has_non_zero_discriminator = m_discriminator != 0;
20396 gdbarch *m_gdbarch;
20398 /* True if we're recording lines.
20399 Otherwise we're building partial symtabs and are just interested in
20400 finding include files mentioned by the line number program. */
20401 bool m_record_lines_p;
20403 /* The line number header. */
20404 line_header *m_line_header;
20406 /* These are part of the standard DWARF line number state machine,
20407 and initialized according to the DWARF spec. */
20409 unsigned char m_op_index = 0;
20410 /* The line table index (1-based) of the current file. */
20411 file_name_index m_file = (file_name_index) 1;
20412 unsigned int m_line = 1;
20414 /* These are initialized in the constructor. */
20416 CORE_ADDR m_address;
20418 unsigned int m_discriminator;
20420 /* Additional bits of state we need to track. */
20422 /* The last file that we called dwarf2_start_subfile for.
20423 This is only used for TLLs. */
20424 unsigned int m_last_file = 0;
20425 /* The last file a line number was recorded for. */
20426 struct subfile *m_last_subfile = NULL;
20428 /* The function to call to record a line. */
20429 record_line_ftype *m_record_line_callback = NULL;
20431 /* The last line number that was recorded, used to coalesce
20432 consecutive entries for the same line. This can happen, for
20433 example, when discriminators are present. PR 17276. */
20434 unsigned int m_last_line = 0;
20435 bool m_line_has_non_zero_discriminator = false;
20439 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20441 CORE_ADDR addr_adj = (((m_op_index + adjust)
20442 / m_line_header->maximum_ops_per_instruction)
20443 * m_line_header->minimum_instruction_length);
20444 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20445 m_op_index = ((m_op_index + adjust)
20446 % m_line_header->maximum_ops_per_instruction);
20450 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20452 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20453 CORE_ADDR addr_adj = (((m_op_index
20454 + (adj_opcode / m_line_header->line_range))
20455 / m_line_header->maximum_ops_per_instruction)
20456 * m_line_header->minimum_instruction_length);
20457 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20458 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20459 % m_line_header->maximum_ops_per_instruction);
20461 int line_delta = (m_line_header->line_base
20462 + (adj_opcode % m_line_header->line_range));
20463 advance_line (line_delta);
20464 record_line (false);
20465 m_discriminator = 0;
20469 lnp_state_machine::handle_set_file (file_name_index file)
20473 const file_entry *fe = current_file ();
20475 dwarf2_debug_line_missing_file_complaint ();
20476 else if (m_record_lines_p)
20478 const char *dir = fe->include_dir (m_line_header);
20480 m_last_subfile = current_subfile;
20481 m_line_has_non_zero_discriminator = m_discriminator != 0;
20482 dwarf2_start_subfile (fe->name, dir);
20487 lnp_state_machine::handle_const_add_pc ()
20490 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20493 = (((m_op_index + adjust)
20494 / m_line_header->maximum_ops_per_instruction)
20495 * m_line_header->minimum_instruction_length);
20497 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20498 m_op_index = ((m_op_index + adjust)
20499 % m_line_header->maximum_ops_per_instruction);
20502 /* Ignore this record_line request. */
20505 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20510 /* Return non-zero if we should add LINE to the line number table.
20511 LINE is the line to add, LAST_LINE is the last line that was added,
20512 LAST_SUBFILE is the subfile for LAST_LINE.
20513 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20514 had a non-zero discriminator.
20516 We have to be careful in the presence of discriminators.
20517 E.g., for this line:
20519 for (i = 0; i < 100000; i++);
20521 clang can emit four line number entries for that one line,
20522 each with a different discriminator.
20523 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20525 However, we want gdb to coalesce all four entries into one.
20526 Otherwise the user could stepi into the middle of the line and
20527 gdb would get confused about whether the pc really was in the
20528 middle of the line.
20530 Things are further complicated by the fact that two consecutive
20531 line number entries for the same line is a heuristic used by gcc
20532 to denote the end of the prologue. So we can't just discard duplicate
20533 entries, we have to be selective about it. The heuristic we use is
20534 that we only collapse consecutive entries for the same line if at least
20535 one of those entries has a non-zero discriminator. PR 17276.
20537 Note: Addresses in the line number state machine can never go backwards
20538 within one sequence, thus this coalescing is ok. */
20541 dwarf_record_line_p (unsigned int line, unsigned int last_line,
20542 int line_has_non_zero_discriminator,
20543 struct subfile *last_subfile)
20545 if (current_subfile != last_subfile)
20547 if (line != last_line)
20549 /* Same line for the same file that we've seen already.
20550 As a last check, for pr 17276, only record the line if the line
20551 has never had a non-zero discriminator. */
20552 if (!line_has_non_zero_discriminator)
20557 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20558 in the line table of subfile SUBFILE. */
20561 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20562 unsigned int line, CORE_ADDR address,
20563 record_line_ftype p_record_line)
20565 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20567 if (dwarf_line_debug)
20569 fprintf_unfiltered (gdb_stdlog,
20570 "Recording line %u, file %s, address %s\n",
20571 line, lbasename (subfile->name),
20572 paddress (gdbarch, address));
20575 (*p_record_line) (subfile, line, addr);
20578 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20579 Mark the end of a set of line number records.
20580 The arguments are the same as for dwarf_record_line_1.
20581 If SUBFILE is NULL the request is ignored. */
20584 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20585 CORE_ADDR address, record_line_ftype p_record_line)
20587 if (subfile == NULL)
20590 if (dwarf_line_debug)
20592 fprintf_unfiltered (gdb_stdlog,
20593 "Finishing current line, file %s, address %s\n",
20594 lbasename (subfile->name),
20595 paddress (gdbarch, address));
20598 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20602 lnp_state_machine::record_line (bool end_sequence)
20604 if (dwarf_line_debug)
20606 fprintf_unfiltered (gdb_stdlog,
20607 "Processing actual line %u: file %u,"
20608 " address %s, is_stmt %u, discrim %u\n",
20609 m_line, to_underlying (m_file),
20610 paddress (m_gdbarch, m_address),
20611 m_is_stmt, m_discriminator);
20614 file_entry *fe = current_file ();
20617 dwarf2_debug_line_missing_file_complaint ();
20618 /* For now we ignore lines not starting on an instruction boundary.
20619 But not when processing end_sequence for compatibility with the
20620 previous version of the code. */
20621 else if (m_op_index == 0 || end_sequence)
20623 fe->included_p = 1;
20624 if (m_record_lines_p && m_is_stmt)
20626 if (m_last_subfile != current_subfile || end_sequence)
20628 dwarf_finish_line (m_gdbarch, m_last_subfile,
20629 m_address, m_record_line_callback);
20634 if (dwarf_record_line_p (m_line, m_last_line,
20635 m_line_has_non_zero_discriminator,
20638 dwarf_record_line_1 (m_gdbarch, current_subfile,
20640 m_record_line_callback);
20642 m_last_subfile = current_subfile;
20643 m_last_line = m_line;
20649 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20650 bool record_lines_p)
20653 m_record_lines_p = record_lines_p;
20654 m_line_header = lh;
20656 m_record_line_callback = ::record_line;
20658 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20659 was a line entry for it so that the backend has a chance to adjust it
20660 and also record it in case it needs it. This is currently used by MIPS
20661 code, cf. `mips_adjust_dwarf2_line'. */
20662 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20663 m_is_stmt = lh->default_is_stmt;
20664 m_discriminator = 0;
20668 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20669 const gdb_byte *line_ptr,
20670 CORE_ADDR lowpc, CORE_ADDR address)
20672 /* If address < lowpc then it's not a usable value, it's outside the
20673 pc range of the CU. However, we restrict the test to only address
20674 values of zero to preserve GDB's previous behaviour which is to
20675 handle the specific case of a function being GC'd by the linker. */
20677 if (address == 0 && address < lowpc)
20679 /* This line table is for a function which has been
20680 GCd by the linker. Ignore it. PR gdb/12528 */
20682 struct objfile *objfile = cu->objfile;
20683 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20685 complaint (&symfile_complaints,
20686 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20687 line_offset, objfile_name (objfile));
20688 m_record_line_callback = noop_record_line;
20689 /* Note: record_line_callback is left as noop_record_line until
20690 we see DW_LNE_end_sequence. */
20694 /* Subroutine of dwarf_decode_lines to simplify it.
20695 Process the line number information in LH.
20696 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20697 program in order to set included_p for every referenced header. */
20700 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20701 const int decode_for_pst_p, CORE_ADDR lowpc)
20703 const gdb_byte *line_ptr, *extended_end;
20704 const gdb_byte *line_end;
20705 unsigned int bytes_read, extended_len;
20706 unsigned char op_code, extended_op;
20707 CORE_ADDR baseaddr;
20708 struct objfile *objfile = cu->objfile;
20709 bfd *abfd = objfile->obfd;
20710 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20711 /* True if we're recording line info (as opposed to building partial
20712 symtabs and just interested in finding include files mentioned by
20713 the line number program). */
20714 bool record_lines_p = !decode_for_pst_p;
20716 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20718 line_ptr = lh->statement_program_start;
20719 line_end = lh->statement_program_end;
20721 /* Read the statement sequences until there's nothing left. */
20722 while (line_ptr < line_end)
20724 /* The DWARF line number program state machine. Reset the state
20725 machine at the start of each sequence. */
20726 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20727 bool end_sequence = false;
20729 if (record_lines_p)
20731 /* Start a subfile for the current file of the state
20733 const file_entry *fe = state_machine.current_file ();
20736 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
20739 /* Decode the table. */
20740 while (line_ptr < line_end && !end_sequence)
20742 op_code = read_1_byte (abfd, line_ptr);
20745 if (op_code >= lh->opcode_base)
20747 /* Special opcode. */
20748 state_machine.handle_special_opcode (op_code);
20750 else switch (op_code)
20752 case DW_LNS_extended_op:
20753 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20755 line_ptr += bytes_read;
20756 extended_end = line_ptr + extended_len;
20757 extended_op = read_1_byte (abfd, line_ptr);
20759 switch (extended_op)
20761 case DW_LNE_end_sequence:
20762 state_machine.handle_end_sequence ();
20763 end_sequence = true;
20765 case DW_LNE_set_address:
20768 = read_address (abfd, line_ptr, cu, &bytes_read);
20769 line_ptr += bytes_read;
20771 state_machine.check_line_address (cu, line_ptr,
20773 state_machine.handle_set_address (baseaddr, address);
20776 case DW_LNE_define_file:
20778 const char *cur_file;
20779 unsigned int mod_time, length;
20782 cur_file = read_direct_string (abfd, line_ptr,
20784 line_ptr += bytes_read;
20785 dindex = (dir_index)
20786 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20787 line_ptr += bytes_read;
20789 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20790 line_ptr += bytes_read;
20792 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20793 line_ptr += bytes_read;
20794 lh->add_file_name (cur_file, dindex, mod_time, length);
20797 case DW_LNE_set_discriminator:
20799 /* The discriminator is not interesting to the
20800 debugger; just ignore it. We still need to
20801 check its value though:
20802 if there are consecutive entries for the same
20803 (non-prologue) line we want to coalesce them.
20806 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20807 line_ptr += bytes_read;
20809 state_machine.handle_set_discriminator (discr);
20813 complaint (&symfile_complaints,
20814 _("mangled .debug_line section"));
20817 /* Make sure that we parsed the extended op correctly. If e.g.
20818 we expected a different address size than the producer used,
20819 we may have read the wrong number of bytes. */
20820 if (line_ptr != extended_end)
20822 complaint (&symfile_complaints,
20823 _("mangled .debug_line section"));
20828 state_machine.handle_copy ();
20830 case DW_LNS_advance_pc:
20833 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20834 line_ptr += bytes_read;
20836 state_machine.handle_advance_pc (adjust);
20839 case DW_LNS_advance_line:
20842 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20843 line_ptr += bytes_read;
20845 state_machine.handle_advance_line (line_delta);
20848 case DW_LNS_set_file:
20850 file_name_index file
20851 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20853 line_ptr += bytes_read;
20855 state_machine.handle_set_file (file);
20858 case DW_LNS_set_column:
20859 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20860 line_ptr += bytes_read;
20862 case DW_LNS_negate_stmt:
20863 state_machine.handle_negate_stmt ();
20865 case DW_LNS_set_basic_block:
20867 /* Add to the address register of the state machine the
20868 address increment value corresponding to special opcode
20869 255. I.e., this value is scaled by the minimum
20870 instruction length since special opcode 255 would have
20871 scaled the increment. */
20872 case DW_LNS_const_add_pc:
20873 state_machine.handle_const_add_pc ();
20875 case DW_LNS_fixed_advance_pc:
20877 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20880 state_machine.handle_fixed_advance_pc (addr_adj);
20885 /* Unknown standard opcode, ignore it. */
20888 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20890 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20891 line_ptr += bytes_read;
20898 dwarf2_debug_line_missing_end_sequence_complaint ();
20900 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20901 in which case we still finish recording the last line). */
20902 state_machine.record_line (true);
20906 /* Decode the Line Number Program (LNP) for the given line_header
20907 structure and CU. The actual information extracted and the type
20908 of structures created from the LNP depends on the value of PST.
20910 1. If PST is NULL, then this procedure uses the data from the program
20911 to create all necessary symbol tables, and their linetables.
20913 2. If PST is not NULL, this procedure reads the program to determine
20914 the list of files included by the unit represented by PST, and
20915 builds all the associated partial symbol tables.
20917 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20918 It is used for relative paths in the line table.
20919 NOTE: When processing partial symtabs (pst != NULL),
20920 comp_dir == pst->dirname.
20922 NOTE: It is important that psymtabs have the same file name (via strcmp)
20923 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20924 symtab we don't use it in the name of the psymtabs we create.
20925 E.g. expand_line_sal requires this when finding psymtabs to expand.
20926 A good testcase for this is mb-inline.exp.
20928 LOWPC is the lowest address in CU (or 0 if not known).
20930 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20931 for its PC<->lines mapping information. Otherwise only the filename
20932 table is read in. */
20935 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20936 struct dwarf2_cu *cu, struct partial_symtab *pst,
20937 CORE_ADDR lowpc, int decode_mapping)
20939 struct objfile *objfile = cu->objfile;
20940 const int decode_for_pst_p = (pst != NULL);
20942 if (decode_mapping)
20943 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20945 if (decode_for_pst_p)
20949 /* Now that we're done scanning the Line Header Program, we can
20950 create the psymtab of each included file. */
20951 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
20952 if (lh->file_names[file_index].included_p == 1)
20954 const char *include_name =
20955 psymtab_include_file_name (lh, file_index, pst, comp_dir);
20956 if (include_name != NULL)
20957 dwarf2_create_include_psymtab (include_name, pst, objfile);
20962 /* Make sure a symtab is created for every file, even files
20963 which contain only variables (i.e. no code with associated
20965 struct compunit_symtab *cust = buildsym_compunit_symtab ();
20968 for (i = 0; i < lh->file_names.size (); i++)
20970 file_entry &fe = lh->file_names[i];
20972 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
20974 if (current_subfile->symtab == NULL)
20976 current_subfile->symtab
20977 = allocate_symtab (cust, current_subfile->name);
20979 fe.symtab = current_subfile->symtab;
20984 /* Start a subfile for DWARF. FILENAME is the name of the file and
20985 DIRNAME the name of the source directory which contains FILENAME
20986 or NULL if not known.
20987 This routine tries to keep line numbers from identical absolute and
20988 relative file names in a common subfile.
20990 Using the `list' example from the GDB testsuite, which resides in
20991 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20992 of /srcdir/list0.c yields the following debugging information for list0.c:
20994 DW_AT_name: /srcdir/list0.c
20995 DW_AT_comp_dir: /compdir
20996 files.files[0].name: list0.h
20997 files.files[0].dir: /srcdir
20998 files.files[1].name: list0.c
20999 files.files[1].dir: /srcdir
21001 The line number information for list0.c has to end up in a single
21002 subfile, so that `break /srcdir/list0.c:1' works as expected.
21003 start_subfile will ensure that this happens provided that we pass the
21004 concatenation of files.files[1].dir and files.files[1].name as the
21008 dwarf2_start_subfile (const char *filename, const char *dirname)
21012 /* In order not to lose the line information directory,
21013 we concatenate it to the filename when it makes sense.
21014 Note that the Dwarf3 standard says (speaking of filenames in line
21015 information): ``The directory index is ignored for file names
21016 that represent full path names''. Thus ignoring dirname in the
21017 `else' branch below isn't an issue. */
21019 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21021 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21025 start_subfile (filename);
21031 /* Start a symtab for DWARF.
21032 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21034 static struct compunit_symtab *
21035 dwarf2_start_symtab (struct dwarf2_cu *cu,
21036 const char *name, const char *comp_dir, CORE_ADDR low_pc)
21038 struct compunit_symtab *cust
21039 = start_symtab (cu->objfile, name, comp_dir, low_pc, cu->language);
21041 record_debugformat ("DWARF 2");
21042 record_producer (cu->producer);
21044 /* We assume that we're processing GCC output. */
21045 processing_gcc_compilation = 2;
21047 cu->processing_has_namespace_info = 0;
21053 var_decode_location (struct attribute *attr, struct symbol *sym,
21054 struct dwarf2_cu *cu)
21056 struct objfile *objfile = cu->objfile;
21057 struct comp_unit_head *cu_header = &cu->header;
21059 /* NOTE drow/2003-01-30: There used to be a comment and some special
21060 code here to turn a symbol with DW_AT_external and a
21061 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21062 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21063 with some versions of binutils) where shared libraries could have
21064 relocations against symbols in their debug information - the
21065 minimal symbol would have the right address, but the debug info
21066 would not. It's no longer necessary, because we will explicitly
21067 apply relocations when we read in the debug information now. */
21069 /* A DW_AT_location attribute with no contents indicates that a
21070 variable has been optimized away. */
21071 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21073 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21077 /* Handle one degenerate form of location expression specially, to
21078 preserve GDB's previous behavior when section offsets are
21079 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21080 then mark this symbol as LOC_STATIC. */
21082 if (attr_form_is_block (attr)
21083 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21084 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21085 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21086 && (DW_BLOCK (attr)->size
21087 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21089 unsigned int dummy;
21091 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21092 SYMBOL_VALUE_ADDRESS (sym) =
21093 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21095 SYMBOL_VALUE_ADDRESS (sym) =
21096 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21097 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21098 fixup_symbol_section (sym, objfile);
21099 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21100 SYMBOL_SECTION (sym));
21104 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21105 expression evaluator, and use LOC_COMPUTED only when necessary
21106 (i.e. when the value of a register or memory location is
21107 referenced, or a thread-local block, etc.). Then again, it might
21108 not be worthwhile. I'm assuming that it isn't unless performance
21109 or memory numbers show me otherwise. */
21111 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21113 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21114 cu->has_loclist = 1;
21117 /* Given a pointer to a DWARF information entry, figure out if we need
21118 to make a symbol table entry for it, and if so, create a new entry
21119 and return a pointer to it.
21120 If TYPE is NULL, determine symbol type from the die, otherwise
21121 used the passed type.
21122 If SPACE is not NULL, use it to hold the new symbol. If it is
21123 NULL, allocate a new symbol on the objfile's obstack. */
21125 static struct symbol *
21126 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21127 struct symbol *space)
21129 struct objfile *objfile = cu->objfile;
21130 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21131 struct symbol *sym = NULL;
21133 struct attribute *attr = NULL;
21134 struct attribute *attr2 = NULL;
21135 CORE_ADDR baseaddr;
21136 struct pending **list_to_add = NULL;
21138 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21140 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21142 name = dwarf2_name (die, cu);
21145 const char *linkagename;
21146 int suppress_add = 0;
21151 sym = allocate_symbol (objfile);
21152 OBJSTAT (objfile, n_syms++);
21154 /* Cache this symbol's name and the name's demangled form (if any). */
21155 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21156 linkagename = dwarf2_physname (name, die, cu);
21157 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21159 /* Fortran does not have mangling standard and the mangling does differ
21160 between gfortran, iFort etc. */
21161 if (cu->language == language_fortran
21162 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21163 symbol_set_demangled_name (&(sym->ginfo),
21164 dwarf2_full_name (name, die, cu),
21167 /* Default assumptions.
21168 Use the passed type or decode it from the die. */
21169 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21170 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21172 SYMBOL_TYPE (sym) = type;
21174 SYMBOL_TYPE (sym) = die_type (die, cu);
21175 attr = dwarf2_attr (die,
21176 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21180 SYMBOL_LINE (sym) = DW_UNSND (attr);
21183 attr = dwarf2_attr (die,
21184 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21188 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21189 struct file_entry *fe;
21191 if (cu->line_header != NULL)
21192 fe = cu->line_header->file_name_at (file_index);
21197 complaint (&symfile_complaints,
21198 _("file index out of range"));
21200 symbol_set_symtab (sym, fe->symtab);
21206 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21211 addr = attr_value_as_address (attr);
21212 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21213 SYMBOL_VALUE_ADDRESS (sym) = addr;
21215 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21216 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21217 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21218 add_symbol_to_list (sym, cu->list_in_scope);
21220 case DW_TAG_subprogram:
21221 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21223 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21224 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21225 if ((attr2 && (DW_UNSND (attr2) != 0))
21226 || cu->language == language_ada)
21228 /* Subprograms marked external are stored as a global symbol.
21229 Ada subprograms, whether marked external or not, are always
21230 stored as a global symbol, because we want to be able to
21231 access them globally. For instance, we want to be able
21232 to break on a nested subprogram without having to
21233 specify the context. */
21234 list_to_add = &global_symbols;
21238 list_to_add = cu->list_in_scope;
21241 case DW_TAG_inlined_subroutine:
21242 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21244 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21245 SYMBOL_INLINED (sym) = 1;
21246 list_to_add = cu->list_in_scope;
21248 case DW_TAG_template_value_param:
21250 /* Fall through. */
21251 case DW_TAG_constant:
21252 case DW_TAG_variable:
21253 case DW_TAG_member:
21254 /* Compilation with minimal debug info may result in
21255 variables with missing type entries. Change the
21256 misleading `void' type to something sensible. */
21257 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21258 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21260 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21261 /* In the case of DW_TAG_member, we should only be called for
21262 static const members. */
21263 if (die->tag == DW_TAG_member)
21265 /* dwarf2_add_field uses die_is_declaration,
21266 so we do the same. */
21267 gdb_assert (die_is_declaration (die, cu));
21272 dwarf2_const_value (attr, sym, cu);
21273 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21276 if (attr2 && (DW_UNSND (attr2) != 0))
21277 list_to_add = &global_symbols;
21279 list_to_add = cu->list_in_scope;
21283 attr = dwarf2_attr (die, DW_AT_location, cu);
21286 var_decode_location (attr, sym, cu);
21287 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21289 /* Fortran explicitly imports any global symbols to the local
21290 scope by DW_TAG_common_block. */
21291 if (cu->language == language_fortran && die->parent
21292 && die->parent->tag == DW_TAG_common_block)
21295 if (SYMBOL_CLASS (sym) == LOC_STATIC
21296 && SYMBOL_VALUE_ADDRESS (sym) == 0
21297 && !dwarf2_per_objfile->has_section_at_zero)
21299 /* When a static variable is eliminated by the linker,
21300 the corresponding debug information is not stripped
21301 out, but the variable address is set to null;
21302 do not add such variables into symbol table. */
21304 else if (attr2 && (DW_UNSND (attr2) != 0))
21306 /* Workaround gfortran PR debug/40040 - it uses
21307 DW_AT_location for variables in -fPIC libraries which may
21308 get overriden by other libraries/executable and get
21309 a different address. Resolve it by the minimal symbol
21310 which may come from inferior's executable using copy
21311 relocation. Make this workaround only for gfortran as for
21312 other compilers GDB cannot guess the minimal symbol
21313 Fortran mangling kind. */
21314 if (cu->language == language_fortran && die->parent
21315 && die->parent->tag == DW_TAG_module
21317 && startswith (cu->producer, "GNU Fortran"))
21318 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21320 /* A variable with DW_AT_external is never static,
21321 but it may be block-scoped. */
21322 list_to_add = (cu->list_in_scope == &file_symbols
21323 ? &global_symbols : cu->list_in_scope);
21326 list_to_add = cu->list_in_scope;
21330 /* We do not know the address of this symbol.
21331 If it is an external symbol and we have type information
21332 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21333 The address of the variable will then be determined from
21334 the minimal symbol table whenever the variable is
21336 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21338 /* Fortran explicitly imports any global symbols to the local
21339 scope by DW_TAG_common_block. */
21340 if (cu->language == language_fortran && die->parent
21341 && die->parent->tag == DW_TAG_common_block)
21343 /* SYMBOL_CLASS doesn't matter here because
21344 read_common_block is going to reset it. */
21346 list_to_add = cu->list_in_scope;
21348 else if (attr2 && (DW_UNSND (attr2) != 0)
21349 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21351 /* A variable with DW_AT_external is never static, but it
21352 may be block-scoped. */
21353 list_to_add = (cu->list_in_scope == &file_symbols
21354 ? &global_symbols : cu->list_in_scope);
21356 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21358 else if (!die_is_declaration (die, cu))
21360 /* Use the default LOC_OPTIMIZED_OUT class. */
21361 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21363 list_to_add = cu->list_in_scope;
21367 case DW_TAG_formal_parameter:
21368 /* If we are inside a function, mark this as an argument. If
21369 not, we might be looking at an argument to an inlined function
21370 when we do not have enough information to show inlined frames;
21371 pretend it's a local variable in that case so that the user can
21373 if (context_stack_depth > 0
21374 && context_stack[context_stack_depth - 1].name != NULL)
21375 SYMBOL_IS_ARGUMENT (sym) = 1;
21376 attr = dwarf2_attr (die, DW_AT_location, cu);
21379 var_decode_location (attr, sym, cu);
21381 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21384 dwarf2_const_value (attr, sym, cu);
21387 list_to_add = cu->list_in_scope;
21389 case DW_TAG_unspecified_parameters:
21390 /* From varargs functions; gdb doesn't seem to have any
21391 interest in this information, so just ignore it for now.
21394 case DW_TAG_template_type_param:
21396 /* Fall through. */
21397 case DW_TAG_class_type:
21398 case DW_TAG_interface_type:
21399 case DW_TAG_structure_type:
21400 case DW_TAG_union_type:
21401 case DW_TAG_set_type:
21402 case DW_TAG_enumeration_type:
21403 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21404 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21407 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21408 really ever be static objects: otherwise, if you try
21409 to, say, break of a class's method and you're in a file
21410 which doesn't mention that class, it won't work unless
21411 the check for all static symbols in lookup_symbol_aux
21412 saves you. See the OtherFileClass tests in
21413 gdb.c++/namespace.exp. */
21417 list_to_add = (cu->list_in_scope == &file_symbols
21418 && cu->language == language_cplus
21419 ? &global_symbols : cu->list_in_scope);
21421 /* The semantics of C++ state that "struct foo {
21422 ... }" also defines a typedef for "foo". */
21423 if (cu->language == language_cplus
21424 || cu->language == language_ada
21425 || cu->language == language_d
21426 || cu->language == language_rust)
21428 /* The symbol's name is already allocated along
21429 with this objfile, so we don't need to
21430 duplicate it for the type. */
21431 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21432 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21437 case DW_TAG_typedef:
21438 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21439 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21440 list_to_add = cu->list_in_scope;
21442 case DW_TAG_base_type:
21443 case DW_TAG_subrange_type:
21444 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21445 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21446 list_to_add = cu->list_in_scope;
21448 case DW_TAG_enumerator:
21449 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21452 dwarf2_const_value (attr, sym, cu);
21455 /* NOTE: carlton/2003-11-10: See comment above in the
21456 DW_TAG_class_type, etc. block. */
21458 list_to_add = (cu->list_in_scope == &file_symbols
21459 && cu->language == language_cplus
21460 ? &global_symbols : cu->list_in_scope);
21463 case DW_TAG_imported_declaration:
21464 case DW_TAG_namespace:
21465 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21466 list_to_add = &global_symbols;
21468 case DW_TAG_module:
21469 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21470 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21471 list_to_add = &global_symbols;
21473 case DW_TAG_common_block:
21474 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21475 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21476 add_symbol_to_list (sym, cu->list_in_scope);
21479 /* Not a tag we recognize. Hopefully we aren't processing
21480 trash data, but since we must specifically ignore things
21481 we don't recognize, there is nothing else we should do at
21483 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
21484 dwarf_tag_name (die->tag));
21490 sym->hash_next = objfile->template_symbols;
21491 objfile->template_symbols = sym;
21492 list_to_add = NULL;
21495 if (list_to_add != NULL)
21496 add_symbol_to_list (sym, list_to_add);
21498 /* For the benefit of old versions of GCC, check for anonymous
21499 namespaces based on the demangled name. */
21500 if (!cu->processing_has_namespace_info
21501 && cu->language == language_cplus)
21502 cp_scan_for_anonymous_namespaces (sym, objfile);
21507 /* A wrapper for new_symbol_full that always allocates a new symbol. */
21509 static struct symbol *
21510 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
21512 return new_symbol_full (die, type, cu, NULL);
21515 /* Given an attr with a DW_FORM_dataN value in host byte order,
21516 zero-extend it as appropriate for the symbol's type. The DWARF
21517 standard (v4) is not entirely clear about the meaning of using
21518 DW_FORM_dataN for a constant with a signed type, where the type is
21519 wider than the data. The conclusion of a discussion on the DWARF
21520 list was that this is unspecified. We choose to always zero-extend
21521 because that is the interpretation long in use by GCC. */
21524 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21525 struct dwarf2_cu *cu, LONGEST *value, int bits)
21527 struct objfile *objfile = cu->objfile;
21528 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21529 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21530 LONGEST l = DW_UNSND (attr);
21532 if (bits < sizeof (*value) * 8)
21534 l &= ((LONGEST) 1 << bits) - 1;
21537 else if (bits == sizeof (*value) * 8)
21541 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21542 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21549 /* Read a constant value from an attribute. Either set *VALUE, or if
21550 the value does not fit in *VALUE, set *BYTES - either already
21551 allocated on the objfile obstack, or newly allocated on OBSTACK,
21552 or, set *BATON, if we translated the constant to a location
21556 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21557 const char *name, struct obstack *obstack,
21558 struct dwarf2_cu *cu,
21559 LONGEST *value, const gdb_byte **bytes,
21560 struct dwarf2_locexpr_baton **baton)
21562 struct objfile *objfile = cu->objfile;
21563 struct comp_unit_head *cu_header = &cu->header;
21564 struct dwarf_block *blk;
21565 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21566 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21572 switch (attr->form)
21575 case DW_FORM_GNU_addr_index:
21579 if (TYPE_LENGTH (type) != cu_header->addr_size)
21580 dwarf2_const_value_length_mismatch_complaint (name,
21581 cu_header->addr_size,
21582 TYPE_LENGTH (type));
21583 /* Symbols of this form are reasonably rare, so we just
21584 piggyback on the existing location code rather than writing
21585 a new implementation of symbol_computed_ops. */
21586 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21587 (*baton)->per_cu = cu->per_cu;
21588 gdb_assert ((*baton)->per_cu);
21590 (*baton)->size = 2 + cu_header->addr_size;
21591 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21592 (*baton)->data = data;
21594 data[0] = DW_OP_addr;
21595 store_unsigned_integer (&data[1], cu_header->addr_size,
21596 byte_order, DW_ADDR (attr));
21597 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21600 case DW_FORM_string:
21602 case DW_FORM_GNU_str_index:
21603 case DW_FORM_GNU_strp_alt:
21604 /* DW_STRING is already allocated on the objfile obstack, point
21606 *bytes = (const gdb_byte *) DW_STRING (attr);
21608 case DW_FORM_block1:
21609 case DW_FORM_block2:
21610 case DW_FORM_block4:
21611 case DW_FORM_block:
21612 case DW_FORM_exprloc:
21613 case DW_FORM_data16:
21614 blk = DW_BLOCK (attr);
21615 if (TYPE_LENGTH (type) != blk->size)
21616 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21617 TYPE_LENGTH (type));
21618 *bytes = blk->data;
21621 /* The DW_AT_const_value attributes are supposed to carry the
21622 symbol's value "represented as it would be on the target
21623 architecture." By the time we get here, it's already been
21624 converted to host endianness, so we just need to sign- or
21625 zero-extend it as appropriate. */
21626 case DW_FORM_data1:
21627 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21629 case DW_FORM_data2:
21630 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21632 case DW_FORM_data4:
21633 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21635 case DW_FORM_data8:
21636 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21639 case DW_FORM_sdata:
21640 case DW_FORM_implicit_const:
21641 *value = DW_SND (attr);
21644 case DW_FORM_udata:
21645 *value = DW_UNSND (attr);
21649 complaint (&symfile_complaints,
21650 _("unsupported const value attribute form: '%s'"),
21651 dwarf_form_name (attr->form));
21658 /* Copy constant value from an attribute to a symbol. */
21661 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21662 struct dwarf2_cu *cu)
21664 struct objfile *objfile = cu->objfile;
21666 const gdb_byte *bytes;
21667 struct dwarf2_locexpr_baton *baton;
21669 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21670 SYMBOL_PRINT_NAME (sym),
21671 &objfile->objfile_obstack, cu,
21672 &value, &bytes, &baton);
21676 SYMBOL_LOCATION_BATON (sym) = baton;
21677 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21679 else if (bytes != NULL)
21681 SYMBOL_VALUE_BYTES (sym) = bytes;
21682 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21686 SYMBOL_VALUE (sym) = value;
21687 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21691 /* Return the type of the die in question using its DW_AT_type attribute. */
21693 static struct type *
21694 die_type (struct die_info *die, struct dwarf2_cu *cu)
21696 struct attribute *type_attr;
21698 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21701 /* A missing DW_AT_type represents a void type. */
21702 return objfile_type (cu->objfile)->builtin_void;
21705 return lookup_die_type (die, type_attr, cu);
21708 /* True iff CU's producer generates GNAT Ada auxiliary information
21709 that allows to find parallel types through that information instead
21710 of having to do expensive parallel lookups by type name. */
21713 need_gnat_info (struct dwarf2_cu *cu)
21715 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
21716 of GNAT produces this auxiliary information, without any indication
21717 that it is produced. Part of enhancing the FSF version of GNAT
21718 to produce that information will be to put in place an indicator
21719 that we can use in order to determine whether the descriptive type
21720 info is available or not. One suggestion that has been made is
21721 to use a new attribute, attached to the CU die. For now, assume
21722 that the descriptive type info is not available. */
21726 /* Return the auxiliary type of the die in question using its
21727 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21728 attribute is not present. */
21730 static struct type *
21731 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21733 struct attribute *type_attr;
21735 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21739 return lookup_die_type (die, type_attr, cu);
21742 /* If DIE has a descriptive_type attribute, then set the TYPE's
21743 descriptive type accordingly. */
21746 set_descriptive_type (struct type *type, struct die_info *die,
21747 struct dwarf2_cu *cu)
21749 struct type *descriptive_type = die_descriptive_type (die, cu);
21751 if (descriptive_type)
21753 ALLOCATE_GNAT_AUX_TYPE (type);
21754 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21758 /* Return the containing type of the die in question using its
21759 DW_AT_containing_type attribute. */
21761 static struct type *
21762 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21764 struct attribute *type_attr;
21766 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21768 error (_("Dwarf Error: Problem turning containing type into gdb type "
21769 "[in module %s]"), objfile_name (cu->objfile));
21771 return lookup_die_type (die, type_attr, cu);
21774 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21776 static struct type *
21777 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21779 struct objfile *objfile = dwarf2_per_objfile->objfile;
21780 char *message, *saved;
21782 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
21783 objfile_name (objfile),
21784 to_underlying (cu->header.sect_off),
21785 to_underlying (die->sect_off));
21786 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21787 message, strlen (message));
21790 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21793 /* Look up the type of DIE in CU using its type attribute ATTR.
21794 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21795 DW_AT_containing_type.
21796 If there is no type substitute an error marker. */
21798 static struct type *
21799 lookup_die_type (struct die_info *die, const struct attribute *attr,
21800 struct dwarf2_cu *cu)
21802 struct objfile *objfile = cu->objfile;
21803 struct type *this_type;
21805 gdb_assert (attr->name == DW_AT_type
21806 || attr->name == DW_AT_GNAT_descriptive_type
21807 || attr->name == DW_AT_containing_type);
21809 /* First see if we have it cached. */
21811 if (attr->form == DW_FORM_GNU_ref_alt)
21813 struct dwarf2_per_cu_data *per_cu;
21814 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21816 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
21817 this_type = get_die_type_at_offset (sect_off, per_cu);
21819 else if (attr_form_is_ref (attr))
21821 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21823 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21825 else if (attr->form == DW_FORM_ref_sig8)
21827 ULONGEST signature = DW_SIGNATURE (attr);
21829 return get_signatured_type (die, signature, cu);
21833 complaint (&symfile_complaints,
21834 _("Dwarf Error: Bad type attribute %s in DIE"
21835 " at 0x%x [in module %s]"),
21836 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
21837 objfile_name (objfile));
21838 return build_error_marker_type (cu, die);
21841 /* If not cached we need to read it in. */
21843 if (this_type == NULL)
21845 struct die_info *type_die = NULL;
21846 struct dwarf2_cu *type_cu = cu;
21848 if (attr_form_is_ref (attr))
21849 type_die = follow_die_ref (die, attr, &type_cu);
21850 if (type_die == NULL)
21851 return build_error_marker_type (cu, die);
21852 /* If we find the type now, it's probably because the type came
21853 from an inter-CU reference and the type's CU got expanded before
21855 this_type = read_type_die (type_die, type_cu);
21858 /* If we still don't have a type use an error marker. */
21860 if (this_type == NULL)
21861 return build_error_marker_type (cu, die);
21866 /* Return the type in DIE, CU.
21867 Returns NULL for invalid types.
21869 This first does a lookup in die_type_hash,
21870 and only reads the die in if necessary.
21872 NOTE: This can be called when reading in partial or full symbols. */
21874 static struct type *
21875 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21877 struct type *this_type;
21879 this_type = get_die_type (die, cu);
21883 return read_type_die_1 (die, cu);
21886 /* Read the type in DIE, CU.
21887 Returns NULL for invalid types. */
21889 static struct type *
21890 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21892 struct type *this_type = NULL;
21896 case DW_TAG_class_type:
21897 case DW_TAG_interface_type:
21898 case DW_TAG_structure_type:
21899 case DW_TAG_union_type:
21900 this_type = read_structure_type (die, cu);
21902 case DW_TAG_enumeration_type:
21903 this_type = read_enumeration_type (die, cu);
21905 case DW_TAG_subprogram:
21906 case DW_TAG_subroutine_type:
21907 case DW_TAG_inlined_subroutine:
21908 this_type = read_subroutine_type (die, cu);
21910 case DW_TAG_array_type:
21911 this_type = read_array_type (die, cu);
21913 case DW_TAG_set_type:
21914 this_type = read_set_type (die, cu);
21916 case DW_TAG_pointer_type:
21917 this_type = read_tag_pointer_type (die, cu);
21919 case DW_TAG_ptr_to_member_type:
21920 this_type = read_tag_ptr_to_member_type (die, cu);
21922 case DW_TAG_reference_type:
21923 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21925 case DW_TAG_rvalue_reference_type:
21926 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21928 case DW_TAG_const_type:
21929 this_type = read_tag_const_type (die, cu);
21931 case DW_TAG_volatile_type:
21932 this_type = read_tag_volatile_type (die, cu);
21934 case DW_TAG_restrict_type:
21935 this_type = read_tag_restrict_type (die, cu);
21937 case DW_TAG_string_type:
21938 this_type = read_tag_string_type (die, cu);
21940 case DW_TAG_typedef:
21941 this_type = read_typedef (die, cu);
21943 case DW_TAG_subrange_type:
21944 this_type = read_subrange_type (die, cu);
21946 case DW_TAG_base_type:
21947 this_type = read_base_type (die, cu);
21949 case DW_TAG_unspecified_type:
21950 this_type = read_unspecified_type (die, cu);
21952 case DW_TAG_namespace:
21953 this_type = read_namespace_type (die, cu);
21955 case DW_TAG_module:
21956 this_type = read_module_type (die, cu);
21958 case DW_TAG_atomic_type:
21959 this_type = read_tag_atomic_type (die, cu);
21962 complaint (&symfile_complaints,
21963 _("unexpected tag in read_type_die: '%s'"),
21964 dwarf_tag_name (die->tag));
21971 /* See if we can figure out if the class lives in a namespace. We do
21972 this by looking for a member function; its demangled name will
21973 contain namespace info, if there is any.
21974 Return the computed name or NULL.
21975 Space for the result is allocated on the objfile's obstack.
21976 This is the full-die version of guess_partial_die_structure_name.
21977 In this case we know DIE has no useful parent. */
21980 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21982 struct die_info *spec_die;
21983 struct dwarf2_cu *spec_cu;
21984 struct die_info *child;
21987 spec_die = die_specification (die, &spec_cu);
21988 if (spec_die != NULL)
21994 for (child = die->child;
21996 child = child->sibling)
21998 if (child->tag == DW_TAG_subprogram)
22000 const char *linkage_name = dw2_linkage_name (child, cu);
22002 if (linkage_name != NULL)
22005 = language_class_name_from_physname (cu->language_defn,
22009 if (actual_name != NULL)
22011 const char *die_name = dwarf2_name (die, cu);
22013 if (die_name != NULL
22014 && strcmp (die_name, actual_name) != 0)
22016 /* Strip off the class name from the full name.
22017 We want the prefix. */
22018 int die_name_len = strlen (die_name);
22019 int actual_name_len = strlen (actual_name);
22021 /* Test for '::' as a sanity check. */
22022 if (actual_name_len > die_name_len + 2
22023 && actual_name[actual_name_len
22024 - die_name_len - 1] == ':')
22025 name = (char *) obstack_copy0 (
22026 &cu->objfile->per_bfd->storage_obstack,
22027 actual_name, actual_name_len - die_name_len - 2);
22030 xfree (actual_name);
22039 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22040 prefix part in such case. See
22041 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22043 static const char *
22044 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22046 struct attribute *attr;
22049 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22050 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22053 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22056 attr = dw2_linkage_name_attr (die, cu);
22057 if (attr == NULL || DW_STRING (attr) == NULL)
22060 /* dwarf2_name had to be already called. */
22061 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22063 /* Strip the base name, keep any leading namespaces/classes. */
22064 base = strrchr (DW_STRING (attr), ':');
22065 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22068 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
22070 &base[-1] - DW_STRING (attr));
22073 /* Return the name of the namespace/class that DIE is defined within,
22074 or "" if we can't tell. The caller should not xfree the result.
22076 For example, if we're within the method foo() in the following
22086 then determine_prefix on foo's die will return "N::C". */
22088 static const char *
22089 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22091 struct die_info *parent, *spec_die;
22092 struct dwarf2_cu *spec_cu;
22093 struct type *parent_type;
22094 const char *retval;
22096 if (cu->language != language_cplus
22097 && cu->language != language_fortran && cu->language != language_d
22098 && cu->language != language_rust)
22101 retval = anonymous_struct_prefix (die, cu);
22105 /* We have to be careful in the presence of DW_AT_specification.
22106 For example, with GCC 3.4, given the code
22110 // Definition of N::foo.
22114 then we'll have a tree of DIEs like this:
22116 1: DW_TAG_compile_unit
22117 2: DW_TAG_namespace // N
22118 3: DW_TAG_subprogram // declaration of N::foo
22119 4: DW_TAG_subprogram // definition of N::foo
22120 DW_AT_specification // refers to die #3
22122 Thus, when processing die #4, we have to pretend that we're in
22123 the context of its DW_AT_specification, namely the contex of die
22126 spec_die = die_specification (die, &spec_cu);
22127 if (spec_die == NULL)
22128 parent = die->parent;
22131 parent = spec_die->parent;
22135 if (parent == NULL)
22137 else if (parent->building_fullname)
22140 const char *parent_name;
22142 /* It has been seen on RealView 2.2 built binaries,
22143 DW_TAG_template_type_param types actually _defined_ as
22144 children of the parent class:
22147 template class <class Enum> Class{};
22148 Class<enum E> class_e;
22150 1: DW_TAG_class_type (Class)
22151 2: DW_TAG_enumeration_type (E)
22152 3: DW_TAG_enumerator (enum1:0)
22153 3: DW_TAG_enumerator (enum2:1)
22155 2: DW_TAG_template_type_param
22156 DW_AT_type DW_FORM_ref_udata (E)
22158 Besides being broken debug info, it can put GDB into an
22159 infinite loop. Consider:
22161 When we're building the full name for Class<E>, we'll start
22162 at Class, and go look over its template type parameters,
22163 finding E. We'll then try to build the full name of E, and
22164 reach here. We're now trying to build the full name of E,
22165 and look over the parent DIE for containing scope. In the
22166 broken case, if we followed the parent DIE of E, we'd again
22167 find Class, and once again go look at its template type
22168 arguments, etc., etc. Simply don't consider such parent die
22169 as source-level parent of this die (it can't be, the language
22170 doesn't allow it), and break the loop here. */
22171 name = dwarf2_name (die, cu);
22172 parent_name = dwarf2_name (parent, cu);
22173 complaint (&symfile_complaints,
22174 _("template param type '%s' defined within parent '%s'"),
22175 name ? name : "<unknown>",
22176 parent_name ? parent_name : "<unknown>");
22180 switch (parent->tag)
22182 case DW_TAG_namespace:
22183 parent_type = read_type_die (parent, cu);
22184 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22185 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22186 Work around this problem here. */
22187 if (cu->language == language_cplus
22188 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22190 /* We give a name to even anonymous namespaces. */
22191 return TYPE_TAG_NAME (parent_type);
22192 case DW_TAG_class_type:
22193 case DW_TAG_interface_type:
22194 case DW_TAG_structure_type:
22195 case DW_TAG_union_type:
22196 case DW_TAG_module:
22197 parent_type = read_type_die (parent, cu);
22198 if (TYPE_TAG_NAME (parent_type) != NULL)
22199 return TYPE_TAG_NAME (parent_type);
22201 /* An anonymous structure is only allowed non-static data
22202 members; no typedefs, no member functions, et cetera.
22203 So it does not need a prefix. */
22205 case DW_TAG_compile_unit:
22206 case DW_TAG_partial_unit:
22207 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22208 if (cu->language == language_cplus
22209 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22210 && die->child != NULL
22211 && (die->tag == DW_TAG_class_type
22212 || die->tag == DW_TAG_structure_type
22213 || die->tag == DW_TAG_union_type))
22215 char *name = guess_full_die_structure_name (die, cu);
22220 case DW_TAG_enumeration_type:
22221 parent_type = read_type_die (parent, cu);
22222 if (TYPE_DECLARED_CLASS (parent_type))
22224 if (TYPE_TAG_NAME (parent_type) != NULL)
22225 return TYPE_TAG_NAME (parent_type);
22228 /* Fall through. */
22230 return determine_prefix (parent, cu);
22234 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22235 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22236 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22237 an obconcat, otherwise allocate storage for the result. The CU argument is
22238 used to determine the language and hence, the appropriate separator. */
22240 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22243 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22244 int physname, struct dwarf2_cu *cu)
22246 const char *lead = "";
22249 if (suffix == NULL || suffix[0] == '\0'
22250 || prefix == NULL || prefix[0] == '\0')
22252 else if (cu->language == language_d)
22254 /* For D, the 'main' function could be defined in any module, but it
22255 should never be prefixed. */
22256 if (strcmp (suffix, "D main") == 0)
22264 else if (cu->language == language_fortran && physname)
22266 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22267 DW_AT_MIPS_linkage_name is preferred and used instead. */
22275 if (prefix == NULL)
22277 if (suffix == NULL)
22284 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22286 strcpy (retval, lead);
22287 strcat (retval, prefix);
22288 strcat (retval, sep);
22289 strcat (retval, suffix);
22294 /* We have an obstack. */
22295 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22299 /* Return sibling of die, NULL if no sibling. */
22301 static struct die_info *
22302 sibling_die (struct die_info *die)
22304 return die->sibling;
22307 /* Get name of a die, return NULL if not found. */
22309 static const char *
22310 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22311 struct obstack *obstack)
22313 if (name && cu->language == language_cplus)
22315 std::string canon_name = cp_canonicalize_string (name);
22317 if (!canon_name.empty ())
22319 if (canon_name != name)
22320 name = (const char *) obstack_copy0 (obstack,
22321 canon_name.c_str (),
22322 canon_name.length ());
22329 /* Get name of a die, return NULL if not found.
22330 Anonymous namespaces are converted to their magic string. */
22332 static const char *
22333 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22335 struct attribute *attr;
22337 attr = dwarf2_attr (die, DW_AT_name, cu);
22338 if ((!attr || !DW_STRING (attr))
22339 && die->tag != DW_TAG_namespace
22340 && die->tag != DW_TAG_class_type
22341 && die->tag != DW_TAG_interface_type
22342 && die->tag != DW_TAG_structure_type
22343 && die->tag != DW_TAG_union_type)
22348 case DW_TAG_compile_unit:
22349 case DW_TAG_partial_unit:
22350 /* Compilation units have a DW_AT_name that is a filename, not
22351 a source language identifier. */
22352 case DW_TAG_enumeration_type:
22353 case DW_TAG_enumerator:
22354 /* These tags always have simple identifiers already; no need
22355 to canonicalize them. */
22356 return DW_STRING (attr);
22358 case DW_TAG_namespace:
22359 if (attr != NULL && DW_STRING (attr) != NULL)
22360 return DW_STRING (attr);
22361 return CP_ANONYMOUS_NAMESPACE_STR;
22363 case DW_TAG_class_type:
22364 case DW_TAG_interface_type:
22365 case DW_TAG_structure_type:
22366 case DW_TAG_union_type:
22367 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22368 structures or unions. These were of the form "._%d" in GCC 4.1,
22369 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22370 and GCC 4.4. We work around this problem by ignoring these. */
22371 if (attr && DW_STRING (attr)
22372 && (startswith (DW_STRING (attr), "._")
22373 || startswith (DW_STRING (attr), "<anonymous")))
22376 /* GCC might emit a nameless typedef that has a linkage name. See
22377 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22378 if (!attr || DW_STRING (attr) == NULL)
22380 char *demangled = NULL;
22382 attr = dw2_linkage_name_attr (die, cu);
22383 if (attr == NULL || DW_STRING (attr) == NULL)
22386 /* Avoid demangling DW_STRING (attr) the second time on a second
22387 call for the same DIE. */
22388 if (!DW_STRING_IS_CANONICAL (attr))
22389 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22395 /* FIXME: we already did this for the partial symbol... */
22398 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
22399 demangled, strlen (demangled)));
22400 DW_STRING_IS_CANONICAL (attr) = 1;
22403 /* Strip any leading namespaces/classes, keep only the base name.
22404 DW_AT_name for named DIEs does not contain the prefixes. */
22405 base = strrchr (DW_STRING (attr), ':');
22406 if (base && base > DW_STRING (attr) && base[-1] == ':')
22409 return DW_STRING (attr);
22418 if (!DW_STRING_IS_CANONICAL (attr))
22421 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22422 &cu->objfile->per_bfd->storage_obstack);
22423 DW_STRING_IS_CANONICAL (attr) = 1;
22425 return DW_STRING (attr);
22428 /* Return the die that this die in an extension of, or NULL if there
22429 is none. *EXT_CU is the CU containing DIE on input, and the CU
22430 containing the return value on output. */
22432 static struct die_info *
22433 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22435 struct attribute *attr;
22437 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22441 return follow_die_ref (die, attr, ext_cu);
22444 /* Convert a DIE tag into its string name. */
22446 static const char *
22447 dwarf_tag_name (unsigned tag)
22449 const char *name = get_DW_TAG_name (tag);
22452 return "DW_TAG_<unknown>";
22457 /* Convert a DWARF attribute code into its string name. */
22459 static const char *
22460 dwarf_attr_name (unsigned attr)
22464 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22465 if (attr == DW_AT_MIPS_fde)
22466 return "DW_AT_MIPS_fde";
22468 if (attr == DW_AT_HP_block_index)
22469 return "DW_AT_HP_block_index";
22472 name = get_DW_AT_name (attr);
22475 return "DW_AT_<unknown>";
22480 /* Convert a DWARF value form code into its string name. */
22482 static const char *
22483 dwarf_form_name (unsigned form)
22485 const char *name = get_DW_FORM_name (form);
22488 return "DW_FORM_<unknown>";
22493 static const char *
22494 dwarf_bool_name (unsigned mybool)
22502 /* Convert a DWARF type code into its string name. */
22504 static const char *
22505 dwarf_type_encoding_name (unsigned enc)
22507 const char *name = get_DW_ATE_name (enc);
22510 return "DW_ATE_<unknown>";
22516 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22520 print_spaces (indent, f);
22521 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
22522 dwarf_tag_name (die->tag), die->abbrev,
22523 to_underlying (die->sect_off));
22525 if (die->parent != NULL)
22527 print_spaces (indent, f);
22528 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
22529 to_underlying (die->parent->sect_off));
22532 print_spaces (indent, f);
22533 fprintf_unfiltered (f, " has children: %s\n",
22534 dwarf_bool_name (die->child != NULL));
22536 print_spaces (indent, f);
22537 fprintf_unfiltered (f, " attributes:\n");
22539 for (i = 0; i < die->num_attrs; ++i)
22541 print_spaces (indent, f);
22542 fprintf_unfiltered (f, " %s (%s) ",
22543 dwarf_attr_name (die->attrs[i].name),
22544 dwarf_form_name (die->attrs[i].form));
22546 switch (die->attrs[i].form)
22549 case DW_FORM_GNU_addr_index:
22550 fprintf_unfiltered (f, "address: ");
22551 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22553 case DW_FORM_block2:
22554 case DW_FORM_block4:
22555 case DW_FORM_block:
22556 case DW_FORM_block1:
22557 fprintf_unfiltered (f, "block: size %s",
22558 pulongest (DW_BLOCK (&die->attrs[i])->size));
22560 case DW_FORM_exprloc:
22561 fprintf_unfiltered (f, "expression: size %s",
22562 pulongest (DW_BLOCK (&die->attrs[i])->size));
22564 case DW_FORM_data16:
22565 fprintf_unfiltered (f, "constant of 16 bytes");
22567 case DW_FORM_ref_addr:
22568 fprintf_unfiltered (f, "ref address: ");
22569 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22571 case DW_FORM_GNU_ref_alt:
22572 fprintf_unfiltered (f, "alt ref address: ");
22573 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22579 case DW_FORM_ref_udata:
22580 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22581 (long) (DW_UNSND (&die->attrs[i])));
22583 case DW_FORM_data1:
22584 case DW_FORM_data2:
22585 case DW_FORM_data4:
22586 case DW_FORM_data8:
22587 case DW_FORM_udata:
22588 case DW_FORM_sdata:
22589 fprintf_unfiltered (f, "constant: %s",
22590 pulongest (DW_UNSND (&die->attrs[i])));
22592 case DW_FORM_sec_offset:
22593 fprintf_unfiltered (f, "section offset: %s",
22594 pulongest (DW_UNSND (&die->attrs[i])));
22596 case DW_FORM_ref_sig8:
22597 fprintf_unfiltered (f, "signature: %s",
22598 hex_string (DW_SIGNATURE (&die->attrs[i])));
22600 case DW_FORM_string:
22602 case DW_FORM_line_strp:
22603 case DW_FORM_GNU_str_index:
22604 case DW_FORM_GNU_strp_alt:
22605 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22606 DW_STRING (&die->attrs[i])
22607 ? DW_STRING (&die->attrs[i]) : "",
22608 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22611 if (DW_UNSND (&die->attrs[i]))
22612 fprintf_unfiltered (f, "flag: TRUE");
22614 fprintf_unfiltered (f, "flag: FALSE");
22616 case DW_FORM_flag_present:
22617 fprintf_unfiltered (f, "flag: TRUE");
22619 case DW_FORM_indirect:
22620 /* The reader will have reduced the indirect form to
22621 the "base form" so this form should not occur. */
22622 fprintf_unfiltered (f,
22623 "unexpected attribute form: DW_FORM_indirect");
22625 case DW_FORM_implicit_const:
22626 fprintf_unfiltered (f, "constant: %s",
22627 plongest (DW_SND (&die->attrs[i])));
22630 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22631 die->attrs[i].form);
22634 fprintf_unfiltered (f, "\n");
22639 dump_die_for_error (struct die_info *die)
22641 dump_die_shallow (gdb_stderr, 0, die);
22645 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22647 int indent = level * 4;
22649 gdb_assert (die != NULL);
22651 if (level >= max_level)
22654 dump_die_shallow (f, indent, die);
22656 if (die->child != NULL)
22658 print_spaces (indent, f);
22659 fprintf_unfiltered (f, " Children:");
22660 if (level + 1 < max_level)
22662 fprintf_unfiltered (f, "\n");
22663 dump_die_1 (f, level + 1, max_level, die->child);
22667 fprintf_unfiltered (f,
22668 " [not printed, max nesting level reached]\n");
22672 if (die->sibling != NULL && level > 0)
22674 dump_die_1 (f, level, max_level, die->sibling);
22678 /* This is called from the pdie macro in gdbinit.in.
22679 It's not static so gcc will keep a copy callable from gdb. */
22682 dump_die (struct die_info *die, int max_level)
22684 dump_die_1 (gdb_stdlog, 0, max_level, die);
22688 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22692 slot = htab_find_slot_with_hash (cu->die_hash, die,
22693 to_underlying (die->sect_off),
22699 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22703 dwarf2_get_ref_die_offset (const struct attribute *attr)
22705 if (attr_form_is_ref (attr))
22706 return (sect_offset) DW_UNSND (attr);
22708 complaint (&symfile_complaints,
22709 _("unsupported die ref attribute form: '%s'"),
22710 dwarf_form_name (attr->form));
22714 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22715 * the value held by the attribute is not constant. */
22718 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22720 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22721 return DW_SND (attr);
22722 else if (attr->form == DW_FORM_udata
22723 || attr->form == DW_FORM_data1
22724 || attr->form == DW_FORM_data2
22725 || attr->form == DW_FORM_data4
22726 || attr->form == DW_FORM_data8)
22727 return DW_UNSND (attr);
22730 /* For DW_FORM_data16 see attr_form_is_constant. */
22731 complaint (&symfile_complaints,
22732 _("Attribute value is not a constant (%s)"),
22733 dwarf_form_name (attr->form));
22734 return default_value;
22738 /* Follow reference or signature attribute ATTR of SRC_DIE.
22739 On entry *REF_CU is the CU of SRC_DIE.
22740 On exit *REF_CU is the CU of the result. */
22742 static struct die_info *
22743 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22744 struct dwarf2_cu **ref_cu)
22746 struct die_info *die;
22748 if (attr_form_is_ref (attr))
22749 die = follow_die_ref (src_die, attr, ref_cu);
22750 else if (attr->form == DW_FORM_ref_sig8)
22751 die = follow_die_sig (src_die, attr, ref_cu);
22754 dump_die_for_error (src_die);
22755 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22756 objfile_name ((*ref_cu)->objfile));
22762 /* Follow reference OFFSET.
22763 On entry *REF_CU is the CU of the source die referencing OFFSET.
22764 On exit *REF_CU is the CU of the result.
22765 Returns NULL if OFFSET is invalid. */
22767 static struct die_info *
22768 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22769 struct dwarf2_cu **ref_cu)
22771 struct die_info temp_die;
22772 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22774 gdb_assert (cu->per_cu != NULL);
22778 if (cu->per_cu->is_debug_types)
22780 /* .debug_types CUs cannot reference anything outside their CU.
22781 If they need to, they have to reference a signatured type via
22782 DW_FORM_ref_sig8. */
22783 if (!offset_in_cu_p (&cu->header, sect_off))
22786 else if (offset_in_dwz != cu->per_cu->is_dwz
22787 || !offset_in_cu_p (&cu->header, sect_off))
22789 struct dwarf2_per_cu_data *per_cu;
22791 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22794 /* If necessary, add it to the queue and load its DIEs. */
22795 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22796 load_full_comp_unit (per_cu, cu->language);
22798 target_cu = per_cu->cu;
22800 else if (cu->dies == NULL)
22802 /* We're loading full DIEs during partial symbol reading. */
22803 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22804 load_full_comp_unit (cu->per_cu, language_minimal);
22807 *ref_cu = target_cu;
22808 temp_die.sect_off = sect_off;
22809 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22811 to_underlying (sect_off));
22814 /* Follow reference attribute ATTR of SRC_DIE.
22815 On entry *REF_CU is the CU of SRC_DIE.
22816 On exit *REF_CU is the CU of the result. */
22818 static struct die_info *
22819 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22820 struct dwarf2_cu **ref_cu)
22822 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22823 struct dwarf2_cu *cu = *ref_cu;
22824 struct die_info *die;
22826 die = follow_die_offset (sect_off,
22827 (attr->form == DW_FORM_GNU_ref_alt
22828 || cu->per_cu->is_dwz),
22831 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
22832 "at 0x%x [in module %s]"),
22833 to_underlying (sect_off), to_underlying (src_die->sect_off),
22834 objfile_name (cu->objfile));
22839 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22840 Returned value is intended for DW_OP_call*. Returned
22841 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
22843 struct dwarf2_locexpr_baton
22844 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22845 struct dwarf2_per_cu_data *per_cu,
22846 CORE_ADDR (*get_frame_pc) (void *baton),
22849 struct dwarf2_cu *cu;
22850 struct die_info *die;
22851 struct attribute *attr;
22852 struct dwarf2_locexpr_baton retval;
22854 dw2_setup (per_cu->objfile);
22856 if (per_cu->cu == NULL)
22861 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22862 Instead just throw an error, not much else we can do. */
22863 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
22864 to_underlying (sect_off), objfile_name (per_cu->objfile));
22867 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22869 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
22870 to_underlying (sect_off), objfile_name (per_cu->objfile));
22872 attr = dwarf2_attr (die, DW_AT_location, cu);
22875 /* DWARF: "If there is no such attribute, then there is no effect.".
22876 DATA is ignored if SIZE is 0. */
22878 retval.data = NULL;
22881 else if (attr_form_is_section_offset (attr))
22883 struct dwarf2_loclist_baton loclist_baton;
22884 CORE_ADDR pc = (*get_frame_pc) (baton);
22887 fill_in_loclist_baton (cu, &loclist_baton, attr);
22889 retval.data = dwarf2_find_location_expression (&loclist_baton,
22891 retval.size = size;
22895 if (!attr_form_is_block (attr))
22896 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
22897 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22898 to_underlying (sect_off), objfile_name (per_cu->objfile));
22900 retval.data = DW_BLOCK (attr)->data;
22901 retval.size = DW_BLOCK (attr)->size;
22903 retval.per_cu = cu->per_cu;
22905 age_cached_comp_units ();
22910 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
22913 struct dwarf2_locexpr_baton
22914 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22915 struct dwarf2_per_cu_data *per_cu,
22916 CORE_ADDR (*get_frame_pc) (void *baton),
22919 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22921 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22924 /* Write a constant of a given type as target-ordered bytes into
22927 static const gdb_byte *
22928 write_constant_as_bytes (struct obstack *obstack,
22929 enum bfd_endian byte_order,
22936 *len = TYPE_LENGTH (type);
22937 result = (gdb_byte *) obstack_alloc (obstack, *len);
22938 store_unsigned_integer (result, *len, byte_order, value);
22943 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
22944 pointer to the constant bytes and set LEN to the length of the
22945 data. If memory is needed, allocate it on OBSTACK. If the DIE
22946 does not have a DW_AT_const_value, return NULL. */
22949 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22950 struct dwarf2_per_cu_data *per_cu,
22951 struct obstack *obstack,
22954 struct dwarf2_cu *cu;
22955 struct die_info *die;
22956 struct attribute *attr;
22957 const gdb_byte *result = NULL;
22960 enum bfd_endian byte_order;
22962 dw2_setup (per_cu->objfile);
22964 if (per_cu->cu == NULL)
22969 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22970 Instead just throw an error, not much else we can do. */
22971 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
22972 to_underlying (sect_off), objfile_name (per_cu->objfile));
22975 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22977 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
22978 to_underlying (sect_off), objfile_name (per_cu->objfile));
22981 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22985 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
22986 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22988 switch (attr->form)
22991 case DW_FORM_GNU_addr_index:
22995 *len = cu->header.addr_size;
22996 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22997 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23001 case DW_FORM_string:
23003 case DW_FORM_GNU_str_index:
23004 case DW_FORM_GNU_strp_alt:
23005 /* DW_STRING is already allocated on the objfile obstack, point
23007 result = (const gdb_byte *) DW_STRING (attr);
23008 *len = strlen (DW_STRING (attr));
23010 case DW_FORM_block1:
23011 case DW_FORM_block2:
23012 case DW_FORM_block4:
23013 case DW_FORM_block:
23014 case DW_FORM_exprloc:
23015 case DW_FORM_data16:
23016 result = DW_BLOCK (attr)->data;
23017 *len = DW_BLOCK (attr)->size;
23020 /* The DW_AT_const_value attributes are supposed to carry the
23021 symbol's value "represented as it would be on the target
23022 architecture." By the time we get here, it's already been
23023 converted to host endianness, so we just need to sign- or
23024 zero-extend it as appropriate. */
23025 case DW_FORM_data1:
23026 type = die_type (die, cu);
23027 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23028 if (result == NULL)
23029 result = write_constant_as_bytes (obstack, byte_order,
23032 case DW_FORM_data2:
23033 type = die_type (die, cu);
23034 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23035 if (result == NULL)
23036 result = write_constant_as_bytes (obstack, byte_order,
23039 case DW_FORM_data4:
23040 type = die_type (die, cu);
23041 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23042 if (result == NULL)
23043 result = write_constant_as_bytes (obstack, byte_order,
23046 case DW_FORM_data8:
23047 type = die_type (die, cu);
23048 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23049 if (result == NULL)
23050 result = write_constant_as_bytes (obstack, byte_order,
23054 case DW_FORM_sdata:
23055 case DW_FORM_implicit_const:
23056 type = die_type (die, cu);
23057 result = write_constant_as_bytes (obstack, byte_order,
23058 type, DW_SND (attr), len);
23061 case DW_FORM_udata:
23062 type = die_type (die, cu);
23063 result = write_constant_as_bytes (obstack, byte_order,
23064 type, DW_UNSND (attr), len);
23068 complaint (&symfile_complaints,
23069 _("unsupported const value attribute form: '%s'"),
23070 dwarf_form_name (attr->form));
23077 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23078 valid type for this die is found. */
23081 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23082 struct dwarf2_per_cu_data *per_cu)
23084 struct dwarf2_cu *cu;
23085 struct die_info *die;
23087 dw2_setup (per_cu->objfile);
23089 if (per_cu->cu == NULL)
23095 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23099 return die_type (die, cu);
23102 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23106 dwarf2_get_die_type (cu_offset die_offset,
23107 struct dwarf2_per_cu_data *per_cu)
23109 dw2_setup (per_cu->objfile);
23111 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23112 return get_die_type_at_offset (die_offset_sect, per_cu);
23115 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23116 On entry *REF_CU is the CU of SRC_DIE.
23117 On exit *REF_CU is the CU of the result.
23118 Returns NULL if the referenced DIE isn't found. */
23120 static struct die_info *
23121 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23122 struct dwarf2_cu **ref_cu)
23124 struct die_info temp_die;
23125 struct dwarf2_cu *sig_cu;
23126 struct die_info *die;
23128 /* While it might be nice to assert sig_type->type == NULL here,
23129 we can get here for DW_AT_imported_declaration where we need
23130 the DIE not the type. */
23132 /* If necessary, add it to the queue and load its DIEs. */
23134 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23135 read_signatured_type (sig_type);
23137 sig_cu = sig_type->per_cu.cu;
23138 gdb_assert (sig_cu != NULL);
23139 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23140 temp_die.sect_off = sig_type->type_offset_in_section;
23141 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23142 to_underlying (temp_die.sect_off));
23145 /* For .gdb_index version 7 keep track of included TUs.
23146 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23147 if (dwarf2_per_objfile->index_table != NULL
23148 && dwarf2_per_objfile->index_table->version <= 7)
23150 VEC_safe_push (dwarf2_per_cu_ptr,
23151 (*ref_cu)->per_cu->imported_symtabs,
23162 /* Follow signatured type referenced by ATTR in SRC_DIE.
23163 On entry *REF_CU is the CU of SRC_DIE.
23164 On exit *REF_CU is the CU of the result.
23165 The result is the DIE of the type.
23166 If the referenced type cannot be found an error is thrown. */
23168 static struct die_info *
23169 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23170 struct dwarf2_cu **ref_cu)
23172 ULONGEST signature = DW_SIGNATURE (attr);
23173 struct signatured_type *sig_type;
23174 struct die_info *die;
23176 gdb_assert (attr->form == DW_FORM_ref_sig8);
23178 sig_type = lookup_signatured_type (*ref_cu, signature);
23179 /* sig_type will be NULL if the signatured type is missing from
23181 if (sig_type == NULL)
23183 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23184 " from DIE at 0x%x [in module %s]"),
23185 hex_string (signature), to_underlying (src_die->sect_off),
23186 objfile_name ((*ref_cu)->objfile));
23189 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23192 dump_die_for_error (src_die);
23193 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23194 " from DIE at 0x%x [in module %s]"),
23195 hex_string (signature), to_underlying (src_die->sect_off),
23196 objfile_name ((*ref_cu)->objfile));
23202 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23203 reading in and processing the type unit if necessary. */
23205 static struct type *
23206 get_signatured_type (struct die_info *die, ULONGEST signature,
23207 struct dwarf2_cu *cu)
23209 struct signatured_type *sig_type;
23210 struct dwarf2_cu *type_cu;
23211 struct die_info *type_die;
23214 sig_type = lookup_signatured_type (cu, signature);
23215 /* sig_type will be NULL if the signatured type is missing from
23217 if (sig_type == NULL)
23219 complaint (&symfile_complaints,
23220 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23221 " from DIE at 0x%x [in module %s]"),
23222 hex_string (signature), to_underlying (die->sect_off),
23223 objfile_name (dwarf2_per_objfile->objfile));
23224 return build_error_marker_type (cu, die);
23227 /* If we already know the type we're done. */
23228 if (sig_type->type != NULL)
23229 return sig_type->type;
23232 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23233 if (type_die != NULL)
23235 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23236 is created. This is important, for example, because for c++ classes
23237 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23238 type = read_type_die (type_die, type_cu);
23241 complaint (&symfile_complaints,
23242 _("Dwarf Error: Cannot build signatured type %s"
23243 " referenced from DIE at 0x%x [in module %s]"),
23244 hex_string (signature), to_underlying (die->sect_off),
23245 objfile_name (dwarf2_per_objfile->objfile));
23246 type = build_error_marker_type (cu, die);
23251 complaint (&symfile_complaints,
23252 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23253 " from DIE at 0x%x [in module %s]"),
23254 hex_string (signature), to_underlying (die->sect_off),
23255 objfile_name (dwarf2_per_objfile->objfile));
23256 type = build_error_marker_type (cu, die);
23258 sig_type->type = type;
23263 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23264 reading in and processing the type unit if necessary. */
23266 static struct type *
23267 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23268 struct dwarf2_cu *cu) /* ARI: editCase function */
23270 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23271 if (attr_form_is_ref (attr))
23273 struct dwarf2_cu *type_cu = cu;
23274 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23276 return read_type_die (type_die, type_cu);
23278 else if (attr->form == DW_FORM_ref_sig8)
23280 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23284 complaint (&symfile_complaints,
23285 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23286 " at 0x%x [in module %s]"),
23287 dwarf_form_name (attr->form), to_underlying (die->sect_off),
23288 objfile_name (dwarf2_per_objfile->objfile));
23289 return build_error_marker_type (cu, die);
23293 /* Load the DIEs associated with type unit PER_CU into memory. */
23296 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23298 struct signatured_type *sig_type;
23300 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23301 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23303 /* We have the per_cu, but we need the signatured_type.
23304 Fortunately this is an easy translation. */
23305 gdb_assert (per_cu->is_debug_types);
23306 sig_type = (struct signatured_type *) per_cu;
23308 gdb_assert (per_cu->cu == NULL);
23310 read_signatured_type (sig_type);
23312 gdb_assert (per_cu->cu != NULL);
23315 /* die_reader_func for read_signatured_type.
23316 This is identical to load_full_comp_unit_reader,
23317 but is kept separate for now. */
23320 read_signatured_type_reader (const struct die_reader_specs *reader,
23321 const gdb_byte *info_ptr,
23322 struct die_info *comp_unit_die,
23326 struct dwarf2_cu *cu = reader->cu;
23328 gdb_assert (cu->die_hash == NULL);
23330 htab_create_alloc_ex (cu->header.length / 12,
23334 &cu->comp_unit_obstack,
23335 hashtab_obstack_allocate,
23336 dummy_obstack_deallocate);
23339 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23340 &info_ptr, comp_unit_die);
23341 cu->dies = comp_unit_die;
23342 /* comp_unit_die is not stored in die_hash, no need. */
23344 /* We try not to read any attributes in this function, because not
23345 all CUs needed for references have been loaded yet, and symbol
23346 table processing isn't initialized. But we have to set the CU language,
23347 or we won't be able to build types correctly.
23348 Similarly, if we do not read the producer, we can not apply
23349 producer-specific interpretation. */
23350 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23353 /* Read in a signatured type and build its CU and DIEs.
23354 If the type is a stub for the real type in a DWO file,
23355 read in the real type from the DWO file as well. */
23358 read_signatured_type (struct signatured_type *sig_type)
23360 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23362 gdb_assert (per_cu->is_debug_types);
23363 gdb_assert (per_cu->cu == NULL);
23365 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23366 read_signatured_type_reader, NULL);
23367 sig_type->per_cu.tu_read = 1;
23370 /* Decode simple location descriptions.
23371 Given a pointer to a dwarf block that defines a location, compute
23372 the location and return the value.
23374 NOTE drow/2003-11-18: This function is called in two situations
23375 now: for the address of static or global variables (partial symbols
23376 only) and for offsets into structures which are expected to be
23377 (more or less) constant. The partial symbol case should go away,
23378 and only the constant case should remain. That will let this
23379 function complain more accurately. A few special modes are allowed
23380 without complaint for global variables (for instance, global
23381 register values and thread-local values).
23383 A location description containing no operations indicates that the
23384 object is optimized out. The return value is 0 for that case.
23385 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23386 callers will only want a very basic result and this can become a
23389 Note that stack[0] is unused except as a default error return. */
23392 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23394 struct objfile *objfile = cu->objfile;
23396 size_t size = blk->size;
23397 const gdb_byte *data = blk->data;
23398 CORE_ADDR stack[64];
23400 unsigned int bytes_read, unsnd;
23406 stack[++stacki] = 0;
23445 stack[++stacki] = op - DW_OP_lit0;
23480 stack[++stacki] = op - DW_OP_reg0;
23482 dwarf2_complex_location_expr_complaint ();
23486 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23488 stack[++stacki] = unsnd;
23490 dwarf2_complex_location_expr_complaint ();
23494 stack[++stacki] = read_address (objfile->obfd, &data[i],
23499 case DW_OP_const1u:
23500 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23504 case DW_OP_const1s:
23505 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23509 case DW_OP_const2u:
23510 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23514 case DW_OP_const2s:
23515 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23519 case DW_OP_const4u:
23520 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23524 case DW_OP_const4s:
23525 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23529 case DW_OP_const8u:
23530 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23535 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23541 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23546 stack[stacki + 1] = stack[stacki];
23551 stack[stacki - 1] += stack[stacki];
23555 case DW_OP_plus_uconst:
23556 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23562 stack[stacki - 1] -= stack[stacki];
23567 /* If we're not the last op, then we definitely can't encode
23568 this using GDB's address_class enum. This is valid for partial
23569 global symbols, although the variable's address will be bogus
23572 dwarf2_complex_location_expr_complaint ();
23575 case DW_OP_GNU_push_tls_address:
23576 case DW_OP_form_tls_address:
23577 /* The top of the stack has the offset from the beginning
23578 of the thread control block at which the variable is located. */
23579 /* Nothing should follow this operator, so the top of stack would
23581 /* This is valid for partial global symbols, but the variable's
23582 address will be bogus in the psymtab. Make it always at least
23583 non-zero to not look as a variable garbage collected by linker
23584 which have DW_OP_addr 0. */
23586 dwarf2_complex_location_expr_complaint ();
23590 case DW_OP_GNU_uninit:
23593 case DW_OP_GNU_addr_index:
23594 case DW_OP_GNU_const_index:
23595 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23602 const char *name = get_DW_OP_name (op);
23605 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23608 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23612 return (stack[stacki]);
23615 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23616 outside of the allocated space. Also enforce minimum>0. */
23617 if (stacki >= ARRAY_SIZE (stack) - 1)
23619 complaint (&symfile_complaints,
23620 _("location description stack overflow"));
23626 complaint (&symfile_complaints,
23627 _("location description stack underflow"));
23631 return (stack[stacki]);
23634 /* memory allocation interface */
23636 static struct dwarf_block *
23637 dwarf_alloc_block (struct dwarf2_cu *cu)
23639 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23642 static struct die_info *
23643 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23645 struct die_info *die;
23646 size_t size = sizeof (struct die_info);
23649 size += (num_attrs - 1) * sizeof (struct attribute);
23651 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23652 memset (die, 0, sizeof (struct die_info));
23657 /* Macro support. */
23659 /* Return file name relative to the compilation directory of file number I in
23660 *LH's file name table. The result is allocated using xmalloc; the caller is
23661 responsible for freeing it. */
23664 file_file_name (int file, struct line_header *lh)
23666 /* Is the file number a valid index into the line header's file name
23667 table? Remember that file numbers start with one, not zero. */
23668 if (1 <= file && file <= lh->file_names.size ())
23670 const file_entry &fe = lh->file_names[file - 1];
23672 if (!IS_ABSOLUTE_PATH (fe.name))
23674 const char *dir = fe.include_dir (lh);
23676 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23678 return xstrdup (fe.name);
23682 /* The compiler produced a bogus file number. We can at least
23683 record the macro definitions made in the file, even if we
23684 won't be able to find the file by name. */
23685 char fake_name[80];
23687 xsnprintf (fake_name, sizeof (fake_name),
23688 "<bad macro file number %d>", file);
23690 complaint (&symfile_complaints,
23691 _("bad file number in macro information (%d)"),
23694 return xstrdup (fake_name);
23698 /* Return the full name of file number I in *LH's file name table.
23699 Use COMP_DIR as the name of the current directory of the
23700 compilation. The result is allocated using xmalloc; the caller is
23701 responsible for freeing it. */
23703 file_full_name (int file, struct line_header *lh, const char *comp_dir)
23705 /* Is the file number a valid index into the line header's file name
23706 table? Remember that file numbers start with one, not zero. */
23707 if (1 <= file && file <= lh->file_names.size ())
23709 char *relative = file_file_name (file, lh);
23711 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23713 return reconcat (relative, comp_dir, SLASH_STRING,
23714 relative, (char *) NULL);
23717 return file_file_name (file, lh);
23721 static struct macro_source_file *
23722 macro_start_file (int file, int line,
23723 struct macro_source_file *current_file,
23724 struct line_header *lh)
23726 /* File name relative to the compilation directory of this source file. */
23727 char *file_name = file_file_name (file, lh);
23729 if (! current_file)
23731 /* Note: We don't create a macro table for this compilation unit
23732 at all until we actually get a filename. */
23733 struct macro_table *macro_table = get_macro_table ();
23735 /* If we have no current file, then this must be the start_file
23736 directive for the compilation unit's main source file. */
23737 current_file = macro_set_main (macro_table, file_name);
23738 macro_define_special (macro_table);
23741 current_file = macro_include (current_file, line, file_name);
23745 return current_file;
23748 static const char *
23749 consume_improper_spaces (const char *p, const char *body)
23753 complaint (&symfile_complaints,
23754 _("macro definition contains spaces "
23755 "in formal argument list:\n`%s'"),
23767 parse_macro_definition (struct macro_source_file *file, int line,
23772 /* The body string takes one of two forms. For object-like macro
23773 definitions, it should be:
23775 <macro name> " " <definition>
23777 For function-like macro definitions, it should be:
23779 <macro name> "() " <definition>
23781 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23783 Spaces may appear only where explicitly indicated, and in the
23786 The Dwarf 2 spec says that an object-like macro's name is always
23787 followed by a space, but versions of GCC around March 2002 omit
23788 the space when the macro's definition is the empty string.
23790 The Dwarf 2 spec says that there should be no spaces between the
23791 formal arguments in a function-like macro's formal argument list,
23792 but versions of GCC around March 2002 include spaces after the
23796 /* Find the extent of the macro name. The macro name is terminated
23797 by either a space or null character (for an object-like macro) or
23798 an opening paren (for a function-like macro). */
23799 for (p = body; *p; p++)
23800 if (*p == ' ' || *p == '(')
23803 if (*p == ' ' || *p == '\0')
23805 /* It's an object-like macro. */
23806 int name_len = p - body;
23807 char *name = savestring (body, name_len);
23808 const char *replacement;
23811 replacement = body + name_len + 1;
23814 dwarf2_macro_malformed_definition_complaint (body);
23815 replacement = body + name_len;
23818 macro_define_object (file, line, name, replacement);
23822 else if (*p == '(')
23824 /* It's a function-like macro. */
23825 char *name = savestring (body, p - body);
23828 char **argv = XNEWVEC (char *, argv_size);
23832 p = consume_improper_spaces (p, body);
23834 /* Parse the formal argument list. */
23835 while (*p && *p != ')')
23837 /* Find the extent of the current argument name. */
23838 const char *arg_start = p;
23840 while (*p && *p != ',' && *p != ')' && *p != ' ')
23843 if (! *p || p == arg_start)
23844 dwarf2_macro_malformed_definition_complaint (body);
23847 /* Make sure argv has room for the new argument. */
23848 if (argc >= argv_size)
23851 argv = XRESIZEVEC (char *, argv, argv_size);
23854 argv[argc++] = savestring (arg_start, p - arg_start);
23857 p = consume_improper_spaces (p, body);
23859 /* Consume the comma, if present. */
23864 p = consume_improper_spaces (p, body);
23873 /* Perfectly formed definition, no complaints. */
23874 macro_define_function (file, line, name,
23875 argc, (const char **) argv,
23877 else if (*p == '\0')
23879 /* Complain, but do define it. */
23880 dwarf2_macro_malformed_definition_complaint (body);
23881 macro_define_function (file, line, name,
23882 argc, (const char **) argv,
23886 /* Just complain. */
23887 dwarf2_macro_malformed_definition_complaint (body);
23890 /* Just complain. */
23891 dwarf2_macro_malformed_definition_complaint (body);
23897 for (i = 0; i < argc; i++)
23903 dwarf2_macro_malformed_definition_complaint (body);
23906 /* Skip some bytes from BYTES according to the form given in FORM.
23907 Returns the new pointer. */
23909 static const gdb_byte *
23910 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
23911 enum dwarf_form form,
23912 unsigned int offset_size,
23913 struct dwarf2_section_info *section)
23915 unsigned int bytes_read;
23919 case DW_FORM_data1:
23924 case DW_FORM_data2:
23928 case DW_FORM_data4:
23932 case DW_FORM_data8:
23936 case DW_FORM_data16:
23940 case DW_FORM_string:
23941 read_direct_string (abfd, bytes, &bytes_read);
23942 bytes += bytes_read;
23945 case DW_FORM_sec_offset:
23947 case DW_FORM_GNU_strp_alt:
23948 bytes += offset_size;
23951 case DW_FORM_block:
23952 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23953 bytes += bytes_read;
23956 case DW_FORM_block1:
23957 bytes += 1 + read_1_byte (abfd, bytes);
23959 case DW_FORM_block2:
23960 bytes += 2 + read_2_bytes (abfd, bytes);
23962 case DW_FORM_block4:
23963 bytes += 4 + read_4_bytes (abfd, bytes);
23966 case DW_FORM_sdata:
23967 case DW_FORM_udata:
23968 case DW_FORM_GNU_addr_index:
23969 case DW_FORM_GNU_str_index:
23970 bytes = gdb_skip_leb128 (bytes, buffer_end);
23973 dwarf2_section_buffer_overflow_complaint (section);
23978 case DW_FORM_implicit_const:
23983 complaint (&symfile_complaints,
23984 _("invalid form 0x%x in `%s'"),
23985 form, get_section_name (section));
23993 /* A helper for dwarf_decode_macros that handles skipping an unknown
23994 opcode. Returns an updated pointer to the macro data buffer; or,
23995 on error, issues a complaint and returns NULL. */
23997 static const gdb_byte *
23998 skip_unknown_opcode (unsigned int opcode,
23999 const gdb_byte **opcode_definitions,
24000 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24002 unsigned int offset_size,
24003 struct dwarf2_section_info *section)
24005 unsigned int bytes_read, i;
24007 const gdb_byte *defn;
24009 if (opcode_definitions[opcode] == NULL)
24011 complaint (&symfile_complaints,
24012 _("unrecognized DW_MACFINO opcode 0x%x"),
24017 defn = opcode_definitions[opcode];
24018 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24019 defn += bytes_read;
24021 for (i = 0; i < arg; ++i)
24023 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24024 (enum dwarf_form) defn[i], offset_size,
24026 if (mac_ptr == NULL)
24028 /* skip_form_bytes already issued the complaint. */
24036 /* A helper function which parses the header of a macro section.
24037 If the macro section is the extended (for now called "GNU") type,
24038 then this updates *OFFSET_SIZE. Returns a pointer to just after
24039 the header, or issues a complaint and returns NULL on error. */
24041 static const gdb_byte *
24042 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24044 const gdb_byte *mac_ptr,
24045 unsigned int *offset_size,
24046 int section_is_gnu)
24048 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24050 if (section_is_gnu)
24052 unsigned int version, flags;
24054 version = read_2_bytes (abfd, mac_ptr);
24055 if (version != 4 && version != 5)
24057 complaint (&symfile_complaints,
24058 _("unrecognized version `%d' in .debug_macro section"),
24064 flags = read_1_byte (abfd, mac_ptr);
24066 *offset_size = (flags & 1) ? 8 : 4;
24068 if ((flags & 2) != 0)
24069 /* We don't need the line table offset. */
24070 mac_ptr += *offset_size;
24072 /* Vendor opcode descriptions. */
24073 if ((flags & 4) != 0)
24075 unsigned int i, count;
24077 count = read_1_byte (abfd, mac_ptr);
24079 for (i = 0; i < count; ++i)
24081 unsigned int opcode, bytes_read;
24084 opcode = read_1_byte (abfd, mac_ptr);
24086 opcode_definitions[opcode] = mac_ptr;
24087 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24088 mac_ptr += bytes_read;
24097 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24098 including DW_MACRO_import. */
24101 dwarf_decode_macro_bytes (bfd *abfd,
24102 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24103 struct macro_source_file *current_file,
24104 struct line_header *lh,
24105 struct dwarf2_section_info *section,
24106 int section_is_gnu, int section_is_dwz,
24107 unsigned int offset_size,
24108 htab_t include_hash)
24110 struct objfile *objfile = dwarf2_per_objfile->objfile;
24111 enum dwarf_macro_record_type macinfo_type;
24112 int at_commandline;
24113 const gdb_byte *opcode_definitions[256];
24115 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24116 &offset_size, section_is_gnu);
24117 if (mac_ptr == NULL)
24119 /* We already issued a complaint. */
24123 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24124 GDB is still reading the definitions from command line. First
24125 DW_MACINFO_start_file will need to be ignored as it was already executed
24126 to create CURRENT_FILE for the main source holding also the command line
24127 definitions. On first met DW_MACINFO_start_file this flag is reset to
24128 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24130 at_commandline = 1;
24134 /* Do we at least have room for a macinfo type byte? */
24135 if (mac_ptr >= mac_end)
24137 dwarf2_section_buffer_overflow_complaint (section);
24141 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24144 /* Note that we rely on the fact that the corresponding GNU and
24145 DWARF constants are the same. */
24146 switch (macinfo_type)
24148 /* A zero macinfo type indicates the end of the macro
24153 case DW_MACRO_define:
24154 case DW_MACRO_undef:
24155 case DW_MACRO_define_strp:
24156 case DW_MACRO_undef_strp:
24157 case DW_MACRO_define_sup:
24158 case DW_MACRO_undef_sup:
24160 unsigned int bytes_read;
24165 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24166 mac_ptr += bytes_read;
24168 if (macinfo_type == DW_MACRO_define
24169 || macinfo_type == DW_MACRO_undef)
24171 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24172 mac_ptr += bytes_read;
24176 LONGEST str_offset;
24178 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24179 mac_ptr += offset_size;
24181 if (macinfo_type == DW_MACRO_define_sup
24182 || macinfo_type == DW_MACRO_undef_sup
24185 struct dwz_file *dwz = dwarf2_get_dwz_file ();
24187 body = read_indirect_string_from_dwz (dwz, str_offset);
24190 body = read_indirect_string_at_offset (abfd, str_offset);
24193 is_define = (macinfo_type == DW_MACRO_define
24194 || macinfo_type == DW_MACRO_define_strp
24195 || macinfo_type == DW_MACRO_define_sup);
24196 if (! current_file)
24198 /* DWARF violation as no main source is present. */
24199 complaint (&symfile_complaints,
24200 _("debug info with no main source gives macro %s "
24202 is_define ? _("definition") : _("undefinition"),
24206 if ((line == 0 && !at_commandline)
24207 || (line != 0 && at_commandline))
24208 complaint (&symfile_complaints,
24209 _("debug info gives %s macro %s with %s line %d: %s"),
24210 at_commandline ? _("command-line") : _("in-file"),
24211 is_define ? _("definition") : _("undefinition"),
24212 line == 0 ? _("zero") : _("non-zero"), line, body);
24215 parse_macro_definition (current_file, line, body);
24218 gdb_assert (macinfo_type == DW_MACRO_undef
24219 || macinfo_type == DW_MACRO_undef_strp
24220 || macinfo_type == DW_MACRO_undef_sup);
24221 macro_undef (current_file, line, body);
24226 case DW_MACRO_start_file:
24228 unsigned int bytes_read;
24231 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24232 mac_ptr += bytes_read;
24233 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24234 mac_ptr += bytes_read;
24236 if ((line == 0 && !at_commandline)
24237 || (line != 0 && at_commandline))
24238 complaint (&symfile_complaints,
24239 _("debug info gives source %d included "
24240 "from %s at %s line %d"),
24241 file, at_commandline ? _("command-line") : _("file"),
24242 line == 0 ? _("zero") : _("non-zero"), line);
24244 if (at_commandline)
24246 /* This DW_MACRO_start_file was executed in the
24248 at_commandline = 0;
24251 current_file = macro_start_file (file, line, current_file, lh);
24255 case DW_MACRO_end_file:
24256 if (! current_file)
24257 complaint (&symfile_complaints,
24258 _("macro debug info has an unmatched "
24259 "`close_file' directive"));
24262 current_file = current_file->included_by;
24263 if (! current_file)
24265 enum dwarf_macro_record_type next_type;
24267 /* GCC circa March 2002 doesn't produce the zero
24268 type byte marking the end of the compilation
24269 unit. Complain if it's not there, but exit no
24272 /* Do we at least have room for a macinfo type byte? */
24273 if (mac_ptr >= mac_end)
24275 dwarf2_section_buffer_overflow_complaint (section);
24279 /* We don't increment mac_ptr here, so this is just
24282 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24284 if (next_type != 0)
24285 complaint (&symfile_complaints,
24286 _("no terminating 0-type entry for "
24287 "macros in `.debug_macinfo' section"));
24294 case DW_MACRO_import:
24295 case DW_MACRO_import_sup:
24299 bfd *include_bfd = abfd;
24300 struct dwarf2_section_info *include_section = section;
24301 const gdb_byte *include_mac_end = mac_end;
24302 int is_dwz = section_is_dwz;
24303 const gdb_byte *new_mac_ptr;
24305 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24306 mac_ptr += offset_size;
24308 if (macinfo_type == DW_MACRO_import_sup)
24310 struct dwz_file *dwz = dwarf2_get_dwz_file ();
24312 dwarf2_read_section (objfile, &dwz->macro);
24314 include_section = &dwz->macro;
24315 include_bfd = get_section_bfd_owner (include_section);
24316 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24320 new_mac_ptr = include_section->buffer + offset;
24321 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24325 /* This has actually happened; see
24326 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24327 complaint (&symfile_complaints,
24328 _("recursive DW_MACRO_import in "
24329 ".debug_macro section"));
24333 *slot = (void *) new_mac_ptr;
24335 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
24336 include_mac_end, current_file, lh,
24337 section, section_is_gnu, is_dwz,
24338 offset_size, include_hash);
24340 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24345 case DW_MACINFO_vendor_ext:
24346 if (!section_is_gnu)
24348 unsigned int bytes_read;
24350 /* This reads the constant, but since we don't recognize
24351 any vendor extensions, we ignore it. */
24352 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24353 mac_ptr += bytes_read;
24354 read_direct_string (abfd, mac_ptr, &bytes_read);
24355 mac_ptr += bytes_read;
24357 /* We don't recognize any vendor extensions. */
24363 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24364 mac_ptr, mac_end, abfd, offset_size,
24366 if (mac_ptr == NULL)
24370 } while (macinfo_type != 0);
24374 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24375 int section_is_gnu)
24377 struct objfile *objfile = dwarf2_per_objfile->objfile;
24378 struct line_header *lh = cu->line_header;
24380 const gdb_byte *mac_ptr, *mac_end;
24381 struct macro_source_file *current_file = 0;
24382 enum dwarf_macro_record_type macinfo_type;
24383 unsigned int offset_size = cu->header.offset_size;
24384 const gdb_byte *opcode_definitions[256];
24386 struct dwarf2_section_info *section;
24387 const char *section_name;
24389 if (cu->dwo_unit != NULL)
24391 if (section_is_gnu)
24393 section = &cu->dwo_unit->dwo_file->sections.macro;
24394 section_name = ".debug_macro.dwo";
24398 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24399 section_name = ".debug_macinfo.dwo";
24404 if (section_is_gnu)
24406 section = &dwarf2_per_objfile->macro;
24407 section_name = ".debug_macro";
24411 section = &dwarf2_per_objfile->macinfo;
24412 section_name = ".debug_macinfo";
24416 dwarf2_read_section (objfile, section);
24417 if (section->buffer == NULL)
24419 complaint (&symfile_complaints, _("missing %s section"), section_name);
24422 abfd = get_section_bfd_owner (section);
24424 /* First pass: Find the name of the base filename.
24425 This filename is needed in order to process all macros whose definition
24426 (or undefinition) comes from the command line. These macros are defined
24427 before the first DW_MACINFO_start_file entry, and yet still need to be
24428 associated to the base file.
24430 To determine the base file name, we scan the macro definitions until we
24431 reach the first DW_MACINFO_start_file entry. We then initialize
24432 CURRENT_FILE accordingly so that any macro definition found before the
24433 first DW_MACINFO_start_file can still be associated to the base file. */
24435 mac_ptr = section->buffer + offset;
24436 mac_end = section->buffer + section->size;
24438 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24439 &offset_size, section_is_gnu);
24440 if (mac_ptr == NULL)
24442 /* We already issued a complaint. */
24448 /* Do we at least have room for a macinfo type byte? */
24449 if (mac_ptr >= mac_end)
24451 /* Complaint is printed during the second pass as GDB will probably
24452 stop the first pass earlier upon finding
24453 DW_MACINFO_start_file. */
24457 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24460 /* Note that we rely on the fact that the corresponding GNU and
24461 DWARF constants are the same. */
24462 switch (macinfo_type)
24464 /* A zero macinfo type indicates the end of the macro
24469 case DW_MACRO_define:
24470 case DW_MACRO_undef:
24471 /* Only skip the data by MAC_PTR. */
24473 unsigned int bytes_read;
24475 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24476 mac_ptr += bytes_read;
24477 read_direct_string (abfd, mac_ptr, &bytes_read);
24478 mac_ptr += bytes_read;
24482 case DW_MACRO_start_file:
24484 unsigned int bytes_read;
24487 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24488 mac_ptr += bytes_read;
24489 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24490 mac_ptr += bytes_read;
24492 current_file = macro_start_file (file, line, current_file, lh);
24496 case DW_MACRO_end_file:
24497 /* No data to skip by MAC_PTR. */
24500 case DW_MACRO_define_strp:
24501 case DW_MACRO_undef_strp:
24502 case DW_MACRO_define_sup:
24503 case DW_MACRO_undef_sup:
24505 unsigned int bytes_read;
24507 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24508 mac_ptr += bytes_read;
24509 mac_ptr += offset_size;
24513 case DW_MACRO_import:
24514 case DW_MACRO_import_sup:
24515 /* Note that, according to the spec, a transparent include
24516 chain cannot call DW_MACRO_start_file. So, we can just
24517 skip this opcode. */
24518 mac_ptr += offset_size;
24521 case DW_MACINFO_vendor_ext:
24522 /* Only skip the data by MAC_PTR. */
24523 if (!section_is_gnu)
24525 unsigned int bytes_read;
24527 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24528 mac_ptr += bytes_read;
24529 read_direct_string (abfd, mac_ptr, &bytes_read);
24530 mac_ptr += bytes_read;
24535 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24536 mac_ptr, mac_end, abfd, offset_size,
24538 if (mac_ptr == NULL)
24542 } while (macinfo_type != 0 && current_file == NULL);
24544 /* Second pass: Process all entries.
24546 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24547 command-line macro definitions/undefinitions. This flag is unset when we
24548 reach the first DW_MACINFO_start_file entry. */
24550 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24552 NULL, xcalloc, xfree));
24553 mac_ptr = section->buffer + offset;
24554 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24555 *slot = (void *) mac_ptr;
24556 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
24557 current_file, lh, section,
24558 section_is_gnu, 0, offset_size,
24559 include_hash.get ());
24562 /* Check if the attribute's form is a DW_FORM_block*
24563 if so return true else false. */
24566 attr_form_is_block (const struct attribute *attr)
24568 return (attr == NULL ? 0 :
24569 attr->form == DW_FORM_block1
24570 || attr->form == DW_FORM_block2
24571 || attr->form == DW_FORM_block4
24572 || attr->form == DW_FORM_block
24573 || attr->form == DW_FORM_exprloc);
24576 /* Return non-zero if ATTR's value is a section offset --- classes
24577 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24578 You may use DW_UNSND (attr) to retrieve such offsets.
24580 Section 7.5.4, "Attribute Encodings", explains that no attribute
24581 may have a value that belongs to more than one of these classes; it
24582 would be ambiguous if we did, because we use the same forms for all
24586 attr_form_is_section_offset (const struct attribute *attr)
24588 return (attr->form == DW_FORM_data4
24589 || attr->form == DW_FORM_data8
24590 || attr->form == DW_FORM_sec_offset);
24593 /* Return non-zero if ATTR's value falls in the 'constant' class, or
24594 zero otherwise. When this function returns true, you can apply
24595 dwarf2_get_attr_constant_value to it.
24597 However, note that for some attributes you must check
24598 attr_form_is_section_offset before using this test. DW_FORM_data4
24599 and DW_FORM_data8 are members of both the constant class, and of
24600 the classes that contain offsets into other debug sections
24601 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24602 that, if an attribute's can be either a constant or one of the
24603 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
24604 taken as section offsets, not constants.
24606 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24607 cannot handle that. */
24610 attr_form_is_constant (const struct attribute *attr)
24612 switch (attr->form)
24614 case DW_FORM_sdata:
24615 case DW_FORM_udata:
24616 case DW_FORM_data1:
24617 case DW_FORM_data2:
24618 case DW_FORM_data4:
24619 case DW_FORM_data8:
24620 case DW_FORM_implicit_const:
24628 /* DW_ADDR is always stored already as sect_offset; despite for the forms
24629 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24632 attr_form_is_ref (const struct attribute *attr)
24634 switch (attr->form)
24636 case DW_FORM_ref_addr:
24641 case DW_FORM_ref_udata:
24642 case DW_FORM_GNU_ref_alt:
24649 /* Return the .debug_loc section to use for CU.
24650 For DWO files use .debug_loc.dwo. */
24652 static struct dwarf2_section_info *
24653 cu_debug_loc_section (struct dwarf2_cu *cu)
24657 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24659 return cu->header.version >= 5 ? §ions->loclists : §ions->loc;
24661 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24662 : &dwarf2_per_objfile->loc);
24665 /* A helper function that fills in a dwarf2_loclist_baton. */
24668 fill_in_loclist_baton (struct dwarf2_cu *cu,
24669 struct dwarf2_loclist_baton *baton,
24670 const struct attribute *attr)
24672 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24674 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
24676 baton->per_cu = cu->per_cu;
24677 gdb_assert (baton->per_cu);
24678 /* We don't know how long the location list is, but make sure we
24679 don't run off the edge of the section. */
24680 baton->size = section->size - DW_UNSND (attr);
24681 baton->data = section->buffer + DW_UNSND (attr);
24682 baton->base_address = cu->base_address;
24683 baton->from_dwo = cu->dwo_unit != NULL;
24687 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24688 struct dwarf2_cu *cu, int is_block)
24690 struct objfile *objfile = dwarf2_per_objfile->objfile;
24691 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24693 if (attr_form_is_section_offset (attr)
24694 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24695 the section. If so, fall through to the complaint in the
24697 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
24699 struct dwarf2_loclist_baton *baton;
24701 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24703 fill_in_loclist_baton (cu, baton, attr);
24705 if (cu->base_known == 0)
24706 complaint (&symfile_complaints,
24707 _("Location list used without "
24708 "specifying the CU base address."));
24710 SYMBOL_ACLASS_INDEX (sym) = (is_block
24711 ? dwarf2_loclist_block_index
24712 : dwarf2_loclist_index);
24713 SYMBOL_LOCATION_BATON (sym) = baton;
24717 struct dwarf2_locexpr_baton *baton;
24719 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24720 baton->per_cu = cu->per_cu;
24721 gdb_assert (baton->per_cu);
24723 if (attr_form_is_block (attr))
24725 /* Note that we're just copying the block's data pointer
24726 here, not the actual data. We're still pointing into the
24727 info_buffer for SYM's objfile; right now we never release
24728 that buffer, but when we do clean up properly this may
24730 baton->size = DW_BLOCK (attr)->size;
24731 baton->data = DW_BLOCK (attr)->data;
24735 dwarf2_invalid_attrib_class_complaint ("location description",
24736 SYMBOL_NATURAL_NAME (sym));
24740 SYMBOL_ACLASS_INDEX (sym) = (is_block
24741 ? dwarf2_locexpr_block_index
24742 : dwarf2_locexpr_index);
24743 SYMBOL_LOCATION_BATON (sym) = baton;
24747 /* Return the OBJFILE associated with the compilation unit CU. If CU
24748 came from a separate debuginfo file, then the master objfile is
24752 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24754 struct objfile *objfile = per_cu->objfile;
24756 /* Return the master objfile, so that we can report and look up the
24757 correct file containing this variable. */
24758 if (objfile->separate_debug_objfile_backlink)
24759 objfile = objfile->separate_debug_objfile_backlink;
24764 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24765 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24766 CU_HEADERP first. */
24768 static const struct comp_unit_head *
24769 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24770 struct dwarf2_per_cu_data *per_cu)
24772 const gdb_byte *info_ptr;
24775 return &per_cu->cu->header;
24777 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24779 memset (cu_headerp, 0, sizeof (*cu_headerp));
24780 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24781 rcuh_kind::COMPILE);
24786 /* Return the address size given in the compilation unit header for CU. */
24789 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24791 struct comp_unit_head cu_header_local;
24792 const struct comp_unit_head *cu_headerp;
24794 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24796 return cu_headerp->addr_size;
24799 /* Return the offset size given in the compilation unit header for CU. */
24802 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24804 struct comp_unit_head cu_header_local;
24805 const struct comp_unit_head *cu_headerp;
24807 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24809 return cu_headerp->offset_size;
24812 /* See its dwarf2loc.h declaration. */
24815 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24817 struct comp_unit_head cu_header_local;
24818 const struct comp_unit_head *cu_headerp;
24820 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24822 if (cu_headerp->version == 2)
24823 return cu_headerp->addr_size;
24825 return cu_headerp->offset_size;
24828 /* Return the text offset of the CU. The returned offset comes from
24829 this CU's objfile. If this objfile came from a separate debuginfo
24830 file, then the offset may be different from the corresponding
24831 offset in the parent objfile. */
24834 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24836 struct objfile *objfile = per_cu->objfile;
24838 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24841 /* Return DWARF version number of PER_CU. */
24844 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24846 return per_cu->dwarf_version;
24849 /* Locate the .debug_info compilation unit from CU's objfile which contains
24850 the DIE at OFFSET. Raises an error on failure. */
24852 static struct dwarf2_per_cu_data *
24853 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24854 unsigned int offset_in_dwz,
24855 struct objfile *objfile)
24857 struct dwarf2_per_cu_data *this_cu;
24859 const sect_offset *cu_off;
24862 high = dwarf2_per_objfile->n_comp_units - 1;
24865 struct dwarf2_per_cu_data *mid_cu;
24866 int mid = low + (high - low) / 2;
24868 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
24869 cu_off = &mid_cu->sect_off;
24870 if (mid_cu->is_dwz > offset_in_dwz
24871 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
24876 gdb_assert (low == high);
24877 this_cu = dwarf2_per_objfile->all_comp_units[low];
24878 cu_off = &this_cu->sect_off;
24879 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
24881 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24882 error (_("Dwarf Error: could not find partial DIE containing "
24883 "offset 0x%x [in module %s]"),
24884 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
24886 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24888 return dwarf2_per_objfile->all_comp_units[low-1];
24892 this_cu = dwarf2_per_objfile->all_comp_units[low];
24893 if (low == dwarf2_per_objfile->n_comp_units - 1
24894 && sect_off >= this_cu->sect_off + this_cu->length)
24895 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
24896 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24901 /* Initialize dwarf2_cu CU, owned by PER_CU. */
24904 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
24906 memset (cu, 0, sizeof (*cu));
24908 cu->per_cu = per_cu;
24909 cu->objfile = per_cu->objfile;
24910 obstack_init (&cu->comp_unit_obstack);
24913 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24916 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24917 enum language pretend_language)
24919 struct attribute *attr;
24921 /* Set the language we're debugging. */
24922 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24924 set_cu_language (DW_UNSND (attr), cu);
24927 cu->language = pretend_language;
24928 cu->language_defn = language_def (cu->language);
24931 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24934 /* Release one cached compilation unit, CU. We unlink it from the tree
24935 of compilation units, but we don't remove it from the read_in_chain;
24936 the caller is responsible for that.
24937 NOTE: DATA is a void * because this function is also used as a
24938 cleanup routine. */
24941 free_heap_comp_unit (void *data)
24943 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
24945 gdb_assert (cu->per_cu != NULL);
24946 cu->per_cu->cu = NULL;
24949 obstack_free (&cu->comp_unit_obstack, NULL);
24954 /* This cleanup function is passed the address of a dwarf2_cu on the stack
24955 when we're finished with it. We can't free the pointer itself, but be
24956 sure to unlink it from the cache. Also release any associated storage. */
24959 free_stack_comp_unit (void *data)
24961 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
24963 gdb_assert (cu->per_cu != NULL);
24964 cu->per_cu->cu = NULL;
24967 obstack_free (&cu->comp_unit_obstack, NULL);
24968 cu->partial_dies = NULL;
24971 /* Free all cached compilation units. */
24974 free_cached_comp_units (void *data)
24976 dwarf2_per_objfile->free_cached_comp_units ();
24979 /* Increase the age counter on each cached compilation unit, and free
24980 any that are too old. */
24983 age_cached_comp_units (void)
24985 struct dwarf2_per_cu_data *per_cu, **last_chain;
24987 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
24988 per_cu = dwarf2_per_objfile->read_in_chain;
24989 while (per_cu != NULL)
24991 per_cu->cu->last_used ++;
24992 if (per_cu->cu->last_used <= dwarf_max_cache_age)
24993 dwarf2_mark (per_cu->cu);
24994 per_cu = per_cu->cu->read_in_chain;
24997 per_cu = dwarf2_per_objfile->read_in_chain;
24998 last_chain = &dwarf2_per_objfile->read_in_chain;
24999 while (per_cu != NULL)
25001 struct dwarf2_per_cu_data *next_cu;
25003 next_cu = per_cu->cu->read_in_chain;
25005 if (!per_cu->cu->mark)
25007 free_heap_comp_unit (per_cu->cu);
25008 *last_chain = next_cu;
25011 last_chain = &per_cu->cu->read_in_chain;
25017 /* Remove a single compilation unit from the cache. */
25020 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25022 struct dwarf2_per_cu_data *per_cu, **last_chain;
25024 per_cu = dwarf2_per_objfile->read_in_chain;
25025 last_chain = &dwarf2_per_objfile->read_in_chain;
25026 while (per_cu != NULL)
25028 struct dwarf2_per_cu_data *next_cu;
25030 next_cu = per_cu->cu->read_in_chain;
25032 if (per_cu == target_per_cu)
25034 free_heap_comp_unit (per_cu->cu);
25036 *last_chain = next_cu;
25040 last_chain = &per_cu->cu->read_in_chain;
25046 /* Release all extra memory associated with OBJFILE. */
25049 dwarf2_free_objfile (struct objfile *objfile)
25052 = (struct dwarf2_per_objfile *) objfile_data (objfile,
25053 dwarf2_objfile_data_key);
25055 if (dwarf2_per_objfile == NULL)
25058 dwarf2_per_objfile->~dwarf2_per_objfile ();
25061 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25062 We store these in a hash table separate from the DIEs, and preserve them
25063 when the DIEs are flushed out of cache.
25065 The CU "per_cu" pointer is needed because offset alone is not enough to
25066 uniquely identify the type. A file may have multiple .debug_types sections,
25067 or the type may come from a DWO file. Furthermore, while it's more logical
25068 to use per_cu->section+offset, with Fission the section with the data is in
25069 the DWO file but we don't know that section at the point we need it.
25070 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25071 because we can enter the lookup routine, get_die_type_at_offset, from
25072 outside this file, and thus won't necessarily have PER_CU->cu.
25073 Fortunately, PER_CU is stable for the life of the objfile. */
25075 struct dwarf2_per_cu_offset_and_type
25077 const struct dwarf2_per_cu_data *per_cu;
25078 sect_offset sect_off;
25082 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25085 per_cu_offset_and_type_hash (const void *item)
25087 const struct dwarf2_per_cu_offset_and_type *ofs
25088 = (const struct dwarf2_per_cu_offset_and_type *) item;
25090 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25093 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25096 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25098 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25099 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25100 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25101 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25103 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25104 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25107 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25108 table if necessary. For convenience, return TYPE.
25110 The DIEs reading must have careful ordering to:
25111 * Not cause infite loops trying to read in DIEs as a prerequisite for
25112 reading current DIE.
25113 * Not trying to dereference contents of still incompletely read in types
25114 while reading in other DIEs.
25115 * Enable referencing still incompletely read in types just by a pointer to
25116 the type without accessing its fields.
25118 Therefore caller should follow these rules:
25119 * Try to fetch any prerequisite types we may need to build this DIE type
25120 before building the type and calling set_die_type.
25121 * After building type call set_die_type for current DIE as soon as
25122 possible before fetching more types to complete the current type.
25123 * Make the type as complete as possible before fetching more types. */
25125 static struct type *
25126 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25128 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25129 struct objfile *objfile = cu->objfile;
25130 struct attribute *attr;
25131 struct dynamic_prop prop;
25133 /* For Ada types, make sure that the gnat-specific data is always
25134 initialized (if not already set). There are a few types where
25135 we should not be doing so, because the type-specific area is
25136 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25137 where the type-specific area is used to store the floatformat).
25138 But this is not a problem, because the gnat-specific information
25139 is actually not needed for these types. */
25140 if (need_gnat_info (cu)
25141 && TYPE_CODE (type) != TYPE_CODE_FUNC
25142 && TYPE_CODE (type) != TYPE_CODE_FLT
25143 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25144 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25145 && TYPE_CODE (type) != TYPE_CODE_METHOD
25146 && !HAVE_GNAT_AUX_INFO (type))
25147 INIT_GNAT_SPECIFIC (type);
25149 /* Read DW_AT_allocated and set in type. */
25150 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25151 if (attr_form_is_block (attr))
25153 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25154 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
25156 else if (attr != NULL)
25158 complaint (&symfile_complaints,
25159 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
25160 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25161 to_underlying (die->sect_off));
25164 /* Read DW_AT_associated and set in type. */
25165 attr = dwarf2_attr (die, DW_AT_associated, cu);
25166 if (attr_form_is_block (attr))
25168 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25169 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
25171 else if (attr != NULL)
25173 complaint (&symfile_complaints,
25174 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
25175 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25176 to_underlying (die->sect_off));
25179 /* Read DW_AT_data_location and set in type. */
25180 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25181 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25182 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
25184 if (dwarf2_per_objfile->die_type_hash == NULL)
25186 dwarf2_per_objfile->die_type_hash =
25187 htab_create_alloc_ex (127,
25188 per_cu_offset_and_type_hash,
25189 per_cu_offset_and_type_eq,
25191 &objfile->objfile_obstack,
25192 hashtab_obstack_allocate,
25193 dummy_obstack_deallocate);
25196 ofs.per_cu = cu->per_cu;
25197 ofs.sect_off = die->sect_off;
25199 slot = (struct dwarf2_per_cu_offset_and_type **)
25200 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25202 complaint (&symfile_complaints,
25203 _("A problem internal to GDB: DIE 0x%x has type already set"),
25204 to_underlying (die->sect_off));
25205 *slot = XOBNEW (&objfile->objfile_obstack,
25206 struct dwarf2_per_cu_offset_and_type);
25211 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25212 or return NULL if the die does not have a saved type. */
25214 static struct type *
25215 get_die_type_at_offset (sect_offset sect_off,
25216 struct dwarf2_per_cu_data *per_cu)
25218 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25220 if (dwarf2_per_objfile->die_type_hash == NULL)
25223 ofs.per_cu = per_cu;
25224 ofs.sect_off = sect_off;
25225 slot = ((struct dwarf2_per_cu_offset_and_type *)
25226 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25233 /* Look up the type for DIE in CU in die_type_hash,
25234 or return NULL if DIE does not have a saved type. */
25236 static struct type *
25237 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25239 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25242 /* Add a dependence relationship from CU to REF_PER_CU. */
25245 dwarf2_add_dependence (struct dwarf2_cu *cu,
25246 struct dwarf2_per_cu_data *ref_per_cu)
25250 if (cu->dependencies == NULL)
25252 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25253 NULL, &cu->comp_unit_obstack,
25254 hashtab_obstack_allocate,
25255 dummy_obstack_deallocate);
25257 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25259 *slot = ref_per_cu;
25262 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25263 Set the mark field in every compilation unit in the
25264 cache that we must keep because we are keeping CU. */
25267 dwarf2_mark_helper (void **slot, void *data)
25269 struct dwarf2_per_cu_data *per_cu;
25271 per_cu = (struct dwarf2_per_cu_data *) *slot;
25273 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25274 reading of the chain. As such dependencies remain valid it is not much
25275 useful to track and undo them during QUIT cleanups. */
25276 if (per_cu->cu == NULL)
25279 if (per_cu->cu->mark)
25281 per_cu->cu->mark = 1;
25283 if (per_cu->cu->dependencies != NULL)
25284 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25289 /* Set the mark field in CU and in every other compilation unit in the
25290 cache that we must keep because we are keeping CU. */
25293 dwarf2_mark (struct dwarf2_cu *cu)
25298 if (cu->dependencies != NULL)
25299 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25303 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25307 per_cu->cu->mark = 0;
25308 per_cu = per_cu->cu->read_in_chain;
25312 /* Trivial hash function for partial_die_info: the hash value of a DIE
25313 is its offset in .debug_info for this objfile. */
25316 partial_die_hash (const void *item)
25318 const struct partial_die_info *part_die
25319 = (const struct partial_die_info *) item;
25321 return to_underlying (part_die->sect_off);
25324 /* Trivial comparison function for partial_die_info structures: two DIEs
25325 are equal if they have the same offset. */
25328 partial_die_eq (const void *item_lhs, const void *item_rhs)
25330 const struct partial_die_info *part_die_lhs
25331 = (const struct partial_die_info *) item_lhs;
25332 const struct partial_die_info *part_die_rhs
25333 = (const struct partial_die_info *) item_rhs;
25335 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25338 static struct cmd_list_element *set_dwarf_cmdlist;
25339 static struct cmd_list_element *show_dwarf_cmdlist;
25342 set_dwarf_cmd (const char *args, int from_tty)
25344 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25349 show_dwarf_cmd (const char *args, int from_tty)
25351 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25354 /* Free data associated with OBJFILE, if necessary. */
25357 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
25359 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
25362 /* Make sure we don't accidentally use dwarf2_per_objfile while
25364 dwarf2_per_objfile = NULL;
25366 for (ix = 0; ix < data->n_comp_units; ++ix)
25367 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
25369 for (ix = 0; ix < data->n_type_units; ++ix)
25370 VEC_free (dwarf2_per_cu_ptr,
25371 data->all_type_units[ix]->per_cu.imported_symtabs);
25372 xfree (data->all_type_units);
25374 VEC_free (dwarf2_section_info_def, data->types);
25376 if (data->dwo_files)
25377 free_dwo_files (data->dwo_files, objfile);
25378 if (data->dwp_file)
25379 gdb_bfd_unref (data->dwp_file->dbfd);
25381 if (data->dwz_file && data->dwz_file->dwz_bfd)
25382 gdb_bfd_unref (data->dwz_file->dwz_bfd);
25384 if (data->index_table != NULL)
25385 data->index_table->~mapped_index ();
25389 /* The "save gdb-index" command. */
25391 /* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
25395 file_write (FILE *file, const void *data, size_t size)
25397 if (fwrite (data, 1, size, file) != size)
25398 error (_("couldn't data write to file"));
25401 /* Write the contents of VEC to FILE, with error checking. */
25403 template<typename Elem, typename Alloc>
25405 file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
25407 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
25410 /* In-memory buffer to prepare data to be written later to a file. */
25414 /* Copy DATA to the end of the buffer. */
25415 template<typename T>
25416 void append_data (const T &data)
25418 std::copy (reinterpret_cast<const gdb_byte *> (&data),
25419 reinterpret_cast<const gdb_byte *> (&data + 1),
25420 grow (sizeof (data)));
25423 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
25424 terminating zero is appended too. */
25425 void append_cstr0 (const char *cstr)
25427 const size_t size = strlen (cstr) + 1;
25428 std::copy (cstr, cstr + size, grow (size));
25431 /* Store INPUT as ULEB128 to the end of buffer. */
25432 void append_unsigned_leb128 (ULONGEST input)
25436 gdb_byte output = input & 0x7f;
25440 append_data (output);
25446 /* Accept a host-format integer in VAL and append it to the buffer
25447 as a target-format integer which is LEN bytes long. */
25448 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
25450 ::store_unsigned_integer (grow (len), len, byte_order, val);
25453 /* Return the size of the buffer. */
25454 size_t size () const
25456 return m_vec.size ();
25459 /* Return true iff the buffer is empty. */
25460 bool empty () const
25462 return m_vec.empty ();
25465 /* Write the buffer to FILE. */
25466 void file_write (FILE *file) const
25468 ::file_write (file, m_vec);
25472 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
25473 the start of the new block. */
25474 gdb_byte *grow (size_t size)
25476 m_vec.resize (m_vec.size () + size);
25477 return &*m_vec.end () - size;
25480 gdb::byte_vector m_vec;
25483 /* An entry in the symbol table. */
25484 struct symtab_index_entry
25486 /* The name of the symbol. */
25488 /* The offset of the name in the constant pool. */
25489 offset_type index_offset;
25490 /* A sorted vector of the indices of all the CUs that hold an object
25492 std::vector<offset_type> cu_indices;
25495 /* The symbol table. This is a power-of-2-sized hash table. */
25496 struct mapped_symtab
25500 data.resize (1024);
25503 offset_type n_elements = 0;
25504 std::vector<symtab_index_entry> data;
25507 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
25510 Function is used only during write_hash_table so no index format backward
25511 compatibility is needed. */
25513 static symtab_index_entry &
25514 find_slot (struct mapped_symtab *symtab, const char *name)
25516 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
25518 index = hash & (symtab->data.size () - 1);
25519 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
25523 if (symtab->data[index].name == NULL
25524 || strcmp (name, symtab->data[index].name) == 0)
25525 return symtab->data[index];
25526 index = (index + step) & (symtab->data.size () - 1);
25530 /* Expand SYMTAB's hash table. */
25533 hash_expand (struct mapped_symtab *symtab)
25535 auto old_entries = std::move (symtab->data);
25537 symtab->data.clear ();
25538 symtab->data.resize (old_entries.size () * 2);
25540 for (auto &it : old_entries)
25541 if (it.name != NULL)
25543 auto &ref = find_slot (symtab, it.name);
25544 ref = std::move (it);
25548 /* Add an entry to SYMTAB. NAME is the name of the symbol.
25549 CU_INDEX is the index of the CU in which the symbol appears.
25550 IS_STATIC is one if the symbol is static, otherwise zero (global). */
25553 add_index_entry (struct mapped_symtab *symtab, const char *name,
25554 int is_static, gdb_index_symbol_kind kind,
25555 offset_type cu_index)
25557 offset_type cu_index_and_attrs;
25559 ++symtab->n_elements;
25560 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
25561 hash_expand (symtab);
25563 symtab_index_entry &slot = find_slot (symtab, name);
25564 if (slot.name == NULL)
25567 /* index_offset is set later. */
25570 cu_index_and_attrs = 0;
25571 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
25572 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
25573 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
25575 /* We don't want to record an index value twice as we want to avoid the
25577 We process all global symbols and then all static symbols
25578 (which would allow us to avoid the duplication by only having to check
25579 the last entry pushed), but a symbol could have multiple kinds in one CU.
25580 To keep things simple we don't worry about the duplication here and
25581 sort and uniqufy the list after we've processed all symbols. */
25582 slot.cu_indices.push_back (cu_index_and_attrs);
25585 /* Sort and remove duplicates of all symbols' cu_indices lists. */
25588 uniquify_cu_indices (struct mapped_symtab *symtab)
25590 for (auto &entry : symtab->data)
25592 if (entry.name != NULL && !entry.cu_indices.empty ())
25594 auto &cu_indices = entry.cu_indices;
25595 std::sort (cu_indices.begin (), cu_indices.end ());
25596 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
25597 cu_indices.erase (from, cu_indices.end ());
25602 /* A form of 'const char *' suitable for container keys. Only the
25603 pointer is stored. The strings themselves are compared, not the
25608 c_str_view (const char *cstr)
25612 bool operator== (const c_str_view &other) const
25614 return strcmp (m_cstr, other.m_cstr) == 0;
25617 /* Return the underlying C string. Note, the returned string is
25618 only a reference with lifetime of this object. */
25619 const char *c_str () const
25625 friend class c_str_view_hasher;
25626 const char *const m_cstr;
25629 /* A std::unordered_map::hasher for c_str_view that uses the right
25630 hash function for strings in a mapped index. */
25631 class c_str_view_hasher
25634 size_t operator () (const c_str_view &x) const
25636 return mapped_index_string_hash (INT_MAX, x.m_cstr);
25640 /* A std::unordered_map::hasher for std::vector<>. */
25641 template<typename T>
25642 class vector_hasher
25645 size_t operator () (const std::vector<T> &key) const
25647 return iterative_hash (key.data (),
25648 sizeof (key.front ()) * key.size (), 0);
25652 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
25653 constant pool entries going into the data buffer CPOOL. */
25656 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
25659 /* Elements are sorted vectors of the indices of all the CUs that
25660 hold an object of this name. */
25661 std::unordered_map<std::vector<offset_type>, offset_type,
25662 vector_hasher<offset_type>>
25665 /* We add all the index vectors to the constant pool first, to
25666 ensure alignment is ok. */
25667 for (symtab_index_entry &entry : symtab->data)
25669 if (entry.name == NULL)
25671 gdb_assert (entry.index_offset == 0);
25673 /* Finding before inserting is faster than always trying to
25674 insert, because inserting always allocates a node, does the
25675 lookup, and then destroys the new node if another node
25676 already had the same key. C++17 try_emplace will avoid
25679 = symbol_hash_table.find (entry.cu_indices);
25680 if (found != symbol_hash_table.end ())
25682 entry.index_offset = found->second;
25686 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
25687 entry.index_offset = cpool.size ();
25688 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
25689 for (const auto index : entry.cu_indices)
25690 cpool.append_data (MAYBE_SWAP (index));
25694 /* Now write out the hash table. */
25695 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
25696 for (const auto &entry : symtab->data)
25698 offset_type str_off, vec_off;
25700 if (entry.name != NULL)
25702 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
25703 if (insertpair.second)
25704 cpool.append_cstr0 (entry.name);
25705 str_off = insertpair.first->second;
25706 vec_off = entry.index_offset;
25710 /* While 0 is a valid constant pool index, it is not valid
25711 to have 0 for both offsets. */
25716 output.append_data (MAYBE_SWAP (str_off));
25717 output.append_data (MAYBE_SWAP (vec_off));
25721 typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
25723 /* Helper struct for building the address table. */
25724 struct addrmap_index_data
25726 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
25727 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
25730 struct objfile *objfile;
25731 data_buf &addr_vec;
25732 psym_index_map &cu_index_htab;
25734 /* Non-zero if the previous_* fields are valid.
25735 We can't write an entry until we see the next entry (since it is only then
25736 that we know the end of the entry). */
25737 int previous_valid;
25738 /* Index of the CU in the table of all CUs in the index file. */
25739 unsigned int previous_cu_index;
25740 /* Start address of the CU. */
25741 CORE_ADDR previous_cu_start;
25744 /* Write an address entry to ADDR_VEC. */
25747 add_address_entry (struct objfile *objfile, data_buf &addr_vec,
25748 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
25750 CORE_ADDR baseaddr;
25752 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25754 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
25755 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
25756 addr_vec.append_data (MAYBE_SWAP (cu_index));
25759 /* Worker function for traversing an addrmap to build the address table. */
25762 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
25764 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
25765 struct partial_symtab *pst = (struct partial_symtab *) obj;
25767 if (data->previous_valid)
25768 add_address_entry (data->objfile, data->addr_vec,
25769 data->previous_cu_start, start_addr,
25770 data->previous_cu_index);
25772 data->previous_cu_start = start_addr;
25775 const auto it = data->cu_index_htab.find (pst);
25776 gdb_assert (it != data->cu_index_htab.cend ());
25777 data->previous_cu_index = it->second;
25778 data->previous_valid = 1;
25781 data->previous_valid = 0;
25786 /* Write OBJFILE's address map to ADDR_VEC.
25787 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
25788 in the index file. */
25791 write_address_map (struct objfile *objfile, data_buf &addr_vec,
25792 psym_index_map &cu_index_htab)
25794 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
25796 /* When writing the address table, we have to cope with the fact that
25797 the addrmap iterator only provides the start of a region; we have to
25798 wait until the next invocation to get the start of the next region. */
25800 addrmap_index_data.objfile = objfile;
25801 addrmap_index_data.previous_valid = 0;
25803 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
25804 &addrmap_index_data);
25806 /* It's highly unlikely the last entry (end address = 0xff...ff)
25807 is valid, but we should still handle it.
25808 The end address is recorded as the start of the next region, but that
25809 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
25811 if (addrmap_index_data.previous_valid)
25812 add_address_entry (objfile, addr_vec,
25813 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
25814 addrmap_index_data.previous_cu_index);
25817 /* Return the symbol kind of PSYM. */
25819 static gdb_index_symbol_kind
25820 symbol_kind (struct partial_symbol *psym)
25822 domain_enum domain = PSYMBOL_DOMAIN (psym);
25823 enum address_class aclass = PSYMBOL_CLASS (psym);
25831 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
25833 return GDB_INDEX_SYMBOL_KIND_TYPE;
25835 case LOC_CONST_BYTES:
25836 case LOC_OPTIMIZED_OUT:
25838 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25840 /* Note: It's currently impossible to recognize psyms as enum values
25841 short of reading the type info. For now punt. */
25842 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25844 /* There are other LOC_FOO values that one might want to classify
25845 as variables, but dwarf2read.c doesn't currently use them. */
25846 return GDB_INDEX_SYMBOL_KIND_OTHER;
25848 case STRUCT_DOMAIN:
25849 return GDB_INDEX_SYMBOL_KIND_TYPE;
25851 return GDB_INDEX_SYMBOL_KIND_OTHER;
25855 /* Add a list of partial symbols to SYMTAB. */
25858 write_psymbols (struct mapped_symtab *symtab,
25859 std::unordered_set<partial_symbol *> &psyms_seen,
25860 struct partial_symbol **psymp,
25862 offset_type cu_index,
25865 for (; count-- > 0; ++psymp)
25867 struct partial_symbol *psym = *psymp;
25869 if (SYMBOL_LANGUAGE (psym) == language_ada)
25870 error (_("Ada is not currently supported by the index"));
25872 /* Only add a given psymbol once. */
25873 if (psyms_seen.insert (psym).second)
25875 gdb_index_symbol_kind kind = symbol_kind (psym);
25877 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
25878 is_static, kind, cu_index);
25883 /* A helper struct used when iterating over debug_types. */
25884 struct signatured_type_index_data
25886 signatured_type_index_data (data_buf &types_list_,
25887 std::unordered_set<partial_symbol *> &psyms_seen_)
25888 : types_list (types_list_), psyms_seen (psyms_seen_)
25891 struct objfile *objfile;
25892 struct mapped_symtab *symtab;
25893 data_buf &types_list;
25894 std::unordered_set<partial_symbol *> &psyms_seen;
25898 /* A helper function that writes a single signatured_type to an
25902 write_one_signatured_type (void **slot, void *d)
25904 struct signatured_type_index_data *info
25905 = (struct signatured_type_index_data *) d;
25906 struct signatured_type *entry = (struct signatured_type *) *slot;
25907 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
25909 write_psymbols (info->symtab,
25911 &info->objfile->global_psymbols[psymtab->globals_offset],
25912 psymtab->n_global_syms, info->cu_index,
25914 write_psymbols (info->symtab,
25916 &info->objfile->static_psymbols[psymtab->statics_offset],
25917 psymtab->n_static_syms, info->cu_index,
25920 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
25921 to_underlying (entry->per_cu.sect_off));
25922 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
25923 to_underlying (entry->type_offset_in_tu));
25924 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
25931 /* Recurse into all "included" dependencies and count their symbols as
25932 if they appeared in this psymtab. */
25935 recursively_count_psymbols (struct partial_symtab *psymtab,
25936 size_t &psyms_seen)
25938 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
25939 if (psymtab->dependencies[i]->user != NULL)
25940 recursively_count_psymbols (psymtab->dependencies[i],
25943 psyms_seen += psymtab->n_global_syms;
25944 psyms_seen += psymtab->n_static_syms;
25947 /* Recurse into all "included" dependencies and write their symbols as
25948 if they appeared in this psymtab. */
25951 recursively_write_psymbols (struct objfile *objfile,
25952 struct partial_symtab *psymtab,
25953 struct mapped_symtab *symtab,
25954 std::unordered_set<partial_symbol *> &psyms_seen,
25955 offset_type cu_index)
25959 for (i = 0; i < psymtab->number_of_dependencies; ++i)
25960 if (psymtab->dependencies[i]->user != NULL)
25961 recursively_write_psymbols (objfile, psymtab->dependencies[i],
25962 symtab, psyms_seen, cu_index);
25964 write_psymbols (symtab,
25966 &objfile->global_psymbols[psymtab->globals_offset],
25967 psymtab->n_global_syms, cu_index,
25969 write_psymbols (symtab,
25971 &objfile->static_psymbols[psymtab->statics_offset],
25972 psymtab->n_static_syms, cu_index,
25976 /* DWARF-5 .debug_names builder. */
25980 debug_names (bool is_dwarf64, bfd_endian dwarf5_byte_order)
25981 : m_dwarf5_byte_order (dwarf5_byte_order),
25982 m_dwarf32 (dwarf5_byte_order),
25983 m_dwarf64 (dwarf5_byte_order),
25984 m_dwarf (is_dwarf64
25985 ? static_cast<dwarf &> (m_dwarf64)
25986 : static_cast<dwarf &> (m_dwarf32)),
25987 m_name_table_string_offs (m_dwarf.name_table_string_offs),
25988 m_name_table_entry_offs (m_dwarf.name_table_entry_offs)
25991 /* Insert one symbol. */
25992 void insert (const partial_symbol *psym, int cu_index, bool is_static)
25994 const int dwarf_tag = psymbol_tag (psym);
25995 if (dwarf_tag == 0)
25997 const char *const name = SYMBOL_SEARCH_NAME (psym);
25998 const auto insertpair
25999 = m_name_to_value_set.emplace (c_str_view (name),
26000 std::set<symbol_value> ());
26001 std::set<symbol_value> &value_set = insertpair.first->second;
26002 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static));
26005 /* Build all the tables. All symbols must be already inserted.
26006 This function does not call file_write, caller has to do it
26010 /* Verify the build method has not be called twice. */
26011 gdb_assert (m_abbrev_table.empty ());
26012 const size_t name_count = m_name_to_value_set.size ();
26013 m_bucket_table.resize
26014 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
26015 m_hash_table.reserve (name_count);
26016 m_name_table_string_offs.reserve (name_count);
26017 m_name_table_entry_offs.reserve (name_count);
26019 /* Map each hash of symbol to its name and value. */
26020 struct hash_it_pair
26023 decltype (m_name_to_value_set)::const_iterator it;
26025 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
26026 bucket_hash.resize (m_bucket_table.size ());
26027 for (decltype (m_name_to_value_set)::const_iterator it
26028 = m_name_to_value_set.cbegin ();
26029 it != m_name_to_value_set.cend ();
26032 const char *const name = it->first.c_str ();
26033 const uint32_t hash = dwarf5_djb_hash (name);
26034 hash_it_pair hashitpair;
26035 hashitpair.hash = hash;
26036 hashitpair.it = it;
26037 auto &slot = bucket_hash[hash % bucket_hash.size()];
26038 slot.push_front (std::move (hashitpair));
26040 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
26042 const std::forward_list<hash_it_pair> &hashitlist
26043 = bucket_hash[bucket_ix];
26044 if (hashitlist.empty ())
26046 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
26047 /* The hashes array is indexed starting at 1. */
26048 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
26049 sizeof (bucket_slot), m_dwarf5_byte_order,
26050 m_hash_table.size () + 1);
26051 for (const hash_it_pair &hashitpair : hashitlist)
26053 m_hash_table.push_back (0);
26054 store_unsigned_integer (reinterpret_cast<gdb_byte *>
26055 (&m_hash_table.back ()),
26056 sizeof (m_hash_table.back ()),
26057 m_dwarf5_byte_order, hashitpair.hash);
26058 const c_str_view &name = hashitpair.it->first;
26059 const std::set<symbol_value> &value_set = hashitpair.it->second;
26060 m_name_table_string_offs.push_back_reorder
26061 (m_debugstrlookup.lookup (name.c_str ()));
26062 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
26063 gdb_assert (!value_set.empty ());
26064 for (const symbol_value &value : value_set)
26066 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
26070 idx = m_idx_next++;
26071 m_abbrev_table.append_unsigned_leb128 (idx);
26072 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
26073 m_abbrev_table.append_unsigned_leb128 (DW_IDX_compile_unit);
26074 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
26075 m_abbrev_table.append_unsigned_leb128 (value.is_static
26076 ? DW_IDX_GNU_internal
26077 : DW_IDX_GNU_external);
26078 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
26080 /* Terminate attributes list. */
26081 m_abbrev_table.append_unsigned_leb128 (0);
26082 m_abbrev_table.append_unsigned_leb128 (0);
26085 m_entry_pool.append_unsigned_leb128 (idx);
26086 m_entry_pool.append_unsigned_leb128 (value.cu_index);
26089 /* Terminate the list of CUs. */
26090 m_entry_pool.append_unsigned_leb128 (0);
26093 gdb_assert (m_hash_table.size () == name_count);
26095 /* Terminate tags list. */
26096 m_abbrev_table.append_unsigned_leb128 (0);
26099 /* Return .debug_names bucket count. This must be called only after
26100 calling the build method. */
26101 uint32_t bucket_count () const
26103 /* Verify the build method has been already called. */
26104 gdb_assert (!m_abbrev_table.empty ());
26105 const uint32_t retval = m_bucket_table.size ();
26107 /* Check for overflow. */
26108 gdb_assert (retval == m_bucket_table.size ());
26112 /* Return .debug_names names count. This must be called only after
26113 calling the build method. */
26114 uint32_t name_count () const
26116 /* Verify the build method has been already called. */
26117 gdb_assert (!m_abbrev_table.empty ());
26118 const uint32_t retval = m_hash_table.size ();
26120 /* Check for overflow. */
26121 gdb_assert (retval == m_hash_table.size ());
26125 /* Return number of bytes of .debug_names abbreviation table. This
26126 must be called only after calling the build method. */
26127 uint32_t abbrev_table_bytes () const
26129 gdb_assert (!m_abbrev_table.empty ());
26130 return m_abbrev_table.size ();
26133 /* Recurse into all "included" dependencies and store their symbols
26134 as if they appeared in this psymtab. */
26135 void recursively_write_psymbols
26136 (struct objfile *objfile,
26137 struct partial_symtab *psymtab,
26138 std::unordered_set<partial_symbol *> &psyms_seen,
26141 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26142 if (psymtab->dependencies[i]->user != NULL)
26143 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26144 psyms_seen, cu_index);
26146 write_psymbols (psyms_seen,
26147 &objfile->global_psymbols[psymtab->globals_offset],
26148 psymtab->n_global_syms, cu_index, false);
26149 write_psymbols (psyms_seen,
26150 &objfile->static_psymbols[psymtab->statics_offset],
26151 psymtab->n_static_syms, cu_index, true);
26154 /* Return number of bytes the .debug_names section will have. This
26155 must be called only after calling the build method. */
26156 size_t bytes () const
26158 /* Verify the build method has been already called. */
26159 gdb_assert (!m_abbrev_table.empty ());
26160 size_t expected_bytes = 0;
26161 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
26162 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
26163 expected_bytes += m_name_table_string_offs.bytes ();
26164 expected_bytes += m_name_table_entry_offs.bytes ();
26165 expected_bytes += m_abbrev_table.size ();
26166 expected_bytes += m_entry_pool.size ();
26167 return expected_bytes;
26170 /* Write .debug_names to FILE_NAMES and .debug_str addition to
26171 FILE_STR. This must be called only after calling the build
26173 void file_write (FILE *file_names, FILE *file_str) const
26175 /* Verify the build method has been already called. */
26176 gdb_assert (!m_abbrev_table.empty ());
26177 ::file_write (file_names, m_bucket_table);
26178 ::file_write (file_names, m_hash_table);
26179 m_name_table_string_offs.file_write (file_names);
26180 m_name_table_entry_offs.file_write (file_names);
26181 m_abbrev_table.file_write (file_names);
26182 m_entry_pool.file_write (file_names);
26183 m_debugstrlookup.file_write (file_str);
26188 /* Storage for symbol names mapping them to their .debug_str section
26190 class debug_str_lookup
26194 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
26195 All .debug_str section strings are automatically stored. */
26196 debug_str_lookup ()
26197 : m_abfd (dwarf2_per_objfile->objfile->obfd)
26199 dwarf2_read_section (dwarf2_per_objfile->objfile,
26200 &dwarf2_per_objfile->str);
26201 if (dwarf2_per_objfile->str.buffer == NULL)
26203 for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
26204 data < (dwarf2_per_objfile->str.buffer
26205 + dwarf2_per_objfile->str.size);)
26207 const char *const s = reinterpret_cast<const char *> (data);
26208 const auto insertpair
26209 = m_str_table.emplace (c_str_view (s),
26210 data - dwarf2_per_objfile->str.buffer);
26211 if (!insertpair.second)
26212 complaint (&symfile_complaints,
26213 _("Duplicate string \"%s\" in "
26214 ".debug_str section [in module %s]"),
26215 s, bfd_get_filename (m_abfd));
26216 data += strlen (s) + 1;
26220 /* Return offset of symbol name S in the .debug_str section. Add
26221 such symbol to the section's end if it does not exist there
26223 size_t lookup (const char *s)
26225 const auto it = m_str_table.find (c_str_view (s));
26226 if (it != m_str_table.end ())
26228 const size_t offset = (dwarf2_per_objfile->str.size
26229 + m_str_add_buf.size ());
26230 m_str_table.emplace (c_str_view (s), offset);
26231 m_str_add_buf.append_cstr0 (s);
26235 /* Append the end of the .debug_str section to FILE. */
26236 void file_write (FILE *file) const
26238 m_str_add_buf.file_write (file);
26242 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
26245 /* Data to add at the end of .debug_str for new needed symbol names. */
26246 data_buf m_str_add_buf;
26249 /* Container to map used DWARF tags to their .debug_names abbreviation
26254 index_key (int dwarf_tag_, bool is_static_)
26255 : dwarf_tag (dwarf_tag_), is_static (is_static_)
26260 operator== (const index_key &other) const
26262 return dwarf_tag == other.dwarf_tag && is_static == other.is_static;
26265 const int dwarf_tag;
26266 const bool is_static;
26269 /* Provide std::unordered_map::hasher for index_key. */
26270 class index_key_hasher
26274 operator () (const index_key &key) const
26276 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
26280 /* Parameters of one symbol entry. */
26284 const int dwarf_tag, cu_index;
26285 const bool is_static;
26287 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_)
26288 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_)
26292 operator< (const symbol_value &other) const
26311 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
26316 const bfd_endian dwarf5_byte_order;
26318 explicit offset_vec (bfd_endian dwarf5_byte_order_)
26319 : dwarf5_byte_order (dwarf5_byte_order_)
26322 /* Call std::vector::reserve for NELEM elements. */
26323 virtual void reserve (size_t nelem) = 0;
26325 /* Call std::vector::push_back with store_unsigned_integer byte
26326 reordering for ELEM. */
26327 virtual void push_back_reorder (size_t elem) = 0;
26329 /* Return expected output size in bytes. */
26330 virtual size_t bytes () const = 0;
26332 /* Write name table to FILE. */
26333 virtual void file_write (FILE *file) const = 0;
26336 /* Template to unify DWARF-32 and DWARF-64 output. */
26337 template<typename OffsetSize>
26338 class offset_vec_tmpl : public offset_vec
26341 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
26342 : offset_vec (dwarf5_byte_order_)
26345 /* Implement offset_vec::reserve. */
26346 void reserve (size_t nelem) override
26348 m_vec.reserve (nelem);
26351 /* Implement offset_vec::push_back_reorder. */
26352 void push_back_reorder (size_t elem) override
26354 m_vec.push_back (elem);
26355 /* Check for overflow. */
26356 gdb_assert (m_vec.back () == elem);
26357 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
26358 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
26361 /* Implement offset_vec::bytes. */
26362 size_t bytes () const override
26364 return m_vec.size () * sizeof (m_vec[0]);
26367 /* Implement offset_vec::file_write. */
26368 void file_write (FILE *file) const override
26370 ::file_write (file, m_vec);
26374 std::vector<OffsetSize> m_vec;
26377 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
26378 respecting name table width. */
26382 offset_vec &name_table_string_offs, &name_table_entry_offs;
26384 dwarf (offset_vec &name_table_string_offs_,
26385 offset_vec &name_table_entry_offs_)
26386 : name_table_string_offs (name_table_string_offs_),
26387 name_table_entry_offs (name_table_entry_offs_)
26392 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
26393 respecting name table width. */
26394 template<typename OffsetSize>
26395 class dwarf_tmpl : public dwarf
26398 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
26399 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
26400 m_name_table_string_offs (dwarf5_byte_order_),
26401 m_name_table_entry_offs (dwarf5_byte_order_)
26405 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
26406 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
26409 /* Try to reconstruct original DWARF tag for given partial_symbol.
26410 This function is not DWARF-5 compliant but it is sufficient for
26411 GDB as a DWARF-5 index consumer. */
26412 static int psymbol_tag (const struct partial_symbol *psym)
26414 domain_enum domain = PSYMBOL_DOMAIN (psym);
26415 enum address_class aclass = PSYMBOL_CLASS (psym);
26423 return DW_TAG_subprogram;
26425 return DW_TAG_typedef;
26427 case LOC_CONST_BYTES:
26428 case LOC_OPTIMIZED_OUT:
26430 return DW_TAG_variable;
26432 /* Note: It's currently impossible to recognize psyms as enum values
26433 short of reading the type info. For now punt. */
26434 return DW_TAG_variable;
26436 /* There are other LOC_FOO values that one might want to classify
26437 as variables, but dwarf2read.c doesn't currently use them. */
26438 return DW_TAG_variable;
26440 case STRUCT_DOMAIN:
26441 return DW_TAG_structure_type;
26447 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
26448 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
26449 struct partial_symbol **psymp, int count, int cu_index,
26452 for (; count-- > 0; ++psymp)
26454 struct partial_symbol *psym = *psymp;
26456 if (SYMBOL_LANGUAGE (psym) == language_ada)
26457 error (_("Ada is not currently supported by the index"));
26459 /* Only add a given psymbol once. */
26460 if (psyms_seen.insert (psym).second)
26461 insert (psym, cu_index, is_static);
26465 /* Store value of each symbol. */
26466 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
26467 m_name_to_value_set;
26469 /* Tables of DWARF-5 .debug_names. They are in object file byte
26471 std::vector<uint32_t> m_bucket_table;
26472 std::vector<uint32_t> m_hash_table;
26474 const bfd_endian m_dwarf5_byte_order;
26475 dwarf_tmpl<uint32_t> m_dwarf32;
26476 dwarf_tmpl<uint64_t> m_dwarf64;
26478 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
26479 debug_str_lookup m_debugstrlookup;
26481 /* Map each used .debug_names abbreviation tag parameter to its
26483 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
26485 /* Next unused .debug_names abbreviation tag for
26486 m_indexkey_to_idx. */
26487 int m_idx_next = 1;
26489 /* .debug_names abbreviation table. */
26490 data_buf m_abbrev_table;
26492 /* .debug_names entry pool. */
26493 data_buf m_entry_pool;
26496 /* Return iff any of the needed offsets does not fit into 32-bit
26497 .debug_names section. */
26500 check_dwarf64_offsets ()
26502 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26504 const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
26506 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26509 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
26511 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
26512 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
26514 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26520 /* The psyms_seen set is potentially going to be largish (~40k
26521 elements when indexing a -g3 build of GDB itself). Estimate the
26522 number of elements in order to avoid too many rehashes, which
26523 require rebuilding buckets and thus many trips to
26529 size_t psyms_count = 0;
26530 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26532 struct dwarf2_per_cu_data *per_cu
26533 = dwarf2_per_objfile->all_comp_units[i];
26534 struct partial_symtab *psymtab = per_cu->v.psymtab;
26536 if (psymtab != NULL && psymtab->user == NULL)
26537 recursively_count_psymbols (psymtab, psyms_count);
26539 /* Generating an index for gdb itself shows a ratio of
26540 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
26541 return psyms_count / 4;
26544 /* Write new .gdb_index section for OBJFILE into OUT_FILE.
26545 Return how many bytes were expected to be written into OUT_FILE. */
26548 write_gdbindex (struct objfile *objfile, FILE *out_file)
26550 mapped_symtab symtab;
26553 /* While we're scanning CU's create a table that maps a psymtab pointer
26554 (which is what addrmap records) to its index (which is what is recorded
26555 in the index file). This will later be needed to write the address
26557 psym_index_map cu_index_htab;
26558 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
26560 /* The CU list is already sorted, so we don't need to do additional
26561 work here. Also, the debug_types entries do not appear in
26562 all_comp_units, but only in their own hash table. */
26564 std::unordered_set<partial_symbol *> psyms_seen (psyms_seen_size ());
26565 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26567 struct dwarf2_per_cu_data *per_cu
26568 = dwarf2_per_objfile->all_comp_units[i];
26569 struct partial_symtab *psymtab = per_cu->v.psymtab;
26571 /* CU of a shared file from 'dwz -m' may be unused by this main file.
26572 It may be referenced from a local scope but in such case it does not
26573 need to be present in .gdb_index. */
26574 if (psymtab == NULL)
26577 if (psymtab->user == NULL)
26578 recursively_write_psymbols (objfile, psymtab, &symtab,
26581 const auto insertpair = cu_index_htab.emplace (psymtab, i);
26582 gdb_assert (insertpair.second);
26584 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
26585 to_underlying (per_cu->sect_off));
26586 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
26589 /* Dump the address map. */
26591 write_address_map (objfile, addr_vec, cu_index_htab);
26593 /* Write out the .debug_type entries, if any. */
26594 data_buf types_cu_list;
26595 if (dwarf2_per_objfile->signatured_types)
26597 signatured_type_index_data sig_data (types_cu_list,
26600 sig_data.objfile = objfile;
26601 sig_data.symtab = &symtab;
26602 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
26603 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26604 write_one_signatured_type, &sig_data);
26607 /* Now that we've processed all symbols we can shrink their cu_indices
26609 uniquify_cu_indices (&symtab);
26611 data_buf symtab_vec, constant_pool;
26612 write_hash_table (&symtab, symtab_vec, constant_pool);
26615 const offset_type size_of_contents = 6 * sizeof (offset_type);
26616 offset_type total_len = size_of_contents;
26618 /* The version number. */
26619 contents.append_data (MAYBE_SWAP (8));
26621 /* The offset of the CU list from the start of the file. */
26622 contents.append_data (MAYBE_SWAP (total_len));
26623 total_len += cu_list.size ();
26625 /* The offset of the types CU list from the start of the file. */
26626 contents.append_data (MAYBE_SWAP (total_len));
26627 total_len += types_cu_list.size ();
26629 /* The offset of the address table from the start of the file. */
26630 contents.append_data (MAYBE_SWAP (total_len));
26631 total_len += addr_vec.size ();
26633 /* The offset of the symbol table from the start of the file. */
26634 contents.append_data (MAYBE_SWAP (total_len));
26635 total_len += symtab_vec.size ();
26637 /* The offset of the constant pool from the start of the file. */
26638 contents.append_data (MAYBE_SWAP (total_len));
26639 total_len += constant_pool.size ();
26641 gdb_assert (contents.size () == size_of_contents);
26643 contents.file_write (out_file);
26644 cu_list.file_write (out_file);
26645 types_cu_list.file_write (out_file);
26646 addr_vec.file_write (out_file);
26647 symtab_vec.file_write (out_file);
26648 constant_pool.file_write (out_file);
26653 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
26654 static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
26656 /* Write a new .debug_names section for OBJFILE into OUT_FILE, write
26657 needed addition to .debug_str section to OUT_FILE_STR. Return how
26658 many bytes were expected to be written into OUT_FILE. */
26661 write_debug_names (struct objfile *objfile, FILE *out_file, FILE *out_file_str)
26663 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets ();
26664 const int dwarf5_offset_size = dwarf5_is_dwarf64 ? 8 : 4;
26665 const enum bfd_endian dwarf5_byte_order
26666 = gdbarch_byte_order (get_objfile_arch (objfile));
26668 /* The CU list is already sorted, so we don't need to do additional
26669 work here. Also, the debug_types entries do not appear in
26670 all_comp_units, but only in their own hash table. */
26672 debug_names nametable (dwarf5_is_dwarf64, dwarf5_byte_order);
26673 std::unordered_set<partial_symbol *> psyms_seen (psyms_seen_size ());
26674 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26676 const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
26677 partial_symtab *psymtab = per_cu->v.psymtab;
26679 /* CU of a shared file from 'dwz -m' may be unused by this main
26680 file. It may be referenced from a local scope but in such
26681 case it does not need to be present in .debug_names. */
26682 if (psymtab == NULL)
26685 if (psymtab->user == NULL)
26686 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
26688 cu_list.append_uint (dwarf5_offset_size, dwarf5_byte_order,
26689 to_underlying (per_cu->sect_off));
26691 nametable.build ();
26693 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
26695 data_buf types_cu_list;
26696 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
26698 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
26699 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
26701 types_cu_list.append_uint (dwarf5_offset_size, dwarf5_byte_order,
26702 to_underlying (per_cu.sect_off));
26705 const offset_type bytes_of_header
26706 = ((dwarf5_is_dwarf64 ? 12 : 4)
26708 + sizeof (dwarf5_gdb_augmentation));
26709 size_t expected_bytes = 0;
26710 expected_bytes += bytes_of_header;
26711 expected_bytes += cu_list.size ();
26712 expected_bytes += types_cu_list.size ();
26713 expected_bytes += nametable.bytes ();
26716 if (!dwarf5_is_dwarf64)
26718 const uint64_t size64 = expected_bytes - 4;
26719 gdb_assert (size64 < 0xfffffff0);
26720 header.append_uint (4, dwarf5_byte_order, size64);
26724 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
26725 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
26728 /* The version number. */
26729 header.append_uint (2, dwarf5_byte_order, 5);
26732 header.append_uint (2, dwarf5_byte_order, 0);
26734 /* comp_unit_count - The number of CUs in the CU list. */
26735 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
26737 /* local_type_unit_count - The number of TUs in the local TU
26739 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
26741 /* foreign_type_unit_count - The number of TUs in the foreign TU
26743 header.append_uint (4, dwarf5_byte_order, 0);
26745 /* bucket_count - The number of hash buckets in the hash lookup
26747 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
26749 /* name_count - The number of unique names in the index. */
26750 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
26752 /* abbrev_table_size - The size in bytes of the abbreviations
26754 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
26756 /* augmentation_string_size - The size in bytes of the augmentation
26757 string. This value is rounded up to a multiple of 4. */
26758 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
26759 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
26760 header.append_data (dwarf5_gdb_augmentation);
26762 gdb_assert (header.size () == bytes_of_header);
26764 header.file_write (out_file);
26765 cu_list.file_write (out_file);
26766 types_cu_list.file_write (out_file);
26767 nametable.file_write (out_file, out_file_str);
26769 return expected_bytes;
26772 /* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
26773 position is at the end of the file. */
26776 assert_file_size (FILE *file, const char *filename, size_t expected_size)
26778 const auto file_size = ftell (file);
26779 if (file_size == -1)
26780 error (_("Can't get `%s' size"), filename);
26781 gdb_assert (file_size == expected_size);
26784 /* An index variant. */
26787 /* GDB's own .gdb_index format. */
26790 /* DWARF5 .debug_names. */
26794 /* Create an index file for OBJFILE in the directory DIR. */
26797 write_psymtabs_to_index (struct objfile *objfile, const char *dir,
26798 dw_index_kind index_kind)
26800 if (dwarf2_per_objfile->using_index)
26801 error (_("Cannot use an index to create the index"));
26803 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
26804 error (_("Cannot make an index when the file has multiple .debug_types sections"));
26806 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
26810 if (stat (objfile_name (objfile), &st) < 0)
26811 perror_with_name (objfile_name (objfile));
26813 std::string filename (std::string (dir) + SLASH_STRING
26814 + lbasename (objfile_name (objfile))
26815 + (index_kind == dw_index_kind::DEBUG_NAMES
26816 ? INDEX5_SUFFIX : INDEX4_SUFFIX));
26818 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
26820 error (_("Can't open `%s' for writing"), filename.c_str ());
26822 /* Order matters here; we want FILE to be closed before FILENAME is
26823 unlinked, because on MS-Windows one cannot delete a file that is
26824 still open. (Don't call anything here that might throw until
26825 file_closer is created.) */
26826 gdb::unlinker unlink_file (filename.c_str ());
26827 gdb_file_up close_out_file (out_file);
26829 if (index_kind == dw_index_kind::DEBUG_NAMES)
26831 std::string filename_str (std::string (dir) + SLASH_STRING
26832 + lbasename (objfile_name (objfile))
26833 + DEBUG_STR_SUFFIX);
26835 = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
26837 error (_("Can't open `%s' for writing"), filename_str.c_str ());
26838 gdb::unlinker unlink_file_str (filename_str.c_str ());
26839 gdb_file_up close_out_file_str (out_file_str);
26841 const size_t total_len
26842 = write_debug_names (objfile, out_file, out_file_str);
26843 assert_file_size (out_file, filename.c_str (), total_len);
26845 /* We want to keep the file .debug_str file too. */
26846 unlink_file_str.keep ();
26850 const size_t total_len
26851 = write_gdbindex (objfile, out_file);
26852 assert_file_size (out_file, filename.c_str (), total_len);
26855 /* We want to keep the file. */
26856 unlink_file.keep ();
26859 /* Implementation of the `save gdb-index' command.
26861 Note that the .gdb_index file format used by this command is
26862 documented in the GDB manual. Any changes here must be documented
26866 save_gdb_index_command (const char *arg, int from_tty)
26868 struct objfile *objfile;
26869 const char dwarf5space[] = "-dwarf-5 ";
26870 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
26875 arg = skip_spaces (arg);
26876 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
26878 index_kind = dw_index_kind::DEBUG_NAMES;
26879 arg += strlen (dwarf5space);
26880 arg = skip_spaces (arg);
26884 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
26886 ALL_OBJFILES (objfile)
26890 /* If the objfile does not correspond to an actual file, skip it. */
26891 if (stat (objfile_name (objfile), &st) < 0)
26895 = (struct dwarf2_per_objfile *) objfile_data (objfile,
26896 dwarf2_objfile_data_key);
26897 if (dwarf2_per_objfile)
26902 write_psymtabs_to_index (objfile, arg, index_kind);
26904 CATCH (except, RETURN_MASK_ERROR)
26906 exception_fprintf (gdb_stderr, except,
26907 _("Error while writing index for `%s': "),
26908 objfile_name (objfile));
26917 int dwarf_always_disassemble;
26920 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
26921 struct cmd_list_element *c, const char *value)
26923 fprintf_filtered (file,
26924 _("Whether to always disassemble "
26925 "DWARF expressions is %s.\n"),
26930 show_check_physname (struct ui_file *file, int from_tty,
26931 struct cmd_list_element *c, const char *value)
26933 fprintf_filtered (file,
26934 _("Whether to check \"physname\" is %s.\n"),
26939 _initialize_dwarf2_read (void)
26941 struct cmd_list_element *c;
26943 dwarf2_objfile_data_key
26944 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
26946 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
26947 Set DWARF specific variables.\n\
26948 Configure DWARF variables such as the cache size"),
26949 &set_dwarf_cmdlist, "maintenance set dwarf ",
26950 0/*allow-unknown*/, &maintenance_set_cmdlist);
26952 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
26953 Show DWARF specific variables\n\
26954 Show DWARF variables such as the cache size"),
26955 &show_dwarf_cmdlist, "maintenance show dwarf ",
26956 0/*allow-unknown*/, &maintenance_show_cmdlist);
26958 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
26959 &dwarf_max_cache_age, _("\
26960 Set the upper bound on the age of cached DWARF compilation units."), _("\
26961 Show the upper bound on the age of cached DWARF compilation units."), _("\
26962 A higher limit means that cached compilation units will be stored\n\
26963 in memory longer, and more total memory will be used. Zero disables\n\
26964 caching, which can slow down startup."),
26966 show_dwarf_max_cache_age,
26967 &set_dwarf_cmdlist,
26968 &show_dwarf_cmdlist);
26970 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
26971 &dwarf_always_disassemble, _("\
26972 Set whether `info address' always disassembles DWARF expressions."), _("\
26973 Show whether `info address' always disassembles DWARF expressions."), _("\
26974 When enabled, DWARF expressions are always printed in an assembly-like\n\
26975 syntax. When disabled, expressions will be printed in a more\n\
26976 conversational style, when possible."),
26978 show_dwarf_always_disassemble,
26979 &set_dwarf_cmdlist,
26980 &show_dwarf_cmdlist);
26982 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
26983 Set debugging of the DWARF reader."), _("\
26984 Show debugging of the DWARF reader."), _("\
26985 When enabled (non-zero), debugging messages are printed during DWARF\n\
26986 reading and symtab expansion. A value of 1 (one) provides basic\n\
26987 information. A value greater than 1 provides more verbose information."),
26990 &setdebuglist, &showdebuglist);
26992 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
26993 Set debugging of the DWARF DIE reader."), _("\
26994 Show debugging of the DWARF DIE reader."), _("\
26995 When enabled (non-zero), DIEs are dumped after they are read in.\n\
26996 The value is the maximum depth to print."),
26999 &setdebuglist, &showdebuglist);
27001 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
27002 Set debugging of the dwarf line reader."), _("\
27003 Show debugging of the dwarf line reader."), _("\
27004 When enabled (non-zero), line number entries are dumped as they are read in.\n\
27005 A value of 1 (one) provides basic information.\n\
27006 A value greater than 1 provides more verbose information."),
27009 &setdebuglist, &showdebuglist);
27011 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
27012 Set cross-checking of \"physname\" code against demangler."), _("\
27013 Show cross-checking of \"physname\" code against demangler."), _("\
27014 When enabled, GDB's internal \"physname\" code is checked against\n\
27016 NULL, show_check_physname,
27017 &setdebuglist, &showdebuglist);
27019 add_setshow_boolean_cmd ("use-deprecated-index-sections",
27020 no_class, &use_deprecated_index_sections, _("\
27021 Set whether to use deprecated gdb_index sections."), _("\
27022 Show whether to use deprecated gdb_index sections."), _("\
27023 When enabled, deprecated .gdb_index sections are used anyway.\n\
27024 Normally they are ignored either because of a missing feature or\n\
27025 performance issue.\n\
27026 Warning: This option must be enabled before gdb reads the file."),
27029 &setlist, &showlist);
27031 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
27033 Save a gdb-index file.\n\
27034 Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
27036 No options create one file with .gdb-index extension for pre-DWARF-5\n\
27037 compatible .gdb_index section. With -dwarf-5 creates two files with\n\
27038 extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
27040 set_cmd_completer (c, filename_completer);
27042 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
27043 &dwarf2_locexpr_funcs);
27044 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
27045 &dwarf2_loclist_funcs);
27047 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
27048 &dwarf2_block_frame_base_locexpr_funcs);
27049 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
27050 &dwarf2_block_frame_base_loclist_funcs);
27053 selftests::register_test ("dw2_expand_symtabs_matching",
27054 selftests::dw2_expand_symtabs_matching::run_test);