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 /* A slot/bucket in the symbol table hash. */
242 struct symbol_table_slot
244 const offset_type name;
245 const offset_type vec;
248 /* Index data format version. */
251 /* The total length of the buffer. */
254 /* The address table data. */
255 gdb::array_view<const gdb_byte> address_table;
257 /* The symbol table, implemented as a hash table. */
258 gdb::array_view<symbol_table_slot> symbol_table;
260 /* A pointer to the constant pool. */
261 const char *constant_pool;
263 /* The name_component table (a sorted vector). See name_component's
264 description above. */
265 std::vector<name_component> name_components;
267 /* How NAME_COMPONENTS is sorted. */
268 enum case_sensitivity name_components_casing;
270 /* Convenience method to get at the name of the symbol at IDX in the
272 const char *symbol_name_at (offset_type idx) const
273 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
275 /* Build the symbol name component sorted vector, if we haven't
277 void build_name_components ();
279 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
280 possible matches for LN_NO_PARAMS in the name component
282 std::pair<std::vector<name_component>::const_iterator,
283 std::vector<name_component>::const_iterator>
284 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
287 /* A description of the mapped .debug_names.
288 Uninitialized map has CU_COUNT 0. */
289 struct mapped_debug_names
291 bfd_endian dwarf5_byte_order;
292 bool dwarf5_is_dwarf64;
293 bool augmentation_is_gdb;
295 uint32_t cu_count = 0;
296 uint32_t tu_count, bucket_count, name_count;
297 const gdb_byte *cu_table_reordered, *tu_table_reordered;
298 const uint32_t *bucket_table_reordered, *hash_table_reordered;
299 const gdb_byte *name_table_string_offs_reordered;
300 const gdb_byte *name_table_entry_offs_reordered;
301 const gdb_byte *entry_pool;
308 /* Attribute name DW_IDX_*. */
311 /* Attribute form DW_FORM_*. */
314 /* Value if FORM is DW_FORM_implicit_const. */
315 LONGEST implicit_const;
317 std::vector<attr> attr_vec;
320 std::unordered_map<ULONGEST, index_val> abbrev_map;
322 const char *namei_to_name (uint32_t namei) const;
325 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
326 DEF_VEC_P (dwarf2_per_cu_ptr);
330 int nr_uniq_abbrev_tables;
332 int nr_symtab_sharers;
333 int nr_stmt_less_type_units;
334 int nr_all_type_units_reallocs;
337 /* Collection of data recorded per objfile.
338 This hangs off of dwarf2_objfile_data_key. */
340 struct dwarf2_per_objfile
342 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
343 dwarf2 section names, or is NULL if the standard ELF names are
345 dwarf2_per_objfile (struct objfile *objfile,
346 const dwarf2_debug_sections *names);
348 ~dwarf2_per_objfile ();
350 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
352 /* Free all cached compilation units. */
353 void free_cached_comp_units ();
355 /* This function is mapped across the sections and remembers the
356 offset and size of each of the debugging sections we are
358 void locate_sections (bfd *abfd, asection *sectp,
359 const dwarf2_debug_sections &names);
362 dwarf2_section_info info {};
363 dwarf2_section_info abbrev {};
364 dwarf2_section_info line {};
365 dwarf2_section_info loc {};
366 dwarf2_section_info loclists {};
367 dwarf2_section_info macinfo {};
368 dwarf2_section_info macro {};
369 dwarf2_section_info str {};
370 dwarf2_section_info line_str {};
371 dwarf2_section_info ranges {};
372 dwarf2_section_info rnglists {};
373 dwarf2_section_info addr {};
374 dwarf2_section_info frame {};
375 dwarf2_section_info eh_frame {};
376 dwarf2_section_info gdb_index {};
377 dwarf2_section_info debug_names {};
378 dwarf2_section_info debug_aranges {};
380 VEC (dwarf2_section_info_def) *types = NULL;
383 struct objfile *objfile = NULL;
385 /* Table of all the compilation units. This is used to locate
386 the target compilation unit of a particular reference. */
387 struct dwarf2_per_cu_data **all_comp_units = NULL;
389 /* The number of compilation units in ALL_COMP_UNITS. */
390 int n_comp_units = 0;
392 /* The number of .debug_types-related CUs. */
393 int n_type_units = 0;
395 /* The number of elements allocated in all_type_units.
396 If there are skeleton-less TUs, we add them to all_type_units lazily. */
397 int n_allocated_type_units = 0;
399 /* The .debug_types-related CUs (TUs).
400 This is stored in malloc space because we may realloc it. */
401 struct signatured_type **all_type_units = NULL;
403 /* Table of struct type_unit_group objects.
404 The hash key is the DW_AT_stmt_list value. */
405 htab_t type_unit_groups {};
407 /* A table mapping .debug_types signatures to its signatured_type entry.
408 This is NULL if the .debug_types section hasn't been read in yet. */
409 htab_t signatured_types {};
411 /* Type unit statistics, to see how well the scaling improvements
413 struct tu_stats tu_stats {};
415 /* A chain of compilation units that are currently read in, so that
416 they can be freed later. */
417 dwarf2_per_cu_data *read_in_chain = NULL;
419 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
420 This is NULL if the table hasn't been allocated yet. */
423 /* True if we've checked for whether there is a DWP file. */
424 bool dwp_checked = false;
426 /* The DWP file if there is one, or NULL. */
427 struct dwp_file *dwp_file = NULL;
429 /* The shared '.dwz' file, if one exists. This is used when the
430 original data was compressed using 'dwz -m'. */
431 struct dwz_file *dwz_file = NULL;
433 /* A flag indicating whether this objfile has a section loaded at a
435 bool has_section_at_zero = false;
437 /* True if we are using the mapped index,
438 or we are faking it for OBJF_READNOW's sake. */
439 bool using_index = false;
441 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
442 mapped_index *index_table = NULL;
444 /* The mapped index, or NULL if .debug_names is missing or not being used. */
445 std::unique_ptr<mapped_debug_names> debug_names_table;
447 /* When using index_table, this keeps track of all quick_file_names entries.
448 TUs typically share line table entries with a CU, so we maintain a
449 separate table of all line table entries to support the sharing.
450 Note that while there can be way more TUs than CUs, we've already
451 sorted all the TUs into "type unit groups", grouped by their
452 DW_AT_stmt_list value. Therefore the only sharing done here is with a
453 CU and its associated TU group if there is one. */
454 htab_t quick_file_names_table {};
456 /* Set during partial symbol reading, to prevent queueing of full
458 bool reading_partial_symbols = false;
460 /* Table mapping type DIEs to their struct type *.
461 This is NULL if not allocated yet.
462 The mapping is done via (CU/TU + DIE offset) -> type. */
463 htab_t die_type_hash {};
465 /* The CUs we recently read. */
466 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
468 /* Table containing line_header indexed by offset and offset_in_dwz. */
469 htab_t line_header_hash {};
471 /* Table containing all filenames. This is an optional because the
472 table is lazily constructed on first access. */
473 gdb::optional<filename_seen_cache> filenames_cache;
476 static struct dwarf2_per_objfile *dwarf2_per_objfile;
478 /* Default names of the debugging sections. */
480 /* Note that if the debugging section has been compressed, it might
481 have a name like .zdebug_info. */
483 static const struct dwarf2_debug_sections dwarf2_elf_names =
485 { ".debug_info", ".zdebug_info" },
486 { ".debug_abbrev", ".zdebug_abbrev" },
487 { ".debug_line", ".zdebug_line" },
488 { ".debug_loc", ".zdebug_loc" },
489 { ".debug_loclists", ".zdebug_loclists" },
490 { ".debug_macinfo", ".zdebug_macinfo" },
491 { ".debug_macro", ".zdebug_macro" },
492 { ".debug_str", ".zdebug_str" },
493 { ".debug_line_str", ".zdebug_line_str" },
494 { ".debug_ranges", ".zdebug_ranges" },
495 { ".debug_rnglists", ".zdebug_rnglists" },
496 { ".debug_types", ".zdebug_types" },
497 { ".debug_addr", ".zdebug_addr" },
498 { ".debug_frame", ".zdebug_frame" },
499 { ".eh_frame", NULL },
500 { ".gdb_index", ".zgdb_index" },
501 { ".debug_names", ".zdebug_names" },
502 { ".debug_aranges", ".zdebug_aranges" },
506 /* List of DWO/DWP sections. */
508 static const struct dwop_section_names
510 struct dwarf2_section_names abbrev_dwo;
511 struct dwarf2_section_names info_dwo;
512 struct dwarf2_section_names line_dwo;
513 struct dwarf2_section_names loc_dwo;
514 struct dwarf2_section_names loclists_dwo;
515 struct dwarf2_section_names macinfo_dwo;
516 struct dwarf2_section_names macro_dwo;
517 struct dwarf2_section_names str_dwo;
518 struct dwarf2_section_names str_offsets_dwo;
519 struct dwarf2_section_names types_dwo;
520 struct dwarf2_section_names cu_index;
521 struct dwarf2_section_names tu_index;
525 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
526 { ".debug_info.dwo", ".zdebug_info.dwo" },
527 { ".debug_line.dwo", ".zdebug_line.dwo" },
528 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
529 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
530 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
531 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
532 { ".debug_str.dwo", ".zdebug_str.dwo" },
533 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
534 { ".debug_types.dwo", ".zdebug_types.dwo" },
535 { ".debug_cu_index", ".zdebug_cu_index" },
536 { ".debug_tu_index", ".zdebug_tu_index" },
539 /* local data types */
541 /* The data in a compilation unit header, after target2host
542 translation, looks like this. */
543 struct comp_unit_head
547 unsigned char addr_size;
548 unsigned char signed_addr_p;
549 sect_offset abbrev_sect_off;
551 /* Size of file offsets; either 4 or 8. */
552 unsigned int offset_size;
554 /* Size of the length field; either 4 or 12. */
555 unsigned int initial_length_size;
557 enum dwarf_unit_type unit_type;
559 /* Offset to the first byte of this compilation unit header in the
560 .debug_info section, for resolving relative reference dies. */
561 sect_offset sect_off;
563 /* Offset to first die in this cu from the start of the cu.
564 This will be the first byte following the compilation unit header. */
565 cu_offset first_die_cu_offset;
567 /* 64-bit signature of this type unit - it is valid only for
568 UNIT_TYPE DW_UT_type. */
571 /* For types, offset in the type's DIE of the type defined by this TU. */
572 cu_offset type_cu_offset_in_tu;
575 /* Type used for delaying computation of method physnames.
576 See comments for compute_delayed_physnames. */
577 struct delayed_method_info
579 /* The type to which the method is attached, i.e., its parent class. */
582 /* The index of the method in the type's function fieldlists. */
585 /* The index of the method in the fieldlist. */
588 /* The name of the DIE. */
591 /* The DIE associated with this method. */
592 struct die_info *die;
595 typedef struct delayed_method_info delayed_method_info;
596 DEF_VEC_O (delayed_method_info);
598 /* Internal state when decoding a particular compilation unit. */
601 /* The objfile containing this compilation unit. */
602 struct objfile *objfile;
604 /* The header of the compilation unit. */
605 struct comp_unit_head header;
607 /* Base address of this compilation unit. */
608 CORE_ADDR base_address;
610 /* Non-zero if base_address has been set. */
613 /* The language we are debugging. */
614 enum language language;
615 const struct language_defn *language_defn;
617 const char *producer;
619 /* The generic symbol table building routines have separate lists for
620 file scope symbols and all all other scopes (local scopes). So
621 we need to select the right one to pass to add_symbol_to_list().
622 We do it by keeping a pointer to the correct list in list_in_scope.
624 FIXME: The original dwarf code just treated the file scope as the
625 first local scope, and all other local scopes as nested local
626 scopes, and worked fine. Check to see if we really need to
627 distinguish these in buildsym.c. */
628 struct pending **list_in_scope;
630 /* The abbrev table for this CU.
631 Normally this points to the abbrev table in the objfile.
632 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
633 struct abbrev_table *abbrev_table;
635 /* Hash table holding all the loaded partial DIEs
636 with partial_die->offset.SECT_OFF as hash. */
639 /* Storage for things with the same lifetime as this read-in compilation
640 unit, including partial DIEs. */
641 struct obstack comp_unit_obstack;
643 /* When multiple dwarf2_cu structures are living in memory, this field
644 chains them all together, so that they can be released efficiently.
645 We will probably also want a generation counter so that most-recently-used
646 compilation units are cached... */
647 struct dwarf2_per_cu_data *read_in_chain;
649 /* Backlink to our per_cu entry. */
650 struct dwarf2_per_cu_data *per_cu;
652 /* How many compilation units ago was this CU last referenced? */
655 /* A hash table of DIE cu_offset for following references with
656 die_info->offset.sect_off as hash. */
659 /* Full DIEs if read in. */
660 struct die_info *dies;
662 /* A set of pointers to dwarf2_per_cu_data objects for compilation
663 units referenced by this one. Only set during full symbol processing;
664 partial symbol tables do not have dependencies. */
667 /* Header data from the line table, during full symbol processing. */
668 struct line_header *line_header;
669 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
670 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
671 this is the DW_TAG_compile_unit die for this CU. We'll hold on
672 to the line header as long as this DIE is being processed. See
673 process_die_scope. */
674 die_info *line_header_die_owner;
676 /* A list of methods which need to have physnames computed
677 after all type information has been read. */
678 VEC (delayed_method_info) *method_list;
680 /* To be copied to symtab->call_site_htab. */
681 htab_t call_site_htab;
683 /* Non-NULL if this CU came from a DWO file.
684 There is an invariant here that is important to remember:
685 Except for attributes copied from the top level DIE in the "main"
686 (or "stub") file in preparation for reading the DWO file
687 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
688 Either there isn't a DWO file (in which case this is NULL and the point
689 is moot), or there is and either we're not going to read it (in which
690 case this is NULL) or there is and we are reading it (in which case this
692 struct dwo_unit *dwo_unit;
694 /* The DW_AT_addr_base attribute if present, zero otherwise
695 (zero is a valid value though).
696 Note this value comes from the Fission stub CU/TU's DIE. */
699 /* The DW_AT_ranges_base attribute if present, zero otherwise
700 (zero is a valid value though).
701 Note this value comes from the Fission stub CU/TU's DIE.
702 Also note that the value is zero in the non-DWO case so this value can
703 be used without needing to know whether DWO files are in use or not.
704 N.B. This does not apply to DW_AT_ranges appearing in
705 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
706 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
707 DW_AT_ranges_base *would* have to be applied, and we'd have to care
708 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
709 ULONGEST ranges_base;
711 /* Mark used when releasing cached dies. */
712 unsigned int mark : 1;
714 /* This CU references .debug_loc. See the symtab->locations_valid field.
715 This test is imperfect as there may exist optimized debug code not using
716 any location list and still facing inlining issues if handled as
717 unoptimized code. For a future better test see GCC PR other/32998. */
718 unsigned int has_loclist : 1;
720 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
721 if all the producer_is_* fields are valid. This information is cached
722 because profiling CU expansion showed excessive time spent in
723 producer_is_gxx_lt_4_6. */
724 unsigned int checked_producer : 1;
725 unsigned int producer_is_gxx_lt_4_6 : 1;
726 unsigned int producer_is_gcc_lt_4_3 : 1;
727 unsigned int producer_is_icc_lt_14 : 1;
729 /* When set, the file that we're processing is known to have
730 debugging info for C++ namespaces. GCC 3.3.x did not produce
731 this information, but later versions do. */
733 unsigned int processing_has_namespace_info : 1;
736 /* Persistent data held for a compilation unit, even when not
737 processing it. We put a pointer to this structure in the
738 read_symtab_private field of the psymtab. */
740 struct dwarf2_per_cu_data
742 /* The start offset and length of this compilation unit.
743 NOTE: Unlike comp_unit_head.length, this length includes
745 If the DIE refers to a DWO file, this is always of the original die,
747 sect_offset sect_off;
750 /* DWARF standard version this data has been read from (such as 4 or 5). */
753 /* Flag indicating this compilation unit will be read in before
754 any of the current compilation units are processed. */
755 unsigned int queued : 1;
757 /* This flag will be set when reading partial DIEs if we need to load
758 absolutely all DIEs for this compilation unit, instead of just the ones
759 we think are interesting. It gets set if we look for a DIE in the
760 hash table and don't find it. */
761 unsigned int load_all_dies : 1;
763 /* Non-zero if this CU is from .debug_types.
764 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
766 unsigned int is_debug_types : 1;
768 /* Non-zero if this CU is from the .dwz file. */
769 unsigned int is_dwz : 1;
771 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
772 This flag is only valid if is_debug_types is true.
773 We can't read a CU directly from a DWO file: There are required
774 attributes in the stub. */
775 unsigned int reading_dwo_directly : 1;
777 /* Non-zero if the TU has been read.
778 This is used to assist the "Stay in DWO Optimization" for Fission:
779 When reading a DWO, it's faster to read TUs from the DWO instead of
780 fetching them from random other DWOs (due to comdat folding).
781 If the TU has already been read, the optimization is unnecessary
782 (and unwise - we don't want to change where gdb thinks the TU lives
784 This flag is only valid if is_debug_types is true. */
785 unsigned int tu_read : 1;
787 /* The section this CU/TU lives in.
788 If the DIE refers to a DWO file, this is always the original die,
790 struct dwarf2_section_info *section;
792 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
793 of the CU cache it gets reset to NULL again. This is left as NULL for
794 dummy CUs (a CU header, but nothing else). */
795 struct dwarf2_cu *cu;
797 /* The corresponding objfile.
798 Normally we can get the objfile from dwarf2_per_objfile.
799 However we can enter this file with just a "per_cu" handle. */
800 struct objfile *objfile;
802 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
803 is active. Otherwise, the 'psymtab' field is active. */
806 /* The partial symbol table associated with this compilation unit,
807 or NULL for unread partial units. */
808 struct partial_symtab *psymtab;
810 /* Data needed by the "quick" functions. */
811 struct dwarf2_per_cu_quick_data *quick;
814 /* The CUs we import using DW_TAG_imported_unit. This is filled in
815 while reading psymtabs, used to compute the psymtab dependencies,
816 and then cleared. Then it is filled in again while reading full
817 symbols, and only deleted when the objfile is destroyed.
819 This is also used to work around a difference between the way gold
820 generates .gdb_index version <=7 and the way gdb does. Arguably this
821 is a gold bug. For symbols coming from TUs, gold records in the index
822 the CU that includes the TU instead of the TU itself. This breaks
823 dw2_lookup_symbol: It assumes that if the index says symbol X lives
824 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
825 will find X. Alas TUs live in their own symtab, so after expanding CU Y
826 we need to look in TU Z to find X. Fortunately, this is akin to
827 DW_TAG_imported_unit, so we just use the same mechanism: For
828 .gdb_index version <=7 this also records the TUs that the CU referred
829 to. Concurrently with this change gdb was modified to emit version 8
830 indices so we only pay a price for gold generated indices.
831 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
832 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
835 /* Entry in the signatured_types hash table. */
837 struct signatured_type
839 /* The "per_cu" object of this type.
840 This struct is used iff per_cu.is_debug_types.
841 N.B.: This is the first member so that it's easy to convert pointers
843 struct dwarf2_per_cu_data per_cu;
845 /* The type's signature. */
848 /* Offset in the TU of the type's DIE, as read from the TU header.
849 If this TU is a DWO stub and the definition lives in a DWO file
850 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
851 cu_offset type_offset_in_tu;
853 /* Offset in the section of the type's DIE.
854 If the definition lives in a DWO file, this is the offset in the
855 .debug_types.dwo section.
856 The value is zero until the actual value is known.
857 Zero is otherwise not a valid section offset. */
858 sect_offset type_offset_in_section;
860 /* Type units are grouped by their DW_AT_stmt_list entry so that they
861 can share them. This points to the containing symtab. */
862 struct type_unit_group *type_unit_group;
865 The first time we encounter this type we fully read it in and install it
866 in the symbol tables. Subsequent times we only need the type. */
869 /* Containing DWO unit.
870 This field is valid iff per_cu.reading_dwo_directly. */
871 struct dwo_unit *dwo_unit;
874 typedef struct signatured_type *sig_type_ptr;
875 DEF_VEC_P (sig_type_ptr);
877 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
878 This includes type_unit_group and quick_file_names. */
880 struct stmt_list_hash
882 /* The DWO unit this table is from or NULL if there is none. */
883 struct dwo_unit *dwo_unit;
885 /* Offset in .debug_line or .debug_line.dwo. */
886 sect_offset line_sect_off;
889 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
890 an object of this type. */
892 struct type_unit_group
894 /* dwarf2read.c's main "handle" on a TU symtab.
895 To simplify things we create an artificial CU that "includes" all the
896 type units using this stmt_list so that the rest of the code still has
897 a "per_cu" handle on the symtab.
898 This PER_CU is recognized by having no section. */
899 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
900 struct dwarf2_per_cu_data per_cu;
902 /* The TUs that share this DW_AT_stmt_list entry.
903 This is added to while parsing type units to build partial symtabs,
904 and is deleted afterwards and not used again. */
905 VEC (sig_type_ptr) *tus;
907 /* The compunit symtab.
908 Type units in a group needn't all be defined in the same source file,
909 so we create an essentially anonymous symtab as the compunit symtab. */
910 struct compunit_symtab *compunit_symtab;
912 /* The data used to construct the hash key. */
913 struct stmt_list_hash hash;
915 /* The number of symtabs from the line header.
916 The value here must match line_header.num_file_names. */
917 unsigned int num_symtabs;
919 /* The symbol tables for this TU (obtained from the files listed in
921 WARNING: The order of entries here must match the order of entries
922 in the line header. After the first TU using this type_unit_group, the
923 line header for the subsequent TUs is recreated from this. This is done
924 because we need to use the same symtabs for each TU using the same
925 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
926 there's no guarantee the line header doesn't have duplicate entries. */
927 struct symtab **symtabs;
930 /* These sections are what may appear in a (real or virtual) DWO file. */
934 struct dwarf2_section_info abbrev;
935 struct dwarf2_section_info line;
936 struct dwarf2_section_info loc;
937 struct dwarf2_section_info loclists;
938 struct dwarf2_section_info macinfo;
939 struct dwarf2_section_info macro;
940 struct dwarf2_section_info str;
941 struct dwarf2_section_info str_offsets;
942 /* In the case of a virtual DWO file, these two are unused. */
943 struct dwarf2_section_info info;
944 VEC (dwarf2_section_info_def) *types;
947 /* CUs/TUs in DWP/DWO files. */
951 /* Backlink to the containing struct dwo_file. */
952 struct dwo_file *dwo_file;
954 /* The "id" that distinguishes this CU/TU.
955 .debug_info calls this "dwo_id", .debug_types calls this "signature".
956 Since signatures came first, we stick with it for consistency. */
959 /* The section this CU/TU lives in, in the DWO file. */
960 struct dwarf2_section_info *section;
962 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
963 sect_offset sect_off;
966 /* For types, offset in the type's DIE of the type defined by this TU. */
967 cu_offset type_offset_in_tu;
970 /* include/dwarf2.h defines the DWP section codes.
971 It defines a max value but it doesn't define a min value, which we
972 use for error checking, so provide one. */
974 enum dwp_v2_section_ids
979 /* Data for one DWO file.
981 This includes virtual DWO files (a virtual DWO file is a DWO file as it
982 appears in a DWP file). DWP files don't really have DWO files per se -
983 comdat folding of types "loses" the DWO file they came from, and from
984 a high level view DWP files appear to contain a mass of random types.
985 However, to maintain consistency with the non-DWP case we pretend DWP
986 files contain virtual DWO files, and we assign each TU with one virtual
987 DWO file (generally based on the line and abbrev section offsets -
988 a heuristic that seems to work in practice). */
992 /* The DW_AT_GNU_dwo_name attribute.
993 For virtual DWO files the name is constructed from the section offsets
994 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
995 from related CU+TUs. */
996 const char *dwo_name;
998 /* The DW_AT_comp_dir attribute. */
999 const char *comp_dir;
1001 /* The bfd, when the file is open. Otherwise this is NULL.
1002 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
1005 /* The sections that make up this DWO file.
1006 Remember that for virtual DWO files in DWP V2, these are virtual
1007 sections (for lack of a better name). */
1008 struct dwo_sections sections;
1010 /* The CUs in the file.
1011 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
1012 an extension to handle LLVM's Link Time Optimization output (where
1013 multiple source files may be compiled into a single object/dwo pair). */
1016 /* Table of TUs in the file.
1017 Each element is a struct dwo_unit. */
1021 /* These sections are what may appear in a DWP file. */
1025 /* These are used by both DWP version 1 and 2. */
1026 struct dwarf2_section_info str;
1027 struct dwarf2_section_info cu_index;
1028 struct dwarf2_section_info tu_index;
1030 /* These are only used by DWP version 2 files.
1031 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
1032 sections are referenced by section number, and are not recorded here.
1033 In DWP version 2 there is at most one copy of all these sections, each
1034 section being (effectively) comprised of the concatenation of all of the
1035 individual sections that exist in the version 1 format.
1036 To keep the code simple we treat each of these concatenated pieces as a
1037 section itself (a virtual section?). */
1038 struct dwarf2_section_info abbrev;
1039 struct dwarf2_section_info info;
1040 struct dwarf2_section_info line;
1041 struct dwarf2_section_info loc;
1042 struct dwarf2_section_info macinfo;
1043 struct dwarf2_section_info macro;
1044 struct dwarf2_section_info str_offsets;
1045 struct dwarf2_section_info types;
1048 /* These sections are what may appear in a virtual DWO file in DWP version 1.
1049 A virtual DWO file is a DWO file as it appears in a DWP file. */
1051 struct virtual_v1_dwo_sections
1053 struct dwarf2_section_info abbrev;
1054 struct dwarf2_section_info line;
1055 struct dwarf2_section_info loc;
1056 struct dwarf2_section_info macinfo;
1057 struct dwarf2_section_info macro;
1058 struct dwarf2_section_info str_offsets;
1059 /* Each DWP hash table entry records one CU or one TU.
1060 That is recorded here, and copied to dwo_unit.section. */
1061 struct dwarf2_section_info info_or_types;
1064 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
1065 In version 2, the sections of the DWO files are concatenated together
1066 and stored in one section of that name. Thus each ELF section contains
1067 several "virtual" sections. */
1069 struct virtual_v2_dwo_sections
1071 bfd_size_type abbrev_offset;
1072 bfd_size_type abbrev_size;
1074 bfd_size_type line_offset;
1075 bfd_size_type line_size;
1077 bfd_size_type loc_offset;
1078 bfd_size_type loc_size;
1080 bfd_size_type macinfo_offset;
1081 bfd_size_type macinfo_size;
1083 bfd_size_type macro_offset;
1084 bfd_size_type macro_size;
1086 bfd_size_type str_offsets_offset;
1087 bfd_size_type str_offsets_size;
1089 /* Each DWP hash table entry records one CU or one TU.
1090 That is recorded here, and copied to dwo_unit.section. */
1091 bfd_size_type info_or_types_offset;
1092 bfd_size_type info_or_types_size;
1095 /* Contents of DWP hash tables. */
1097 struct dwp_hash_table
1099 uint32_t version, nr_columns;
1100 uint32_t nr_units, nr_slots;
1101 const gdb_byte *hash_table, *unit_table;
1106 const gdb_byte *indices;
1110 /* This is indexed by column number and gives the id of the section
1112 #define MAX_NR_V2_DWO_SECTIONS \
1113 (1 /* .debug_info or .debug_types */ \
1114 + 1 /* .debug_abbrev */ \
1115 + 1 /* .debug_line */ \
1116 + 1 /* .debug_loc */ \
1117 + 1 /* .debug_str_offsets */ \
1118 + 1 /* .debug_macro or .debug_macinfo */)
1119 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1120 const gdb_byte *offsets;
1121 const gdb_byte *sizes;
1126 /* Data for one DWP file. */
1130 /* Name of the file. */
1133 /* File format version. */
1139 /* Section info for this file. */
1140 struct dwp_sections sections;
1142 /* Table of CUs in the file. */
1143 const struct dwp_hash_table *cus;
1145 /* Table of TUs in the file. */
1146 const struct dwp_hash_table *tus;
1148 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1152 /* Table to map ELF section numbers to their sections.
1153 This is only needed for the DWP V1 file format. */
1154 unsigned int num_sections;
1155 asection **elf_sections;
1158 /* This represents a '.dwz' file. */
1162 /* A dwz file can only contain a few sections. */
1163 struct dwarf2_section_info abbrev;
1164 struct dwarf2_section_info info;
1165 struct dwarf2_section_info str;
1166 struct dwarf2_section_info line;
1167 struct dwarf2_section_info macro;
1168 struct dwarf2_section_info gdb_index;
1169 struct dwarf2_section_info debug_names;
1171 /* The dwz's BFD. */
1175 /* Struct used to pass misc. parameters to read_die_and_children, et
1176 al. which are used for both .debug_info and .debug_types dies.
1177 All parameters here are unchanging for the life of the call. This
1178 struct exists to abstract away the constant parameters of die reading. */
1180 struct die_reader_specs
1182 /* The bfd of die_section. */
1185 /* The CU of the DIE we are parsing. */
1186 struct dwarf2_cu *cu;
1188 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1189 struct dwo_file *dwo_file;
1191 /* The section the die comes from.
1192 This is either .debug_info or .debug_types, or the .dwo variants. */
1193 struct dwarf2_section_info *die_section;
1195 /* die_section->buffer. */
1196 const gdb_byte *buffer;
1198 /* The end of the buffer. */
1199 const gdb_byte *buffer_end;
1201 /* The value of the DW_AT_comp_dir attribute. */
1202 const char *comp_dir;
1205 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1206 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1207 const gdb_byte *info_ptr,
1208 struct die_info *comp_unit_die,
1212 /* A 1-based directory index. This is a strong typedef to prevent
1213 accidentally using a directory index as a 0-based index into an
1215 enum class dir_index : unsigned int {};
1217 /* Likewise, a 1-based file name index. */
1218 enum class file_name_index : unsigned int {};
1222 file_entry () = default;
1224 file_entry (const char *name_, dir_index d_index_,
1225 unsigned int mod_time_, unsigned int length_)
1228 mod_time (mod_time_),
1232 /* Return the include directory at D_INDEX stored in LH. Returns
1233 NULL if D_INDEX is out of bounds. */
1234 const char *include_dir (const line_header *lh) const;
1236 /* The file name. Note this is an observing pointer. The memory is
1237 owned by debug_line_buffer. */
1238 const char *name {};
1240 /* The directory index (1-based). */
1241 dir_index d_index {};
1243 unsigned int mod_time {};
1245 unsigned int length {};
1247 /* True if referenced by the Line Number Program. */
1250 /* The associated symbol table, if any. */
1251 struct symtab *symtab {};
1254 /* The line number information for a compilation unit (found in the
1255 .debug_line section) begins with a "statement program header",
1256 which contains the following information. */
1263 /* Add an entry to the include directory table. */
1264 void add_include_dir (const char *include_dir);
1266 /* Add an entry to the file name table. */
1267 void add_file_name (const char *name, dir_index d_index,
1268 unsigned int mod_time, unsigned int length);
1270 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
1271 is out of bounds. */
1272 const char *include_dir_at (dir_index index) const
1274 /* Convert directory index number (1-based) to vector index
1276 size_t vec_index = to_underlying (index) - 1;
1278 if (vec_index >= include_dirs.size ())
1280 return include_dirs[vec_index];
1283 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
1284 is out of bounds. */
1285 file_entry *file_name_at (file_name_index index)
1287 /* Convert file name index number (1-based) to vector index
1289 size_t vec_index = to_underlying (index) - 1;
1291 if (vec_index >= file_names.size ())
1293 return &file_names[vec_index];
1296 /* Const version of the above. */
1297 const file_entry *file_name_at (unsigned int index) const
1299 if (index >= file_names.size ())
1301 return &file_names[index];
1304 /* Offset of line number information in .debug_line section. */
1305 sect_offset sect_off {};
1307 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1308 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1310 unsigned int total_length {};
1311 unsigned short version {};
1312 unsigned int header_length {};
1313 unsigned char minimum_instruction_length {};
1314 unsigned char maximum_ops_per_instruction {};
1315 unsigned char default_is_stmt {};
1317 unsigned char line_range {};
1318 unsigned char opcode_base {};
1320 /* standard_opcode_lengths[i] is the number of operands for the
1321 standard opcode whose value is i. This means that
1322 standard_opcode_lengths[0] is unused, and the last meaningful
1323 element is standard_opcode_lengths[opcode_base - 1]. */
1324 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1326 /* The include_directories table. Note these are observing
1327 pointers. The memory is owned by debug_line_buffer. */
1328 std::vector<const char *> include_dirs;
1330 /* The file_names table. */
1331 std::vector<file_entry> file_names;
1333 /* The start and end of the statement program following this
1334 header. These point into dwarf2_per_objfile->line_buffer. */
1335 const gdb_byte *statement_program_start {}, *statement_program_end {};
1338 typedef std::unique_ptr<line_header> line_header_up;
1341 file_entry::include_dir (const line_header *lh) const
1343 return lh->include_dir_at (d_index);
1346 /* When we construct a partial symbol table entry we only
1347 need this much information. */
1348 struct partial_die_info
1350 /* Offset of this DIE. */
1351 sect_offset sect_off;
1353 /* DWARF-2 tag for this DIE. */
1354 ENUM_BITFIELD(dwarf_tag) tag : 16;
1356 /* Assorted flags describing the data found in this DIE. */
1357 unsigned int has_children : 1;
1358 unsigned int is_external : 1;
1359 unsigned int is_declaration : 1;
1360 unsigned int has_type : 1;
1361 unsigned int has_specification : 1;
1362 unsigned int has_pc_info : 1;
1363 unsigned int may_be_inlined : 1;
1365 /* This DIE has been marked DW_AT_main_subprogram. */
1366 unsigned int main_subprogram : 1;
1368 /* Flag set if the SCOPE field of this structure has been
1370 unsigned int scope_set : 1;
1372 /* Flag set if the DIE has a byte_size attribute. */
1373 unsigned int has_byte_size : 1;
1375 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1376 unsigned int has_const_value : 1;
1378 /* Flag set if any of the DIE's children are template arguments. */
1379 unsigned int has_template_arguments : 1;
1381 /* Flag set if fixup_partial_die has been called on this die. */
1382 unsigned int fixup_called : 1;
1384 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1385 unsigned int is_dwz : 1;
1387 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1388 unsigned int spec_is_dwz : 1;
1390 /* The name of this DIE. Normally the value of DW_AT_name, but
1391 sometimes a default name for unnamed DIEs. */
1394 /* The linkage name, if present. */
1395 const char *linkage_name;
1397 /* The scope to prepend to our children. This is generally
1398 allocated on the comp_unit_obstack, so will disappear
1399 when this compilation unit leaves the cache. */
1402 /* Some data associated with the partial DIE. The tag determines
1403 which field is live. */
1406 /* The location description associated with this DIE, if any. */
1407 struct dwarf_block *locdesc;
1408 /* The offset of an import, for DW_TAG_imported_unit. */
1409 sect_offset sect_off;
1412 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1416 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1417 DW_AT_sibling, if any. */
1418 /* NOTE: This member isn't strictly necessary, read_partial_die could
1419 return DW_AT_sibling values to its caller load_partial_dies. */
1420 const gdb_byte *sibling;
1422 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1423 DW_AT_specification (or DW_AT_abstract_origin or
1424 DW_AT_extension). */
1425 sect_offset spec_offset;
1427 /* Pointers to this DIE's parent, first child, and next sibling,
1429 struct partial_die_info *die_parent, *die_child, *die_sibling;
1432 /* This data structure holds the information of an abbrev. */
1435 unsigned int number; /* number identifying abbrev */
1436 enum dwarf_tag tag; /* dwarf tag */
1437 unsigned short has_children; /* boolean */
1438 unsigned short num_attrs; /* number of attributes */
1439 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1440 struct abbrev_info *next; /* next in chain */
1445 ENUM_BITFIELD(dwarf_attribute) name : 16;
1446 ENUM_BITFIELD(dwarf_form) form : 16;
1448 /* It is valid only if FORM is DW_FORM_implicit_const. */
1449 LONGEST implicit_const;
1452 /* Size of abbrev_table.abbrev_hash_table. */
1453 #define ABBREV_HASH_SIZE 121
1455 /* Top level data structure to contain an abbreviation table. */
1459 /* Where the abbrev table came from.
1460 This is used as a sanity check when the table is used. */
1461 sect_offset sect_off;
1463 /* Storage for the abbrev table. */
1464 struct obstack abbrev_obstack;
1466 /* Hash table of abbrevs.
1467 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1468 It could be statically allocated, but the previous code didn't so we
1470 struct abbrev_info **abbrevs;
1473 /* Attributes have a name and a value. */
1476 ENUM_BITFIELD(dwarf_attribute) name : 16;
1477 ENUM_BITFIELD(dwarf_form) form : 15;
1479 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1480 field should be in u.str (existing only for DW_STRING) but it is kept
1481 here for better struct attribute alignment. */
1482 unsigned int string_is_canonical : 1;
1487 struct dwarf_block *blk;
1496 /* This data structure holds a complete die structure. */
1499 /* DWARF-2 tag for this DIE. */
1500 ENUM_BITFIELD(dwarf_tag) tag : 16;
1502 /* Number of attributes */
1503 unsigned char num_attrs;
1505 /* True if we're presently building the full type name for the
1506 type derived from this DIE. */
1507 unsigned char building_fullname : 1;
1509 /* True if this die is in process. PR 16581. */
1510 unsigned char in_process : 1;
1513 unsigned int abbrev;
1515 /* Offset in .debug_info or .debug_types section. */
1516 sect_offset sect_off;
1518 /* The dies in a compilation unit form an n-ary tree. PARENT
1519 points to this die's parent; CHILD points to the first child of
1520 this node; and all the children of a given node are chained
1521 together via their SIBLING fields. */
1522 struct die_info *child; /* Its first child, if any. */
1523 struct die_info *sibling; /* Its next sibling, if any. */
1524 struct die_info *parent; /* Its parent, if any. */
1526 /* An array of attributes, with NUM_ATTRS elements. There may be
1527 zero, but it's not common and zero-sized arrays are not
1528 sufficiently portable C. */
1529 struct attribute attrs[1];
1532 /* Get at parts of an attribute structure. */
1534 #define DW_STRING(attr) ((attr)->u.str)
1535 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1536 #define DW_UNSND(attr) ((attr)->u.unsnd)
1537 #define DW_BLOCK(attr) ((attr)->u.blk)
1538 #define DW_SND(attr) ((attr)->u.snd)
1539 #define DW_ADDR(attr) ((attr)->u.addr)
1540 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1542 /* Blocks are a bunch of untyped bytes. */
1547 /* Valid only if SIZE is not zero. */
1548 const gdb_byte *data;
1551 #ifndef ATTR_ALLOC_CHUNK
1552 #define ATTR_ALLOC_CHUNK 4
1555 /* Allocate fields for structs, unions and enums in this size. */
1556 #ifndef DW_FIELD_ALLOC_CHUNK
1557 #define DW_FIELD_ALLOC_CHUNK 4
1560 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1561 but this would require a corresponding change in unpack_field_as_long
1563 static int bits_per_byte = 8;
1567 struct nextfield *next;
1575 struct nextfnfield *next;
1576 struct fn_field fnfield;
1583 struct nextfnfield *head;
1586 struct decl_field_list
1588 struct decl_field field;
1589 struct decl_field_list *next;
1592 /* The routines that read and process dies for a C struct or C++ class
1593 pass lists of data member fields and lists of member function fields
1594 in an instance of a field_info structure, as defined below. */
1597 /* List of data member and baseclasses fields. */
1598 struct nextfield *fields, *baseclasses;
1600 /* Number of fields (including baseclasses). */
1603 /* Number of baseclasses. */
1606 /* Set if the accesibility of one of the fields is not public. */
1607 int non_public_fields;
1609 /* Member function fieldlist array, contains name of possibly overloaded
1610 member function, number of overloaded member functions and a pointer
1611 to the head of the member function field chain. */
1612 struct fnfieldlist *fnfieldlists;
1614 /* Number of entries in the fnfieldlists array. */
1617 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1618 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1619 struct decl_field_list *typedef_field_list;
1620 unsigned typedef_field_list_count;
1622 /* Nested types defined by this class and the number of elements in this
1624 struct decl_field_list *nested_types_list;
1625 unsigned nested_types_list_count;
1628 /* One item on the queue of compilation units to read in full symbols
1630 struct dwarf2_queue_item
1632 struct dwarf2_per_cu_data *per_cu;
1633 enum language pretend_language;
1634 struct dwarf2_queue_item *next;
1637 /* The current queue. */
1638 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1640 /* Loaded secondary compilation units are kept in memory until they
1641 have not been referenced for the processing of this many
1642 compilation units. Set this to zero to disable caching. Cache
1643 sizes of up to at least twenty will improve startup time for
1644 typical inter-CU-reference binaries, at an obvious memory cost. */
1645 static int dwarf_max_cache_age = 5;
1647 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1648 struct cmd_list_element *c, const char *value)
1650 fprintf_filtered (file, _("The upper bound on the age of cached "
1651 "DWARF compilation units is %s.\n"),
1655 /* local function prototypes */
1657 static const char *get_section_name (const struct dwarf2_section_info *);
1659 static const char *get_section_file_name (const struct dwarf2_section_info *);
1661 static void dwarf2_find_base_address (struct die_info *die,
1662 struct dwarf2_cu *cu);
1664 static struct partial_symtab *create_partial_symtab
1665 (struct dwarf2_per_cu_data *per_cu, const char *name);
1667 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1668 const gdb_byte *info_ptr,
1669 struct die_info *type_unit_die,
1670 int has_children, void *data);
1672 static void dwarf2_build_psymtabs_hard (struct objfile *);
1674 static void scan_partial_symbols (struct partial_die_info *,
1675 CORE_ADDR *, CORE_ADDR *,
1676 int, struct dwarf2_cu *);
1678 static void add_partial_symbol (struct partial_die_info *,
1679 struct dwarf2_cu *);
1681 static void add_partial_namespace (struct partial_die_info *pdi,
1682 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1683 int set_addrmap, struct dwarf2_cu *cu);
1685 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1686 CORE_ADDR *highpc, int set_addrmap,
1687 struct dwarf2_cu *cu);
1689 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1690 struct dwarf2_cu *cu);
1692 static void add_partial_subprogram (struct partial_die_info *pdi,
1693 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1694 int need_pc, struct dwarf2_cu *cu);
1696 static void dwarf2_read_symtab (struct partial_symtab *,
1699 static void psymtab_to_symtab_1 (struct partial_symtab *);
1701 static struct abbrev_info *abbrev_table_lookup_abbrev
1702 (const struct abbrev_table *, unsigned int);
1704 static struct abbrev_table *abbrev_table_read_table
1705 (struct dwarf2_section_info *, sect_offset);
1707 static void abbrev_table_free (struct abbrev_table *);
1709 static void abbrev_table_free_cleanup (void *);
1711 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1712 struct dwarf2_section_info *);
1714 static void dwarf2_free_abbrev_table (void *);
1716 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1718 static struct partial_die_info *load_partial_dies
1719 (const struct die_reader_specs *, const gdb_byte *, int);
1721 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1722 struct partial_die_info *,
1723 struct abbrev_info *,
1727 static struct partial_die_info *find_partial_die (sect_offset, int,
1728 struct dwarf2_cu *);
1730 static void fixup_partial_die (struct partial_die_info *,
1731 struct dwarf2_cu *);
1733 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1734 struct attribute *, struct attr_abbrev *,
1737 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1739 static int read_1_signed_byte (bfd *, const gdb_byte *);
1741 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1743 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1745 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1747 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1750 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1752 static LONGEST read_checked_initial_length_and_offset
1753 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1754 unsigned int *, unsigned int *);
1756 static LONGEST read_offset (bfd *, const gdb_byte *,
1757 const struct comp_unit_head *,
1760 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1762 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1765 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1767 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1769 static const char *read_indirect_string (bfd *, const gdb_byte *,
1770 const struct comp_unit_head *,
1773 static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1774 const struct comp_unit_head *,
1777 static const char *read_indirect_string_at_offset (bfd *abfd,
1778 LONGEST str_offset);
1780 static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1782 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1784 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1788 static const char *read_str_index (const struct die_reader_specs *reader,
1789 ULONGEST str_index);
1791 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1793 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1794 struct dwarf2_cu *);
1796 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1799 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1800 struct dwarf2_cu *cu);
1802 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1803 struct dwarf2_cu *cu);
1805 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1807 static struct die_info *die_specification (struct die_info *die,
1808 struct dwarf2_cu **);
1810 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1811 struct dwarf2_cu *cu);
1813 static void dwarf_decode_lines (struct line_header *, const char *,
1814 struct dwarf2_cu *, struct partial_symtab *,
1815 CORE_ADDR, int decode_mapping);
1817 static void dwarf2_start_subfile (const char *, const char *);
1819 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1820 const char *, const char *,
1823 static struct symbol *new_symbol (struct die_info *, struct type *,
1824 struct dwarf2_cu *);
1826 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1827 struct dwarf2_cu *, struct symbol *);
1829 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1830 struct dwarf2_cu *);
1832 static void dwarf2_const_value_attr (const struct attribute *attr,
1835 struct obstack *obstack,
1836 struct dwarf2_cu *cu, LONGEST *value,
1837 const gdb_byte **bytes,
1838 struct dwarf2_locexpr_baton **baton);
1840 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1842 static int need_gnat_info (struct dwarf2_cu *);
1844 static struct type *die_descriptive_type (struct die_info *,
1845 struct dwarf2_cu *);
1847 static void set_descriptive_type (struct type *, struct die_info *,
1848 struct dwarf2_cu *);
1850 static struct type *die_containing_type (struct die_info *,
1851 struct dwarf2_cu *);
1853 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1854 struct dwarf2_cu *);
1856 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1858 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1860 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1862 static char *typename_concat (struct obstack *obs, const char *prefix,
1863 const char *suffix, int physname,
1864 struct dwarf2_cu *cu);
1866 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1868 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1870 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1872 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1874 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1876 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1878 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1879 struct dwarf2_cu *, struct partial_symtab *);
1881 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1882 values. Keep the items ordered with increasing constraints compliance. */
1885 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1886 PC_BOUNDS_NOT_PRESENT,
1888 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1889 were present but they do not form a valid range of PC addresses. */
1892 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1895 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1899 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1900 CORE_ADDR *, CORE_ADDR *,
1902 struct partial_symtab *);
1904 static void get_scope_pc_bounds (struct die_info *,
1905 CORE_ADDR *, CORE_ADDR *,
1906 struct dwarf2_cu *);
1908 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1909 CORE_ADDR, struct dwarf2_cu *);
1911 static void dwarf2_add_field (struct field_info *, struct die_info *,
1912 struct dwarf2_cu *);
1914 static void dwarf2_attach_fields_to_type (struct field_info *,
1915 struct type *, struct dwarf2_cu *);
1917 static void dwarf2_add_member_fn (struct field_info *,
1918 struct die_info *, struct type *,
1919 struct dwarf2_cu *);
1921 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1923 struct dwarf2_cu *);
1925 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1927 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1929 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1931 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1933 static struct using_direct **using_directives (enum language);
1935 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1937 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1939 static struct type *read_module_type (struct die_info *die,
1940 struct dwarf2_cu *cu);
1942 static const char *namespace_name (struct die_info *die,
1943 int *is_anonymous, struct dwarf2_cu *);
1945 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1947 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1949 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1950 struct dwarf2_cu *);
1952 static struct die_info *read_die_and_siblings_1
1953 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1956 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1957 const gdb_byte *info_ptr,
1958 const gdb_byte **new_info_ptr,
1959 struct die_info *parent);
1961 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1962 struct die_info **, const gdb_byte *,
1965 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1966 struct die_info **, const gdb_byte *,
1969 static void process_die (struct die_info *, struct dwarf2_cu *);
1971 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1974 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1976 static const char *dwarf2_full_name (const char *name,
1977 struct die_info *die,
1978 struct dwarf2_cu *cu);
1980 static const char *dwarf2_physname (const char *name, struct die_info *die,
1981 struct dwarf2_cu *cu);
1983 static struct die_info *dwarf2_extension (struct die_info *die,
1984 struct dwarf2_cu **);
1986 static const char *dwarf_tag_name (unsigned int);
1988 static const char *dwarf_attr_name (unsigned int);
1990 static const char *dwarf_form_name (unsigned int);
1992 static const char *dwarf_bool_name (unsigned int);
1994 static const char *dwarf_type_encoding_name (unsigned int);
1996 static struct die_info *sibling_die (struct die_info *);
1998 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
2000 static void dump_die_for_error (struct die_info *);
2002 static void dump_die_1 (struct ui_file *, int level, int max_level,
2005 /*static*/ void dump_die (struct die_info *, int max_level);
2007 static void store_in_ref_table (struct die_info *,
2008 struct dwarf2_cu *);
2010 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
2012 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
2014 static struct die_info *follow_die_ref_or_sig (struct die_info *,
2015 const struct attribute *,
2016 struct dwarf2_cu **);
2018 static struct die_info *follow_die_ref (struct die_info *,
2019 const struct attribute *,
2020 struct dwarf2_cu **);
2022 static struct die_info *follow_die_sig (struct die_info *,
2023 const struct attribute *,
2024 struct dwarf2_cu **);
2026 static struct type *get_signatured_type (struct die_info *, ULONGEST,
2027 struct dwarf2_cu *);
2029 static struct type *get_DW_AT_signature_type (struct die_info *,
2030 const struct attribute *,
2031 struct dwarf2_cu *);
2033 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
2035 static void read_signatured_type (struct signatured_type *);
2037 static int attr_to_dynamic_prop (const struct attribute *attr,
2038 struct die_info *die, struct dwarf2_cu *cu,
2039 struct dynamic_prop *prop);
2041 /* memory allocation interface */
2043 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
2045 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
2047 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2049 static int attr_form_is_block (const struct attribute *);
2051 static int attr_form_is_section_offset (const struct attribute *);
2053 static int attr_form_is_constant (const struct attribute *);
2055 static int attr_form_is_ref (const struct attribute *);
2057 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
2058 struct dwarf2_loclist_baton *baton,
2059 const struct attribute *attr);
2061 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
2063 struct dwarf2_cu *cu,
2066 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
2067 const gdb_byte *info_ptr,
2068 struct abbrev_info *abbrev);
2070 static void free_stack_comp_unit (void *);
2072 static hashval_t partial_die_hash (const void *item);
2074 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2076 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
2077 (sect_offset sect_off, unsigned int offset_in_dwz, struct objfile *objfile);
2079 static void init_one_comp_unit (struct dwarf2_cu *cu,
2080 struct dwarf2_per_cu_data *per_cu);
2082 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
2083 struct die_info *comp_unit_die,
2084 enum language pretend_language);
2086 static void free_heap_comp_unit (void *);
2088 static void free_cached_comp_units (void *);
2090 static void age_cached_comp_units (void);
2092 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
2094 static struct type *set_die_type (struct die_info *, struct type *,
2095 struct dwarf2_cu *);
2097 static void create_all_comp_units (struct objfile *);
2099 static int create_all_type_units (struct objfile *);
2101 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2104 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2107 static void process_full_type_unit (struct dwarf2_per_cu_data *,
2110 static void dwarf2_add_dependence (struct dwarf2_cu *,
2111 struct dwarf2_per_cu_data *);
2113 static void dwarf2_mark (struct dwarf2_cu *);
2115 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2117 static struct type *get_die_type_at_offset (sect_offset,
2118 struct dwarf2_per_cu_data *);
2120 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
2122 static void dwarf2_release_queue (void *dummy);
2124 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2125 enum language pretend_language);
2127 static void process_queue (void);
2129 /* The return type of find_file_and_directory. Note, the enclosed
2130 string pointers are only valid while this object is valid. */
2132 struct file_and_directory
2134 /* The filename. This is never NULL. */
2137 /* The compilation directory. NULL if not known. If we needed to
2138 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2139 points directly to the DW_AT_comp_dir string attribute owned by
2140 the obstack that owns the DIE. */
2141 const char *comp_dir;
2143 /* If we needed to build a new string for comp_dir, this is what
2144 owns the storage. */
2145 std::string comp_dir_storage;
2148 static file_and_directory find_file_and_directory (struct die_info *die,
2149 struct dwarf2_cu *cu);
2151 static char *file_full_name (int file, struct line_header *lh,
2152 const char *comp_dir);
2154 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
2155 enum class rcuh_kind { COMPILE, TYPE };
2157 static const gdb_byte *read_and_check_comp_unit_head
2158 (struct comp_unit_head *header,
2159 struct dwarf2_section_info *section,
2160 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
2161 rcuh_kind section_kind);
2163 static void init_cutu_and_read_dies
2164 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2165 int use_existing_cu, int keep,
2166 die_reader_func_ftype *die_reader_func, void *data);
2168 static void init_cutu_and_read_dies_simple
2169 (struct dwarf2_per_cu_data *this_cu,
2170 die_reader_func_ftype *die_reader_func, void *data);
2172 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2174 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2176 static struct dwo_unit *lookup_dwo_unit_in_dwp
2177 (struct dwp_file *dwp_file, const char *comp_dir,
2178 ULONGEST signature, int is_debug_types);
2180 static struct dwp_file *get_dwp_file (void);
2182 static struct dwo_unit *lookup_dwo_comp_unit
2183 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2185 static struct dwo_unit *lookup_dwo_type_unit
2186 (struct signatured_type *, const char *, const char *);
2188 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2190 static void free_dwo_file_cleanup (void *);
2192 static void process_cu_includes (void);
2194 static void check_producer (struct dwarf2_cu *cu);
2196 static void free_line_header_voidp (void *arg);
2198 /* Various complaints about symbol reading that don't abort the process. */
2201 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2203 complaint (&symfile_complaints,
2204 _("statement list doesn't fit in .debug_line section"));
2208 dwarf2_debug_line_missing_file_complaint (void)
2210 complaint (&symfile_complaints,
2211 _(".debug_line section has line data without a file"));
2215 dwarf2_debug_line_missing_end_sequence_complaint (void)
2217 complaint (&symfile_complaints,
2218 _(".debug_line section has line "
2219 "program sequence without an end"));
2223 dwarf2_complex_location_expr_complaint (void)
2225 complaint (&symfile_complaints, _("location expression too complex"));
2229 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2232 complaint (&symfile_complaints,
2233 _("const value length mismatch for '%s', got %d, expected %d"),
2238 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2240 complaint (&symfile_complaints,
2241 _("debug info runs off end of %s section"
2243 get_section_name (section),
2244 get_section_file_name (section));
2248 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2250 complaint (&symfile_complaints,
2251 _("macro debug info contains a "
2252 "malformed macro definition:\n`%s'"),
2257 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2259 complaint (&symfile_complaints,
2260 _("invalid attribute class or form for '%s' in '%s'"),
2264 /* Hash function for line_header_hash. */
2267 line_header_hash (const struct line_header *ofs)
2269 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2272 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2275 line_header_hash_voidp (const void *item)
2277 const struct line_header *ofs = (const struct line_header *) item;
2279 return line_header_hash (ofs);
2282 /* Equality function for line_header_hash. */
2285 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2287 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2288 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2290 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2291 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2296 /* Read the given attribute value as an address, taking the attribute's
2297 form into account. */
2300 attr_value_as_address (struct attribute *attr)
2304 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2306 /* Aside from a few clearly defined exceptions, attributes that
2307 contain an address must always be in DW_FORM_addr form.
2308 Unfortunately, some compilers happen to be violating this
2309 requirement by encoding addresses using other forms, such
2310 as DW_FORM_data4 for example. For those broken compilers,
2311 we try to do our best, without any guarantee of success,
2312 to interpret the address correctly. It would also be nice
2313 to generate a complaint, but that would require us to maintain
2314 a list of legitimate cases where a non-address form is allowed,
2315 as well as update callers to pass in at least the CU's DWARF
2316 version. This is more overhead than what we're willing to
2317 expand for a pretty rare case. */
2318 addr = DW_UNSND (attr);
2321 addr = DW_ADDR (attr);
2326 /* The suffix for an index file. */
2327 #define INDEX4_SUFFIX ".gdb-index"
2328 #define INDEX5_SUFFIX ".debug_names"
2329 #define DEBUG_STR_SUFFIX ".debug_str"
2331 /* See declaration. */
2333 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2334 const dwarf2_debug_sections *names)
2335 : objfile (objfile_)
2338 names = &dwarf2_elf_names;
2340 bfd *obfd = objfile->obfd;
2342 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2343 locate_sections (obfd, sec, *names);
2346 dwarf2_per_objfile::~dwarf2_per_objfile ()
2348 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2349 free_cached_comp_units ();
2351 if (quick_file_names_table)
2352 htab_delete (quick_file_names_table);
2354 if (line_header_hash)
2355 htab_delete (line_header_hash);
2357 /* Everything else should be on the objfile obstack. */
2360 /* See declaration. */
2363 dwarf2_per_objfile::free_cached_comp_units ()
2365 dwarf2_per_cu_data *per_cu = read_in_chain;
2366 dwarf2_per_cu_data **last_chain = &read_in_chain;
2367 while (per_cu != NULL)
2369 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2371 free_heap_comp_unit (per_cu->cu);
2372 *last_chain = next_cu;
2377 /* Try to locate the sections we need for DWARF 2 debugging
2378 information and return true if we have enough to do something.
2379 NAMES points to the dwarf2 section names, or is NULL if the standard
2380 ELF names are used. */
2383 dwarf2_has_info (struct objfile *objfile,
2384 const struct dwarf2_debug_sections *names)
2386 if (objfile->flags & OBJF_READNEVER)
2389 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2390 objfile_data (objfile, dwarf2_objfile_data_key));
2391 if (!dwarf2_per_objfile)
2393 /* Initialize per-objfile state. */
2394 struct dwarf2_per_objfile *data
2395 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
2397 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
2398 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
2400 return (!dwarf2_per_objfile->info.is_virtual
2401 && dwarf2_per_objfile->info.s.section != NULL
2402 && !dwarf2_per_objfile->abbrev.is_virtual
2403 && dwarf2_per_objfile->abbrev.s.section != NULL);
2406 /* Return the containing section of virtual section SECTION. */
2408 static struct dwarf2_section_info *
2409 get_containing_section (const struct dwarf2_section_info *section)
2411 gdb_assert (section->is_virtual);
2412 return section->s.containing_section;
2415 /* Return the bfd owner of SECTION. */
2418 get_section_bfd_owner (const struct dwarf2_section_info *section)
2420 if (section->is_virtual)
2422 section = get_containing_section (section);
2423 gdb_assert (!section->is_virtual);
2425 return section->s.section->owner;
2428 /* Return the bfd section of SECTION.
2429 Returns NULL if the section is not present. */
2432 get_section_bfd_section (const struct dwarf2_section_info *section)
2434 if (section->is_virtual)
2436 section = get_containing_section (section);
2437 gdb_assert (!section->is_virtual);
2439 return section->s.section;
2442 /* Return the name of SECTION. */
2445 get_section_name (const struct dwarf2_section_info *section)
2447 asection *sectp = get_section_bfd_section (section);
2449 gdb_assert (sectp != NULL);
2450 return bfd_section_name (get_section_bfd_owner (section), sectp);
2453 /* Return the name of the file SECTION is in. */
2456 get_section_file_name (const struct dwarf2_section_info *section)
2458 bfd *abfd = get_section_bfd_owner (section);
2460 return bfd_get_filename (abfd);
2463 /* Return the id of SECTION.
2464 Returns 0 if SECTION doesn't exist. */
2467 get_section_id (const struct dwarf2_section_info *section)
2469 asection *sectp = get_section_bfd_section (section);
2476 /* Return the flags of SECTION.
2477 SECTION (or containing section if this is a virtual section) must exist. */
2480 get_section_flags (const struct dwarf2_section_info *section)
2482 asection *sectp = get_section_bfd_section (section);
2484 gdb_assert (sectp != NULL);
2485 return bfd_get_section_flags (sectp->owner, sectp);
2488 /* When loading sections, we look either for uncompressed section or for
2489 compressed section names. */
2492 section_is_p (const char *section_name,
2493 const struct dwarf2_section_names *names)
2495 if (names->normal != NULL
2496 && strcmp (section_name, names->normal) == 0)
2498 if (names->compressed != NULL
2499 && strcmp (section_name, names->compressed) == 0)
2504 /* See declaration. */
2507 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2508 const dwarf2_debug_sections &names)
2510 flagword aflag = bfd_get_section_flags (abfd, sectp);
2512 if ((aflag & SEC_HAS_CONTENTS) == 0)
2515 else if (section_is_p (sectp->name, &names.info))
2517 this->info.s.section = sectp;
2518 this->info.size = bfd_get_section_size (sectp);
2520 else if (section_is_p (sectp->name, &names.abbrev))
2522 this->abbrev.s.section = sectp;
2523 this->abbrev.size = bfd_get_section_size (sectp);
2525 else if (section_is_p (sectp->name, &names.line))
2527 this->line.s.section = sectp;
2528 this->line.size = bfd_get_section_size (sectp);
2530 else if (section_is_p (sectp->name, &names.loc))
2532 this->loc.s.section = sectp;
2533 this->loc.size = bfd_get_section_size (sectp);
2535 else if (section_is_p (sectp->name, &names.loclists))
2537 this->loclists.s.section = sectp;
2538 this->loclists.size = bfd_get_section_size (sectp);
2540 else if (section_is_p (sectp->name, &names.macinfo))
2542 this->macinfo.s.section = sectp;
2543 this->macinfo.size = bfd_get_section_size (sectp);
2545 else if (section_is_p (sectp->name, &names.macro))
2547 this->macro.s.section = sectp;
2548 this->macro.size = bfd_get_section_size (sectp);
2550 else if (section_is_p (sectp->name, &names.str))
2552 this->str.s.section = sectp;
2553 this->str.size = bfd_get_section_size (sectp);
2555 else if (section_is_p (sectp->name, &names.line_str))
2557 this->line_str.s.section = sectp;
2558 this->line_str.size = bfd_get_section_size (sectp);
2560 else if (section_is_p (sectp->name, &names.addr))
2562 this->addr.s.section = sectp;
2563 this->addr.size = bfd_get_section_size (sectp);
2565 else if (section_is_p (sectp->name, &names.frame))
2567 this->frame.s.section = sectp;
2568 this->frame.size = bfd_get_section_size (sectp);
2570 else if (section_is_p (sectp->name, &names.eh_frame))
2572 this->eh_frame.s.section = sectp;
2573 this->eh_frame.size = bfd_get_section_size (sectp);
2575 else if (section_is_p (sectp->name, &names.ranges))
2577 this->ranges.s.section = sectp;
2578 this->ranges.size = bfd_get_section_size (sectp);
2580 else if (section_is_p (sectp->name, &names.rnglists))
2582 this->rnglists.s.section = sectp;
2583 this->rnglists.size = bfd_get_section_size (sectp);
2585 else if (section_is_p (sectp->name, &names.types))
2587 struct dwarf2_section_info type_section;
2589 memset (&type_section, 0, sizeof (type_section));
2590 type_section.s.section = sectp;
2591 type_section.size = bfd_get_section_size (sectp);
2593 VEC_safe_push (dwarf2_section_info_def, this->types,
2596 else if (section_is_p (sectp->name, &names.gdb_index))
2598 this->gdb_index.s.section = sectp;
2599 this->gdb_index.size = bfd_get_section_size (sectp);
2601 else if (section_is_p (sectp->name, &names.debug_names))
2603 this->debug_names.s.section = sectp;
2604 this->debug_names.size = bfd_get_section_size (sectp);
2606 else if (section_is_p (sectp->name, &names.debug_aranges))
2608 this->debug_aranges.s.section = sectp;
2609 this->debug_aranges.size = bfd_get_section_size (sectp);
2612 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2613 && bfd_section_vma (abfd, sectp) == 0)
2614 this->has_section_at_zero = true;
2617 /* A helper function that decides whether a section is empty,
2621 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2623 if (section->is_virtual)
2624 return section->size == 0;
2625 return section->s.section == NULL || section->size == 0;
2628 /* Read the contents of the section INFO.
2629 OBJFILE is the main object file, but not necessarily the file where
2630 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2632 If the section is compressed, uncompress it before returning. */
2635 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2639 gdb_byte *buf, *retbuf;
2643 info->buffer = NULL;
2646 if (dwarf2_section_empty_p (info))
2649 sectp = get_section_bfd_section (info);
2651 /* If this is a virtual section we need to read in the real one first. */
2652 if (info->is_virtual)
2654 struct dwarf2_section_info *containing_section =
2655 get_containing_section (info);
2657 gdb_assert (sectp != NULL);
2658 if ((sectp->flags & SEC_RELOC) != 0)
2660 error (_("Dwarf Error: DWP format V2 with relocations is not"
2661 " supported in section %s [in module %s]"),
2662 get_section_name (info), get_section_file_name (info));
2664 dwarf2_read_section (objfile, containing_section);
2665 /* Other code should have already caught virtual sections that don't
2667 gdb_assert (info->virtual_offset + info->size
2668 <= containing_section->size);
2669 /* If the real section is empty or there was a problem reading the
2670 section we shouldn't get here. */
2671 gdb_assert (containing_section->buffer != NULL);
2672 info->buffer = containing_section->buffer + info->virtual_offset;
2676 /* If the section has relocations, we must read it ourselves.
2677 Otherwise we attach it to the BFD. */
2678 if ((sectp->flags & SEC_RELOC) == 0)
2680 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2684 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2687 /* When debugging .o files, we may need to apply relocations; see
2688 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2689 We never compress sections in .o files, so we only need to
2690 try this when the section is not compressed. */
2691 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2694 info->buffer = retbuf;
2698 abfd = get_section_bfd_owner (info);
2699 gdb_assert (abfd != NULL);
2701 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2702 || bfd_bread (buf, info->size, abfd) != info->size)
2704 error (_("Dwarf Error: Can't read DWARF data"
2705 " in section %s [in module %s]"),
2706 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2710 /* A helper function that returns the size of a section in a safe way.
2711 If you are positive that the section has been read before using the
2712 size, then it is safe to refer to the dwarf2_section_info object's
2713 "size" field directly. In other cases, you must call this
2714 function, because for compressed sections the size field is not set
2715 correctly until the section has been read. */
2717 static bfd_size_type
2718 dwarf2_section_size (struct objfile *objfile,
2719 struct dwarf2_section_info *info)
2722 dwarf2_read_section (objfile, info);
2726 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2730 dwarf2_get_section_info (struct objfile *objfile,
2731 enum dwarf2_section_enum sect,
2732 asection **sectp, const gdb_byte **bufp,
2733 bfd_size_type *sizep)
2735 struct dwarf2_per_objfile *data
2736 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2737 dwarf2_objfile_data_key);
2738 struct dwarf2_section_info *info;
2740 /* We may see an objfile without any DWARF, in which case we just
2751 case DWARF2_DEBUG_FRAME:
2752 info = &data->frame;
2754 case DWARF2_EH_FRAME:
2755 info = &data->eh_frame;
2758 gdb_assert_not_reached ("unexpected section");
2761 dwarf2_read_section (objfile, info);
2763 *sectp = get_section_bfd_section (info);
2764 *bufp = info->buffer;
2765 *sizep = info->size;
2768 /* A helper function to find the sections for a .dwz file. */
2771 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2773 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2775 /* Note that we only support the standard ELF names, because .dwz
2776 is ELF-only (at the time of writing). */
2777 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2779 dwz_file->abbrev.s.section = sectp;
2780 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2782 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2784 dwz_file->info.s.section = sectp;
2785 dwz_file->info.size = bfd_get_section_size (sectp);
2787 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2789 dwz_file->str.s.section = sectp;
2790 dwz_file->str.size = bfd_get_section_size (sectp);
2792 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2794 dwz_file->line.s.section = sectp;
2795 dwz_file->line.size = bfd_get_section_size (sectp);
2797 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2799 dwz_file->macro.s.section = sectp;
2800 dwz_file->macro.size = bfd_get_section_size (sectp);
2802 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2804 dwz_file->gdb_index.s.section = sectp;
2805 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2807 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2809 dwz_file->debug_names.s.section = sectp;
2810 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2814 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2815 there is no .gnu_debugaltlink section in the file. Error if there
2816 is such a section but the file cannot be found. */
2818 static struct dwz_file *
2819 dwarf2_get_dwz_file (void)
2821 const char *filename;
2822 struct dwz_file *result;
2823 bfd_size_type buildid_len_arg;
2827 if (dwarf2_per_objfile->dwz_file != NULL)
2828 return dwarf2_per_objfile->dwz_file;
2830 bfd_set_error (bfd_error_no_error);
2831 gdb::unique_xmalloc_ptr<char> data
2832 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2833 &buildid_len_arg, &buildid));
2836 if (bfd_get_error () == bfd_error_no_error)
2838 error (_("could not read '.gnu_debugaltlink' section: %s"),
2839 bfd_errmsg (bfd_get_error ()));
2842 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2844 buildid_len = (size_t) buildid_len_arg;
2846 filename = data.get ();
2848 std::string abs_storage;
2849 if (!IS_ABSOLUTE_PATH (filename))
2851 gdb::unique_xmalloc_ptr<char> abs
2852 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2854 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2855 filename = abs_storage.c_str ();
2858 /* First try the file name given in the section. If that doesn't
2859 work, try to use the build-id instead. */
2860 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2861 if (dwz_bfd != NULL)
2863 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2867 if (dwz_bfd == NULL)
2868 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2870 if (dwz_bfd == NULL)
2871 error (_("could not find '.gnu_debugaltlink' file for %s"),
2872 objfile_name (dwarf2_per_objfile->objfile));
2874 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2876 result->dwz_bfd = dwz_bfd.release ();
2878 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
2880 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
2881 dwarf2_per_objfile->dwz_file = result;
2885 /* DWARF quick_symbols_functions support. */
2887 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2888 unique line tables, so we maintain a separate table of all .debug_line
2889 derived entries to support the sharing.
2890 All the quick functions need is the list of file names. We discard the
2891 line_header when we're done and don't need to record it here. */
2892 struct quick_file_names
2894 /* The data used to construct the hash key. */
2895 struct stmt_list_hash hash;
2897 /* The number of entries in file_names, real_names. */
2898 unsigned int num_file_names;
2900 /* The file names from the line table, after being run through
2902 const char **file_names;
2904 /* The file names from the line table after being run through
2905 gdb_realpath. These are computed lazily. */
2906 const char **real_names;
2909 /* When using the index (and thus not using psymtabs), each CU has an
2910 object of this type. This is used to hold information needed by
2911 the various "quick" methods. */
2912 struct dwarf2_per_cu_quick_data
2914 /* The file table. This can be NULL if there was no file table
2915 or it's currently not read in.
2916 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2917 struct quick_file_names *file_names;
2919 /* The corresponding symbol table. This is NULL if symbols for this
2920 CU have not yet been read. */
2921 struct compunit_symtab *compunit_symtab;
2923 /* A temporary mark bit used when iterating over all CUs in
2924 expand_symtabs_matching. */
2925 unsigned int mark : 1;
2927 /* True if we've tried to read the file table and found there isn't one.
2928 There will be no point in trying to read it again next time. */
2929 unsigned int no_file_data : 1;
2932 /* Utility hash function for a stmt_list_hash. */
2935 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2939 if (stmt_list_hash->dwo_unit != NULL)
2940 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2941 v += to_underlying (stmt_list_hash->line_sect_off);
2945 /* Utility equality function for a stmt_list_hash. */
2948 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2949 const struct stmt_list_hash *rhs)
2951 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2953 if (lhs->dwo_unit != NULL
2954 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2957 return lhs->line_sect_off == rhs->line_sect_off;
2960 /* Hash function for a quick_file_names. */
2963 hash_file_name_entry (const void *e)
2965 const struct quick_file_names *file_data
2966 = (const struct quick_file_names *) e;
2968 return hash_stmt_list_entry (&file_data->hash);
2971 /* Equality function for a quick_file_names. */
2974 eq_file_name_entry (const void *a, const void *b)
2976 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2977 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2979 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2982 /* Delete function for a quick_file_names. */
2985 delete_file_name_entry (void *e)
2987 struct quick_file_names *file_data = (struct quick_file_names *) e;
2990 for (i = 0; i < file_data->num_file_names; ++i)
2992 xfree ((void*) file_data->file_names[i]);
2993 if (file_data->real_names)
2994 xfree ((void*) file_data->real_names[i]);
2997 /* The space for the struct itself lives on objfile_obstack,
2998 so we don't free it here. */
3001 /* Create a quick_file_names hash table. */
3004 create_quick_file_names_table (unsigned int nr_initial_entries)
3006 return htab_create_alloc (nr_initial_entries,
3007 hash_file_name_entry, eq_file_name_entry,
3008 delete_file_name_entry, xcalloc, xfree);
3011 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
3012 have to be created afterwards. You should call age_cached_comp_units after
3013 processing PER_CU->CU. dw2_setup must have been already called. */
3016 load_cu (struct dwarf2_per_cu_data *per_cu)
3018 if (per_cu->is_debug_types)
3019 load_full_type_unit (per_cu);
3021 load_full_comp_unit (per_cu, language_minimal);
3023 if (per_cu->cu == NULL)
3024 return; /* Dummy CU. */
3026 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
3029 /* Read in the symbols for PER_CU. */
3032 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3034 struct cleanup *back_to;
3036 /* Skip type_unit_groups, reading the type units they contain
3037 is handled elsewhere. */
3038 if (IS_TYPE_UNIT_GROUP (per_cu))
3041 back_to = make_cleanup (dwarf2_release_queue, NULL);
3043 if (dwarf2_per_objfile->using_index
3044 ? per_cu->v.quick->compunit_symtab == NULL
3045 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
3047 queue_comp_unit (per_cu, language_minimal);
3050 /* If we just loaded a CU from a DWO, and we're working with an index
3051 that may badly handle TUs, load all the TUs in that DWO as well.
3052 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
3053 if (!per_cu->is_debug_types
3054 && per_cu->cu != NULL
3055 && per_cu->cu->dwo_unit != NULL
3056 && dwarf2_per_objfile->index_table != NULL
3057 && dwarf2_per_objfile->index_table->version <= 7
3058 /* DWP files aren't supported yet. */
3059 && get_dwp_file () == NULL)
3060 queue_and_load_all_dwo_tus (per_cu);
3065 /* Age the cache, releasing compilation units that have not
3066 been used recently. */
3067 age_cached_comp_units ();
3069 do_cleanups (back_to);
3072 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
3073 the objfile from which this CU came. Returns the resulting symbol
3076 static struct compunit_symtab *
3077 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3079 gdb_assert (dwarf2_per_objfile->using_index);
3080 if (!per_cu->v.quick->compunit_symtab)
3082 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
3083 scoped_restore decrementer = increment_reading_symtab ();
3084 dw2_do_instantiate_symtab (per_cu);
3085 process_cu_includes ();
3086 do_cleanups (back_to);
3089 return per_cu->v.quick->compunit_symtab;
3092 /* Return the CU/TU given its index.
3094 This is intended for loops like:
3096 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3097 + dwarf2_per_objfile->n_type_units); ++i)
3099 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3105 static struct dwarf2_per_cu_data *
3106 dw2_get_cutu (int index)
3108 if (index >= dwarf2_per_objfile->n_comp_units)
3110 index -= dwarf2_per_objfile->n_comp_units;
3111 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3112 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
3115 return dwarf2_per_objfile->all_comp_units[index];
3118 /* Return the CU given its index.
3119 This differs from dw2_get_cutu in that it's for when you know INDEX
3122 static struct dwarf2_per_cu_data *
3123 dw2_get_cu (int index)
3125 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
3127 return dwarf2_per_objfile->all_comp_units[index];
3130 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3131 objfile_obstack, and constructed with the specified field
3134 static dwarf2_per_cu_data *
3135 create_cu_from_index_list (struct objfile *objfile,
3136 struct dwarf2_section_info *section,
3138 sect_offset sect_off, ULONGEST length)
3140 dwarf2_per_cu_data *the_cu
3141 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3142 struct dwarf2_per_cu_data);
3143 the_cu->sect_off = sect_off;
3144 the_cu->length = length;
3145 the_cu->objfile = objfile;
3146 the_cu->section = section;
3147 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3148 struct dwarf2_per_cu_quick_data);
3149 the_cu->is_dwz = is_dwz;
3153 /* A helper for create_cus_from_index that handles a given list of
3157 create_cus_from_index_list (struct objfile *objfile,
3158 const gdb_byte *cu_list, offset_type n_elements,
3159 struct dwarf2_section_info *section,
3165 for (i = 0; i < n_elements; i += 2)
3167 gdb_static_assert (sizeof (ULONGEST) >= 8);
3169 sect_offset sect_off
3170 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3171 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3174 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
3175 = create_cu_from_index_list (objfile, section, is_dwz, sect_off, length);
3179 /* Read the CU list from the mapped index, and use it to create all
3180 the CU objects for this objfile. */
3183 create_cus_from_index (struct objfile *objfile,
3184 const gdb_byte *cu_list, offset_type cu_list_elements,
3185 const gdb_byte *dwz_list, offset_type dwz_elements)
3187 struct dwz_file *dwz;
3189 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
3190 dwarf2_per_objfile->all_comp_units =
3191 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3192 dwarf2_per_objfile->n_comp_units);
3194 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3195 &dwarf2_per_objfile->info, 0, 0);
3197 if (dwz_elements == 0)
3200 dwz = dwarf2_get_dwz_file ();
3201 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3202 cu_list_elements / 2);
3205 /* Create the signatured type hash table from the index. */
3208 create_signatured_type_table_from_index (struct objfile *objfile,
3209 struct dwarf2_section_info *section,
3210 const gdb_byte *bytes,
3211 offset_type elements)
3214 htab_t sig_types_hash;
3216 dwarf2_per_objfile->n_type_units
3217 = dwarf2_per_objfile->n_allocated_type_units
3219 dwarf2_per_objfile->all_type_units =
3220 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3222 sig_types_hash = allocate_signatured_type_table (objfile);
3224 for (i = 0; i < elements; i += 3)
3226 struct signatured_type *sig_type;
3229 cu_offset type_offset_in_tu;
3231 gdb_static_assert (sizeof (ULONGEST) >= 8);
3232 sect_offset sect_off
3233 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3235 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3237 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3240 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3241 struct signatured_type);
3242 sig_type->signature = signature;
3243 sig_type->type_offset_in_tu = type_offset_in_tu;
3244 sig_type->per_cu.is_debug_types = 1;
3245 sig_type->per_cu.section = section;
3246 sig_type->per_cu.sect_off = sect_off;
3247 sig_type->per_cu.objfile = objfile;
3248 sig_type->per_cu.v.quick
3249 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3250 struct dwarf2_per_cu_quick_data);
3252 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3255 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3258 dwarf2_per_objfile->signatured_types = sig_types_hash;
3261 /* Create the signatured type hash table from .debug_names. */
3264 create_signatured_type_table_from_debug_names
3265 (struct objfile *objfile,
3266 const mapped_debug_names &map,
3267 struct dwarf2_section_info *section,
3268 struct dwarf2_section_info *abbrev_section)
3270 dwarf2_read_section (objfile, section);
3271 dwarf2_read_section (objfile, abbrev_section);
3273 dwarf2_per_objfile->n_type_units
3274 = dwarf2_per_objfile->n_allocated_type_units
3276 dwarf2_per_objfile->all_type_units
3277 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3279 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3281 for (uint32_t i = 0; i < map.tu_count; ++i)
3283 struct signatured_type *sig_type;
3286 cu_offset type_offset_in_tu;
3288 sect_offset sect_off
3289 = (sect_offset) (extract_unsigned_integer
3290 (map.tu_table_reordered + i * map.offset_size,
3292 map.dwarf5_byte_order));
3294 comp_unit_head cu_header;
3295 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
3296 section->buffer + to_underlying (sect_off),
3299 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3300 struct signatured_type);
3301 sig_type->signature = cu_header.signature;
3302 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3303 sig_type->per_cu.is_debug_types = 1;
3304 sig_type->per_cu.section = section;
3305 sig_type->per_cu.sect_off = sect_off;
3306 sig_type->per_cu.objfile = objfile;
3307 sig_type->per_cu.v.quick
3308 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3309 struct dwarf2_per_cu_quick_data);
3311 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3314 dwarf2_per_objfile->all_type_units[i] = sig_type;
3317 dwarf2_per_objfile->signatured_types = sig_types_hash;
3320 /* Read the address map data from the mapped index, and use it to
3321 populate the objfile's psymtabs_addrmap. */
3324 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3326 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3327 const gdb_byte *iter, *end;
3328 struct addrmap *mutable_map;
3331 auto_obstack temp_obstack;
3333 mutable_map = addrmap_create_mutable (&temp_obstack);
3335 iter = index->address_table.data ();
3336 end = iter + index->address_table.size ();
3338 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3342 ULONGEST hi, lo, cu_index;
3343 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3345 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3347 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3352 complaint (&symfile_complaints,
3353 _(".gdb_index address table has invalid range (%s - %s)"),
3354 hex_string (lo), hex_string (hi));
3358 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3360 complaint (&symfile_complaints,
3361 _(".gdb_index address table has invalid CU number %u"),
3362 (unsigned) cu_index);
3366 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3367 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3368 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
3371 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3372 &objfile->objfile_obstack);
3375 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3376 populate the objfile's psymtabs_addrmap. */
3379 create_addrmap_from_aranges (struct objfile *objfile,
3380 struct dwarf2_section_info *section)
3382 bfd *abfd = objfile->obfd;
3383 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3384 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3385 SECT_OFF_TEXT (objfile));
3387 auto_obstack temp_obstack;
3388 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3390 std::unordered_map<sect_offset,
3391 dwarf2_per_cu_data *,
3392 gdb::hash_enum<sect_offset>>
3393 debug_info_offset_to_per_cu;
3394 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3396 dwarf2_per_cu_data *per_cu = dw2_get_cutu (cui);
3397 const auto insertpair
3398 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3399 if (!insertpair.second)
3401 warning (_("Section .debug_aranges in %s has duplicate "
3402 "debug_info_offset %u, ignoring .debug_aranges."),
3403 objfile_name (objfile), to_underlying (per_cu->sect_off));
3408 dwarf2_read_section (objfile, section);
3410 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3412 const gdb_byte *addr = section->buffer;
3414 while (addr < section->buffer + section->size)
3416 const gdb_byte *const entry_addr = addr;
3417 unsigned int bytes_read;
3419 const LONGEST entry_length = read_initial_length (abfd, addr,
3423 const gdb_byte *const entry_end = addr + entry_length;
3424 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3425 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3426 if (addr + entry_length > section->buffer + section->size)
3428 warning (_("Section .debug_aranges in %s entry at offset %zu "
3429 "length %s exceeds section length %s, "
3430 "ignoring .debug_aranges."),
3431 objfile_name (objfile), entry_addr - section->buffer,
3432 plongest (bytes_read + entry_length),
3433 pulongest (section->size));
3437 /* The version number. */
3438 const uint16_t version = read_2_bytes (abfd, addr);
3442 warning (_("Section .debug_aranges in %s entry at offset %zu "
3443 "has unsupported version %d, ignoring .debug_aranges."),
3444 objfile_name (objfile), entry_addr - section->buffer,
3449 const uint64_t debug_info_offset
3450 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3451 addr += offset_size;
3452 const auto per_cu_it
3453 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3454 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3456 warning (_("Section .debug_aranges in %s entry at offset %zu "
3457 "debug_info_offset %s does not exists, "
3458 "ignoring .debug_aranges."),
3459 objfile_name (objfile), entry_addr - section->buffer,
3460 pulongest (debug_info_offset));
3463 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3465 const uint8_t address_size = *addr++;
3466 if (address_size < 1 || address_size > 8)
3468 warning (_("Section .debug_aranges in %s entry at offset %zu "
3469 "address_size %u is invalid, ignoring .debug_aranges."),
3470 objfile_name (objfile), entry_addr - section->buffer,
3475 const uint8_t segment_selector_size = *addr++;
3476 if (segment_selector_size != 0)
3478 warning (_("Section .debug_aranges in %s entry at offset %zu "
3479 "segment_selector_size %u is not supported, "
3480 "ignoring .debug_aranges."),
3481 objfile_name (objfile), entry_addr - section->buffer,
3482 segment_selector_size);
3486 /* Must pad to an alignment boundary that is twice the address
3487 size. It is undocumented by the DWARF standard but GCC does
3489 for (size_t padding = ((-(addr - section->buffer))
3490 & (2 * address_size - 1));
3491 padding > 0; padding--)
3494 warning (_("Section .debug_aranges in %s entry at offset %zu "
3495 "padding is not zero, ignoring .debug_aranges."),
3496 objfile_name (objfile), entry_addr - section->buffer);
3502 if (addr + 2 * address_size > entry_end)
3504 warning (_("Section .debug_aranges in %s entry at offset %zu "
3505 "address list is not properly terminated, "
3506 "ignoring .debug_aranges."),
3507 objfile_name (objfile), entry_addr - section->buffer);
3510 ULONGEST start = extract_unsigned_integer (addr, address_size,
3512 addr += address_size;
3513 ULONGEST length = extract_unsigned_integer (addr, address_size,
3515 addr += address_size;
3516 if (start == 0 && length == 0)
3518 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3520 /* Symbol was eliminated due to a COMDAT group. */
3523 ULONGEST end = start + length;
3524 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3525 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3526 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3530 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3531 &objfile->objfile_obstack);
3534 /* The hash function for strings in the mapped index. This is the same as
3535 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3536 implementation. This is necessary because the hash function is tied to the
3537 format of the mapped index file. The hash values do not have to match with
3540 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3543 mapped_index_string_hash (int index_version, const void *p)
3545 const unsigned char *str = (const unsigned char *) p;
3549 while ((c = *str++) != 0)
3551 if (index_version >= 5)
3553 r = r * 67 + c - 113;
3559 /* Find a slot in the mapped index INDEX for the object named NAME.
3560 If NAME is found, set *VEC_OUT to point to the CU vector in the
3561 constant pool and return true. If NAME cannot be found, return
3565 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3566 offset_type **vec_out)
3569 offset_type slot, step;
3570 int (*cmp) (const char *, const char *);
3572 gdb::unique_xmalloc_ptr<char> without_params;
3573 if (current_language->la_language == language_cplus
3574 || current_language->la_language == language_fortran
3575 || current_language->la_language == language_d)
3577 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3580 if (strchr (name, '(') != NULL)
3582 without_params = cp_remove_params (name);
3584 if (without_params != NULL)
3585 name = without_params.get ();
3589 /* Index version 4 did not support case insensitive searches. But the
3590 indices for case insensitive languages are built in lowercase, therefore
3591 simulate our NAME being searched is also lowercased. */
3592 hash = mapped_index_string_hash ((index->version == 4
3593 && case_sensitivity == case_sensitive_off
3594 ? 5 : index->version),
3597 slot = hash & (index->symbol_table.size () - 1);
3598 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3599 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3605 const auto &bucket = index->symbol_table[slot];
3606 if (bucket.name == 0 && bucket.vec == 0)
3609 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3610 if (!cmp (name, str))
3612 *vec_out = (offset_type *) (index->constant_pool
3613 + MAYBE_SWAP (bucket.vec));
3617 slot = (slot + step) & (index->symbol_table.size () - 1);
3621 /* A helper function that reads the .gdb_index from SECTION and fills
3622 in MAP. FILENAME is the name of the file containing the section;
3623 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3624 ok to use deprecated sections.
3626 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3627 out parameters that are filled in with information about the CU and
3628 TU lists in the section.
3630 Returns 1 if all went well, 0 otherwise. */
3633 read_index_from_section (struct objfile *objfile,
3634 const char *filename,
3636 struct dwarf2_section_info *section,
3637 struct mapped_index *map,
3638 const gdb_byte **cu_list,
3639 offset_type *cu_list_elements,
3640 const gdb_byte **types_list,
3641 offset_type *types_list_elements)
3643 const gdb_byte *addr;
3644 offset_type version;
3645 offset_type *metadata;
3648 if (dwarf2_section_empty_p (section))
3651 /* Older elfutils strip versions could keep the section in the main
3652 executable while splitting it for the separate debug info file. */
3653 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3656 dwarf2_read_section (objfile, section);
3658 addr = section->buffer;
3659 /* Version check. */
3660 version = MAYBE_SWAP (*(offset_type *) addr);
3661 /* Versions earlier than 3 emitted every copy of a psymbol. This
3662 causes the index to behave very poorly for certain requests. Version 3
3663 contained incomplete addrmap. So, it seems better to just ignore such
3667 static int warning_printed = 0;
3668 if (!warning_printed)
3670 warning (_("Skipping obsolete .gdb_index section in %s."),
3672 warning_printed = 1;
3676 /* Index version 4 uses a different hash function than index version
3679 Versions earlier than 6 did not emit psymbols for inlined
3680 functions. Using these files will cause GDB not to be able to
3681 set breakpoints on inlined functions by name, so we ignore these
3682 indices unless the user has done
3683 "set use-deprecated-index-sections on". */
3684 if (version < 6 && !deprecated_ok)
3686 static int warning_printed = 0;
3687 if (!warning_printed)
3690 Skipping deprecated .gdb_index section in %s.\n\
3691 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3692 to use the section anyway."),
3694 warning_printed = 1;
3698 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3699 of the TU (for symbols coming from TUs),
3700 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3701 Plus gold-generated indices can have duplicate entries for global symbols,
3702 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3703 These are just performance bugs, and we can't distinguish gdb-generated
3704 indices from gold-generated ones, so issue no warning here. */
3706 /* Indexes with higher version than the one supported by GDB may be no
3707 longer backward compatible. */
3711 map->version = version;
3712 map->total_size = section->size;
3714 metadata = (offset_type *) (addr + sizeof (offset_type));
3717 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3718 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3722 *types_list = addr + MAYBE_SWAP (metadata[i]);
3723 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3724 - MAYBE_SWAP (metadata[i]))
3728 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3729 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3731 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3734 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3735 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3737 = gdb::array_view<mapped_index::symbol_table_slot>
3738 ((mapped_index::symbol_table_slot *) symbol_table,
3739 (mapped_index::symbol_table_slot *) symbol_table_end);
3742 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3747 /* Read .gdb_index. If everything went ok, initialize the "quick"
3748 elements of all the CUs and return 1. Otherwise, return 0. */
3751 dwarf2_read_index (struct objfile *objfile)
3753 struct mapped_index local_map, *map;
3754 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3755 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3756 struct dwz_file *dwz;
3758 if (!read_index_from_section (objfile, objfile_name (objfile),
3759 use_deprecated_index_sections,
3760 &dwarf2_per_objfile->gdb_index, &local_map,
3761 &cu_list, &cu_list_elements,
3762 &types_list, &types_list_elements))
3765 /* Don't use the index if it's empty. */
3766 if (local_map.symbol_table.empty ())
3769 /* If there is a .dwz file, read it so we can get its CU list as
3771 dwz = dwarf2_get_dwz_file ();
3774 struct mapped_index dwz_map;
3775 const gdb_byte *dwz_types_ignore;
3776 offset_type dwz_types_elements_ignore;
3778 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3780 &dwz->gdb_index, &dwz_map,
3781 &dwz_list, &dwz_list_elements,
3783 &dwz_types_elements_ignore))
3785 warning (_("could not read '.gdb_index' section from %s; skipping"),
3786 bfd_get_filename (dwz->dwz_bfd));
3791 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3794 if (types_list_elements)
3796 struct dwarf2_section_info *section;
3798 /* We can only handle a single .debug_types when we have an
3800 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3803 section = VEC_index (dwarf2_section_info_def,
3804 dwarf2_per_objfile->types, 0);
3806 create_signatured_type_table_from_index (objfile, section, types_list,
3807 types_list_elements);
3810 create_addrmap_from_index (objfile, &local_map);
3812 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3813 map = new (map) mapped_index ();
3816 dwarf2_per_objfile->index_table = map;
3817 dwarf2_per_objfile->using_index = 1;
3818 dwarf2_per_objfile->quick_file_names_table =
3819 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3824 /* A helper for the "quick" functions which sets the global
3825 dwarf2_per_objfile according to OBJFILE. */
3828 dw2_setup (struct objfile *objfile)
3830 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3831 objfile_data (objfile, dwarf2_objfile_data_key));
3832 gdb_assert (dwarf2_per_objfile);
3835 /* die_reader_func for dw2_get_file_names. */
3838 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3839 const gdb_byte *info_ptr,
3840 struct die_info *comp_unit_die,
3844 struct dwarf2_cu *cu = reader->cu;
3845 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3846 struct objfile *objfile = dwarf2_per_objfile->objfile;
3847 struct dwarf2_per_cu_data *lh_cu;
3848 struct attribute *attr;
3851 struct quick_file_names *qfn;
3853 gdb_assert (! this_cu->is_debug_types);
3855 /* Our callers never want to match partial units -- instead they
3856 will match the enclosing full CU. */
3857 if (comp_unit_die->tag == DW_TAG_partial_unit)
3859 this_cu->v.quick->no_file_data = 1;
3867 sect_offset line_offset {};
3869 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3872 struct quick_file_names find_entry;
3874 line_offset = (sect_offset) DW_UNSND (attr);
3876 /* We may have already read in this line header (TU line header sharing).
3877 If we have we're done. */
3878 find_entry.hash.dwo_unit = cu->dwo_unit;
3879 find_entry.hash.line_sect_off = line_offset;
3880 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3881 &find_entry, INSERT);
3884 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3888 lh = dwarf_decode_line_header (line_offset, cu);
3892 lh_cu->v.quick->no_file_data = 1;
3896 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3897 qfn->hash.dwo_unit = cu->dwo_unit;
3898 qfn->hash.line_sect_off = line_offset;
3899 gdb_assert (slot != NULL);
3902 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3904 qfn->num_file_names = lh->file_names.size ();
3906 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3907 for (i = 0; i < lh->file_names.size (); ++i)
3908 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3909 qfn->real_names = NULL;
3911 lh_cu->v.quick->file_names = qfn;
3914 /* A helper for the "quick" functions which attempts to read the line
3915 table for THIS_CU. */
3917 static struct quick_file_names *
3918 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3920 /* This should never be called for TUs. */
3921 gdb_assert (! this_cu->is_debug_types);
3922 /* Nor type unit groups. */
3923 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3925 if (this_cu->v.quick->file_names != NULL)
3926 return this_cu->v.quick->file_names;
3927 /* If we know there is no line data, no point in looking again. */
3928 if (this_cu->v.quick->no_file_data)
3931 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3933 if (this_cu->v.quick->no_file_data)
3935 return this_cu->v.quick->file_names;
3938 /* A helper for the "quick" functions which computes and caches the
3939 real path for a given file name from the line table. */
3942 dw2_get_real_path (struct objfile *objfile,
3943 struct quick_file_names *qfn, int index)
3945 if (qfn->real_names == NULL)
3946 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3947 qfn->num_file_names, const char *);
3949 if (qfn->real_names[index] == NULL)
3950 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3952 return qfn->real_names[index];
3955 static struct symtab *
3956 dw2_find_last_source_symtab (struct objfile *objfile)
3958 struct compunit_symtab *cust;
3961 dw2_setup (objfile);
3962 index = dwarf2_per_objfile->n_comp_units - 1;
3963 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3966 return compunit_primary_filetab (cust);
3969 /* Traversal function for dw2_forget_cached_source_info. */
3972 dw2_free_cached_file_names (void **slot, void *info)
3974 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3976 if (file_data->real_names)
3980 for (i = 0; i < file_data->num_file_names; ++i)
3982 xfree ((void*) file_data->real_names[i]);
3983 file_data->real_names[i] = NULL;
3991 dw2_forget_cached_source_info (struct objfile *objfile)
3993 dw2_setup (objfile);
3995 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3996 dw2_free_cached_file_names, NULL);
3999 /* Helper function for dw2_map_symtabs_matching_filename that expands
4000 the symtabs and calls the iterator. */
4003 dw2_map_expand_apply (struct objfile *objfile,
4004 struct dwarf2_per_cu_data *per_cu,
4005 const char *name, const char *real_path,
4006 gdb::function_view<bool (symtab *)> callback)
4008 struct compunit_symtab *last_made = objfile->compunit_symtabs;
4010 /* Don't visit already-expanded CUs. */
4011 if (per_cu->v.quick->compunit_symtab)
4014 /* This may expand more than one symtab, and we want to iterate over
4016 dw2_instantiate_symtab (per_cu);
4018 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
4019 last_made, callback);
4022 /* Implementation of the map_symtabs_matching_filename method. */
4025 dw2_map_symtabs_matching_filename
4026 (struct objfile *objfile, const char *name, const char *real_path,
4027 gdb::function_view<bool (symtab *)> callback)
4030 const char *name_basename = lbasename (name);
4032 dw2_setup (objfile);
4034 /* The rule is CUs specify all the files, including those used by
4035 any TU, so there's no need to scan TUs here. */
4037 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4040 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4041 struct quick_file_names *file_data;
4043 /* We only need to look at symtabs not already expanded. */
4044 if (per_cu->v.quick->compunit_symtab)
4047 file_data = dw2_get_file_names (per_cu);
4048 if (file_data == NULL)
4051 for (j = 0; j < file_data->num_file_names; ++j)
4053 const char *this_name = file_data->file_names[j];
4054 const char *this_real_name;
4056 if (compare_filenames_for_search (this_name, name))
4058 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4064 /* Before we invoke realpath, which can get expensive when many
4065 files are involved, do a quick comparison of the basenames. */
4066 if (! basenames_may_differ
4067 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
4070 this_real_name = dw2_get_real_path (objfile, file_data, j);
4071 if (compare_filenames_for_search (this_real_name, name))
4073 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4079 if (real_path != NULL)
4081 gdb_assert (IS_ABSOLUTE_PATH (real_path));
4082 gdb_assert (IS_ABSOLUTE_PATH (name));
4083 if (this_real_name != NULL
4084 && FILENAME_CMP (real_path, this_real_name) == 0)
4086 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4098 /* Struct used to manage iterating over all CUs looking for a symbol. */
4100 struct dw2_symtab_iterator
4102 /* The internalized form of .gdb_index. */
4103 struct mapped_index *index;
4104 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
4105 int want_specific_block;
4106 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
4107 Unused if !WANT_SPECIFIC_BLOCK. */
4109 /* The kind of symbol we're looking for. */
4111 /* The list of CUs from the index entry of the symbol,
4112 or NULL if not found. */
4114 /* The next element in VEC to look at. */
4116 /* The number of elements in VEC, or zero if there is no match. */
4118 /* Have we seen a global version of the symbol?
4119 If so we can ignore all further global instances.
4120 This is to work around gold/15646, inefficient gold-generated
4125 /* Initialize the index symtab iterator ITER.
4126 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
4127 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
4130 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
4131 struct mapped_index *index,
4132 int want_specific_block,
4137 iter->index = index;
4138 iter->want_specific_block = want_specific_block;
4139 iter->block_index = block_index;
4140 iter->domain = domain;
4142 iter->global_seen = 0;
4144 if (find_slot_in_mapped_hash (index, name, &iter->vec))
4145 iter->length = MAYBE_SWAP (*iter->vec);
4153 /* Return the next matching CU or NULL if there are no more. */
4155 static struct dwarf2_per_cu_data *
4156 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4158 for ( ; iter->next < iter->length; ++iter->next)
4160 offset_type cu_index_and_attrs =
4161 MAYBE_SWAP (iter->vec[iter->next + 1]);
4162 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4163 struct dwarf2_per_cu_data *per_cu;
4164 int want_static = iter->block_index != GLOBAL_BLOCK;
4165 /* This value is only valid for index versions >= 7. */
4166 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4167 gdb_index_symbol_kind symbol_kind =
4168 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4169 /* Only check the symbol attributes if they're present.
4170 Indices prior to version 7 don't record them,
4171 and indices >= 7 may elide them for certain symbols
4172 (gold does this). */
4174 (iter->index->version >= 7
4175 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4177 /* Don't crash on bad data. */
4178 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4179 + dwarf2_per_objfile->n_type_units))
4181 complaint (&symfile_complaints,
4182 _(".gdb_index entry has bad CU index"
4184 objfile_name (dwarf2_per_objfile->objfile));
4188 per_cu = dw2_get_cutu (cu_index);
4190 /* Skip if already read in. */
4191 if (per_cu->v.quick->compunit_symtab)
4194 /* Check static vs global. */
4197 if (iter->want_specific_block
4198 && want_static != is_static)
4200 /* Work around gold/15646. */
4201 if (!is_static && iter->global_seen)
4204 iter->global_seen = 1;
4207 /* Only check the symbol's kind if it has one. */
4210 switch (iter->domain)
4213 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4214 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4215 /* Some types are also in VAR_DOMAIN. */
4216 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4220 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4224 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4239 static struct compunit_symtab *
4240 dw2_lookup_symbol (struct objfile *objfile, int block_index,
4241 const char *name, domain_enum domain)
4243 struct compunit_symtab *stab_best = NULL;
4244 struct mapped_index *index;
4246 dw2_setup (objfile);
4248 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4250 index = dwarf2_per_objfile->index_table;
4252 /* index is NULL if OBJF_READNOW. */
4255 struct dw2_symtab_iterator iter;
4256 struct dwarf2_per_cu_data *per_cu;
4258 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
4260 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4262 struct symbol *sym, *with_opaque = NULL;
4263 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4264 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4265 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4267 sym = block_find_symbol (block, name, domain,
4268 block_find_non_opaque_type_preferred,
4271 /* Some caution must be observed with overloaded functions
4272 and methods, since the index will not contain any overload
4273 information (but NAME might contain it). */
4276 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4278 if (with_opaque != NULL
4279 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4282 /* Keep looking through other CUs. */
4290 dw2_print_stats (struct objfile *objfile)
4292 int i, total, count;
4294 dw2_setup (objfile);
4295 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4297 for (i = 0; i < total; ++i)
4299 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4301 if (!per_cu->v.quick->compunit_symtab)
4304 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4305 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4308 /* This dumps minimal information about the index.
4309 It is called via "mt print objfiles".
4310 One use is to verify .gdb_index has been loaded by the
4311 gdb.dwarf2/gdb-index.exp testcase. */
4314 dw2_dump (struct objfile *objfile)
4316 dw2_setup (objfile);
4317 gdb_assert (dwarf2_per_objfile->using_index);
4318 printf_filtered (".gdb_index:");
4319 if (dwarf2_per_objfile->index_table != NULL)
4321 printf_filtered (" version %d\n",
4322 dwarf2_per_objfile->index_table->version);
4325 printf_filtered (" faked for \"readnow\"\n");
4326 printf_filtered ("\n");
4330 dw2_relocate (struct objfile *objfile,
4331 const struct section_offsets *new_offsets,
4332 const struct section_offsets *delta)
4334 /* There's nothing to relocate here. */
4338 dw2_expand_symtabs_for_function (struct objfile *objfile,
4339 const char *func_name)
4341 struct mapped_index *index;
4343 dw2_setup (objfile);
4345 index = dwarf2_per_objfile->index_table;
4347 /* index is NULL if OBJF_READNOW. */
4350 struct dw2_symtab_iterator iter;
4351 struct dwarf2_per_cu_data *per_cu;
4353 /* Note: It doesn't matter what we pass for block_index here. */
4354 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4357 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4358 dw2_instantiate_symtab (per_cu);
4363 dw2_expand_all_symtabs (struct objfile *objfile)
4367 dw2_setup (objfile);
4369 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
4370 + dwarf2_per_objfile->n_type_units); ++i)
4372 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4374 dw2_instantiate_symtab (per_cu);
4379 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4380 const char *fullname)
4384 dw2_setup (objfile);
4386 /* We don't need to consider type units here.
4387 This is only called for examining code, e.g. expand_line_sal.
4388 There can be an order of magnitude (or more) more type units
4389 than comp units, and we avoid them if we can. */
4391 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4394 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4395 struct quick_file_names *file_data;
4397 /* We only need to look at symtabs not already expanded. */
4398 if (per_cu->v.quick->compunit_symtab)
4401 file_data = dw2_get_file_names (per_cu);
4402 if (file_data == NULL)
4405 for (j = 0; j < file_data->num_file_names; ++j)
4407 const char *this_fullname = file_data->file_names[j];
4409 if (filename_cmp (this_fullname, fullname) == 0)
4411 dw2_instantiate_symtab (per_cu);
4419 dw2_map_matching_symbols (struct objfile *objfile,
4420 const char * name, domain_enum domain,
4422 int (*callback) (struct block *,
4423 struct symbol *, void *),
4424 void *data, symbol_name_match_type match,
4425 symbol_compare_ftype *ordered_compare)
4427 /* Currently unimplemented; used for Ada. The function can be called if the
4428 current language is Ada for a non-Ada objfile using GNU index. As Ada
4429 does not look for non-Ada symbols this function should just return. */
4432 /* Symbol name matcher for .gdb_index names.
4434 Symbol names in .gdb_index have a few particularities:
4436 - There's no indication of which is the language of each symbol.
4438 Since each language has its own symbol name matching algorithm,
4439 and we don't know which language is the right one, we must match
4440 each symbol against all languages. This would be a potential
4441 performance problem if it were not mitigated by the
4442 mapped_index::name_components lookup table, which significantly
4443 reduces the number of times we need to call into this matcher,
4444 making it a non-issue.
4446 - Symbol names in the index have no overload (parameter)
4447 information. I.e., in C++, "foo(int)" and "foo(long)" both
4448 appear as "foo" in the index, for example.
4450 This means that the lookup names passed to the symbol name
4451 matcher functions must have no parameter information either
4452 because (e.g.) symbol search name "foo" does not match
4453 lookup-name "foo(int)" [while swapping search name for lookup
4456 class gdb_index_symbol_name_matcher
4459 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4460 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4462 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4463 Returns true if any matcher matches. */
4464 bool matches (const char *symbol_name);
4467 /* A reference to the lookup name we're matching against. */
4468 const lookup_name_info &m_lookup_name;
4470 /* A vector holding all the different symbol name matchers, for all
4472 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4475 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4476 (const lookup_name_info &lookup_name)
4477 : m_lookup_name (lookup_name)
4479 /* Prepare the vector of comparison functions upfront, to avoid
4480 doing the same work for each symbol. Care is taken to avoid
4481 matching with the same matcher more than once if/when multiple
4482 languages use the same matcher function. */
4483 auto &matchers = m_symbol_name_matcher_funcs;
4484 matchers.reserve (nr_languages);
4486 matchers.push_back (default_symbol_name_matcher);
4488 for (int i = 0; i < nr_languages; i++)
4490 const language_defn *lang = language_def ((enum language) i);
4491 if (lang->la_get_symbol_name_matcher != NULL)
4493 symbol_name_matcher_ftype *name_matcher
4494 = lang->la_get_symbol_name_matcher (m_lookup_name);
4496 /* Don't insert the same comparison routine more than once.
4497 Note that we do this linear walk instead of a cheaper
4498 sorted insert, or use a std::set or something like that,
4499 because relative order of function addresses is not
4500 stable. This is not a problem in practice because the
4501 number of supported languages is low, and the cost here
4502 is tiny compared to the number of searches we'll do
4503 afterwards using this object. */
4504 if (std::find (matchers.begin (), matchers.end (), name_matcher)
4506 matchers.push_back (name_matcher);
4512 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4514 for (auto matches_name : m_symbol_name_matcher_funcs)
4515 if (matches_name (symbol_name, m_lookup_name, NULL))
4521 /* Starting from a search name, return the string that finds the upper
4522 bound of all strings that start with SEARCH_NAME in a sorted name
4523 list. Returns the empty string to indicate that the upper bound is
4524 the end of the list. */
4527 make_sort_after_prefix_name (const char *search_name)
4529 /* When looking to complete "func", we find the upper bound of all
4530 symbols that start with "func" by looking for where we'd insert
4531 the closest string that would follow "func" in lexicographical
4532 order. Usually, that's "func"-with-last-character-incremented,
4533 i.e. "fund". Mind non-ASCII characters, though. Usually those
4534 will be UTF-8 multi-byte sequences, but we can't be certain.
4535 Especially mind the 0xff character, which is a valid character in
4536 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4537 rule out compilers allowing it in identifiers. Note that
4538 conveniently, strcmp/strcasecmp are specified to compare
4539 characters interpreted as unsigned char. So what we do is treat
4540 the whole string as a base 256 number composed of a sequence of
4541 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4542 to 0, and carries 1 to the following more-significant position.
4543 If the very first character in SEARCH_NAME ends up incremented
4544 and carries/overflows, then the upper bound is the end of the
4545 list. The string after the empty string is also the empty
4548 Some examples of this operation:
4550 SEARCH_NAME => "+1" RESULT
4554 "\xff" "a" "\xff" => "\xff" "b"
4559 Then, with these symbols for example:
4565 completing "func" looks for symbols between "func" and
4566 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4567 which finds "func" and "func1", but not "fund".
4571 funcÿ (Latin1 'ÿ' [0xff])
4575 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4576 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4580 ÿÿ (Latin1 'ÿ' [0xff])
4583 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4584 the end of the list.
4586 std::string after = search_name;
4587 while (!after.empty () && (unsigned char) after.back () == 0xff)
4589 if (!after.empty ())
4590 after.back () = (unsigned char) after.back () + 1;
4594 /* See declaration. */
4596 std::pair<std::vector<name_component>::const_iterator,
4597 std::vector<name_component>::const_iterator>
4598 mapped_index::find_name_components_bounds
4599 (const lookup_name_info &lookup_name_without_params) const
4602 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4605 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4607 /* Comparison function object for lower_bound that matches against a
4608 given symbol name. */
4609 auto lookup_compare_lower = [&] (const name_component &elem,
4612 const char *elem_qualified = this->symbol_name_at (elem.idx);
4613 const char *elem_name = elem_qualified + elem.name_offset;
4614 return name_cmp (elem_name, name) < 0;
4617 /* Comparison function object for upper_bound that matches against a
4618 given symbol name. */
4619 auto lookup_compare_upper = [&] (const char *name,
4620 const name_component &elem)
4622 const char *elem_qualified = this->symbol_name_at (elem.idx);
4623 const char *elem_name = elem_qualified + elem.name_offset;
4624 return name_cmp (name, elem_name) < 0;
4627 auto begin = this->name_components.begin ();
4628 auto end = this->name_components.end ();
4630 /* Find the lower bound. */
4633 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4636 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4639 /* Find the upper bound. */
4642 if (lookup_name_without_params.completion_mode ())
4644 /* In completion mode, we want UPPER to point past all
4645 symbols names that have the same prefix. I.e., with
4646 these symbols, and completing "func":
4648 function << lower bound
4650 other_function << upper bound
4652 We find the upper bound by looking for the insertion
4653 point of "func"-with-last-character-incremented,
4655 std::string after = make_sort_after_prefix_name (cplus);
4658 return std::lower_bound (lower, end, after.c_str (),
4659 lookup_compare_lower);
4662 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4665 return {lower, upper};
4668 /* See declaration. */
4671 mapped_index::build_name_components ()
4673 if (!this->name_components.empty ())
4676 this->name_components_casing = case_sensitivity;
4678 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4680 /* The code below only knows how to break apart components of C++
4681 symbol names (and other languages that use '::' as
4682 namespace/module separator). If we add support for wild matching
4683 to some language that uses some other operator (E.g., Ada, Go and
4684 D use '.'), then we'll need to try splitting the symbol name
4685 according to that language too. Note that Ada does support wild
4686 matching, but doesn't currently support .gdb_index. */
4687 for (offset_type idx = 0; idx < this->symbol_table.size (); ++idx)
4689 auto &bucket = this->symbol_table[idx];
4691 if (bucket.name == 0 && bucket.vec == 0)
4694 const char *name = this->symbol_name_at (idx);
4696 /* Add each name component to the name component table. */
4697 unsigned int previous_len = 0;
4698 for (unsigned int current_len = cp_find_first_component (name);
4699 name[current_len] != '\0';
4700 current_len += cp_find_first_component (name + current_len))
4702 gdb_assert (name[current_len] == ':');
4703 this->name_components.push_back ({previous_len, idx});
4704 /* Skip the '::'. */
4706 previous_len = current_len;
4708 this->name_components.push_back ({previous_len, idx});
4711 /* Sort name_components elements by name. */
4712 auto name_comp_compare = [&] (const name_component &left,
4713 const name_component &right)
4715 const char *left_qualified = this->symbol_name_at (left.idx);
4716 const char *right_qualified = this->symbol_name_at (right.idx);
4718 const char *left_name = left_qualified + left.name_offset;
4719 const char *right_name = right_qualified + right.name_offset;
4721 return name_cmp (left_name, right_name) < 0;
4724 std::sort (this->name_components.begin (),
4725 this->name_components.end (),
4729 /* Helper for dw2_expand_symtabs_matching that works with a
4730 mapped_index instead of the containing objfile. This is split to a
4731 separate function in order to be able to unit test the
4732 name_components matching using a mock mapped_index. For each
4733 symbol name that matches, calls MATCH_CALLBACK, passing it the
4734 symbol's index in the mapped_index symbol table. */
4737 dw2_expand_symtabs_matching_symbol
4738 (mapped_index &index,
4739 const lookup_name_info &lookup_name_in,
4740 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4741 enum search_domain kind,
4742 gdb::function_view<void (offset_type)> match_callback)
4744 lookup_name_info lookup_name_without_params
4745 = lookup_name_in.make_ignore_params ();
4746 gdb_index_symbol_name_matcher lookup_name_matcher
4747 (lookup_name_without_params);
4749 /* Build the symbol name component sorted vector, if we haven't
4751 index.build_name_components ();
4753 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4755 /* Now for each symbol name in range, check to see if we have a name
4756 match, and if so, call the MATCH_CALLBACK callback. */
4758 /* The same symbol may appear more than once in the range though.
4759 E.g., if we're looking for symbols that complete "w", and we have
4760 a symbol named "w1::w2", we'll find the two name components for
4761 that same symbol in the range. To be sure we only call the
4762 callback once per symbol, we first collect the symbol name
4763 indexes that matched in a temporary vector and ignore
4765 std::vector<offset_type> matches;
4766 matches.reserve (std::distance (bounds.first, bounds.second));
4768 for (; bounds.first != bounds.second; ++bounds.first)
4770 const char *qualified = index.symbol_name_at (bounds.first->idx);
4772 if (!lookup_name_matcher.matches (qualified)
4773 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4776 matches.push_back (bounds.first->idx);
4779 std::sort (matches.begin (), matches.end ());
4781 /* Finally call the callback, once per match. */
4783 for (offset_type idx : matches)
4787 match_callback (idx);
4792 /* Above we use a type wider than idx's for 'prev', since 0 and
4793 (offset_type)-1 are both possible values. */
4794 static_assert (sizeof (prev) > sizeof (offset_type), "");
4799 namespace selftests { namespace dw2_expand_symtabs_matching {
4801 /* A wrapper around mapped_index that builds a mock mapped_index, from
4802 the symbol list passed as parameter to the constructor. */
4803 class mock_mapped_index
4807 mock_mapped_index (const char *(&symbols)[N])
4808 : mock_mapped_index (symbols, N)
4811 /* Access the built index. */
4812 mapped_index &index ()
4816 mock_mapped_index(const mock_mapped_index &) = delete;
4817 void operator= (const mock_mapped_index &) = delete;
4820 mock_mapped_index (const char **symbols, size_t symbols_size)
4822 /* No string can live at offset zero. Add a dummy entry. */
4823 obstack_grow_str0 (&m_constant_pool, "");
4825 for (size_t i = 0; i < symbols_size; i++)
4827 const char *sym = symbols[i];
4828 size_t offset = obstack_object_size (&m_constant_pool);
4829 obstack_grow_str0 (&m_constant_pool, sym);
4830 m_symbol_table.push_back ({offset, 0});
4833 m_index.constant_pool = (const char *) obstack_base (&m_constant_pool);
4834 m_index.symbol_table = m_symbol_table;
4838 /* The built mapped_index. */
4839 mapped_index m_index{};
4841 /* The storage that the built mapped_index uses for symbol and
4842 constant pool tables. */
4843 std::vector<mapped_index::symbol_table_slot> m_symbol_table;
4844 auto_obstack m_constant_pool;
4847 /* Convenience function that converts a NULL pointer to a "<null>"
4848 string, to pass to print routines. */
4851 string_or_null (const char *str)
4853 return str != NULL ? str : "<null>";
4856 /* Check if a lookup_name_info built from
4857 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4858 index. EXPECTED_LIST is the list of expected matches, in expected
4859 matching order. If no match expected, then an empty list is
4860 specified. Returns true on success. On failure prints a warning
4861 indicating the file:line that failed, and returns false. */
4864 check_match (const char *file, int line,
4865 mock_mapped_index &mock_index,
4866 const char *name, symbol_name_match_type match_type,
4867 bool completion_mode,
4868 std::initializer_list<const char *> expected_list)
4870 lookup_name_info lookup_name (name, match_type, completion_mode);
4872 bool matched = true;
4874 auto mismatch = [&] (const char *expected_str,
4877 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4878 "expected=\"%s\", got=\"%s\"\n"),
4880 (match_type == symbol_name_match_type::FULL
4882 name, string_or_null (expected_str), string_or_null (got));
4886 auto expected_it = expected_list.begin ();
4887 auto expected_end = expected_list.end ();
4889 dw2_expand_symtabs_matching_symbol (mock_index.index (), lookup_name,
4891 [&] (offset_type idx)
4893 const char *matched_name = mock_index.index ().symbol_name_at (idx);
4894 const char *expected_str
4895 = expected_it == expected_end ? NULL : *expected_it++;
4897 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4898 mismatch (expected_str, matched_name);
4901 const char *expected_str
4902 = expected_it == expected_end ? NULL : *expected_it++;
4903 if (expected_str != NULL)
4904 mismatch (expected_str, NULL);
4909 /* The symbols added to the mock mapped_index for testing (in
4911 static const char *test_symbols[] = {
4920 "ns2::tmpl<int>::foo2",
4921 "(anonymous namespace)::A::B::C",
4923 /* These are used to check that the increment-last-char in the
4924 matching algorithm for completion doesn't match "t1_fund" when
4925 completing "t1_func". */
4931 /* A UTF-8 name with multi-byte sequences to make sure that
4932 cp-name-parser understands this as a single identifier ("função"
4933 is "function" in PT). */
4936 /* \377 (0xff) is Latin1 'ÿ'. */
4939 /* \377 (0xff) is Latin1 'ÿ'. */
4943 /* A name with all sorts of complications. Starts with "z" to make
4944 it easier for the completion tests below. */
4945 #define Z_SYM_NAME \
4946 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4947 "::tuple<(anonymous namespace)::ui*, " \
4948 "std::default_delete<(anonymous namespace)::ui>, void>"
4953 /* Returns true if the mapped_index::find_name_component_bounds method
4954 finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME, in
4958 check_find_bounds_finds (mapped_index &index,
4959 const char *search_name,
4960 gdb::array_view<const char *> expected_syms)
4962 lookup_name_info lookup_name (search_name,
4963 symbol_name_match_type::FULL, true);
4965 auto bounds = index.find_name_components_bounds (lookup_name);
4967 size_t distance = std::distance (bounds.first, bounds.second);
4968 if (distance != expected_syms.size ())
4971 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4973 auto nc_elem = bounds.first + exp_elem;
4974 const char *qualified = index.symbol_name_at (nc_elem->idx);
4975 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4982 /* Test the lower-level mapped_index::find_name_component_bounds
4986 test_mapped_index_find_name_component_bounds ()
4988 mock_mapped_index mock_index (test_symbols);
4990 mock_index.index ().build_name_components ();
4992 /* Test the lower-level mapped_index::find_name_component_bounds
4993 method in completion mode. */
4995 static const char *expected_syms[] = {
5000 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
5001 "t1_func", expected_syms));
5004 /* Check that the increment-last-char in the name matching algorithm
5005 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
5007 static const char *expected_syms1[] = {
5011 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
5012 "\377", expected_syms1));
5014 static const char *expected_syms2[] = {
5017 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
5018 "\377\377", expected_syms2));
5022 /* Test dw2_expand_symtabs_matching_symbol. */
5025 test_dw2_expand_symtabs_matching_symbol ()
5027 mock_mapped_index mock_index (test_symbols);
5029 /* We let all tests run until the end even if some fails, for debug
5031 bool any_mismatch = false;
5033 /* Create the expected symbols list (an initializer_list). Needed
5034 because lists have commas, and we need to pass them to CHECK,
5035 which is a macro. */
5036 #define EXPECT(...) { __VA_ARGS__ }
5038 /* Wrapper for check_match that passes down the current
5039 __FILE__/__LINE__. */
5040 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
5041 any_mismatch |= !check_match (__FILE__, __LINE__, \
5043 NAME, MATCH_TYPE, COMPLETION_MODE, \
5046 /* Identity checks. */
5047 for (const char *sym : test_symbols)
5049 /* Should be able to match all existing symbols. */
5050 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
5053 /* Should be able to match all existing symbols with
5055 std::string with_params = std::string (sym) + "(int)";
5056 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5059 /* Should be able to match all existing symbols with
5060 parameters and qualifiers. */
5061 with_params = std::string (sym) + " ( int ) const";
5062 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5065 /* This should really find sym, but cp-name-parser.y doesn't
5066 know about lvalue/rvalue qualifiers yet. */
5067 with_params = std::string (sym) + " ( int ) &&";
5068 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5072 /* Check that the name matching algorithm for completion doesn't get
5073 confused with Latin1 'ÿ' / 0xff. */
5075 static const char str[] = "\377";
5076 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5077 EXPECT ("\377", "\377\377123"));
5080 /* Check that the increment-last-char in the matching algorithm for
5081 completion doesn't match "t1_fund" when completing "t1_func". */
5083 static const char str[] = "t1_func";
5084 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5085 EXPECT ("t1_func", "t1_func1"));
5088 /* Check that completion mode works at each prefix of the expected
5091 static const char str[] = "function(int)";
5092 size_t len = strlen (str);
5095 for (size_t i = 1; i < len; i++)
5097 lookup.assign (str, i);
5098 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5099 EXPECT ("function"));
5103 /* While "w" is a prefix of both components, the match function
5104 should still only be called once. */
5106 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
5108 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
5112 /* Same, with a "complicated" symbol. */
5114 static const char str[] = Z_SYM_NAME;
5115 size_t len = strlen (str);
5118 for (size_t i = 1; i < len; i++)
5120 lookup.assign (str, i);
5121 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5122 EXPECT (Z_SYM_NAME));
5126 /* In FULL mode, an incomplete symbol doesn't match. */
5128 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
5132 /* A complete symbol with parameters matches any overload, since the
5133 index has no overload info. */
5135 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
5136 EXPECT ("std::zfunction", "std::zfunction2"));
5137 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
5138 EXPECT ("std::zfunction", "std::zfunction2"));
5139 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
5140 EXPECT ("std::zfunction", "std::zfunction2"));
5143 /* Check that whitespace is ignored appropriately. A symbol with a
5144 template argument list. */
5146 static const char expected[] = "ns::foo<int>";
5147 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
5149 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
5153 /* Check that whitespace is ignored appropriately. A symbol with a
5154 template argument list that includes a pointer. */
5156 static const char expected[] = "ns::foo<char*>";
5157 /* Try both completion and non-completion modes. */
5158 static const bool completion_mode[2] = {false, true};
5159 for (size_t i = 0; i < 2; i++)
5161 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
5162 completion_mode[i], EXPECT (expected));
5163 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
5164 completion_mode[i], EXPECT (expected));
5166 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
5167 completion_mode[i], EXPECT (expected));
5168 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
5169 completion_mode[i], EXPECT (expected));
5174 /* Check method qualifiers are ignored. */
5175 static const char expected[] = "ns::foo<char*>";
5176 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
5177 symbol_name_match_type::FULL, true, EXPECT (expected));
5178 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
5179 symbol_name_match_type::FULL, true, EXPECT (expected));
5180 CHECK_MATCH ("foo < char * > ( int ) const",
5181 symbol_name_match_type::WILD, true, EXPECT (expected));
5182 CHECK_MATCH ("foo < char * > ( int ) &&",
5183 symbol_name_match_type::WILD, true, EXPECT (expected));
5186 /* Test lookup names that don't match anything. */
5188 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
5191 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
5195 /* Some wild matching tests, exercising "(anonymous namespace)",
5196 which should not be confused with a parameter list. */
5198 static const char *syms[] = {
5202 "A :: B :: C ( int )",
5207 for (const char *s : syms)
5209 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
5210 EXPECT ("(anonymous namespace)::A::B::C"));
5215 static const char expected[] = "ns2::tmpl<int>::foo2";
5216 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5218 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5222 SELF_CHECK (!any_mismatch);
5231 test_mapped_index_find_name_component_bounds ();
5232 test_dw2_expand_symtabs_matching_symbol ();
5235 }} // namespace selftests::dw2_expand_symtabs_matching
5237 #endif /* GDB_SELF_TEST */
5239 /* If FILE_MATCHER is NULL or if PER_CU has
5240 dwarf2_per_cu_quick_data::MARK set (see
5241 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5242 EXPANSION_NOTIFY on it. */
5245 dw2_expand_symtabs_matching_one
5246 (struct dwarf2_per_cu_data *per_cu,
5247 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5248 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5250 if (file_matcher == NULL || per_cu->v.quick->mark)
5252 bool symtab_was_null
5253 = (per_cu->v.quick->compunit_symtab == NULL);
5255 dw2_instantiate_symtab (per_cu);
5257 if (expansion_notify != NULL
5259 && per_cu->v.quick->compunit_symtab != NULL)
5260 expansion_notify (per_cu->v.quick->compunit_symtab);
5264 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5265 matched, to expand corresponding CUs that were marked. IDX is the
5266 index of the symbol name that matched. */
5269 dw2_expand_marked_cus
5270 (mapped_index &index, offset_type idx,
5271 struct objfile *objfile,
5272 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5273 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5276 offset_type *vec, vec_len, vec_idx;
5277 bool global_seen = false;
5279 vec = (offset_type *) (index.constant_pool
5280 + MAYBE_SWAP (index.symbol_table[idx].vec));
5281 vec_len = MAYBE_SWAP (vec[0]);
5282 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5284 struct dwarf2_per_cu_data *per_cu;
5285 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5286 /* This value is only valid for index versions >= 7. */
5287 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5288 gdb_index_symbol_kind symbol_kind =
5289 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5290 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5291 /* Only check the symbol attributes if they're present.
5292 Indices prior to version 7 don't record them,
5293 and indices >= 7 may elide them for certain symbols
5294 (gold does this). */
5297 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5299 /* Work around gold/15646. */
5302 if (!is_static && global_seen)
5308 /* Only check the symbol's kind if it has one. */
5313 case VARIABLES_DOMAIN:
5314 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5317 case FUNCTIONS_DOMAIN:
5318 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5322 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5330 /* Don't crash on bad data. */
5331 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5332 + dwarf2_per_objfile->n_type_units))
5334 complaint (&symfile_complaints,
5335 _(".gdb_index entry has bad CU index"
5336 " [in module %s]"), objfile_name (objfile));
5340 per_cu = dw2_get_cutu (cu_index);
5341 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5346 /* If FILE_MATCHER is non-NULL, set all the
5347 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5348 that match FILE_MATCHER. */
5351 dw_expand_symtabs_matching_file_matcher
5352 (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5354 if (file_matcher == NULL)
5357 objfile *const objfile = dwarf2_per_objfile->objfile;
5359 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5361 NULL, xcalloc, xfree));
5362 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5364 NULL, xcalloc, xfree));
5366 /* The rule is CUs specify all the files, including those used by
5367 any TU, so there's no need to scan TUs here. */
5369 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5372 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5373 struct quick_file_names *file_data;
5378 per_cu->v.quick->mark = 0;
5380 /* We only need to look at symtabs not already expanded. */
5381 if (per_cu->v.quick->compunit_symtab)
5384 file_data = dw2_get_file_names (per_cu);
5385 if (file_data == NULL)
5388 if (htab_find (visited_not_found.get (), file_data) != NULL)
5390 else if (htab_find (visited_found.get (), file_data) != NULL)
5392 per_cu->v.quick->mark = 1;
5396 for (j = 0; j < file_data->num_file_names; ++j)
5398 const char *this_real_name;
5400 if (file_matcher (file_data->file_names[j], false))
5402 per_cu->v.quick->mark = 1;
5406 /* Before we invoke realpath, which can get expensive when many
5407 files are involved, do a quick comparison of the basenames. */
5408 if (!basenames_may_differ
5409 && !file_matcher (lbasename (file_data->file_names[j]),
5413 this_real_name = dw2_get_real_path (objfile, file_data, j);
5414 if (file_matcher (this_real_name, false))
5416 per_cu->v.quick->mark = 1;
5421 slot = htab_find_slot (per_cu->v.quick->mark
5422 ? visited_found.get ()
5423 : visited_not_found.get (),
5430 dw2_expand_symtabs_matching
5431 (struct objfile *objfile,
5432 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5433 const lookup_name_info &lookup_name,
5434 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5435 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5436 enum search_domain kind)
5440 dw2_setup (objfile);
5442 /* index_table is NULL if OBJF_READNOW. */
5443 if (!dwarf2_per_objfile->index_table)
5446 dw_expand_symtabs_matching_file_matcher (file_matcher);
5448 mapped_index &index = *dwarf2_per_objfile->index_table;
5450 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5452 kind, [&] (offset_type idx)
5454 dw2_expand_marked_cus (index, idx, objfile, file_matcher,
5455 expansion_notify, kind);
5459 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5462 static struct compunit_symtab *
5463 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5468 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5469 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5472 if (cust->includes == NULL)
5475 for (i = 0; cust->includes[i]; ++i)
5477 struct compunit_symtab *s = cust->includes[i];
5479 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5487 static struct compunit_symtab *
5488 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5489 struct bound_minimal_symbol msymbol,
5491 struct obj_section *section,
5494 struct dwarf2_per_cu_data *data;
5495 struct compunit_symtab *result;
5497 dw2_setup (objfile);
5499 if (!objfile->psymtabs_addrmap)
5502 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5507 if (warn_if_readin && data->v.quick->compunit_symtab)
5508 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5509 paddress (get_objfile_arch (objfile), pc));
5512 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5514 gdb_assert (result != NULL);
5519 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5520 void *data, int need_fullname)
5522 dw2_setup (objfile);
5524 if (!dwarf2_per_objfile->filenames_cache)
5526 dwarf2_per_objfile->filenames_cache.emplace ();
5528 htab_up visited (htab_create_alloc (10,
5529 htab_hash_pointer, htab_eq_pointer,
5530 NULL, xcalloc, xfree));
5532 /* The rule is CUs specify all the files, including those used
5533 by any TU, so there's no need to scan TUs here. We can
5534 ignore file names coming from already-expanded CUs. */
5536 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5538 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
5540 if (per_cu->v.quick->compunit_symtab)
5542 void **slot = htab_find_slot (visited.get (),
5543 per_cu->v.quick->file_names,
5546 *slot = per_cu->v.quick->file_names;
5550 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5552 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5553 struct quick_file_names *file_data;
5556 /* We only need to look at symtabs not already expanded. */
5557 if (per_cu->v.quick->compunit_symtab)
5560 file_data = dw2_get_file_names (per_cu);
5561 if (file_data == NULL)
5564 slot = htab_find_slot (visited.get (), file_data, INSERT);
5567 /* Already visited. */
5572 for (int j = 0; j < file_data->num_file_names; ++j)
5574 const char *filename = file_data->file_names[j];
5575 dwarf2_per_objfile->filenames_cache->seen (filename);
5580 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5582 gdb::unique_xmalloc_ptr<char> this_real_name;
5585 this_real_name = gdb_realpath (filename);
5586 (*fun) (filename, this_real_name.get (), data);
5591 dw2_has_symbols (struct objfile *objfile)
5596 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5599 dw2_find_last_source_symtab,
5600 dw2_forget_cached_source_info,
5601 dw2_map_symtabs_matching_filename,
5606 dw2_expand_symtabs_for_function,
5607 dw2_expand_all_symtabs,
5608 dw2_expand_symtabs_with_fullname,
5609 dw2_map_matching_symbols,
5610 dw2_expand_symtabs_matching,
5611 dw2_find_pc_sect_compunit_symtab,
5613 dw2_map_symbol_filenames
5616 /* DWARF-5 debug_names reader. */
5618 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5619 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5621 /* A helper function that reads the .debug_names section in SECTION
5622 and fills in MAP. FILENAME is the name of the file containing the
5623 section; it is used for error reporting.
5625 Returns true if all went well, false otherwise. */
5628 read_debug_names_from_section (struct objfile *objfile,
5629 const char *filename,
5630 struct dwarf2_section_info *section,
5631 mapped_debug_names &map)
5633 if (dwarf2_section_empty_p (section))
5636 /* Older elfutils strip versions could keep the section in the main
5637 executable while splitting it for the separate debug info file. */
5638 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5641 dwarf2_read_section (objfile, section);
5643 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5645 const gdb_byte *addr = section->buffer;
5647 bfd *const abfd = get_section_bfd_owner (section);
5649 unsigned int bytes_read;
5650 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5653 map.dwarf5_is_dwarf64 = bytes_read != 4;
5654 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5655 if (bytes_read + length != section->size)
5657 /* There may be multiple per-CU indices. */
5658 warning (_("Section .debug_names in %s length %s does not match "
5659 "section length %s, ignoring .debug_names."),
5660 filename, plongest (bytes_read + length),
5661 pulongest (section->size));
5665 /* The version number. */
5666 uint16_t version = read_2_bytes (abfd, addr);
5670 warning (_("Section .debug_names in %s has unsupported version %d, "
5671 "ignoring .debug_names."),
5677 uint16_t padding = read_2_bytes (abfd, addr);
5681 warning (_("Section .debug_names in %s has unsupported padding %d, "
5682 "ignoring .debug_names."),
5687 /* comp_unit_count - The number of CUs in the CU list. */
5688 map.cu_count = read_4_bytes (abfd, addr);
5691 /* local_type_unit_count - The number of TUs in the local TU
5693 map.tu_count = read_4_bytes (abfd, addr);
5696 /* foreign_type_unit_count - The number of TUs in the foreign TU
5698 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5700 if (foreign_tu_count != 0)
5702 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5703 "ignoring .debug_names."),
5704 filename, static_cast<unsigned long> (foreign_tu_count));
5708 /* bucket_count - The number of hash buckets in the hash lookup
5710 map.bucket_count = read_4_bytes (abfd, addr);
5713 /* name_count - The number of unique names in the index. */
5714 map.name_count = read_4_bytes (abfd, addr);
5717 /* abbrev_table_size - The size in bytes of the abbreviations
5719 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5722 /* augmentation_string_size - The size in bytes of the augmentation
5723 string. This value is rounded up to a multiple of 4. */
5724 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5726 map.augmentation_is_gdb = ((augmentation_string_size
5727 == sizeof (dwarf5_augmentation))
5728 && memcmp (addr, dwarf5_augmentation,
5729 sizeof (dwarf5_augmentation)) == 0);
5730 augmentation_string_size += (-augmentation_string_size) & 3;
5731 addr += augmentation_string_size;
5734 map.cu_table_reordered = addr;
5735 addr += map.cu_count * map.offset_size;
5737 /* List of Local TUs */
5738 map.tu_table_reordered = addr;
5739 addr += map.tu_count * map.offset_size;
5741 /* Hash Lookup Table */
5742 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5743 addr += map.bucket_count * 4;
5744 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5745 addr += map.name_count * 4;
5748 map.name_table_string_offs_reordered = addr;
5749 addr += map.name_count * map.offset_size;
5750 map.name_table_entry_offs_reordered = addr;
5751 addr += map.name_count * map.offset_size;
5753 const gdb_byte *abbrev_table_start = addr;
5756 unsigned int bytes_read;
5757 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5762 const auto insertpair
5763 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5764 if (!insertpair.second)
5766 warning (_("Section .debug_names in %s has duplicate index %s, "
5767 "ignoring .debug_names."),
5768 filename, pulongest (index_num));
5771 mapped_debug_names::index_val &indexval = insertpair.first->second;
5772 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5777 mapped_debug_names::index_val::attr attr;
5778 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5780 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5782 if (attr.form == DW_FORM_implicit_const)
5784 attr.implicit_const = read_signed_leb128 (abfd, addr,
5788 if (attr.dw_idx == 0 && attr.form == 0)
5790 indexval.attr_vec.push_back (std::move (attr));
5793 if (addr != abbrev_table_start + abbrev_table_size)
5795 warning (_("Section .debug_names in %s has abbreviation_table "
5796 "of size %zu vs. written as %u, ignoring .debug_names."),
5797 filename, addr - abbrev_table_start, abbrev_table_size);
5800 map.entry_pool = addr;
5805 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5809 create_cus_from_debug_names_list (struct objfile *objfile,
5810 const mapped_debug_names &map,
5811 dwarf2_section_info §ion,
5812 bool is_dwz, int base_offset)
5814 sect_offset sect_off_prev;
5815 for (uint32_t i = 0; i <= map.cu_count; ++i)
5817 sect_offset sect_off_next;
5818 if (i < map.cu_count)
5821 = (sect_offset) (extract_unsigned_integer
5822 (map.cu_table_reordered + i * map.offset_size,
5824 map.dwarf5_byte_order));
5827 sect_off_next = (sect_offset) section.size;
5830 const ULONGEST length = sect_off_next - sect_off_prev;
5831 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
5832 = create_cu_from_index_list (objfile, §ion, is_dwz,
5833 sect_off_prev, length);
5835 sect_off_prev = sect_off_next;
5839 /* Read the CU list from the mapped index, and use it to create all
5840 the CU objects for this objfile. */
5843 create_cus_from_debug_names (struct objfile *objfile,
5844 const mapped_debug_names &map,
5845 const mapped_debug_names &dwz_map)
5848 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
5849 dwarf2_per_objfile->all_comp_units
5850 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
5851 dwarf2_per_objfile->n_comp_units);
5853 create_cus_from_debug_names_list (objfile, map, dwarf2_per_objfile->info,
5855 0 /* base_offset */);
5857 if (dwz_map.cu_count == 0)
5860 dwz_file *dwz = dwarf2_get_dwz_file ();
5861 create_cus_from_debug_names_list (objfile, dwz_map, dwz->info,
5863 map.cu_count /* base_offset */);
5866 /* Read .debug_names. If everything went ok, initialize the "quick"
5867 elements of all the CUs and return true. Otherwise, return false. */
5870 dwarf2_read_debug_names (struct objfile *objfile)
5872 mapped_debug_names local_map, dwz_map;
5874 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5875 &dwarf2_per_objfile->debug_names,
5879 /* Don't use the index if it's empty. */
5880 if (local_map.name_count == 0)
5883 /* If there is a .dwz file, read it so we can get its CU list as
5885 dwz_file *dwz = dwarf2_get_dwz_file ();
5888 if (!read_debug_names_from_section (objfile,
5889 bfd_get_filename (dwz->dwz_bfd),
5890 &dwz->debug_names, dwz_map))
5892 warning (_("could not read '.debug_names' section from %s; skipping"),
5893 bfd_get_filename (dwz->dwz_bfd));
5898 create_cus_from_debug_names (objfile, local_map, dwz_map);
5900 if (local_map.tu_count != 0)
5902 /* We can only handle a single .debug_types when we have an
5904 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5907 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5908 dwarf2_per_objfile->types, 0);
5910 create_signatured_type_table_from_debug_names
5911 (objfile, local_map, section, &dwarf2_per_objfile->abbrev);
5914 create_addrmap_from_aranges (objfile, &dwarf2_per_objfile->debug_aranges);
5916 dwarf2_per_objfile->debug_names_table.reset (new mapped_debug_names);
5917 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
5918 dwarf2_per_objfile->using_index = 1;
5919 dwarf2_per_objfile->quick_file_names_table =
5920 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
5925 /* Symbol name hashing function as specified by DWARF-5. */
5928 dwarf5_djb_hash (const char *str_)
5930 const unsigned char *str = (const unsigned char *) str_;
5932 /* Note: tolower here ignores UTF-8, which isn't fully compliant.
5933 See http://dwarfstd.org/ShowIssue.php?issue=161027.1. */
5935 uint32_t hash = 5381;
5936 while (int c = *str++)
5937 hash = hash * 33 + tolower (c);
5941 /* Type used to manage iterating over all CUs looking for a symbol for
5944 class dw2_debug_names_iterator
5947 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
5948 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
5949 dw2_debug_names_iterator (const mapped_debug_names &map,
5950 bool want_specific_block,
5951 block_enum block_index, domain_enum domain,
5953 : m_map (map), m_want_specific_block (want_specific_block),
5954 m_block_index (block_index), m_domain (domain),
5955 m_addr (find_vec_in_debug_names (map, name))
5958 dw2_debug_names_iterator (const mapped_debug_names &map,
5959 search_domain search, uint32_t namei)
5962 m_addr (find_vec_in_debug_names (map, namei))
5965 /* Return the next matching CU or NULL if there are no more. */
5966 dwarf2_per_cu_data *next ();
5969 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5971 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5974 /* The internalized form of .debug_names. */
5975 const mapped_debug_names &m_map;
5977 /* If true, only look for symbols that match BLOCK_INDEX. */
5978 const bool m_want_specific_block = false;
5980 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
5981 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
5983 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
5985 /* The kind of symbol we're looking for. */
5986 const domain_enum m_domain = UNDEF_DOMAIN;
5987 const search_domain m_search = ALL_DOMAIN;
5989 /* The list of CUs from the index entry of the symbol, or NULL if
5991 const gdb_byte *m_addr;
5995 mapped_debug_names::namei_to_name (uint32_t namei) const
5997 const ULONGEST namei_string_offs
5998 = extract_unsigned_integer ((name_table_string_offs_reordered
5999 + namei * offset_size),
6002 return read_indirect_string_at_offset
6003 (dwarf2_per_objfile->objfile->obfd, namei_string_offs);
6006 /* Find a slot in .debug_names for the object named NAME. If NAME is
6007 found, return pointer to its pool data. If NAME cannot be found,
6011 dw2_debug_names_iterator::find_vec_in_debug_names
6012 (const mapped_debug_names &map, const char *name)
6014 int (*cmp) (const char *, const char *);
6016 if (current_language->la_language == language_cplus
6017 || current_language->la_language == language_fortran
6018 || current_language->la_language == language_d)
6020 /* NAME is already canonical. Drop any qualifiers as
6021 .debug_names does not contain any. */
6023 if (strchr (name, '(') != NULL)
6025 gdb::unique_xmalloc_ptr<char> without_params
6026 = cp_remove_params (name);
6028 if (without_params != NULL)
6030 name = without_params.get();
6035 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
6037 const uint32_t full_hash = dwarf5_djb_hash (name);
6039 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6040 (map.bucket_table_reordered
6041 + (full_hash % map.bucket_count)), 4,
6042 map.dwarf5_byte_order);
6046 if (namei >= map.name_count)
6048 complaint (&symfile_complaints,
6049 _("Wrong .debug_names with name index %u but name_count=%u "
6051 namei, map.name_count,
6052 objfile_name (dwarf2_per_objfile->objfile));
6058 const uint32_t namei_full_hash
6059 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6060 (map.hash_table_reordered + namei), 4,
6061 map.dwarf5_byte_order);
6062 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
6065 if (full_hash == namei_full_hash)
6067 const char *const namei_string = map.namei_to_name (namei);
6069 #if 0 /* An expensive sanity check. */
6070 if (namei_full_hash != dwarf5_djb_hash (namei_string))
6072 complaint (&symfile_complaints,
6073 _("Wrong .debug_names hash for string at index %u "
6075 namei, objfile_name (dwarf2_per_objfile->objfile));
6080 if (cmp (namei_string, name) == 0)
6082 const ULONGEST namei_entry_offs
6083 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6084 + namei * map.offset_size),
6085 map.offset_size, map.dwarf5_byte_order);
6086 return map.entry_pool + namei_entry_offs;
6091 if (namei >= map.name_count)
6097 dw2_debug_names_iterator::find_vec_in_debug_names
6098 (const mapped_debug_names &map, uint32_t namei)
6100 if (namei >= map.name_count)
6102 complaint (&symfile_complaints,
6103 _("Wrong .debug_names with name index %u but name_count=%u "
6105 namei, map.name_count,
6106 objfile_name (dwarf2_per_objfile->objfile));
6110 const ULONGEST namei_entry_offs
6111 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6112 + namei * map.offset_size),
6113 map.offset_size, map.dwarf5_byte_order);
6114 return map.entry_pool + namei_entry_offs;
6117 /* See dw2_debug_names_iterator. */
6119 dwarf2_per_cu_data *
6120 dw2_debug_names_iterator::next ()
6125 bfd *const abfd = dwarf2_per_objfile->objfile->obfd;
6129 unsigned int bytes_read;
6130 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6131 m_addr += bytes_read;
6135 const auto indexval_it = m_map.abbrev_map.find (abbrev);
6136 if (indexval_it == m_map.abbrev_map.cend ())
6138 complaint (&symfile_complaints,
6139 _("Wrong .debug_names undefined abbrev code %s "
6141 pulongest (abbrev), objfile_name (dwarf2_per_objfile->objfile));
6144 const mapped_debug_names::index_val &indexval = indexval_it->second;
6145 bool have_is_static = false;
6147 dwarf2_per_cu_data *per_cu = NULL;
6148 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
6153 case DW_FORM_implicit_const:
6154 ull = attr.implicit_const;
6156 case DW_FORM_flag_present:
6160 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6161 m_addr += bytes_read;
6164 complaint (&symfile_complaints,
6165 _("Unsupported .debug_names form %s [in module %s]"),
6166 dwarf_form_name (attr.form),
6167 objfile_name (dwarf2_per_objfile->objfile));
6170 switch (attr.dw_idx)
6172 case DW_IDX_compile_unit:
6173 /* Don't crash on bad data. */
6174 if (ull >= (dwarf2_per_objfile->n_comp_units
6175 + dwarf2_per_objfile->n_type_units))
6177 complaint (&symfile_complaints,
6178 _(".debug_names entry has bad CU index %s"
6181 objfile_name (dwarf2_per_objfile->objfile));
6184 per_cu = dw2_get_cutu (ull);
6186 case DW_IDX_GNU_internal:
6187 if (!m_map.augmentation_is_gdb)
6189 have_is_static = true;
6192 case DW_IDX_GNU_external:
6193 if (!m_map.augmentation_is_gdb)
6195 have_is_static = true;
6201 /* Skip if already read in. */
6202 if (per_cu->v.quick->compunit_symtab)
6205 /* Check static vs global. */
6208 const bool want_static = m_block_index != GLOBAL_BLOCK;
6209 if (m_want_specific_block && want_static != is_static)
6213 /* Match dw2_symtab_iter_next, symbol_kind
6214 and debug_names::psymbol_tag. */
6218 switch (indexval.dwarf_tag)
6220 case DW_TAG_variable:
6221 case DW_TAG_subprogram:
6222 /* Some types are also in VAR_DOMAIN. */
6223 case DW_TAG_typedef:
6224 case DW_TAG_structure_type:
6231 switch (indexval.dwarf_tag)
6233 case DW_TAG_typedef:
6234 case DW_TAG_structure_type:
6241 switch (indexval.dwarf_tag)
6244 case DW_TAG_variable:
6254 /* Match dw2_expand_symtabs_matching, symbol_kind and
6255 debug_names::psymbol_tag. */
6258 case VARIABLES_DOMAIN:
6259 switch (indexval.dwarf_tag)
6261 case DW_TAG_variable:
6267 case FUNCTIONS_DOMAIN:
6268 switch (indexval.dwarf_tag)
6270 case DW_TAG_subprogram:
6277 switch (indexval.dwarf_tag)
6279 case DW_TAG_typedef:
6280 case DW_TAG_structure_type:
6293 static struct compunit_symtab *
6294 dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6295 const char *name, domain_enum domain)
6297 const block_enum block_index = static_cast<block_enum> (block_index_int);
6298 dw2_setup (objfile);
6300 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6303 /* index is NULL if OBJF_READNOW. */
6306 const auto &map = *mapp;
6308 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6309 block_index, domain, name);
6311 struct compunit_symtab *stab_best = NULL;
6312 struct dwarf2_per_cu_data *per_cu;
6313 while ((per_cu = iter.next ()) != NULL)
6315 struct symbol *sym, *with_opaque = NULL;
6316 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6317 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6318 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6320 sym = block_find_symbol (block, name, domain,
6321 block_find_non_opaque_type_preferred,
6324 /* Some caution must be observed with overloaded functions and
6325 methods, since the index will not contain any overload
6326 information (but NAME might contain it). */
6329 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6331 if (with_opaque != NULL
6332 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6335 /* Keep looking through other CUs. */
6341 /* This dumps minimal information about .debug_names. It is called
6342 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6343 uses this to verify that .debug_names has been loaded. */
6346 dw2_debug_names_dump (struct objfile *objfile)
6348 dw2_setup (objfile);
6349 gdb_assert (dwarf2_per_objfile->using_index);
6350 printf_filtered (".debug_names:");
6351 if (dwarf2_per_objfile->debug_names_table)
6352 printf_filtered (" exists\n");
6354 printf_filtered (" faked for \"readnow\"\n");
6355 printf_filtered ("\n");
6359 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6360 const char *func_name)
6362 dw2_setup (objfile);
6364 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6365 if (dwarf2_per_objfile->debug_names_table)
6367 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6369 /* Note: It doesn't matter what we pass for block_index here. */
6370 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6371 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
6373 struct dwarf2_per_cu_data *per_cu;
6374 while ((per_cu = iter.next ()) != NULL)
6375 dw2_instantiate_symtab (per_cu);
6380 dw2_debug_names_expand_symtabs_matching
6381 (struct objfile *objfile,
6382 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6383 const lookup_name_info &lookup_name,
6384 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6385 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6386 enum search_domain kind)
6388 dw2_setup (objfile);
6390 /* debug_names_table is NULL if OBJF_READNOW. */
6391 if (!dwarf2_per_objfile->debug_names_table)
6394 dw_expand_symtabs_matching_file_matcher (file_matcher);
6396 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6398 for (uint32_t namei = 0; namei < map.name_count; ++namei)
6402 const char *const namei_string = map.namei_to_name (namei);
6403 if (symbol_matcher != NULL && !symbol_matcher (namei_string))
6406 /* The name was matched, now expand corresponding CUs that were
6408 dw2_debug_names_iterator iter (map, kind, namei);
6410 struct dwarf2_per_cu_data *per_cu;
6411 while ((per_cu = iter.next ()) != NULL)
6412 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6417 const struct quick_symbol_functions dwarf2_debug_names_functions =
6420 dw2_find_last_source_symtab,
6421 dw2_forget_cached_source_info,
6422 dw2_map_symtabs_matching_filename,
6423 dw2_debug_names_lookup_symbol,
6425 dw2_debug_names_dump,
6427 dw2_debug_names_expand_symtabs_for_function,
6428 dw2_expand_all_symtabs,
6429 dw2_expand_symtabs_with_fullname,
6430 dw2_map_matching_symbols,
6431 dw2_debug_names_expand_symtabs_matching,
6432 dw2_find_pc_sect_compunit_symtab,
6434 dw2_map_symbol_filenames
6437 /* Initialize for reading DWARF for this objfile. Return 0 if this
6438 file will use psymtabs, or 1 if using the GNU index. */
6441 dwarf2_initialize_objfile (struct objfile *objfile)
6443 /* If we're about to read full symbols, don't bother with the
6444 indices. In this case we also don't care if some other debug
6445 format is making psymtabs, because they are all about to be
6447 if ((objfile->flags & OBJF_READNOW))
6451 dwarf2_per_objfile->using_index = 1;
6452 create_all_comp_units (objfile);
6453 create_all_type_units (objfile);
6454 dwarf2_per_objfile->quick_file_names_table =
6455 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6457 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
6458 + dwarf2_per_objfile->n_type_units); ++i)
6460 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6462 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6463 struct dwarf2_per_cu_quick_data);
6466 /* Return 1 so that gdb sees the "quick" functions. However,
6467 these functions will be no-ops because we will have expanded
6469 return elf_sym_fns_gdb_index;
6472 if (dwarf2_read_debug_names (objfile))
6473 return elf_sym_fns_debug_names;
6475 if (dwarf2_read_index (objfile))
6476 return elf_sym_fns_gdb_index;
6478 return elf_sym_fns_lazy_psyms;
6483 /* Build a partial symbol table. */
6486 dwarf2_build_psymtabs (struct objfile *objfile)
6489 if (objfile->global_psymbols.capacity () == 0
6490 && objfile->static_psymbols.capacity () == 0)
6491 init_psymbol_list (objfile, 1024);
6495 /* This isn't really ideal: all the data we allocate on the
6496 objfile's obstack is still uselessly kept around. However,
6497 freeing it seems unsafe. */
6498 psymtab_discarder psymtabs (objfile);
6499 dwarf2_build_psymtabs_hard (objfile);
6502 CATCH (except, RETURN_MASK_ERROR)
6504 exception_print (gdb_stderr, except);
6509 /* Return the total length of the CU described by HEADER. */
6512 get_cu_length (const struct comp_unit_head *header)
6514 return header->initial_length_size + header->length;
6517 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6520 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6522 sect_offset bottom = cu_header->sect_off;
6523 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6525 return sect_off >= bottom && sect_off < top;
6528 /* Find the base address of the compilation unit for range lists and
6529 location lists. It will normally be specified by DW_AT_low_pc.
6530 In DWARF-3 draft 4, the base address could be overridden by
6531 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6532 compilation units with discontinuous ranges. */
6535 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6537 struct attribute *attr;
6540 cu->base_address = 0;
6542 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6545 cu->base_address = attr_value_as_address (attr);
6550 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6553 cu->base_address = attr_value_as_address (attr);
6559 /* Read in the comp unit header information from the debug_info at info_ptr.
6560 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6561 NOTE: This leaves members offset, first_die_offset to be filled in
6564 static const gdb_byte *
6565 read_comp_unit_head (struct comp_unit_head *cu_header,
6566 const gdb_byte *info_ptr,
6567 struct dwarf2_section_info *section,
6568 rcuh_kind section_kind)
6571 unsigned int bytes_read;
6572 const char *filename = get_section_file_name (section);
6573 bfd *abfd = get_section_bfd_owner (section);
6575 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6576 cu_header->initial_length_size = bytes_read;
6577 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6578 info_ptr += bytes_read;
6579 cu_header->version = read_2_bytes (abfd, info_ptr);
6581 if (cu_header->version < 5)
6582 switch (section_kind)
6584 case rcuh_kind::COMPILE:
6585 cu_header->unit_type = DW_UT_compile;
6587 case rcuh_kind::TYPE:
6588 cu_header->unit_type = DW_UT_type;
6591 internal_error (__FILE__, __LINE__,
6592 _("read_comp_unit_head: invalid section_kind"));
6596 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6597 (read_1_byte (abfd, info_ptr));
6599 switch (cu_header->unit_type)
6602 if (section_kind != rcuh_kind::COMPILE)
6603 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6604 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6608 section_kind = rcuh_kind::TYPE;
6611 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6612 "(is %d, should be %d or %d) [in module %s]"),
6613 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6616 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6619 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6622 info_ptr += bytes_read;
6623 if (cu_header->version < 5)
6625 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6628 signed_addr = bfd_get_sign_extend_vma (abfd);
6629 if (signed_addr < 0)
6630 internal_error (__FILE__, __LINE__,
6631 _("read_comp_unit_head: dwarf from non elf file"));
6632 cu_header->signed_addr_p = signed_addr;
6634 if (section_kind == rcuh_kind::TYPE)
6636 LONGEST type_offset;
6638 cu_header->signature = read_8_bytes (abfd, info_ptr);
6641 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6642 info_ptr += bytes_read;
6643 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6644 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6645 error (_("Dwarf Error: Too big type_offset in compilation unit "
6646 "header (is %s) [in module %s]"), plongest (type_offset),
6653 /* Helper function that returns the proper abbrev section for
6656 static struct dwarf2_section_info *
6657 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6659 struct dwarf2_section_info *abbrev;
6661 if (this_cu->is_dwz)
6662 abbrev = &dwarf2_get_dwz_file ()->abbrev;
6664 abbrev = &dwarf2_per_objfile->abbrev;
6669 /* Subroutine of read_and_check_comp_unit_head and
6670 read_and_check_type_unit_head to simplify them.
6671 Perform various error checking on the header. */
6674 error_check_comp_unit_head (struct comp_unit_head *header,
6675 struct dwarf2_section_info *section,
6676 struct dwarf2_section_info *abbrev_section)
6678 const char *filename = get_section_file_name (section);
6680 if (header->version < 2 || header->version > 5)
6681 error (_("Dwarf Error: wrong version in compilation unit header "
6682 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
6685 if (to_underlying (header->abbrev_sect_off)
6686 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6687 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
6688 "(offset 0x%x + 6) [in module %s]"),
6689 to_underlying (header->abbrev_sect_off),
6690 to_underlying (header->sect_off),
6693 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6694 avoid potential 32-bit overflow. */
6695 if (((ULONGEST) header->sect_off + get_cu_length (header))
6697 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6698 "(offset 0x%x + 0) [in module %s]"),
6699 header->length, to_underlying (header->sect_off),
6703 /* Read in a CU/TU header and perform some basic error checking.
6704 The contents of the header are stored in HEADER.
6705 The result is a pointer to the start of the first DIE. */
6707 static const gdb_byte *
6708 read_and_check_comp_unit_head (struct comp_unit_head *header,
6709 struct dwarf2_section_info *section,
6710 struct dwarf2_section_info *abbrev_section,
6711 const gdb_byte *info_ptr,
6712 rcuh_kind section_kind)
6714 const gdb_byte *beg_of_comp_unit = info_ptr;
6716 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6718 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6720 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6722 error_check_comp_unit_head (header, section, abbrev_section);
6727 /* Fetch the abbreviation table offset from a comp or type unit header. */
6730 read_abbrev_offset (struct dwarf2_section_info *section,
6731 sect_offset sect_off)
6733 bfd *abfd = get_section_bfd_owner (section);
6734 const gdb_byte *info_ptr;
6735 unsigned int initial_length_size, offset_size;
6738 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6739 info_ptr = section->buffer + to_underlying (sect_off);
6740 read_initial_length (abfd, info_ptr, &initial_length_size);
6741 offset_size = initial_length_size == 4 ? 4 : 8;
6742 info_ptr += initial_length_size;
6744 version = read_2_bytes (abfd, info_ptr);
6748 /* Skip unit type and address size. */
6752 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6755 /* Allocate a new partial symtab for file named NAME and mark this new
6756 partial symtab as being an include of PST. */
6759 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6760 struct objfile *objfile)
6762 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6764 if (!IS_ABSOLUTE_PATH (subpst->filename))
6766 /* It shares objfile->objfile_obstack. */
6767 subpst->dirname = pst->dirname;
6770 subpst->textlow = 0;
6771 subpst->texthigh = 0;
6773 subpst->dependencies
6774 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
6775 subpst->dependencies[0] = pst;
6776 subpst->number_of_dependencies = 1;
6778 subpst->globals_offset = 0;
6779 subpst->n_global_syms = 0;
6780 subpst->statics_offset = 0;
6781 subpst->n_static_syms = 0;
6782 subpst->compunit_symtab = NULL;
6783 subpst->read_symtab = pst->read_symtab;
6786 /* No private part is necessary for include psymtabs. This property
6787 can be used to differentiate between such include psymtabs and
6788 the regular ones. */
6789 subpst->read_symtab_private = NULL;
6792 /* Read the Line Number Program data and extract the list of files
6793 included by the source file represented by PST. Build an include
6794 partial symtab for each of these included files. */
6797 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6798 struct die_info *die,
6799 struct partial_symtab *pst)
6802 struct attribute *attr;
6804 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6806 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6808 return; /* No linetable, so no includes. */
6810 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
6811 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
6815 hash_signatured_type (const void *item)
6817 const struct signatured_type *sig_type
6818 = (const struct signatured_type *) item;
6820 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6821 return sig_type->signature;
6825 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6827 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6828 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6830 return lhs->signature == rhs->signature;
6833 /* Allocate a hash table for signatured types. */
6836 allocate_signatured_type_table (struct objfile *objfile)
6838 return htab_create_alloc_ex (41,
6839 hash_signatured_type,
6842 &objfile->objfile_obstack,
6843 hashtab_obstack_allocate,
6844 dummy_obstack_deallocate);
6847 /* A helper function to add a signatured type CU to a table. */
6850 add_signatured_type_cu_to_table (void **slot, void *datum)
6852 struct signatured_type *sigt = (struct signatured_type *) *slot;
6853 struct signatured_type ***datap = (struct signatured_type ***) datum;
6861 /* A helper for create_debug_types_hash_table. Read types from SECTION
6862 and fill them into TYPES_HTAB. It will process only type units,
6863 therefore DW_UT_type. */
6866 create_debug_type_hash_table (struct dwo_file *dwo_file,
6867 dwarf2_section_info *section, htab_t &types_htab,
6868 rcuh_kind section_kind)
6870 struct objfile *objfile = dwarf2_per_objfile->objfile;
6871 struct dwarf2_section_info *abbrev_section;
6873 const gdb_byte *info_ptr, *end_ptr;
6875 abbrev_section = (dwo_file != NULL
6876 ? &dwo_file->sections.abbrev
6877 : &dwarf2_per_objfile->abbrev);
6879 if (dwarf_read_debug)
6880 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6881 get_section_name (section),
6882 get_section_file_name (abbrev_section));
6884 dwarf2_read_section (objfile, section);
6885 info_ptr = section->buffer;
6887 if (info_ptr == NULL)
6890 /* We can't set abfd until now because the section may be empty or
6891 not present, in which case the bfd is unknown. */
6892 abfd = get_section_bfd_owner (section);
6894 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6895 because we don't need to read any dies: the signature is in the
6898 end_ptr = info_ptr + section->size;
6899 while (info_ptr < end_ptr)
6901 struct signatured_type *sig_type;
6902 struct dwo_unit *dwo_tu;
6904 const gdb_byte *ptr = info_ptr;
6905 struct comp_unit_head header;
6906 unsigned int length;
6908 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6910 /* Initialize it due to a false compiler warning. */
6911 header.signature = -1;
6912 header.type_cu_offset_in_tu = (cu_offset) -1;
6914 /* We need to read the type's signature in order to build the hash
6915 table, but we don't need anything else just yet. */
6917 ptr = read_and_check_comp_unit_head (&header, section,
6918 abbrev_section, ptr, section_kind);
6920 length = get_cu_length (&header);
6922 /* Skip dummy type units. */
6923 if (ptr >= info_ptr + length
6924 || peek_abbrev_code (abfd, ptr) == 0
6925 || header.unit_type != DW_UT_type)
6931 if (types_htab == NULL)
6934 types_htab = allocate_dwo_unit_table (objfile);
6936 types_htab = allocate_signatured_type_table (objfile);
6942 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6944 dwo_tu->dwo_file = dwo_file;
6945 dwo_tu->signature = header.signature;
6946 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6947 dwo_tu->section = section;
6948 dwo_tu->sect_off = sect_off;
6949 dwo_tu->length = length;
6953 /* N.B.: type_offset is not usable if this type uses a DWO file.
6954 The real type_offset is in the DWO file. */
6956 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6957 struct signatured_type);
6958 sig_type->signature = header.signature;
6959 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6960 sig_type->per_cu.objfile = objfile;
6961 sig_type->per_cu.is_debug_types = 1;
6962 sig_type->per_cu.section = section;
6963 sig_type->per_cu.sect_off = sect_off;
6964 sig_type->per_cu.length = length;
6967 slot = htab_find_slot (types_htab,
6968 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6970 gdb_assert (slot != NULL);
6973 sect_offset dup_sect_off;
6977 const struct dwo_unit *dup_tu
6978 = (const struct dwo_unit *) *slot;
6980 dup_sect_off = dup_tu->sect_off;
6984 const struct signatured_type *dup_tu
6985 = (const struct signatured_type *) *slot;
6987 dup_sect_off = dup_tu->per_cu.sect_off;
6990 complaint (&symfile_complaints,
6991 _("debug type entry at offset 0x%x is duplicate to"
6992 " the entry at offset 0x%x, signature %s"),
6993 to_underlying (sect_off), to_underlying (dup_sect_off),
6994 hex_string (header.signature));
6996 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6998 if (dwarf_read_debug > 1)
6999 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
7000 to_underlying (sect_off),
7001 hex_string (header.signature));
7007 /* Create the hash table of all entries in the .debug_types
7008 (or .debug_types.dwo) section(s).
7009 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
7010 otherwise it is NULL.
7012 The result is a pointer to the hash table or NULL if there are no types.
7014 Note: This function processes DWO files only, not DWP files. */
7017 create_debug_types_hash_table (struct dwo_file *dwo_file,
7018 VEC (dwarf2_section_info_def) *types,
7022 struct dwarf2_section_info *section;
7024 if (VEC_empty (dwarf2_section_info_def, types))
7028 VEC_iterate (dwarf2_section_info_def, types, ix, section);
7030 create_debug_type_hash_table (dwo_file, section, types_htab,
7034 /* Create the hash table of all entries in the .debug_types section,
7035 and initialize all_type_units.
7036 The result is zero if there is an error (e.g. missing .debug_types section),
7037 otherwise non-zero. */
7040 create_all_type_units (struct objfile *objfile)
7042 htab_t types_htab = NULL;
7043 struct signatured_type **iter;
7045 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
7046 rcuh_kind::COMPILE);
7047 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
7048 if (types_htab == NULL)
7050 dwarf2_per_objfile->signatured_types = NULL;
7054 dwarf2_per_objfile->signatured_types = types_htab;
7056 dwarf2_per_objfile->n_type_units
7057 = dwarf2_per_objfile->n_allocated_type_units
7058 = htab_elements (types_htab);
7059 dwarf2_per_objfile->all_type_units =
7060 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
7061 iter = &dwarf2_per_objfile->all_type_units[0];
7062 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
7063 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
7064 == dwarf2_per_objfile->n_type_units);
7069 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
7070 If SLOT is non-NULL, it is the entry to use in the hash table.
7071 Otherwise we find one. */
7073 static struct signatured_type *
7074 add_type_unit (ULONGEST sig, void **slot)
7076 struct objfile *objfile = dwarf2_per_objfile->objfile;
7077 int n_type_units = dwarf2_per_objfile->n_type_units;
7078 struct signatured_type *sig_type;
7080 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
7082 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
7084 if (dwarf2_per_objfile->n_allocated_type_units == 0)
7085 dwarf2_per_objfile->n_allocated_type_units = 1;
7086 dwarf2_per_objfile->n_allocated_type_units *= 2;
7087 dwarf2_per_objfile->all_type_units
7088 = XRESIZEVEC (struct signatured_type *,
7089 dwarf2_per_objfile->all_type_units,
7090 dwarf2_per_objfile->n_allocated_type_units);
7091 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
7093 dwarf2_per_objfile->n_type_units = n_type_units;
7095 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7096 struct signatured_type);
7097 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
7098 sig_type->signature = sig;
7099 sig_type->per_cu.is_debug_types = 1;
7100 if (dwarf2_per_objfile->using_index)
7102 sig_type->per_cu.v.quick =
7103 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7104 struct dwarf2_per_cu_quick_data);
7109 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7112 gdb_assert (*slot == NULL);
7114 /* The rest of sig_type must be filled in by the caller. */
7118 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7119 Fill in SIG_ENTRY with DWO_ENTRY. */
7122 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
7123 struct signatured_type *sig_entry,
7124 struct dwo_unit *dwo_entry)
7126 /* Make sure we're not clobbering something we don't expect to. */
7127 gdb_assert (! sig_entry->per_cu.queued);
7128 gdb_assert (sig_entry->per_cu.cu == NULL);
7129 if (dwarf2_per_objfile->using_index)
7131 gdb_assert (sig_entry->per_cu.v.quick != NULL);
7132 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
7135 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7136 gdb_assert (sig_entry->signature == dwo_entry->signature);
7137 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7138 gdb_assert (sig_entry->type_unit_group == NULL);
7139 gdb_assert (sig_entry->dwo_unit == NULL);
7141 sig_entry->per_cu.section = dwo_entry->section;
7142 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7143 sig_entry->per_cu.length = dwo_entry->length;
7144 sig_entry->per_cu.reading_dwo_directly = 1;
7145 sig_entry->per_cu.objfile = objfile;
7146 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7147 sig_entry->dwo_unit = dwo_entry;
7150 /* Subroutine of lookup_signatured_type.
7151 If we haven't read the TU yet, create the signatured_type data structure
7152 for a TU to be read in directly from a DWO file, bypassing the stub.
7153 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7154 using .gdb_index, then when reading a CU we want to stay in the DWO file
7155 containing that CU. Otherwise we could end up reading several other DWO
7156 files (due to comdat folding) to process the transitive closure of all the
7157 mentioned TUs, and that can be slow. The current DWO file will have every
7158 type signature that it needs.
7159 We only do this for .gdb_index because in the psymtab case we already have
7160 to read all the DWOs to build the type unit groups. */
7162 static struct signatured_type *
7163 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7165 struct objfile *objfile = dwarf2_per_objfile->objfile;
7166 struct dwo_file *dwo_file;
7167 struct dwo_unit find_dwo_entry, *dwo_entry;
7168 struct signatured_type find_sig_entry, *sig_entry;
7171 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7173 /* If TU skeletons have been removed then we may not have read in any
7175 if (dwarf2_per_objfile->signatured_types == NULL)
7177 dwarf2_per_objfile->signatured_types
7178 = allocate_signatured_type_table (objfile);
7181 /* We only ever need to read in one copy of a signatured type.
7182 Use the global signatured_types array to do our own comdat-folding
7183 of types. If this is the first time we're reading this TU, and
7184 the TU has an entry in .gdb_index, replace the recorded data from
7185 .gdb_index with this TU. */
7187 find_sig_entry.signature = sig;
7188 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7189 &find_sig_entry, INSERT);
7190 sig_entry = (struct signatured_type *) *slot;
7192 /* We can get here with the TU already read, *or* in the process of being
7193 read. Don't reassign the global entry to point to this DWO if that's
7194 the case. Also note that if the TU is already being read, it may not
7195 have come from a DWO, the program may be a mix of Fission-compiled
7196 code and non-Fission-compiled code. */
7198 /* Have we already tried to read this TU?
7199 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7200 needn't exist in the global table yet). */
7201 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7204 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7205 dwo_unit of the TU itself. */
7206 dwo_file = cu->dwo_unit->dwo_file;
7208 /* Ok, this is the first time we're reading this TU. */
7209 if (dwo_file->tus == NULL)
7211 find_dwo_entry.signature = sig;
7212 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7213 if (dwo_entry == NULL)
7216 /* If the global table doesn't have an entry for this TU, add one. */
7217 if (sig_entry == NULL)
7218 sig_entry = add_type_unit (sig, slot);
7220 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
7221 sig_entry->per_cu.tu_read = 1;
7225 /* Subroutine of lookup_signatured_type.
7226 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7227 then try the DWP file. If the TU stub (skeleton) has been removed then
7228 it won't be in .gdb_index. */
7230 static struct signatured_type *
7231 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7233 struct objfile *objfile = dwarf2_per_objfile->objfile;
7234 struct dwp_file *dwp_file = get_dwp_file ();
7235 struct dwo_unit *dwo_entry;
7236 struct signatured_type find_sig_entry, *sig_entry;
7239 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7240 gdb_assert (dwp_file != NULL);
7242 /* If TU skeletons have been removed then we may not have read in any
7244 if (dwarf2_per_objfile->signatured_types == NULL)
7246 dwarf2_per_objfile->signatured_types
7247 = allocate_signatured_type_table (objfile);
7250 find_sig_entry.signature = sig;
7251 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7252 &find_sig_entry, INSERT);
7253 sig_entry = (struct signatured_type *) *slot;
7255 /* Have we already tried to read this TU?
7256 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7257 needn't exist in the global table yet). */
7258 if (sig_entry != NULL)
7261 if (dwp_file->tus == NULL)
7263 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
7264 sig, 1 /* is_debug_types */);
7265 if (dwo_entry == NULL)
7268 sig_entry = add_type_unit (sig, slot);
7269 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
7274 /* Lookup a signature based type for DW_FORM_ref_sig8.
7275 Returns NULL if signature SIG is not present in the table.
7276 It is up to the caller to complain about this. */
7278 static struct signatured_type *
7279 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7282 && dwarf2_per_objfile->using_index)
7284 /* We're in a DWO/DWP file, and we're using .gdb_index.
7285 These cases require special processing. */
7286 if (get_dwp_file () == NULL)
7287 return lookup_dwo_signatured_type (cu, sig);
7289 return lookup_dwp_signatured_type (cu, sig);
7293 struct signatured_type find_entry, *entry;
7295 if (dwarf2_per_objfile->signatured_types == NULL)
7297 find_entry.signature = sig;
7298 entry = ((struct signatured_type *)
7299 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7304 /* Low level DIE reading support. */
7306 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7309 init_cu_die_reader (struct die_reader_specs *reader,
7310 struct dwarf2_cu *cu,
7311 struct dwarf2_section_info *section,
7312 struct dwo_file *dwo_file)
7314 gdb_assert (section->readin && section->buffer != NULL);
7315 reader->abfd = get_section_bfd_owner (section);
7317 reader->dwo_file = dwo_file;
7318 reader->die_section = section;
7319 reader->buffer = section->buffer;
7320 reader->buffer_end = section->buffer + section->size;
7321 reader->comp_dir = NULL;
7324 /* Subroutine of init_cutu_and_read_dies to simplify it.
7325 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7326 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7329 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7330 from it to the DIE in the DWO. If NULL we are skipping the stub.
7331 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7332 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7333 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7334 STUB_COMP_DIR may be non-NULL.
7335 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7336 are filled in with the info of the DIE from the DWO file.
7337 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
7338 provided an abbrev table to use.
7339 The result is non-zero if a valid (non-dummy) DIE was found. */
7342 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7343 struct dwo_unit *dwo_unit,
7344 int abbrev_table_provided,
7345 struct die_info *stub_comp_unit_die,
7346 const char *stub_comp_dir,
7347 struct die_reader_specs *result_reader,
7348 const gdb_byte **result_info_ptr,
7349 struct die_info **result_comp_unit_die,
7350 int *result_has_children)
7352 struct objfile *objfile = dwarf2_per_objfile->objfile;
7353 struct dwarf2_cu *cu = this_cu->cu;
7354 struct dwarf2_section_info *section;
7356 const gdb_byte *begin_info_ptr, *info_ptr;
7357 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7358 int i,num_extra_attrs;
7359 struct dwarf2_section_info *dwo_abbrev_section;
7360 struct attribute *attr;
7361 struct die_info *comp_unit_die;
7363 /* At most one of these may be provided. */
7364 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7366 /* These attributes aren't processed until later:
7367 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7368 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7369 referenced later. However, these attributes are found in the stub
7370 which we won't have later. In order to not impose this complication
7371 on the rest of the code, we read them here and copy them to the
7380 if (stub_comp_unit_die != NULL)
7382 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7384 if (! this_cu->is_debug_types)
7385 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7386 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7387 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7388 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7389 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7391 /* There should be a DW_AT_addr_base attribute here (if needed).
7392 We need the value before we can process DW_FORM_GNU_addr_index. */
7394 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7396 cu->addr_base = DW_UNSND (attr);
7398 /* There should be a DW_AT_ranges_base attribute here (if needed).
7399 We need the value before we can process DW_AT_ranges. */
7400 cu->ranges_base = 0;
7401 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7403 cu->ranges_base = DW_UNSND (attr);
7405 else if (stub_comp_dir != NULL)
7407 /* Reconstruct the comp_dir attribute to simplify the code below. */
7408 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7409 comp_dir->name = DW_AT_comp_dir;
7410 comp_dir->form = DW_FORM_string;
7411 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7412 DW_STRING (comp_dir) = stub_comp_dir;
7415 /* Set up for reading the DWO CU/TU. */
7416 cu->dwo_unit = dwo_unit;
7417 section = dwo_unit->section;
7418 dwarf2_read_section (objfile, section);
7419 abfd = get_section_bfd_owner (section);
7420 begin_info_ptr = info_ptr = (section->buffer
7421 + to_underlying (dwo_unit->sect_off));
7422 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7423 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
7425 if (this_cu->is_debug_types)
7427 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7429 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
7431 info_ptr, rcuh_kind::TYPE);
7432 /* This is not an assert because it can be caused by bad debug info. */
7433 if (sig_type->signature != cu->header.signature)
7435 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7436 " TU at offset 0x%x [in module %s]"),
7437 hex_string (sig_type->signature),
7438 hex_string (cu->header.signature),
7439 to_underlying (dwo_unit->sect_off),
7440 bfd_get_filename (abfd));
7442 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7443 /* For DWOs coming from DWP files, we don't know the CU length
7444 nor the type's offset in the TU until now. */
7445 dwo_unit->length = get_cu_length (&cu->header);
7446 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7448 /* Establish the type offset that can be used to lookup the type.
7449 For DWO files, we don't know it until now. */
7450 sig_type->type_offset_in_section
7451 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7455 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
7457 info_ptr, rcuh_kind::COMPILE);
7458 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7459 /* For DWOs coming from DWP files, we don't know the CU length
7461 dwo_unit->length = get_cu_length (&cu->header);
7464 /* Replace the CU's original abbrev table with the DWO's.
7465 Reminder: We can't read the abbrev table until we've read the header. */
7466 if (abbrev_table_provided)
7468 /* Don't free the provided abbrev table, the caller of
7469 init_cutu_and_read_dies owns it. */
7470 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
7471 /* Ensure the DWO abbrev table gets freed. */
7472 make_cleanup (dwarf2_free_abbrev_table, cu);
7476 dwarf2_free_abbrev_table (cu);
7477 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
7478 /* Leave any existing abbrev table cleanup as is. */
7481 /* Read in the die, but leave space to copy over the attributes
7482 from the stub. This has the benefit of simplifying the rest of
7483 the code - all the work to maintain the illusion of a single
7484 DW_TAG_{compile,type}_unit DIE is done here. */
7485 num_extra_attrs = ((stmt_list != NULL)
7489 + (comp_dir != NULL));
7490 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7491 result_has_children, num_extra_attrs);
7493 /* Copy over the attributes from the stub to the DIE we just read in. */
7494 comp_unit_die = *result_comp_unit_die;
7495 i = comp_unit_die->num_attrs;
7496 if (stmt_list != NULL)
7497 comp_unit_die->attrs[i++] = *stmt_list;
7499 comp_unit_die->attrs[i++] = *low_pc;
7500 if (high_pc != NULL)
7501 comp_unit_die->attrs[i++] = *high_pc;
7503 comp_unit_die->attrs[i++] = *ranges;
7504 if (comp_dir != NULL)
7505 comp_unit_die->attrs[i++] = *comp_dir;
7506 comp_unit_die->num_attrs += num_extra_attrs;
7508 if (dwarf_die_debug)
7510 fprintf_unfiltered (gdb_stdlog,
7511 "Read die from %s@0x%x of %s:\n",
7512 get_section_name (section),
7513 (unsigned) (begin_info_ptr - section->buffer),
7514 bfd_get_filename (abfd));
7515 dump_die (comp_unit_die, dwarf_die_debug);
7518 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7519 TUs by skipping the stub and going directly to the entry in the DWO file.
7520 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7521 to get it via circuitous means. Blech. */
7522 if (comp_dir != NULL)
7523 result_reader->comp_dir = DW_STRING (comp_dir);
7525 /* Skip dummy compilation units. */
7526 if (info_ptr >= begin_info_ptr + dwo_unit->length
7527 || peek_abbrev_code (abfd, info_ptr) == 0)
7530 *result_info_ptr = info_ptr;
7534 /* Subroutine of init_cutu_and_read_dies to simplify it.
7535 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7536 Returns NULL if the specified DWO unit cannot be found. */
7538 static struct dwo_unit *
7539 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7540 struct die_info *comp_unit_die)
7542 struct dwarf2_cu *cu = this_cu->cu;
7544 struct dwo_unit *dwo_unit;
7545 const char *comp_dir, *dwo_name;
7547 gdb_assert (cu != NULL);
7549 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7550 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7551 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7553 if (this_cu->is_debug_types)
7555 struct signatured_type *sig_type;
7557 /* Since this_cu is the first member of struct signatured_type,
7558 we can go from a pointer to one to a pointer to the other. */
7559 sig_type = (struct signatured_type *) this_cu;
7560 signature = sig_type->signature;
7561 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7565 struct attribute *attr;
7567 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7569 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7571 dwo_name, objfile_name (this_cu->objfile));
7572 signature = DW_UNSND (attr);
7573 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7580 /* Subroutine of init_cutu_and_read_dies to simplify it.
7581 See it for a description of the parameters.
7582 Read a TU directly from a DWO file, bypassing the stub.
7584 Note: This function could be a little bit simpler if we shared cleanups
7585 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
7586 to do, so we keep this function self-contained. Or we could move this
7587 into our caller, but it's complex enough already. */
7590 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7591 int use_existing_cu, int keep,
7592 die_reader_func_ftype *die_reader_func,
7595 struct dwarf2_cu *cu;
7596 struct signatured_type *sig_type;
7597 struct cleanup *cleanups, *free_cu_cleanup = NULL;
7598 struct die_reader_specs reader;
7599 const gdb_byte *info_ptr;
7600 struct die_info *comp_unit_die;
7603 /* Verify we can do the following downcast, and that we have the
7605 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7606 sig_type = (struct signatured_type *) this_cu;
7607 gdb_assert (sig_type->dwo_unit != NULL);
7609 cleanups = make_cleanup (null_cleanup, NULL);
7611 if (use_existing_cu && this_cu->cu != NULL)
7613 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7615 /* There's no need to do the rereading_dwo_cu handling that
7616 init_cutu_and_read_dies does since we don't read the stub. */
7620 /* If !use_existing_cu, this_cu->cu must be NULL. */
7621 gdb_assert (this_cu->cu == NULL);
7622 cu = XNEW (struct dwarf2_cu);
7623 init_one_comp_unit (cu, this_cu);
7624 /* If an error occurs while loading, release our storage. */
7625 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
7628 /* A future optimization, if needed, would be to use an existing
7629 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7630 could share abbrev tables. */
7632 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7633 0 /* abbrev_table_provided */,
7634 NULL /* stub_comp_unit_die */,
7635 sig_type->dwo_unit->dwo_file->comp_dir,
7637 &comp_unit_die, &has_children) == 0)
7640 do_cleanups (cleanups);
7644 /* All the "real" work is done here. */
7645 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7647 /* This duplicates the code in init_cutu_and_read_dies,
7648 but the alternative is making the latter more complex.
7649 This function is only for the special case of using DWO files directly:
7650 no point in overly complicating the general case just to handle this. */
7651 if (free_cu_cleanup != NULL)
7655 /* We've successfully allocated this compilation unit. Let our
7656 caller clean it up when finished with it. */
7657 discard_cleanups (free_cu_cleanup);
7659 /* We can only discard free_cu_cleanup and all subsequent cleanups.
7660 So we have to manually free the abbrev table. */
7661 dwarf2_free_abbrev_table (cu);
7663 /* Link this CU into read_in_chain. */
7664 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7665 dwarf2_per_objfile->read_in_chain = this_cu;
7668 do_cleanups (free_cu_cleanup);
7671 do_cleanups (cleanups);
7674 /* Initialize a CU (or TU) and read its DIEs.
7675 If the CU defers to a DWO file, read the DWO file as well.
7677 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7678 Otherwise the table specified in the comp unit header is read in and used.
7679 This is an optimization for when we already have the abbrev table.
7681 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7682 Otherwise, a new CU is allocated with xmalloc.
7684 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7685 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7687 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7688 linker) then DIE_READER_FUNC will not get called. */
7691 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7692 struct abbrev_table *abbrev_table,
7693 int use_existing_cu, int keep,
7694 die_reader_func_ftype *die_reader_func,
7697 struct objfile *objfile = dwarf2_per_objfile->objfile;
7698 struct dwarf2_section_info *section = this_cu->section;
7699 bfd *abfd = get_section_bfd_owner (section);
7700 struct dwarf2_cu *cu;
7701 const gdb_byte *begin_info_ptr, *info_ptr;
7702 struct die_reader_specs reader;
7703 struct die_info *comp_unit_die;
7705 struct attribute *attr;
7706 struct cleanup *cleanups, *free_cu_cleanup = NULL;
7707 struct signatured_type *sig_type = NULL;
7708 struct dwarf2_section_info *abbrev_section;
7709 /* Non-zero if CU currently points to a DWO file and we need to
7710 reread it. When this happens we need to reread the skeleton die
7711 before we can reread the DWO file (this only applies to CUs, not TUs). */
7712 int rereading_dwo_cu = 0;
7714 if (dwarf_die_debug)
7715 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
7716 this_cu->is_debug_types ? "type" : "comp",
7717 to_underlying (this_cu->sect_off));
7719 if (use_existing_cu)
7722 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7723 file (instead of going through the stub), short-circuit all of this. */
7724 if (this_cu->reading_dwo_directly)
7726 /* Narrow down the scope of possibilities to have to understand. */
7727 gdb_assert (this_cu->is_debug_types);
7728 gdb_assert (abbrev_table == NULL);
7729 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7730 die_reader_func, data);
7734 cleanups = make_cleanup (null_cleanup, NULL);
7736 /* This is cheap if the section is already read in. */
7737 dwarf2_read_section (objfile, section);
7739 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7741 abbrev_section = get_abbrev_section_for_cu (this_cu);
7743 if (use_existing_cu && this_cu->cu != NULL)
7746 /* If this CU is from a DWO file we need to start over, we need to
7747 refetch the attributes from the skeleton CU.
7748 This could be optimized by retrieving those attributes from when we
7749 were here the first time: the previous comp_unit_die was stored in
7750 comp_unit_obstack. But there's no data yet that we need this
7752 if (cu->dwo_unit != NULL)
7753 rereading_dwo_cu = 1;
7757 /* If !use_existing_cu, this_cu->cu must be NULL. */
7758 gdb_assert (this_cu->cu == NULL);
7759 cu = XNEW (struct dwarf2_cu);
7760 init_one_comp_unit (cu, this_cu);
7761 /* If an error occurs while loading, release our storage. */
7762 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
7765 /* Get the header. */
7766 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7768 /* We already have the header, there's no need to read it in again. */
7769 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7773 if (this_cu->is_debug_types)
7775 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
7776 abbrev_section, info_ptr,
7779 /* Since per_cu is the first member of struct signatured_type,
7780 we can go from a pointer to one to a pointer to the other. */
7781 sig_type = (struct signatured_type *) this_cu;
7782 gdb_assert (sig_type->signature == cu->header.signature);
7783 gdb_assert (sig_type->type_offset_in_tu
7784 == cu->header.type_cu_offset_in_tu);
7785 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7787 /* LENGTH has not been set yet for type units if we're
7788 using .gdb_index. */
7789 this_cu->length = get_cu_length (&cu->header);
7791 /* Establish the type offset that can be used to lookup the type. */
7792 sig_type->type_offset_in_section =
7793 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7795 this_cu->dwarf_version = cu->header.version;
7799 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
7802 rcuh_kind::COMPILE);
7804 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7805 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7806 this_cu->dwarf_version = cu->header.version;
7810 /* Skip dummy compilation units. */
7811 if (info_ptr >= begin_info_ptr + this_cu->length
7812 || peek_abbrev_code (abfd, info_ptr) == 0)
7814 do_cleanups (cleanups);
7818 /* If we don't have them yet, read the abbrevs for this compilation unit.
7819 And if we need to read them now, make sure they're freed when we're
7820 done. Note that it's important that if the CU had an abbrev table
7821 on entry we don't free it when we're done: Somewhere up the call stack
7822 it may be in use. */
7823 if (abbrev_table != NULL)
7825 gdb_assert (cu->abbrev_table == NULL);
7826 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7827 cu->abbrev_table = abbrev_table;
7829 else if (cu->abbrev_table == NULL)
7831 dwarf2_read_abbrevs (cu, abbrev_section);
7832 make_cleanup (dwarf2_free_abbrev_table, cu);
7834 else if (rereading_dwo_cu)
7836 dwarf2_free_abbrev_table (cu);
7837 dwarf2_read_abbrevs (cu, abbrev_section);
7840 /* Read the top level CU/TU die. */
7841 init_cu_die_reader (&reader, cu, section, NULL);
7842 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7844 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7846 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7847 DWO CU, that this test will fail (the attribute will not be present). */
7848 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7851 struct dwo_unit *dwo_unit;
7852 struct die_info *dwo_comp_unit_die;
7856 complaint (&symfile_complaints,
7857 _("compilation unit with DW_AT_GNU_dwo_name"
7858 " has children (offset 0x%x) [in module %s]"),
7859 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
7861 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7862 if (dwo_unit != NULL)
7864 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7865 abbrev_table != NULL,
7866 comp_unit_die, NULL,
7868 &dwo_comp_unit_die, &has_children) == 0)
7871 do_cleanups (cleanups);
7874 comp_unit_die = dwo_comp_unit_die;
7878 /* Yikes, we couldn't find the rest of the DIE, we only have
7879 the stub. A complaint has already been logged. There's
7880 not much more we can do except pass on the stub DIE to
7881 die_reader_func. We don't want to throw an error on bad
7886 /* All of the above is setup for this call. Yikes. */
7887 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7889 /* Done, clean up. */
7890 if (free_cu_cleanup != NULL)
7894 /* We've successfully allocated this compilation unit. Let our
7895 caller clean it up when finished with it. */
7896 discard_cleanups (free_cu_cleanup);
7898 /* We can only discard free_cu_cleanup and all subsequent cleanups.
7899 So we have to manually free the abbrev table. */
7900 dwarf2_free_abbrev_table (cu);
7902 /* Link this CU into read_in_chain. */
7903 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7904 dwarf2_per_objfile->read_in_chain = this_cu;
7907 do_cleanups (free_cu_cleanup);
7910 do_cleanups (cleanups);
7913 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7914 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7915 to have already done the lookup to find the DWO file).
7917 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7918 THIS_CU->is_debug_types, but nothing else.
7920 We fill in THIS_CU->length.
7922 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7923 linker) then DIE_READER_FUNC will not get called.
7925 THIS_CU->cu is always freed when done.
7926 This is done in order to not leave THIS_CU->cu in a state where we have
7927 to care whether it refers to the "main" CU or the DWO CU. */
7930 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7931 struct dwo_file *dwo_file,
7932 die_reader_func_ftype *die_reader_func,
7935 struct objfile *objfile = dwarf2_per_objfile->objfile;
7936 struct dwarf2_section_info *section = this_cu->section;
7937 bfd *abfd = get_section_bfd_owner (section);
7938 struct dwarf2_section_info *abbrev_section;
7939 struct dwarf2_cu cu;
7940 const gdb_byte *begin_info_ptr, *info_ptr;
7941 struct die_reader_specs reader;
7942 struct cleanup *cleanups;
7943 struct die_info *comp_unit_die;
7946 if (dwarf_die_debug)
7947 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
7948 this_cu->is_debug_types ? "type" : "comp",
7949 to_underlying (this_cu->sect_off));
7951 gdb_assert (this_cu->cu == NULL);
7953 abbrev_section = (dwo_file != NULL
7954 ? &dwo_file->sections.abbrev
7955 : get_abbrev_section_for_cu (this_cu));
7957 /* This is cheap if the section is already read in. */
7958 dwarf2_read_section (objfile, section);
7960 init_one_comp_unit (&cu, this_cu);
7962 cleanups = make_cleanup (free_stack_comp_unit, &cu);
7964 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7965 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
7966 abbrev_section, info_ptr,
7967 (this_cu->is_debug_types
7969 : rcuh_kind::COMPILE));
7971 this_cu->length = get_cu_length (&cu.header);
7973 /* Skip dummy compilation units. */
7974 if (info_ptr >= begin_info_ptr + this_cu->length
7975 || peek_abbrev_code (abfd, info_ptr) == 0)
7977 do_cleanups (cleanups);
7981 dwarf2_read_abbrevs (&cu, abbrev_section);
7982 make_cleanup (dwarf2_free_abbrev_table, &cu);
7984 init_cu_die_reader (&reader, &cu, section, dwo_file);
7985 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7987 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7989 do_cleanups (cleanups);
7992 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7993 does not lookup the specified DWO file.
7994 This cannot be used to read DWO files.
7996 THIS_CU->cu is always freed when done.
7997 This is done in order to not leave THIS_CU->cu in a state where we have
7998 to care whether it refers to the "main" CU or the DWO CU.
7999 We can revisit this if the data shows there's a performance issue. */
8002 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
8003 die_reader_func_ftype *die_reader_func,
8006 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
8009 /* Type Unit Groups.
8011 Type Unit Groups are a way to collapse the set of all TUs (type units) into
8012 a more manageable set. The grouping is done by DW_AT_stmt_list entry
8013 so that all types coming from the same compilation (.o file) are grouped
8014 together. A future step could be to put the types in the same symtab as
8015 the CU the types ultimately came from. */
8018 hash_type_unit_group (const void *item)
8020 const struct type_unit_group *tu_group
8021 = (const struct type_unit_group *) item;
8023 return hash_stmt_list_entry (&tu_group->hash);
8027 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
8029 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
8030 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
8032 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
8035 /* Allocate a hash table for type unit groups. */
8038 allocate_type_unit_groups_table (void)
8040 return htab_create_alloc_ex (3,
8041 hash_type_unit_group,
8044 &dwarf2_per_objfile->objfile->objfile_obstack,
8045 hashtab_obstack_allocate,
8046 dummy_obstack_deallocate);
8049 /* Type units that don't have DW_AT_stmt_list are grouped into their own
8050 partial symtabs. We combine several TUs per psymtab to not let the size
8051 of any one psymtab grow too big. */
8052 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
8053 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
8055 /* Helper routine for get_type_unit_group.
8056 Create the type_unit_group object used to hold one or more TUs. */
8058 static struct type_unit_group *
8059 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
8061 struct objfile *objfile = dwarf2_per_objfile->objfile;
8062 struct dwarf2_per_cu_data *per_cu;
8063 struct type_unit_group *tu_group;
8065 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8066 struct type_unit_group);
8067 per_cu = &tu_group->per_cu;
8068 per_cu->objfile = objfile;
8070 if (dwarf2_per_objfile->using_index)
8072 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8073 struct dwarf2_per_cu_quick_data);
8077 unsigned int line_offset = to_underlying (line_offset_struct);
8078 struct partial_symtab *pst;
8081 /* Give the symtab a useful name for debug purposes. */
8082 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
8083 name = xstrprintf ("<type_units_%d>",
8084 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
8086 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
8088 pst = create_partial_symtab (per_cu, name);
8094 tu_group->hash.dwo_unit = cu->dwo_unit;
8095 tu_group->hash.line_sect_off = line_offset_struct;
8100 /* Look up the type_unit_group for type unit CU, and create it if necessary.
8101 STMT_LIST is a DW_AT_stmt_list attribute. */
8103 static struct type_unit_group *
8104 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
8106 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8107 struct type_unit_group *tu_group;
8109 unsigned int line_offset;
8110 struct type_unit_group type_unit_group_for_lookup;
8112 if (dwarf2_per_objfile->type_unit_groups == NULL)
8114 dwarf2_per_objfile->type_unit_groups =
8115 allocate_type_unit_groups_table ();
8118 /* Do we need to create a new group, or can we use an existing one? */
8122 line_offset = DW_UNSND (stmt_list);
8123 ++tu_stats->nr_symtab_sharers;
8127 /* Ugh, no stmt_list. Rare, but we have to handle it.
8128 We can do various things here like create one group per TU or
8129 spread them over multiple groups to split up the expansion work.
8130 To avoid worst case scenarios (too many groups or too large groups)
8131 we, umm, group them in bunches. */
8132 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
8133 | (tu_stats->nr_stmt_less_type_units
8134 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
8135 ++tu_stats->nr_stmt_less_type_units;
8138 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
8139 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
8140 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8141 &type_unit_group_for_lookup, INSERT);
8144 tu_group = (struct type_unit_group *) *slot;
8145 gdb_assert (tu_group != NULL);
8149 sect_offset line_offset_struct = (sect_offset) line_offset;
8150 tu_group = create_type_unit_group (cu, line_offset_struct);
8152 ++tu_stats->nr_symtabs;
8158 /* Partial symbol tables. */
8160 /* Create a psymtab named NAME and assign it to PER_CU.
8162 The caller must fill in the following details:
8163 dirname, textlow, texthigh. */
8165 static struct partial_symtab *
8166 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8168 struct objfile *objfile = per_cu->objfile;
8169 struct partial_symtab *pst;
8171 pst = start_psymtab_common (objfile, name, 0,
8172 objfile->global_psymbols,
8173 objfile->static_psymbols);
8175 pst->psymtabs_addrmap_supported = 1;
8177 /* This is the glue that links PST into GDB's symbol API. */
8178 pst->read_symtab_private = per_cu;
8179 pst->read_symtab = dwarf2_read_symtab;
8180 per_cu->v.psymtab = pst;
8185 /* The DATA object passed to process_psymtab_comp_unit_reader has this
8188 struct process_psymtab_comp_unit_data
8190 /* True if we are reading a DW_TAG_partial_unit. */
8192 int want_partial_unit;
8194 /* The "pretend" language that is used if the CU doesn't declare a
8197 enum language pretend_language;
8200 /* die_reader_func for process_psymtab_comp_unit. */
8203 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8204 const gdb_byte *info_ptr,
8205 struct die_info *comp_unit_die,
8209 struct dwarf2_cu *cu = reader->cu;
8210 struct objfile *objfile = cu->objfile;
8211 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8212 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8214 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8215 struct partial_symtab *pst;
8216 enum pc_bounds_kind cu_bounds_kind;
8217 const char *filename;
8218 struct process_psymtab_comp_unit_data *info
8219 = (struct process_psymtab_comp_unit_data *) data;
8221 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8224 gdb_assert (! per_cu->is_debug_types);
8226 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8228 cu->list_in_scope = &file_symbols;
8230 /* Allocate a new partial symbol table structure. */
8231 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8232 if (filename == NULL)
8235 pst = create_partial_symtab (per_cu, filename);
8237 /* This must be done before calling dwarf2_build_include_psymtabs. */
8238 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8240 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8242 dwarf2_find_base_address (comp_unit_die, cu);
8244 /* Possibly set the default values of LOWPC and HIGHPC from
8246 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8247 &best_highpc, cu, pst);
8248 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8249 /* Store the contiguous range if it is not empty; it can be empty for
8250 CUs with no code. */
8251 addrmap_set_empty (objfile->psymtabs_addrmap,
8252 gdbarch_adjust_dwarf2_addr (gdbarch,
8253 best_lowpc + baseaddr),
8254 gdbarch_adjust_dwarf2_addr (gdbarch,
8255 best_highpc + baseaddr) - 1,
8258 /* Check if comp unit has_children.
8259 If so, read the rest of the partial symbols from this comp unit.
8260 If not, there's no more debug_info for this comp unit. */
8263 struct partial_die_info *first_die;
8264 CORE_ADDR lowpc, highpc;
8266 lowpc = ((CORE_ADDR) -1);
8267 highpc = ((CORE_ADDR) 0);
8269 first_die = load_partial_dies (reader, info_ptr, 1);
8271 scan_partial_symbols (first_die, &lowpc, &highpc,
8272 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8274 /* If we didn't find a lowpc, set it to highpc to avoid
8275 complaints from `maint check'. */
8276 if (lowpc == ((CORE_ADDR) -1))
8279 /* If the compilation unit didn't have an explicit address range,
8280 then use the information extracted from its child dies. */
8281 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8284 best_highpc = highpc;
8287 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8288 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
8290 end_psymtab_common (objfile, pst);
8292 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8295 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8296 struct dwarf2_per_cu_data *iter;
8298 /* Fill in 'dependencies' here; we fill in 'users' in a
8300 pst->number_of_dependencies = len;
8302 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8304 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8307 pst->dependencies[i] = iter->v.psymtab;
8309 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8312 /* Get the list of files included in the current compilation unit,
8313 and build a psymtab for each of them. */
8314 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8316 if (dwarf_read_debug)
8318 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8320 fprintf_unfiltered (gdb_stdlog,
8321 "Psymtab for %s unit @0x%x: %s - %s"
8322 ", %d global, %d static syms\n",
8323 per_cu->is_debug_types ? "type" : "comp",
8324 to_underlying (per_cu->sect_off),
8325 paddress (gdbarch, pst->textlow),
8326 paddress (gdbarch, pst->texthigh),
8327 pst->n_global_syms, pst->n_static_syms);
8331 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8332 Process compilation unit THIS_CU for a psymtab. */
8335 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8336 int want_partial_unit,
8337 enum language pretend_language)
8339 /* If this compilation unit was already read in, free the
8340 cached copy in order to read it in again. This is
8341 necessary because we skipped some symbols when we first
8342 read in the compilation unit (see load_partial_dies).
8343 This problem could be avoided, but the benefit is unclear. */
8344 if (this_cu->cu != NULL)
8345 free_one_cached_comp_unit (this_cu);
8347 if (this_cu->is_debug_types)
8348 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8352 process_psymtab_comp_unit_data info;
8353 info.want_partial_unit = want_partial_unit;
8354 info.pretend_language = pretend_language;
8355 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8356 process_psymtab_comp_unit_reader, &info);
8359 /* Age out any secondary CUs. */
8360 age_cached_comp_units ();
8363 /* Reader function for build_type_psymtabs. */
8366 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8367 const gdb_byte *info_ptr,
8368 struct die_info *type_unit_die,
8372 struct objfile *objfile = dwarf2_per_objfile->objfile;
8373 struct dwarf2_cu *cu = reader->cu;
8374 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8375 struct signatured_type *sig_type;
8376 struct type_unit_group *tu_group;
8377 struct attribute *attr;
8378 struct partial_die_info *first_die;
8379 CORE_ADDR lowpc, highpc;
8380 struct partial_symtab *pst;
8382 gdb_assert (data == NULL);
8383 gdb_assert (per_cu->is_debug_types);
8384 sig_type = (struct signatured_type *) per_cu;
8389 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8390 tu_group = get_type_unit_group (cu, attr);
8392 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8394 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8395 cu->list_in_scope = &file_symbols;
8396 pst = create_partial_symtab (per_cu, "");
8399 first_die = load_partial_dies (reader, info_ptr, 1);
8401 lowpc = (CORE_ADDR) -1;
8402 highpc = (CORE_ADDR) 0;
8403 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8405 end_psymtab_common (objfile, pst);
8408 /* Struct used to sort TUs by their abbreviation table offset. */
8410 struct tu_abbrev_offset
8412 struct signatured_type *sig_type;
8413 sect_offset abbrev_offset;
8416 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
8419 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
8421 const struct tu_abbrev_offset * const *a
8422 = (const struct tu_abbrev_offset * const*) ap;
8423 const struct tu_abbrev_offset * const *b
8424 = (const struct tu_abbrev_offset * const*) bp;
8425 sect_offset aoff = (*a)->abbrev_offset;
8426 sect_offset boff = (*b)->abbrev_offset;
8428 return (aoff > boff) - (aoff < boff);
8431 /* Efficiently read all the type units.
8432 This does the bulk of the work for build_type_psymtabs.
8434 The efficiency is because we sort TUs by the abbrev table they use and
8435 only read each abbrev table once. In one program there are 200K TUs
8436 sharing 8K abbrev tables.
8438 The main purpose of this function is to support building the
8439 dwarf2_per_objfile->type_unit_groups table.
8440 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8441 can collapse the search space by grouping them by stmt_list.
8442 The savings can be significant, in the same program from above the 200K TUs
8443 share 8K stmt_list tables.
8445 FUNC is expected to call get_type_unit_group, which will create the
8446 struct type_unit_group if necessary and add it to
8447 dwarf2_per_objfile->type_unit_groups. */
8450 build_type_psymtabs_1 (void)
8452 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8453 struct cleanup *cleanups;
8454 struct abbrev_table *abbrev_table;
8455 sect_offset abbrev_offset;
8456 struct tu_abbrev_offset *sorted_by_abbrev;
8459 /* It's up to the caller to not call us multiple times. */
8460 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8462 if (dwarf2_per_objfile->n_type_units == 0)
8465 /* TUs typically share abbrev tables, and there can be way more TUs than
8466 abbrev tables. Sort by abbrev table to reduce the number of times we
8467 read each abbrev table in.
8468 Alternatives are to punt or to maintain a cache of abbrev tables.
8469 This is simpler and efficient enough for now.
8471 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8472 symtab to use). Typically TUs with the same abbrev offset have the same
8473 stmt_list value too so in practice this should work well.
8475 The basic algorithm here is:
8477 sort TUs by abbrev table
8478 for each TU with same abbrev table:
8479 read abbrev table if first user
8480 read TU top level DIE
8481 [IWBN if DWO skeletons had DW_AT_stmt_list]
8484 if (dwarf_read_debug)
8485 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8487 /* Sort in a separate table to maintain the order of all_type_units
8488 for .gdb_index: TU indices directly index all_type_units. */
8489 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
8490 dwarf2_per_objfile->n_type_units);
8491 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8493 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8495 sorted_by_abbrev[i].sig_type = sig_type;
8496 sorted_by_abbrev[i].abbrev_offset =
8497 read_abbrev_offset (sig_type->per_cu.section,
8498 sig_type->per_cu.sect_off);
8500 cleanups = make_cleanup (xfree, sorted_by_abbrev);
8501 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
8502 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
8504 abbrev_offset = (sect_offset) ~(unsigned) 0;
8505 abbrev_table = NULL;
8506 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
8508 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8510 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8512 /* Switch to the next abbrev table if necessary. */
8513 if (abbrev_table == NULL
8514 || tu->abbrev_offset != abbrev_offset)
8516 if (abbrev_table != NULL)
8518 abbrev_table_free (abbrev_table);
8519 /* Reset to NULL in case abbrev_table_read_table throws
8520 an error: abbrev_table_free_cleanup will get called. */
8521 abbrev_table = NULL;
8523 abbrev_offset = tu->abbrev_offset;
8525 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
8527 ++tu_stats->nr_uniq_abbrev_tables;
8530 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
8531 build_type_psymtabs_reader, NULL);
8534 do_cleanups (cleanups);
8537 /* Print collected type unit statistics. */
8540 print_tu_stats (void)
8542 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8544 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8545 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8546 dwarf2_per_objfile->n_type_units);
8547 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8548 tu_stats->nr_uniq_abbrev_tables);
8549 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8550 tu_stats->nr_symtabs);
8551 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8552 tu_stats->nr_symtab_sharers);
8553 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8554 tu_stats->nr_stmt_less_type_units);
8555 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8556 tu_stats->nr_all_type_units_reallocs);
8559 /* Traversal function for build_type_psymtabs. */
8562 build_type_psymtab_dependencies (void **slot, void *info)
8564 struct objfile *objfile = dwarf2_per_objfile->objfile;
8565 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8566 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8567 struct partial_symtab *pst = per_cu->v.psymtab;
8568 int len = VEC_length (sig_type_ptr, tu_group->tus);
8569 struct signatured_type *iter;
8572 gdb_assert (len > 0);
8573 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8575 pst->number_of_dependencies = len;
8577 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8579 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8582 gdb_assert (iter->per_cu.is_debug_types);
8583 pst->dependencies[i] = iter->per_cu.v.psymtab;
8584 iter->type_unit_group = tu_group;
8587 VEC_free (sig_type_ptr, tu_group->tus);
8592 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8593 Build partial symbol tables for the .debug_types comp-units. */
8596 build_type_psymtabs (struct objfile *objfile)
8598 if (! create_all_type_units (objfile))
8601 build_type_psymtabs_1 ();
8604 /* Traversal function for process_skeletonless_type_unit.
8605 Read a TU in a DWO file and build partial symbols for it. */
8608 process_skeletonless_type_unit (void **slot, void *info)
8610 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8611 struct objfile *objfile = (struct objfile *) info;
8612 struct signatured_type find_entry, *entry;
8614 /* If this TU doesn't exist in the global table, add it and read it in. */
8616 if (dwarf2_per_objfile->signatured_types == NULL)
8618 dwarf2_per_objfile->signatured_types
8619 = allocate_signatured_type_table (objfile);
8622 find_entry.signature = dwo_unit->signature;
8623 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8625 /* If we've already seen this type there's nothing to do. What's happening
8626 is we're doing our own version of comdat-folding here. */
8630 /* This does the job that create_all_type_units would have done for
8632 entry = add_type_unit (dwo_unit->signature, slot);
8633 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
8636 /* This does the job that build_type_psymtabs_1 would have done. */
8637 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8638 build_type_psymtabs_reader, NULL);
8643 /* Traversal function for process_skeletonless_type_units. */
8646 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8648 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8650 if (dwo_file->tus != NULL)
8652 htab_traverse_noresize (dwo_file->tus,
8653 process_skeletonless_type_unit, info);
8659 /* Scan all TUs of DWO files, verifying we've processed them.
8660 This is needed in case a TU was emitted without its skeleton.
8661 Note: This can't be done until we know what all the DWO files are. */
8664 process_skeletonless_type_units (struct objfile *objfile)
8666 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8667 if (get_dwp_file () == NULL
8668 && dwarf2_per_objfile->dwo_files != NULL)
8670 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8671 process_dwo_file_for_skeletonless_type_units,
8676 /* Compute the 'user' field for each psymtab in OBJFILE. */
8679 set_partial_user (struct objfile *objfile)
8683 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8685 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
8686 struct partial_symtab *pst = per_cu->v.psymtab;
8692 for (j = 0; j < pst->number_of_dependencies; ++j)
8694 /* Set the 'user' field only if it is not already set. */
8695 if (pst->dependencies[j]->user == NULL)
8696 pst->dependencies[j]->user = pst;
8701 /* Build the partial symbol table by doing a quick pass through the
8702 .debug_info and .debug_abbrev sections. */
8705 dwarf2_build_psymtabs_hard (struct objfile *objfile)
8707 struct cleanup *back_to;
8710 if (dwarf_read_debug)
8712 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8713 objfile_name (objfile));
8716 dwarf2_per_objfile->reading_partial_symbols = 1;
8718 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8720 /* Any cached compilation units will be linked by the per-objfile
8721 read_in_chain. Make sure to free them when we're done. */
8722 back_to = make_cleanup (free_cached_comp_units, NULL);
8724 build_type_psymtabs (objfile);
8726 create_all_comp_units (objfile);
8728 /* Create a temporary address map on a temporary obstack. We later
8729 copy this to the final obstack. */
8730 auto_obstack temp_obstack;
8732 scoped_restore save_psymtabs_addrmap
8733 = make_scoped_restore (&objfile->psymtabs_addrmap,
8734 addrmap_create_mutable (&temp_obstack));
8736 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8738 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
8740 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8743 /* This has to wait until we read the CUs, we need the list of DWOs. */
8744 process_skeletonless_type_units (objfile);
8746 /* Now that all TUs have been processed we can fill in the dependencies. */
8747 if (dwarf2_per_objfile->type_unit_groups != NULL)
8749 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8750 build_type_psymtab_dependencies, NULL);
8753 if (dwarf_read_debug)
8756 set_partial_user (objfile);
8758 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8759 &objfile->objfile_obstack);
8760 /* At this point we want to keep the address map. */
8761 save_psymtabs_addrmap.release ();
8763 do_cleanups (back_to);
8765 if (dwarf_read_debug)
8766 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8767 objfile_name (objfile));
8770 /* die_reader_func for load_partial_comp_unit. */
8773 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8774 const gdb_byte *info_ptr,
8775 struct die_info *comp_unit_die,
8779 struct dwarf2_cu *cu = reader->cu;
8781 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8783 /* Check if comp unit has_children.
8784 If so, read the rest of the partial symbols from this comp unit.
8785 If not, there's no more debug_info for this comp unit. */
8787 load_partial_dies (reader, info_ptr, 0);
8790 /* Load the partial DIEs for a secondary CU into memory.
8791 This is also used when rereading a primary CU with load_all_dies. */
8794 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8796 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8797 load_partial_comp_unit_reader, NULL);
8801 read_comp_units_from_section (struct objfile *objfile,
8802 struct dwarf2_section_info *section,
8803 struct dwarf2_section_info *abbrev_section,
8804 unsigned int is_dwz,
8807 struct dwarf2_per_cu_data ***all_comp_units)
8809 const gdb_byte *info_ptr;
8811 if (dwarf_read_debug)
8812 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8813 get_section_name (section),
8814 get_section_file_name (section));
8816 dwarf2_read_section (objfile, section);
8818 info_ptr = section->buffer;
8820 while (info_ptr < section->buffer + section->size)
8822 struct dwarf2_per_cu_data *this_cu;
8824 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8826 comp_unit_head cu_header;
8827 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
8828 info_ptr, rcuh_kind::COMPILE);
8830 /* Save the compilation unit for later lookup. */
8831 if (cu_header.unit_type != DW_UT_type)
8833 this_cu = XOBNEW (&objfile->objfile_obstack,
8834 struct dwarf2_per_cu_data);
8835 memset (this_cu, 0, sizeof (*this_cu));
8839 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8840 struct signatured_type);
8841 memset (sig_type, 0, sizeof (*sig_type));
8842 sig_type->signature = cu_header.signature;
8843 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8844 this_cu = &sig_type->per_cu;
8846 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8847 this_cu->sect_off = sect_off;
8848 this_cu->length = cu_header.length + cu_header.initial_length_size;
8849 this_cu->is_dwz = is_dwz;
8850 this_cu->objfile = objfile;
8851 this_cu->section = section;
8853 if (*n_comp_units == *n_allocated)
8856 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
8857 *all_comp_units, *n_allocated);
8859 (*all_comp_units)[*n_comp_units] = this_cu;
8862 info_ptr = info_ptr + this_cu->length;
8866 /* Create a list of all compilation units in OBJFILE.
8867 This is only done for -readnow and building partial symtabs. */
8870 create_all_comp_units (struct objfile *objfile)
8874 struct dwarf2_per_cu_data **all_comp_units;
8875 struct dwz_file *dwz;
8879 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
8881 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info,
8882 &dwarf2_per_objfile->abbrev, 0,
8883 &n_allocated, &n_comp_units, &all_comp_units);
8885 dwz = dwarf2_get_dwz_file ();
8887 read_comp_units_from_section (objfile, &dwz->info, &dwz->abbrev, 1,
8888 &n_allocated, &n_comp_units,
8891 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
8892 struct dwarf2_per_cu_data *,
8894 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
8895 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
8896 xfree (all_comp_units);
8897 dwarf2_per_objfile->n_comp_units = n_comp_units;
8900 /* Process all loaded DIEs for compilation unit CU, starting at
8901 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8902 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8903 DW_AT_ranges). See the comments of add_partial_subprogram on how
8904 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8907 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8908 CORE_ADDR *highpc, int set_addrmap,
8909 struct dwarf2_cu *cu)
8911 struct partial_die_info *pdi;
8913 /* Now, march along the PDI's, descending into ones which have
8914 interesting children but skipping the children of the other ones,
8915 until we reach the end of the compilation unit. */
8921 fixup_partial_die (pdi, cu);
8923 /* Anonymous namespaces or modules have no name but have interesting
8924 children, so we need to look at them. Ditto for anonymous
8927 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8928 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8929 || pdi->tag == DW_TAG_imported_unit)
8933 case DW_TAG_subprogram:
8934 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8936 case DW_TAG_constant:
8937 case DW_TAG_variable:
8938 case DW_TAG_typedef:
8939 case DW_TAG_union_type:
8940 if (!pdi->is_declaration)
8942 add_partial_symbol (pdi, cu);
8945 case DW_TAG_class_type:
8946 case DW_TAG_interface_type:
8947 case DW_TAG_structure_type:
8948 if (!pdi->is_declaration)
8950 add_partial_symbol (pdi, cu);
8952 if (cu->language == language_rust && pdi->has_children)
8953 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8956 case DW_TAG_enumeration_type:
8957 if (!pdi->is_declaration)
8958 add_partial_enumeration (pdi, cu);
8960 case DW_TAG_base_type:
8961 case DW_TAG_subrange_type:
8962 /* File scope base type definitions are added to the partial
8964 add_partial_symbol (pdi, cu);
8966 case DW_TAG_namespace:
8967 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8970 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8972 case DW_TAG_imported_unit:
8974 struct dwarf2_per_cu_data *per_cu;
8976 /* For now we don't handle imported units in type units. */
8977 if (cu->per_cu->is_debug_types)
8979 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8980 " supported in type units [in module %s]"),
8981 objfile_name (cu->objfile));
8984 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
8988 /* Go read the partial unit, if needed. */
8989 if (per_cu->v.psymtab == NULL)
8990 process_psymtab_comp_unit (per_cu, 1, cu->language);
8992 VEC_safe_push (dwarf2_per_cu_ptr,
8993 cu->per_cu->imported_symtabs, per_cu);
8996 case DW_TAG_imported_declaration:
8997 add_partial_symbol (pdi, cu);
9004 /* If the die has a sibling, skip to the sibling. */
9006 pdi = pdi->die_sibling;
9010 /* Functions used to compute the fully scoped name of a partial DIE.
9012 Normally, this is simple. For C++, the parent DIE's fully scoped
9013 name is concatenated with "::" and the partial DIE's name.
9014 Enumerators are an exception; they use the scope of their parent
9015 enumeration type, i.e. the name of the enumeration type is not
9016 prepended to the enumerator.
9018 There are two complexities. One is DW_AT_specification; in this
9019 case "parent" means the parent of the target of the specification,
9020 instead of the direct parent of the DIE. The other is compilers
9021 which do not emit DW_TAG_namespace; in this case we try to guess
9022 the fully qualified name of structure types from their members'
9023 linkage names. This must be done using the DIE's children rather
9024 than the children of any DW_AT_specification target. We only need
9025 to do this for structures at the top level, i.e. if the target of
9026 any DW_AT_specification (if any; otherwise the DIE itself) does not
9029 /* Compute the scope prefix associated with PDI's parent, in
9030 compilation unit CU. The result will be allocated on CU's
9031 comp_unit_obstack, or a copy of the already allocated PDI->NAME
9032 field. NULL is returned if no prefix is necessary. */
9034 partial_die_parent_scope (struct partial_die_info *pdi,
9035 struct dwarf2_cu *cu)
9037 const char *grandparent_scope;
9038 struct partial_die_info *parent, *real_pdi;
9040 /* We need to look at our parent DIE; if we have a DW_AT_specification,
9041 then this means the parent of the specification DIE. */
9044 while (real_pdi->has_specification)
9045 real_pdi = find_partial_die (real_pdi->spec_offset,
9046 real_pdi->spec_is_dwz, cu);
9048 parent = real_pdi->die_parent;
9052 if (parent->scope_set)
9053 return parent->scope;
9055 fixup_partial_die (parent, cu);
9057 grandparent_scope = partial_die_parent_scope (parent, cu);
9059 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9060 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9061 Work around this problem here. */
9062 if (cu->language == language_cplus
9063 && parent->tag == DW_TAG_namespace
9064 && strcmp (parent->name, "::") == 0
9065 && grandparent_scope == NULL)
9067 parent->scope = NULL;
9068 parent->scope_set = 1;
9072 if (pdi->tag == DW_TAG_enumerator)
9073 /* Enumerators should not get the name of the enumeration as a prefix. */
9074 parent->scope = grandparent_scope;
9075 else if (parent->tag == DW_TAG_namespace
9076 || parent->tag == DW_TAG_module
9077 || parent->tag == DW_TAG_structure_type
9078 || parent->tag == DW_TAG_class_type
9079 || parent->tag == DW_TAG_interface_type
9080 || parent->tag == DW_TAG_union_type
9081 || parent->tag == DW_TAG_enumeration_type)
9083 if (grandparent_scope == NULL)
9084 parent->scope = parent->name;
9086 parent->scope = typename_concat (&cu->comp_unit_obstack,
9088 parent->name, 0, cu);
9092 /* FIXME drow/2004-04-01: What should we be doing with
9093 function-local names? For partial symbols, we should probably be
9095 complaint (&symfile_complaints,
9096 _("unhandled containing DIE tag %d for DIE at %d"),
9097 parent->tag, to_underlying (pdi->sect_off));
9098 parent->scope = grandparent_scope;
9101 parent->scope_set = 1;
9102 return parent->scope;
9105 /* Return the fully scoped name associated with PDI, from compilation unit
9106 CU. The result will be allocated with malloc. */
9109 partial_die_full_name (struct partial_die_info *pdi,
9110 struct dwarf2_cu *cu)
9112 const char *parent_scope;
9114 /* If this is a template instantiation, we can not work out the
9115 template arguments from partial DIEs. So, unfortunately, we have
9116 to go through the full DIEs. At least any work we do building
9117 types here will be reused if full symbols are loaded later. */
9118 if (pdi->has_template_arguments)
9120 fixup_partial_die (pdi, cu);
9122 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
9124 struct die_info *die;
9125 struct attribute attr;
9126 struct dwarf2_cu *ref_cu = cu;
9128 /* DW_FORM_ref_addr is using section offset. */
9129 attr.name = (enum dwarf_attribute) 0;
9130 attr.form = DW_FORM_ref_addr;
9131 attr.u.unsnd = to_underlying (pdi->sect_off);
9132 die = follow_die_ref (NULL, &attr, &ref_cu);
9134 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
9138 parent_scope = partial_die_parent_scope (pdi, cu);
9139 if (parent_scope == NULL)
9142 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
9146 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
9148 struct objfile *objfile = cu->objfile;
9149 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9151 const char *actual_name = NULL;
9153 char *built_actual_name;
9155 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9157 built_actual_name = partial_die_full_name (pdi, cu);
9158 if (built_actual_name != NULL)
9159 actual_name = built_actual_name;
9161 if (actual_name == NULL)
9162 actual_name = pdi->name;
9166 case DW_TAG_subprogram:
9167 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
9168 if (pdi->is_external || cu->language == language_ada)
9170 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
9171 of the global scope. But in Ada, we want to be able to access
9172 nested procedures globally. So all Ada subprograms are stored
9173 in the global scope. */
9174 add_psymbol_to_list (actual_name, strlen (actual_name),
9175 built_actual_name != NULL,
9176 VAR_DOMAIN, LOC_BLOCK,
9177 &objfile->global_psymbols,
9178 addr, cu->language, objfile);
9182 add_psymbol_to_list (actual_name, strlen (actual_name),
9183 built_actual_name != NULL,
9184 VAR_DOMAIN, LOC_BLOCK,
9185 &objfile->static_psymbols,
9186 addr, cu->language, objfile);
9189 if (pdi->main_subprogram && actual_name != NULL)
9190 set_objfile_main_name (objfile, actual_name, cu->language);
9192 case DW_TAG_constant:
9194 std::vector<partial_symbol *> *list;
9196 if (pdi->is_external)
9197 list = &objfile->global_psymbols;
9199 list = &objfile->static_psymbols;
9200 add_psymbol_to_list (actual_name, strlen (actual_name),
9201 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9202 list, 0, cu->language, objfile);
9205 case DW_TAG_variable:
9207 addr = decode_locdesc (pdi->d.locdesc, cu);
9211 && !dwarf2_per_objfile->has_section_at_zero)
9213 /* A global or static variable may also have been stripped
9214 out by the linker if unused, in which case its address
9215 will be nullified; do not add such variables into partial
9216 symbol table then. */
9218 else if (pdi->is_external)
9221 Don't enter into the minimal symbol tables as there is
9222 a minimal symbol table entry from the ELF symbols already.
9223 Enter into partial symbol table if it has a location
9224 descriptor or a type.
9225 If the location descriptor is missing, new_symbol will create
9226 a LOC_UNRESOLVED symbol, the address of the variable will then
9227 be determined from the minimal symbol table whenever the variable
9229 The address for the partial symbol table entry is not
9230 used by GDB, but it comes in handy for debugging partial symbol
9233 if (pdi->d.locdesc || pdi->has_type)
9234 add_psymbol_to_list (actual_name, strlen (actual_name),
9235 built_actual_name != NULL,
9236 VAR_DOMAIN, LOC_STATIC,
9237 &objfile->global_psymbols,
9239 cu->language, objfile);
9243 int has_loc = pdi->d.locdesc != NULL;
9245 /* Static Variable. Skip symbols whose value we cannot know (those
9246 without location descriptors or constant values). */
9247 if (!has_loc && !pdi->has_const_value)
9249 xfree (built_actual_name);
9253 add_psymbol_to_list (actual_name, strlen (actual_name),
9254 built_actual_name != NULL,
9255 VAR_DOMAIN, LOC_STATIC,
9256 &objfile->static_psymbols,
9257 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
9258 cu->language, objfile);
9261 case DW_TAG_typedef:
9262 case DW_TAG_base_type:
9263 case DW_TAG_subrange_type:
9264 add_psymbol_to_list (actual_name, strlen (actual_name),
9265 built_actual_name != NULL,
9266 VAR_DOMAIN, LOC_TYPEDEF,
9267 &objfile->static_psymbols,
9268 0, cu->language, objfile);
9270 case DW_TAG_imported_declaration:
9271 case DW_TAG_namespace:
9272 add_psymbol_to_list (actual_name, strlen (actual_name),
9273 built_actual_name != NULL,
9274 VAR_DOMAIN, LOC_TYPEDEF,
9275 &objfile->global_psymbols,
9276 0, cu->language, objfile);
9279 add_psymbol_to_list (actual_name, strlen (actual_name),
9280 built_actual_name != NULL,
9281 MODULE_DOMAIN, LOC_TYPEDEF,
9282 &objfile->global_psymbols,
9283 0, cu->language, objfile);
9285 case DW_TAG_class_type:
9286 case DW_TAG_interface_type:
9287 case DW_TAG_structure_type:
9288 case DW_TAG_union_type:
9289 case DW_TAG_enumeration_type:
9290 /* Skip external references. The DWARF standard says in the section
9291 about "Structure, Union, and Class Type Entries": "An incomplete
9292 structure, union or class type is represented by a structure,
9293 union or class entry that does not have a byte size attribute
9294 and that has a DW_AT_declaration attribute." */
9295 if (!pdi->has_byte_size && pdi->is_declaration)
9297 xfree (built_actual_name);
9301 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9302 static vs. global. */
9303 add_psymbol_to_list (actual_name, strlen (actual_name),
9304 built_actual_name != NULL,
9305 STRUCT_DOMAIN, LOC_TYPEDEF,
9306 cu->language == language_cplus
9307 ? &objfile->global_psymbols
9308 : &objfile->static_psymbols,
9309 0, cu->language, objfile);
9312 case DW_TAG_enumerator:
9313 add_psymbol_to_list (actual_name, strlen (actual_name),
9314 built_actual_name != NULL,
9315 VAR_DOMAIN, LOC_CONST,
9316 cu->language == language_cplus
9317 ? &objfile->global_psymbols
9318 : &objfile->static_psymbols,
9319 0, cu->language, objfile);
9325 xfree (built_actual_name);
9328 /* Read a partial die corresponding to a namespace; also, add a symbol
9329 corresponding to that namespace to the symbol table. NAMESPACE is
9330 the name of the enclosing namespace. */
9333 add_partial_namespace (struct partial_die_info *pdi,
9334 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9335 int set_addrmap, struct dwarf2_cu *cu)
9337 /* Add a symbol for the namespace. */
9339 add_partial_symbol (pdi, cu);
9341 /* Now scan partial symbols in that namespace. */
9343 if (pdi->has_children)
9344 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9347 /* Read a partial die corresponding to a Fortran module. */
9350 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9351 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9353 /* Add a symbol for the namespace. */
9355 add_partial_symbol (pdi, cu);
9357 /* Now scan partial symbols in that module. */
9359 if (pdi->has_children)
9360 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9363 /* Read a partial die corresponding to a subprogram and create a partial
9364 symbol for that subprogram. When the CU language allows it, this
9365 routine also defines a partial symbol for each nested subprogram
9366 that this subprogram contains. If SET_ADDRMAP is true, record the
9367 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
9368 and highest PC values found in PDI.
9370 PDI may also be a lexical block, in which case we simply search
9371 recursively for subprograms defined inside that lexical block.
9372 Again, this is only performed when the CU language allows this
9373 type of definitions. */
9376 add_partial_subprogram (struct partial_die_info *pdi,
9377 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9378 int set_addrmap, struct dwarf2_cu *cu)
9380 if (pdi->tag == DW_TAG_subprogram)
9382 if (pdi->has_pc_info)
9384 if (pdi->lowpc < *lowpc)
9385 *lowpc = pdi->lowpc;
9386 if (pdi->highpc > *highpc)
9387 *highpc = pdi->highpc;
9390 struct objfile *objfile = cu->objfile;
9391 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9396 baseaddr = ANOFFSET (objfile->section_offsets,
9397 SECT_OFF_TEXT (objfile));
9398 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9399 pdi->lowpc + baseaddr);
9400 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9401 pdi->highpc + baseaddr);
9402 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9403 cu->per_cu->v.psymtab);
9407 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9409 if (!pdi->is_declaration)
9410 /* Ignore subprogram DIEs that do not have a name, they are
9411 illegal. Do not emit a complaint at this point, we will
9412 do so when we convert this psymtab into a symtab. */
9414 add_partial_symbol (pdi, cu);
9418 if (! pdi->has_children)
9421 if (cu->language == language_ada)
9423 pdi = pdi->die_child;
9426 fixup_partial_die (pdi, cu);
9427 if (pdi->tag == DW_TAG_subprogram
9428 || pdi->tag == DW_TAG_lexical_block)
9429 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9430 pdi = pdi->die_sibling;
9435 /* Read a partial die corresponding to an enumeration type. */
9438 add_partial_enumeration (struct partial_die_info *enum_pdi,
9439 struct dwarf2_cu *cu)
9441 struct partial_die_info *pdi;
9443 if (enum_pdi->name != NULL)
9444 add_partial_symbol (enum_pdi, cu);
9446 pdi = enum_pdi->die_child;
9449 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9450 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
9452 add_partial_symbol (pdi, cu);
9453 pdi = pdi->die_sibling;
9457 /* Return the initial uleb128 in the die at INFO_PTR. */
9460 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9462 unsigned int bytes_read;
9464 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9467 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
9468 Return the corresponding abbrev, or NULL if the number is zero (indicating
9469 an empty DIE). In either case *BYTES_READ will be set to the length of
9470 the initial number. */
9472 static struct abbrev_info *
9473 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
9474 struct dwarf2_cu *cu)
9476 bfd *abfd = cu->objfile->obfd;
9477 unsigned int abbrev_number;
9478 struct abbrev_info *abbrev;
9480 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9482 if (abbrev_number == 0)
9485 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
9488 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9489 " at offset 0x%x [in module %s]"),
9490 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9491 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
9497 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9498 Returns a pointer to the end of a series of DIEs, terminated by an empty
9499 DIE. Any children of the skipped DIEs will also be skipped. */
9501 static const gdb_byte *
9502 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9504 struct dwarf2_cu *cu = reader->cu;
9505 struct abbrev_info *abbrev;
9506 unsigned int bytes_read;
9510 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9512 return info_ptr + bytes_read;
9514 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9518 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9519 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9520 abbrev corresponding to that skipped uleb128 should be passed in
9521 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9524 static const gdb_byte *
9525 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9526 struct abbrev_info *abbrev)
9528 unsigned int bytes_read;
9529 struct attribute attr;
9530 bfd *abfd = reader->abfd;
9531 struct dwarf2_cu *cu = reader->cu;
9532 const gdb_byte *buffer = reader->buffer;
9533 const gdb_byte *buffer_end = reader->buffer_end;
9534 unsigned int form, i;
9536 for (i = 0; i < abbrev->num_attrs; i++)
9538 /* The only abbrev we care about is DW_AT_sibling. */
9539 if (abbrev->attrs[i].name == DW_AT_sibling)
9541 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9542 if (attr.form == DW_FORM_ref_addr)
9543 complaint (&symfile_complaints,
9544 _("ignoring absolute DW_AT_sibling"));
9547 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9548 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9550 if (sibling_ptr < info_ptr)
9551 complaint (&symfile_complaints,
9552 _("DW_AT_sibling points backwards"));
9553 else if (sibling_ptr > reader->buffer_end)
9554 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9560 /* If it isn't DW_AT_sibling, skip this attribute. */
9561 form = abbrev->attrs[i].form;
9565 case DW_FORM_ref_addr:
9566 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9567 and later it is offset sized. */
9568 if (cu->header.version == 2)
9569 info_ptr += cu->header.addr_size;
9571 info_ptr += cu->header.offset_size;
9573 case DW_FORM_GNU_ref_alt:
9574 info_ptr += cu->header.offset_size;
9577 info_ptr += cu->header.addr_size;
9584 case DW_FORM_flag_present:
9585 case DW_FORM_implicit_const:
9597 case DW_FORM_ref_sig8:
9600 case DW_FORM_data16:
9603 case DW_FORM_string:
9604 read_direct_string (abfd, info_ptr, &bytes_read);
9605 info_ptr += bytes_read;
9607 case DW_FORM_sec_offset:
9609 case DW_FORM_GNU_strp_alt:
9610 info_ptr += cu->header.offset_size;
9612 case DW_FORM_exprloc:
9614 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9615 info_ptr += bytes_read;
9617 case DW_FORM_block1:
9618 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9620 case DW_FORM_block2:
9621 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9623 case DW_FORM_block4:
9624 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9628 case DW_FORM_ref_udata:
9629 case DW_FORM_GNU_addr_index:
9630 case DW_FORM_GNU_str_index:
9631 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9633 case DW_FORM_indirect:
9634 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9635 info_ptr += bytes_read;
9636 /* We need to continue parsing from here, so just go back to
9638 goto skip_attribute;
9641 error (_("Dwarf Error: Cannot handle %s "
9642 "in DWARF reader [in module %s]"),
9643 dwarf_form_name (form),
9644 bfd_get_filename (abfd));
9648 if (abbrev->has_children)
9649 return skip_children (reader, info_ptr);
9654 /* Locate ORIG_PDI's sibling.
9655 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9657 static const gdb_byte *
9658 locate_pdi_sibling (const struct die_reader_specs *reader,
9659 struct partial_die_info *orig_pdi,
9660 const gdb_byte *info_ptr)
9662 /* Do we know the sibling already? */
9664 if (orig_pdi->sibling)
9665 return orig_pdi->sibling;
9667 /* Are there any children to deal with? */
9669 if (!orig_pdi->has_children)
9672 /* Skip the children the long way. */
9674 return skip_children (reader, info_ptr);
9677 /* Expand this partial symbol table into a full symbol table. SELF is
9681 dwarf2_read_symtab (struct partial_symtab *self,
9682 struct objfile *objfile)
9686 warning (_("bug: psymtab for %s is already read in."),
9693 printf_filtered (_("Reading in symbols for %s..."),
9695 gdb_flush (gdb_stdout);
9698 /* Restore our global data. */
9700 = (struct dwarf2_per_objfile *) objfile_data (objfile,
9701 dwarf2_objfile_data_key);
9703 /* If this psymtab is constructed from a debug-only objfile, the
9704 has_section_at_zero flag will not necessarily be correct. We
9705 can get the correct value for this flag by looking at the data
9706 associated with the (presumably stripped) associated objfile. */
9707 if (objfile->separate_debug_objfile_backlink)
9709 struct dwarf2_per_objfile *dpo_backlink
9710 = ((struct dwarf2_per_objfile *)
9711 objfile_data (objfile->separate_debug_objfile_backlink,
9712 dwarf2_objfile_data_key));
9714 dwarf2_per_objfile->has_section_at_zero
9715 = dpo_backlink->has_section_at_zero;
9718 dwarf2_per_objfile->reading_partial_symbols = 0;
9720 psymtab_to_symtab_1 (self);
9722 /* Finish up the debug error message. */
9724 printf_filtered (_("done.\n"));
9727 process_cu_includes ();
9730 /* Reading in full CUs. */
9732 /* Add PER_CU to the queue. */
9735 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9736 enum language pretend_language)
9738 struct dwarf2_queue_item *item;
9741 item = XNEW (struct dwarf2_queue_item);
9742 item->per_cu = per_cu;
9743 item->pretend_language = pretend_language;
9746 if (dwarf2_queue == NULL)
9747 dwarf2_queue = item;
9749 dwarf2_queue_tail->next = item;
9751 dwarf2_queue_tail = item;
9754 /* If PER_CU is not yet queued, add it to the queue.
9755 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9757 The result is non-zero if PER_CU was queued, otherwise the result is zero
9758 meaning either PER_CU is already queued or it is already loaded.
9760 N.B. There is an invariant here that if a CU is queued then it is loaded.
9761 The caller is required to load PER_CU if we return non-zero. */
9764 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9765 struct dwarf2_per_cu_data *per_cu,
9766 enum language pretend_language)
9768 /* We may arrive here during partial symbol reading, if we need full
9769 DIEs to process an unusual case (e.g. template arguments). Do
9770 not queue PER_CU, just tell our caller to load its DIEs. */
9771 if (dwarf2_per_objfile->reading_partial_symbols)
9773 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9778 /* Mark the dependence relation so that we don't flush PER_CU
9780 if (dependent_cu != NULL)
9781 dwarf2_add_dependence (dependent_cu, per_cu);
9783 /* If it's already on the queue, we have nothing to do. */
9787 /* If the compilation unit is already loaded, just mark it as
9789 if (per_cu->cu != NULL)
9791 per_cu->cu->last_used = 0;
9795 /* Add it to the queue. */
9796 queue_comp_unit (per_cu, pretend_language);
9801 /* Process the queue. */
9804 process_queue (void)
9806 struct dwarf2_queue_item *item, *next_item;
9808 if (dwarf_read_debug)
9810 fprintf_unfiltered (gdb_stdlog,
9811 "Expanding one or more symtabs of objfile %s ...\n",
9812 objfile_name (dwarf2_per_objfile->objfile));
9815 /* The queue starts out with one item, but following a DIE reference
9816 may load a new CU, adding it to the end of the queue. */
9817 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9819 if ((dwarf2_per_objfile->using_index
9820 ? !item->per_cu->v.quick->compunit_symtab
9821 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9822 /* Skip dummy CUs. */
9823 && item->per_cu->cu != NULL)
9825 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9826 unsigned int debug_print_threshold;
9829 if (per_cu->is_debug_types)
9831 struct signatured_type *sig_type =
9832 (struct signatured_type *) per_cu;
9834 sprintf (buf, "TU %s at offset 0x%x",
9835 hex_string (sig_type->signature),
9836 to_underlying (per_cu->sect_off));
9837 /* There can be 100s of TUs.
9838 Only print them in verbose mode. */
9839 debug_print_threshold = 2;
9843 sprintf (buf, "CU at offset 0x%x",
9844 to_underlying (per_cu->sect_off));
9845 debug_print_threshold = 1;
9848 if (dwarf_read_debug >= debug_print_threshold)
9849 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9851 if (per_cu->is_debug_types)
9852 process_full_type_unit (per_cu, item->pretend_language);
9854 process_full_comp_unit (per_cu, item->pretend_language);
9856 if (dwarf_read_debug >= debug_print_threshold)
9857 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9860 item->per_cu->queued = 0;
9861 next_item = item->next;
9865 dwarf2_queue_tail = NULL;
9867 if (dwarf_read_debug)
9869 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9870 objfile_name (dwarf2_per_objfile->objfile));
9874 /* Free all allocated queue entries. This function only releases anything if
9875 an error was thrown; if the queue was processed then it would have been
9876 freed as we went along. */
9879 dwarf2_release_queue (void *dummy)
9881 struct dwarf2_queue_item *item, *last;
9883 item = dwarf2_queue;
9886 /* Anything still marked queued is likely to be in an
9887 inconsistent state, so discard it. */
9888 if (item->per_cu->queued)
9890 if (item->per_cu->cu != NULL)
9891 free_one_cached_comp_unit (item->per_cu);
9892 item->per_cu->queued = 0;
9900 dwarf2_queue = dwarf2_queue_tail = NULL;
9903 /* Read in full symbols for PST, and anything it depends on. */
9906 psymtab_to_symtab_1 (struct partial_symtab *pst)
9908 struct dwarf2_per_cu_data *per_cu;
9914 for (i = 0; i < pst->number_of_dependencies; i++)
9915 if (!pst->dependencies[i]->readin
9916 && pst->dependencies[i]->user == NULL)
9918 /* Inform about additional files that need to be read in. */
9921 /* FIXME: i18n: Need to make this a single string. */
9922 fputs_filtered (" ", gdb_stdout);
9924 fputs_filtered ("and ", gdb_stdout);
9926 printf_filtered ("%s...", pst->dependencies[i]->filename);
9927 wrap_here (""); /* Flush output. */
9928 gdb_flush (gdb_stdout);
9930 psymtab_to_symtab_1 (pst->dependencies[i]);
9933 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9937 /* It's an include file, no symbols to read for it.
9938 Everything is in the parent symtab. */
9943 dw2_do_instantiate_symtab (per_cu);
9946 /* Trivial hash function for die_info: the hash value of a DIE
9947 is its offset in .debug_info for this objfile. */
9950 die_hash (const void *item)
9952 const struct die_info *die = (const struct die_info *) item;
9954 return to_underlying (die->sect_off);
9957 /* Trivial comparison function for die_info structures: two DIEs
9958 are equal if they have the same offset. */
9961 die_eq (const void *item_lhs, const void *item_rhs)
9963 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9964 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9966 return die_lhs->sect_off == die_rhs->sect_off;
9969 /* die_reader_func for load_full_comp_unit.
9970 This is identical to read_signatured_type_reader,
9971 but is kept separate for now. */
9974 load_full_comp_unit_reader (const struct die_reader_specs *reader,
9975 const gdb_byte *info_ptr,
9976 struct die_info *comp_unit_die,
9980 struct dwarf2_cu *cu = reader->cu;
9981 enum language *language_ptr = (enum language *) data;
9983 gdb_assert (cu->die_hash == NULL);
9985 htab_create_alloc_ex (cu->header.length / 12,
9989 &cu->comp_unit_obstack,
9990 hashtab_obstack_allocate,
9991 dummy_obstack_deallocate);
9994 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9995 &info_ptr, comp_unit_die);
9996 cu->dies = comp_unit_die;
9997 /* comp_unit_die is not stored in die_hash, no need. */
9999 /* We try not to read any attributes in this function, because not
10000 all CUs needed for references have been loaded yet, and symbol
10001 table processing isn't initialized. But we have to set the CU language,
10002 or we won't be able to build types correctly.
10003 Similarly, if we do not read the producer, we can not apply
10004 producer-specific interpretation. */
10005 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
10008 /* Load the DIEs associated with PER_CU into memory. */
10011 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
10012 enum language pretend_language)
10014 gdb_assert (! this_cu->is_debug_types);
10016 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
10017 load_full_comp_unit_reader, &pretend_language);
10020 /* Add a DIE to the delayed physname list. */
10023 add_to_method_list (struct type *type, int fnfield_index, int index,
10024 const char *name, struct die_info *die,
10025 struct dwarf2_cu *cu)
10027 struct delayed_method_info mi;
10029 mi.fnfield_index = fnfield_index;
10033 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
10036 /* A cleanup for freeing the delayed method list. */
10039 free_delayed_list (void *ptr)
10041 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
10042 if (cu->method_list != NULL)
10044 VEC_free (delayed_method_info, cu->method_list);
10045 cu->method_list = NULL;
10049 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
10050 "const" / "volatile". If so, decrements LEN by the length of the
10051 modifier and return true. Otherwise return false. */
10055 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
10057 size_t mod_len = sizeof (mod) - 1;
10058 if (len > mod_len && startswith (physname + (len - mod_len), mod))
10066 /* Compute the physnames of any methods on the CU's method list.
10068 The computation of method physnames is delayed in order to avoid the
10069 (bad) condition that one of the method's formal parameters is of an as yet
10070 incomplete type. */
10073 compute_delayed_physnames (struct dwarf2_cu *cu)
10076 struct delayed_method_info *mi;
10078 /* Only C++ delays computing physnames. */
10079 if (VEC_empty (delayed_method_info, cu->method_list))
10081 gdb_assert (cu->language == language_cplus);
10083 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
10085 const char *physname;
10086 struct fn_fieldlist *fn_flp
10087 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
10088 physname = dwarf2_physname (mi->name, mi->die, cu);
10089 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
10090 = physname ? physname : "";
10092 /* Since there's no tag to indicate whether a method is a
10093 const/volatile overload, extract that information out of the
10095 if (physname != NULL)
10097 size_t len = strlen (physname);
10101 if (physname[len] == ')') /* shortcut */
10103 else if (check_modifier (physname, len, " const"))
10104 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi->index) = 1;
10105 else if (check_modifier (physname, len, " volatile"))
10106 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi->index) = 1;
10114 /* Go objects should be embedded in a DW_TAG_module DIE,
10115 and it's not clear if/how imported objects will appear.
10116 To keep Go support simple until that's worked out,
10117 go back through what we've read and create something usable.
10118 We could do this while processing each DIE, and feels kinda cleaner,
10119 but that way is more invasive.
10120 This is to, for example, allow the user to type "p var" or "b main"
10121 without having to specify the package name, and allow lookups
10122 of module.object to work in contexts that use the expression
10126 fixup_go_packaging (struct dwarf2_cu *cu)
10128 char *package_name = NULL;
10129 struct pending *list;
10132 for (list = global_symbols; list != NULL; list = list->next)
10134 for (i = 0; i < list->nsyms; ++i)
10136 struct symbol *sym = list->symbol[i];
10138 if (SYMBOL_LANGUAGE (sym) == language_go
10139 && SYMBOL_CLASS (sym) == LOC_BLOCK)
10141 char *this_package_name = go_symbol_package_name (sym);
10143 if (this_package_name == NULL)
10145 if (package_name == NULL)
10146 package_name = this_package_name;
10149 if (strcmp (package_name, this_package_name) != 0)
10150 complaint (&symfile_complaints,
10151 _("Symtab %s has objects from two different Go packages: %s and %s"),
10152 (symbol_symtab (sym) != NULL
10153 ? symtab_to_filename_for_display
10154 (symbol_symtab (sym))
10155 : objfile_name (cu->objfile)),
10156 this_package_name, package_name);
10157 xfree (this_package_name);
10163 if (package_name != NULL)
10165 struct objfile *objfile = cu->objfile;
10166 const char *saved_package_name
10167 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
10169 strlen (package_name));
10170 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
10171 saved_package_name);
10172 struct symbol *sym;
10174 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10176 sym = allocate_symbol (objfile);
10177 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
10178 SYMBOL_SET_NAMES (sym, saved_package_name,
10179 strlen (saved_package_name), 0, objfile);
10180 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
10181 e.g., "main" finds the "main" module and not C's main(). */
10182 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10183 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
10184 SYMBOL_TYPE (sym) = type;
10186 add_symbol_to_list (sym, &global_symbols);
10188 xfree (package_name);
10192 /* Return the symtab for PER_CU. This works properly regardless of
10193 whether we're using the index or psymtabs. */
10195 static struct compunit_symtab *
10196 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10198 return (dwarf2_per_objfile->using_index
10199 ? per_cu->v.quick->compunit_symtab
10200 : per_cu->v.psymtab->compunit_symtab);
10203 /* A helper function for computing the list of all symbol tables
10204 included by PER_CU. */
10207 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
10208 htab_t all_children, htab_t all_type_symtabs,
10209 struct dwarf2_per_cu_data *per_cu,
10210 struct compunit_symtab *immediate_parent)
10214 struct compunit_symtab *cust;
10215 struct dwarf2_per_cu_data *iter;
10217 slot = htab_find_slot (all_children, per_cu, INSERT);
10220 /* This inclusion and its children have been processed. */
10225 /* Only add a CU if it has a symbol table. */
10226 cust = get_compunit_symtab (per_cu);
10229 /* If this is a type unit only add its symbol table if we haven't
10230 seen it yet (type unit per_cu's can share symtabs). */
10231 if (per_cu->is_debug_types)
10233 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10237 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10238 if (cust->user == NULL)
10239 cust->user = immediate_parent;
10244 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10245 if (cust->user == NULL)
10246 cust->user = immediate_parent;
10251 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10254 recursively_compute_inclusions (result, all_children,
10255 all_type_symtabs, iter, cust);
10259 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10263 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10265 gdb_assert (! per_cu->is_debug_types);
10267 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10270 struct dwarf2_per_cu_data *per_cu_iter;
10271 struct compunit_symtab *compunit_symtab_iter;
10272 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
10273 htab_t all_children, all_type_symtabs;
10274 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10276 /* If we don't have a symtab, we can just skip this case. */
10280 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10281 NULL, xcalloc, xfree);
10282 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10283 NULL, xcalloc, xfree);
10286 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10290 recursively_compute_inclusions (&result_symtabs, all_children,
10291 all_type_symtabs, per_cu_iter,
10295 /* Now we have a transitive closure of all the included symtabs. */
10296 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10298 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
10299 struct compunit_symtab *, len + 1);
10301 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10302 compunit_symtab_iter);
10304 cust->includes[ix] = compunit_symtab_iter;
10305 cust->includes[len] = NULL;
10307 VEC_free (compunit_symtab_ptr, result_symtabs);
10308 htab_delete (all_children);
10309 htab_delete (all_type_symtabs);
10313 /* Compute the 'includes' field for the symtabs of all the CUs we just
10317 process_cu_includes (void)
10320 struct dwarf2_per_cu_data *iter;
10323 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10327 if (! iter->is_debug_types)
10328 compute_compunit_symtab_includes (iter);
10331 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10334 /* Generate full symbol information for PER_CU, whose DIEs have
10335 already been loaded into memory. */
10338 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10339 enum language pretend_language)
10341 struct dwarf2_cu *cu = per_cu->cu;
10342 struct objfile *objfile = per_cu->objfile;
10343 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10344 CORE_ADDR lowpc, highpc;
10345 struct compunit_symtab *cust;
10346 struct cleanup *delayed_list_cleanup;
10347 CORE_ADDR baseaddr;
10348 struct block *static_block;
10351 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10354 scoped_free_pendings free_pending;
10355 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10357 cu->list_in_scope = &file_symbols;
10359 cu->language = pretend_language;
10360 cu->language_defn = language_def (cu->language);
10362 /* Do line number decoding in read_file_scope () */
10363 process_die (cu->dies, cu);
10365 /* For now fudge the Go package. */
10366 if (cu->language == language_go)
10367 fixup_go_packaging (cu);
10369 /* Now that we have processed all the DIEs in the CU, all the types
10370 should be complete, and it should now be safe to compute all of the
10372 compute_delayed_physnames (cu);
10373 do_cleanups (delayed_list_cleanup);
10375 /* Some compilers don't define a DW_AT_high_pc attribute for the
10376 compilation unit. If the DW_AT_high_pc is missing, synthesize
10377 it, by scanning the DIE's below the compilation unit. */
10378 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10380 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10381 static_block = end_symtab_get_static_block (addr, 0, 1);
10383 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10384 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10385 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10386 addrmap to help ensure it has an accurate map of pc values belonging to
10388 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10390 cust = end_symtab_from_static_block (static_block,
10391 SECT_OFF_TEXT (objfile), 0);
10395 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10397 /* Set symtab language to language from DW_AT_language. If the
10398 compilation is from a C file generated by language preprocessors, do
10399 not set the language if it was already deduced by start_subfile. */
10400 if (!(cu->language == language_c
10401 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10402 COMPUNIT_FILETABS (cust)->language = cu->language;
10404 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10405 produce DW_AT_location with location lists but it can be possibly
10406 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10407 there were bugs in prologue debug info, fixed later in GCC-4.5
10408 by "unwind info for epilogues" patch (which is not directly related).
10410 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10411 needed, it would be wrong due to missing DW_AT_producer there.
10413 Still one can confuse GDB by using non-standard GCC compilation
10414 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10416 if (cu->has_loclist && gcc_4_minor >= 5)
10417 cust->locations_valid = 1;
10419 if (gcc_4_minor >= 5)
10420 cust->epilogue_unwind_valid = 1;
10422 cust->call_site_htab = cu->call_site_htab;
10425 if (dwarf2_per_objfile->using_index)
10426 per_cu->v.quick->compunit_symtab = cust;
10429 struct partial_symtab *pst = per_cu->v.psymtab;
10430 pst->compunit_symtab = cust;
10434 /* Push it for inclusion processing later. */
10435 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
10438 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10439 already been loaded into memory. */
10442 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10443 enum language pretend_language)
10445 struct dwarf2_cu *cu = per_cu->cu;
10446 struct objfile *objfile = per_cu->objfile;
10447 struct compunit_symtab *cust;
10448 struct cleanup *delayed_list_cleanup;
10449 struct signatured_type *sig_type;
10451 gdb_assert (per_cu->is_debug_types);
10452 sig_type = (struct signatured_type *) per_cu;
10455 scoped_free_pendings free_pending;
10456 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10458 cu->list_in_scope = &file_symbols;
10460 cu->language = pretend_language;
10461 cu->language_defn = language_def (cu->language);
10463 /* The symbol tables are set up in read_type_unit_scope. */
10464 process_die (cu->dies, cu);
10466 /* For now fudge the Go package. */
10467 if (cu->language == language_go)
10468 fixup_go_packaging (cu);
10470 /* Now that we have processed all the DIEs in the CU, all the types
10471 should be complete, and it should now be safe to compute all of the
10473 compute_delayed_physnames (cu);
10474 do_cleanups (delayed_list_cleanup);
10476 /* TUs share symbol tables.
10477 If this is the first TU to use this symtab, complete the construction
10478 of it with end_expandable_symtab. Otherwise, complete the addition of
10479 this TU's symbols to the existing symtab. */
10480 if (sig_type->type_unit_group->compunit_symtab == NULL)
10482 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10483 sig_type->type_unit_group->compunit_symtab = cust;
10487 /* Set symtab language to language from DW_AT_language. If the
10488 compilation is from a C file generated by language preprocessors,
10489 do not set the language if it was already deduced by
10491 if (!(cu->language == language_c
10492 && COMPUNIT_FILETABS (cust)->language != language_c))
10493 COMPUNIT_FILETABS (cust)->language = cu->language;
10498 augment_type_symtab ();
10499 cust = sig_type->type_unit_group->compunit_symtab;
10502 if (dwarf2_per_objfile->using_index)
10503 per_cu->v.quick->compunit_symtab = cust;
10506 struct partial_symtab *pst = per_cu->v.psymtab;
10507 pst->compunit_symtab = cust;
10512 /* Process an imported unit DIE. */
10515 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10517 struct attribute *attr;
10519 /* For now we don't handle imported units in type units. */
10520 if (cu->per_cu->is_debug_types)
10522 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10523 " supported in type units [in module %s]"),
10524 objfile_name (cu->objfile));
10527 attr = dwarf2_attr (die, DW_AT_import, cu);
10530 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10531 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10532 dwarf2_per_cu_data *per_cu
10533 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
10535 /* If necessary, add it to the queue and load its DIEs. */
10536 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10537 load_full_comp_unit (per_cu, cu->language);
10539 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10544 /* RAII object that represents a process_die scope: i.e.,
10545 starts/finishes processing a DIE. */
10546 class process_die_scope
10549 process_die_scope (die_info *die, dwarf2_cu *cu)
10550 : m_die (die), m_cu (cu)
10552 /* We should only be processing DIEs not already in process. */
10553 gdb_assert (!m_die->in_process);
10554 m_die->in_process = true;
10557 ~process_die_scope ()
10559 m_die->in_process = false;
10561 /* If we're done processing the DIE for the CU that owns the line
10562 header, we don't need the line header anymore. */
10563 if (m_cu->line_header_die_owner == m_die)
10565 delete m_cu->line_header;
10566 m_cu->line_header = NULL;
10567 m_cu->line_header_die_owner = NULL;
10576 /* Process a die and its children. */
10579 process_die (struct die_info *die, struct dwarf2_cu *cu)
10581 process_die_scope scope (die, cu);
10585 case DW_TAG_padding:
10587 case DW_TAG_compile_unit:
10588 case DW_TAG_partial_unit:
10589 read_file_scope (die, cu);
10591 case DW_TAG_type_unit:
10592 read_type_unit_scope (die, cu);
10594 case DW_TAG_subprogram:
10595 case DW_TAG_inlined_subroutine:
10596 read_func_scope (die, cu);
10598 case DW_TAG_lexical_block:
10599 case DW_TAG_try_block:
10600 case DW_TAG_catch_block:
10601 read_lexical_block_scope (die, cu);
10603 case DW_TAG_call_site:
10604 case DW_TAG_GNU_call_site:
10605 read_call_site_scope (die, cu);
10607 case DW_TAG_class_type:
10608 case DW_TAG_interface_type:
10609 case DW_TAG_structure_type:
10610 case DW_TAG_union_type:
10611 process_structure_scope (die, cu);
10613 case DW_TAG_enumeration_type:
10614 process_enumeration_scope (die, cu);
10617 /* These dies have a type, but processing them does not create
10618 a symbol or recurse to process the children. Therefore we can
10619 read them on-demand through read_type_die. */
10620 case DW_TAG_subroutine_type:
10621 case DW_TAG_set_type:
10622 case DW_TAG_array_type:
10623 case DW_TAG_pointer_type:
10624 case DW_TAG_ptr_to_member_type:
10625 case DW_TAG_reference_type:
10626 case DW_TAG_rvalue_reference_type:
10627 case DW_TAG_string_type:
10630 case DW_TAG_base_type:
10631 case DW_TAG_subrange_type:
10632 case DW_TAG_typedef:
10633 /* Add a typedef symbol for the type definition, if it has a
10635 new_symbol (die, read_type_die (die, cu), cu);
10637 case DW_TAG_common_block:
10638 read_common_block (die, cu);
10640 case DW_TAG_common_inclusion:
10642 case DW_TAG_namespace:
10643 cu->processing_has_namespace_info = 1;
10644 read_namespace (die, cu);
10646 case DW_TAG_module:
10647 cu->processing_has_namespace_info = 1;
10648 read_module (die, cu);
10650 case DW_TAG_imported_declaration:
10651 cu->processing_has_namespace_info = 1;
10652 if (read_namespace_alias (die, cu))
10654 /* The declaration is not a global namespace alias: fall through. */
10655 case DW_TAG_imported_module:
10656 cu->processing_has_namespace_info = 1;
10657 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10658 || cu->language != language_fortran))
10659 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10660 dwarf_tag_name (die->tag));
10661 read_import_statement (die, cu);
10664 case DW_TAG_imported_unit:
10665 process_imported_unit_die (die, cu);
10668 case DW_TAG_variable:
10669 read_variable (die, cu);
10673 new_symbol (die, NULL, cu);
10678 /* DWARF name computation. */
10680 /* A helper function for dwarf2_compute_name which determines whether DIE
10681 needs to have the name of the scope prepended to the name listed in the
10685 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10687 struct attribute *attr;
10691 case DW_TAG_namespace:
10692 case DW_TAG_typedef:
10693 case DW_TAG_class_type:
10694 case DW_TAG_interface_type:
10695 case DW_TAG_structure_type:
10696 case DW_TAG_union_type:
10697 case DW_TAG_enumeration_type:
10698 case DW_TAG_enumerator:
10699 case DW_TAG_subprogram:
10700 case DW_TAG_inlined_subroutine:
10701 case DW_TAG_member:
10702 case DW_TAG_imported_declaration:
10705 case DW_TAG_variable:
10706 case DW_TAG_constant:
10707 /* We only need to prefix "globally" visible variables. These include
10708 any variable marked with DW_AT_external or any variable that
10709 lives in a namespace. [Variables in anonymous namespaces
10710 require prefixing, but they are not DW_AT_external.] */
10712 if (dwarf2_attr (die, DW_AT_specification, cu))
10714 struct dwarf2_cu *spec_cu = cu;
10716 return die_needs_namespace (die_specification (die, &spec_cu),
10720 attr = dwarf2_attr (die, DW_AT_external, cu);
10721 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10722 && die->parent->tag != DW_TAG_module)
10724 /* A variable in a lexical block of some kind does not need a
10725 namespace, even though in C++ such variables may be external
10726 and have a mangled name. */
10727 if (die->parent->tag == DW_TAG_lexical_block
10728 || die->parent->tag == DW_TAG_try_block
10729 || die->parent->tag == DW_TAG_catch_block
10730 || die->parent->tag == DW_TAG_subprogram)
10739 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10740 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10741 defined for the given DIE. */
10743 static struct attribute *
10744 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10746 struct attribute *attr;
10748 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10750 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10755 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10756 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10757 defined for the given DIE. */
10759 static const char *
10760 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10762 const char *linkage_name;
10764 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10765 if (linkage_name == NULL)
10766 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10768 return linkage_name;
10771 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10772 compute the physname for the object, which include a method's:
10773 - formal parameters (C++),
10774 - receiver type (Go),
10776 The term "physname" is a bit confusing.
10777 For C++, for example, it is the demangled name.
10778 For Go, for example, it's the mangled name.
10780 For Ada, return the DIE's linkage name rather than the fully qualified
10781 name. PHYSNAME is ignored..
10783 The result is allocated on the objfile_obstack and canonicalized. */
10785 static const char *
10786 dwarf2_compute_name (const char *name,
10787 struct die_info *die, struct dwarf2_cu *cu,
10790 struct objfile *objfile = cu->objfile;
10793 name = dwarf2_name (die, cu);
10795 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10796 but otherwise compute it by typename_concat inside GDB.
10797 FIXME: Actually this is not really true, or at least not always true.
10798 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10799 Fortran names because there is no mangling standard. So new_symbol_full
10800 will set the demangled name to the result of dwarf2_full_name, and it is
10801 the demangled name that GDB uses if it exists. */
10802 if (cu->language == language_ada
10803 || (cu->language == language_fortran && physname))
10805 /* For Ada unit, we prefer the linkage name over the name, as
10806 the former contains the exported name, which the user expects
10807 to be able to reference. Ideally, we want the user to be able
10808 to reference this entity using either natural or linkage name,
10809 but we haven't started looking at this enhancement yet. */
10810 const char *linkage_name = dw2_linkage_name (die, cu);
10812 if (linkage_name != NULL)
10813 return linkage_name;
10816 /* These are the only languages we know how to qualify names in. */
10818 && (cu->language == language_cplus
10819 || cu->language == language_fortran || cu->language == language_d
10820 || cu->language == language_rust))
10822 if (die_needs_namespace (die, cu))
10824 const char *prefix;
10825 const char *canonical_name = NULL;
10829 prefix = determine_prefix (die, cu);
10830 if (*prefix != '\0')
10832 char *prefixed_name = typename_concat (NULL, prefix, name,
10835 buf.puts (prefixed_name);
10836 xfree (prefixed_name);
10841 /* Template parameters may be specified in the DIE's DW_AT_name, or
10842 as children with DW_TAG_template_type_param or
10843 DW_TAG_value_type_param. If the latter, add them to the name
10844 here. If the name already has template parameters, then
10845 skip this step; some versions of GCC emit both, and
10846 it is more efficient to use the pre-computed name.
10848 Something to keep in mind about this process: it is very
10849 unlikely, or in some cases downright impossible, to produce
10850 something that will match the mangled name of a function.
10851 If the definition of the function has the same debug info,
10852 we should be able to match up with it anyway. But fallbacks
10853 using the minimal symbol, for instance to find a method
10854 implemented in a stripped copy of libstdc++, will not work.
10855 If we do not have debug info for the definition, we will have to
10856 match them up some other way.
10858 When we do name matching there is a related problem with function
10859 templates; two instantiated function templates are allowed to
10860 differ only by their return types, which we do not add here. */
10862 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10864 struct attribute *attr;
10865 struct die_info *child;
10868 die->building_fullname = 1;
10870 for (child = die->child; child != NULL; child = child->sibling)
10874 const gdb_byte *bytes;
10875 struct dwarf2_locexpr_baton *baton;
10878 if (child->tag != DW_TAG_template_type_param
10879 && child->tag != DW_TAG_template_value_param)
10890 attr = dwarf2_attr (child, DW_AT_type, cu);
10893 complaint (&symfile_complaints,
10894 _("template parameter missing DW_AT_type"));
10895 buf.puts ("UNKNOWN_TYPE");
10898 type = die_type (child, cu);
10900 if (child->tag == DW_TAG_template_type_param)
10902 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
10906 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10909 complaint (&symfile_complaints,
10910 _("template parameter missing "
10911 "DW_AT_const_value"));
10912 buf.puts ("UNKNOWN_VALUE");
10916 dwarf2_const_value_attr (attr, type, name,
10917 &cu->comp_unit_obstack, cu,
10918 &value, &bytes, &baton);
10920 if (TYPE_NOSIGN (type))
10921 /* GDB prints characters as NUMBER 'CHAR'. If that's
10922 changed, this can use value_print instead. */
10923 c_printchar (value, type, &buf);
10926 struct value_print_options opts;
10929 v = dwarf2_evaluate_loc_desc (type, NULL,
10933 else if (bytes != NULL)
10935 v = allocate_value (type);
10936 memcpy (value_contents_writeable (v), bytes,
10937 TYPE_LENGTH (type));
10940 v = value_from_longest (type, value);
10942 /* Specify decimal so that we do not depend on
10944 get_formatted_print_options (&opts, 'd');
10946 value_print (v, &buf, &opts);
10952 die->building_fullname = 0;
10956 /* Close the argument list, with a space if necessary
10957 (nested templates). */
10958 if (!buf.empty () && buf.string ().back () == '>')
10965 /* For C++ methods, append formal parameter type
10966 information, if PHYSNAME. */
10968 if (physname && die->tag == DW_TAG_subprogram
10969 && cu->language == language_cplus)
10971 struct type *type = read_type_die (die, cu);
10973 c_type_print_args (type, &buf, 1, cu->language,
10974 &type_print_raw_options);
10976 if (cu->language == language_cplus)
10978 /* Assume that an artificial first parameter is
10979 "this", but do not crash if it is not. RealView
10980 marks unnamed (and thus unused) parameters as
10981 artificial; there is no way to differentiate
10983 if (TYPE_NFIELDS (type) > 0
10984 && TYPE_FIELD_ARTIFICIAL (type, 0)
10985 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10986 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10988 buf.puts (" const");
10992 const std::string &intermediate_name = buf.string ();
10994 if (cu->language == language_cplus)
10996 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10997 &objfile->per_bfd->storage_obstack);
10999 /* If we only computed INTERMEDIATE_NAME, or if
11000 INTERMEDIATE_NAME is already canonical, then we need to
11001 copy it to the appropriate obstack. */
11002 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11003 name = ((const char *)
11004 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11005 intermediate_name.c_str (),
11006 intermediate_name.length ()));
11008 name = canonical_name;
11015 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11016 If scope qualifiers are appropriate they will be added. The result
11017 will be allocated on the storage_obstack, or NULL if the DIE does
11018 not have a name. NAME may either be from a previous call to
11019 dwarf2_name or NULL.
11021 The output string will be canonicalized (if C++). */
11023 static const char *
11024 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11026 return dwarf2_compute_name (name, die, cu, 0);
11029 /* Construct a physname for the given DIE in CU. NAME may either be
11030 from a previous call to dwarf2_name or NULL. The result will be
11031 allocated on the objfile_objstack or NULL if the DIE does not have a
11034 The output string will be canonicalized (if C++). */
11036 static const char *
11037 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11039 struct objfile *objfile = cu->objfile;
11040 const char *retval, *mangled = NULL, *canon = NULL;
11043 /* In this case dwarf2_compute_name is just a shortcut not building anything
11045 if (!die_needs_namespace (die, cu))
11046 return dwarf2_compute_name (name, die, cu, 1);
11048 mangled = dw2_linkage_name (die, cu);
11050 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11051 See https://github.com/rust-lang/rust/issues/32925. */
11052 if (cu->language == language_rust && mangled != NULL
11053 && strchr (mangled, '{') != NULL)
11056 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11058 gdb::unique_xmalloc_ptr<char> demangled;
11059 if (mangled != NULL)
11061 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
11062 type. It is easier for GDB users to search for such functions as
11063 `name(params)' than `long name(params)'. In such case the minimal
11064 symbol names do not match the full symbol names but for template
11065 functions there is never a need to look up their definition from their
11066 declaration so the only disadvantage remains the minimal symbol
11067 variant `long name(params)' does not have the proper inferior type.
11070 if (cu->language == language_go)
11072 /* This is a lie, but we already lie to the caller new_symbol_full.
11073 new_symbol_full assumes we return the mangled name.
11074 This just undoes that lie until things are cleaned up. */
11078 demangled.reset (gdb_demangle (mangled,
11079 (DMGL_PARAMS | DMGL_ANSI
11080 | DMGL_RET_DROP)));
11083 canon = demangled.get ();
11091 if (canon == NULL || check_physname)
11093 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11095 if (canon != NULL && strcmp (physname, canon) != 0)
11097 /* It may not mean a bug in GDB. The compiler could also
11098 compute DW_AT_linkage_name incorrectly. But in such case
11099 GDB would need to be bug-to-bug compatible. */
11101 complaint (&symfile_complaints,
11102 _("Computed physname <%s> does not match demangled <%s> "
11103 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
11104 physname, canon, mangled, to_underlying (die->sect_off),
11105 objfile_name (objfile));
11107 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11108 is available here - over computed PHYSNAME. It is safer
11109 against both buggy GDB and buggy compilers. */
11123 retval = ((const char *)
11124 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11125 retval, strlen (retval)));
11130 /* Inspect DIE in CU for a namespace alias. If one exists, record
11131 a new symbol for it.
11133 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11136 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11138 struct attribute *attr;
11140 /* If the die does not have a name, this is not a namespace
11142 attr = dwarf2_attr (die, DW_AT_name, cu);
11146 struct die_info *d = die;
11147 struct dwarf2_cu *imported_cu = cu;
11149 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11150 keep inspecting DIEs until we hit the underlying import. */
11151 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11152 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11154 attr = dwarf2_attr (d, DW_AT_import, cu);
11158 d = follow_die_ref (d, attr, &imported_cu);
11159 if (d->tag != DW_TAG_imported_declaration)
11163 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11165 complaint (&symfile_complaints,
11166 _("DIE at 0x%x has too many recursively imported "
11167 "declarations"), to_underlying (d->sect_off));
11174 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11176 type = get_die_type_at_offset (sect_off, cu->per_cu);
11177 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11179 /* This declaration is a global namespace alias. Add
11180 a symbol for it whose type is the aliased namespace. */
11181 new_symbol (die, type, cu);
11190 /* Return the using directives repository (global or local?) to use in the
11191 current context for LANGUAGE.
11193 For Ada, imported declarations can materialize renamings, which *may* be
11194 global. However it is impossible (for now?) in DWARF to distinguish
11195 "external" imported declarations and "static" ones. As all imported
11196 declarations seem to be static in all other languages, make them all CU-wide
11197 global only in Ada. */
11199 static struct using_direct **
11200 using_directives (enum language language)
11202 if (language == language_ada && context_stack_depth == 0)
11203 return &global_using_directives;
11205 return &local_using_directives;
11208 /* Read the import statement specified by the given die and record it. */
11211 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11213 struct objfile *objfile = cu->objfile;
11214 struct attribute *import_attr;
11215 struct die_info *imported_die, *child_die;
11216 struct dwarf2_cu *imported_cu;
11217 const char *imported_name;
11218 const char *imported_name_prefix;
11219 const char *canonical_name;
11220 const char *import_alias;
11221 const char *imported_declaration = NULL;
11222 const char *import_prefix;
11223 std::vector<const char *> excludes;
11225 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11226 if (import_attr == NULL)
11228 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11229 dwarf_tag_name (die->tag));
11234 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11235 imported_name = dwarf2_name (imported_die, imported_cu);
11236 if (imported_name == NULL)
11238 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11240 The import in the following code:
11254 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11255 <52> DW_AT_decl_file : 1
11256 <53> DW_AT_decl_line : 6
11257 <54> DW_AT_import : <0x75>
11258 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11259 <59> DW_AT_name : B
11260 <5b> DW_AT_decl_file : 1
11261 <5c> DW_AT_decl_line : 2
11262 <5d> DW_AT_type : <0x6e>
11264 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11265 <76> DW_AT_byte_size : 4
11266 <77> DW_AT_encoding : 5 (signed)
11268 imports the wrong die ( 0x75 instead of 0x58 ).
11269 This case will be ignored until the gcc bug is fixed. */
11273 /* Figure out the local name after import. */
11274 import_alias = dwarf2_name (die, cu);
11276 /* Figure out where the statement is being imported to. */
11277 import_prefix = determine_prefix (die, cu);
11279 /* Figure out what the scope of the imported die is and prepend it
11280 to the name of the imported die. */
11281 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11283 if (imported_die->tag != DW_TAG_namespace
11284 && imported_die->tag != DW_TAG_module)
11286 imported_declaration = imported_name;
11287 canonical_name = imported_name_prefix;
11289 else if (strlen (imported_name_prefix) > 0)
11290 canonical_name = obconcat (&objfile->objfile_obstack,
11291 imported_name_prefix,
11292 (cu->language == language_d ? "." : "::"),
11293 imported_name, (char *) NULL);
11295 canonical_name = imported_name;
11297 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11298 for (child_die = die->child; child_die && child_die->tag;
11299 child_die = sibling_die (child_die))
11301 /* DWARF-4: A Fortran use statement with a “rename list” may be
11302 represented by an imported module entry with an import attribute
11303 referring to the module and owned entries corresponding to those
11304 entities that are renamed as part of being imported. */
11306 if (child_die->tag != DW_TAG_imported_declaration)
11308 complaint (&symfile_complaints,
11309 _("child DW_TAG_imported_declaration expected "
11310 "- DIE at 0x%x [in module %s]"),
11311 to_underlying (child_die->sect_off), objfile_name (objfile));
11315 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11316 if (import_attr == NULL)
11318 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11319 dwarf_tag_name (child_die->tag));
11324 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11326 imported_name = dwarf2_name (imported_die, imported_cu);
11327 if (imported_name == NULL)
11329 complaint (&symfile_complaints,
11330 _("child DW_TAG_imported_declaration has unknown "
11331 "imported name - DIE at 0x%x [in module %s]"),
11332 to_underlying (child_die->sect_off), objfile_name (objfile));
11336 excludes.push_back (imported_name);
11338 process_die (child_die, cu);
11341 add_using_directive (using_directives (cu->language),
11345 imported_declaration,
11348 &objfile->objfile_obstack);
11351 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11352 types, but gives them a size of zero. Starting with version 14,
11353 ICC is compatible with GCC. */
11356 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11358 if (!cu->checked_producer)
11359 check_producer (cu);
11361 return cu->producer_is_icc_lt_14;
11364 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11365 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11366 this, it was first present in GCC release 4.3.0. */
11369 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11371 if (!cu->checked_producer)
11372 check_producer (cu);
11374 return cu->producer_is_gcc_lt_4_3;
11377 static file_and_directory
11378 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11380 file_and_directory res;
11382 /* Find the filename. Do not use dwarf2_name here, since the filename
11383 is not a source language identifier. */
11384 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11385 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11387 if (res.comp_dir == NULL
11388 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11389 && IS_ABSOLUTE_PATH (res.name))
11391 res.comp_dir_storage = ldirname (res.name);
11392 if (!res.comp_dir_storage.empty ())
11393 res.comp_dir = res.comp_dir_storage.c_str ();
11395 if (res.comp_dir != NULL)
11397 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11398 directory, get rid of it. */
11399 const char *cp = strchr (res.comp_dir, ':');
11401 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11402 res.comp_dir = cp + 1;
11405 if (res.name == NULL)
11406 res.name = "<unknown>";
11411 /* Handle DW_AT_stmt_list for a compilation unit.
11412 DIE is the DW_TAG_compile_unit die for CU.
11413 COMP_DIR is the compilation directory. LOWPC is passed to
11414 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11417 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11418 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11420 struct objfile *objfile = dwarf2_per_objfile->objfile;
11421 struct attribute *attr;
11422 struct line_header line_header_local;
11423 hashval_t line_header_local_hash;
11425 int decode_mapping;
11427 gdb_assert (! cu->per_cu->is_debug_types);
11429 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11433 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11435 /* The line header hash table is only created if needed (it exists to
11436 prevent redundant reading of the line table for partial_units).
11437 If we're given a partial_unit, we'll need it. If we're given a
11438 compile_unit, then use the line header hash table if it's already
11439 created, but don't create one just yet. */
11441 if (dwarf2_per_objfile->line_header_hash == NULL
11442 && die->tag == DW_TAG_partial_unit)
11444 dwarf2_per_objfile->line_header_hash
11445 = htab_create_alloc_ex (127, line_header_hash_voidp,
11446 line_header_eq_voidp,
11447 free_line_header_voidp,
11448 &objfile->objfile_obstack,
11449 hashtab_obstack_allocate,
11450 dummy_obstack_deallocate);
11453 line_header_local.sect_off = line_offset;
11454 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11455 line_header_local_hash = line_header_hash (&line_header_local);
11456 if (dwarf2_per_objfile->line_header_hash != NULL)
11458 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11459 &line_header_local,
11460 line_header_local_hash, NO_INSERT);
11462 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11463 is not present in *SLOT (since if there is something in *SLOT then
11464 it will be for a partial_unit). */
11465 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11467 gdb_assert (*slot != NULL);
11468 cu->line_header = (struct line_header *) *slot;
11473 /* dwarf_decode_line_header does not yet provide sufficient information.
11474 We always have to call also dwarf_decode_lines for it. */
11475 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11479 cu->line_header = lh.release ();
11480 cu->line_header_die_owner = die;
11482 if (dwarf2_per_objfile->line_header_hash == NULL)
11486 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11487 &line_header_local,
11488 line_header_local_hash, INSERT);
11489 gdb_assert (slot != NULL);
11491 if (slot != NULL && *slot == NULL)
11493 /* This newly decoded line number information unit will be owned
11494 by line_header_hash hash table. */
11495 *slot = cu->line_header;
11496 cu->line_header_die_owner = NULL;
11500 /* We cannot free any current entry in (*slot) as that struct line_header
11501 may be already used by multiple CUs. Create only temporary decoded
11502 line_header for this CU - it may happen at most once for each line
11503 number information unit. And if we're not using line_header_hash
11504 then this is what we want as well. */
11505 gdb_assert (die->tag != DW_TAG_partial_unit);
11507 decode_mapping = (die->tag != DW_TAG_partial_unit);
11508 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11513 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11516 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11518 struct objfile *objfile = dwarf2_per_objfile->objfile;
11519 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11520 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11521 CORE_ADDR highpc = ((CORE_ADDR) 0);
11522 struct attribute *attr;
11523 struct die_info *child_die;
11524 CORE_ADDR baseaddr;
11526 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11528 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11530 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11531 from finish_block. */
11532 if (lowpc == ((CORE_ADDR) -1))
11534 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11536 file_and_directory fnd = find_file_and_directory (die, cu);
11538 prepare_one_comp_unit (cu, die, cu->language);
11540 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11541 standardised yet. As a workaround for the language detection we fall
11542 back to the DW_AT_producer string. */
11543 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11544 cu->language = language_opencl;
11546 /* Similar hack for Go. */
11547 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11548 set_cu_language (DW_LANG_Go, cu);
11550 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
11552 /* Decode line number information if present. We do this before
11553 processing child DIEs, so that the line header table is available
11554 for DW_AT_decl_file. */
11555 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11557 /* Process all dies in compilation unit. */
11558 if (die->child != NULL)
11560 child_die = die->child;
11561 while (child_die && child_die->tag)
11563 process_die (child_die, cu);
11564 child_die = sibling_die (child_die);
11568 /* Decode macro information, if present. Dwarf 2 macro information
11569 refers to information in the line number info statement program
11570 header, so we can only read it if we've read the header
11572 attr = dwarf2_attr (die, DW_AT_macros, cu);
11574 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11575 if (attr && cu->line_header)
11577 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11578 complaint (&symfile_complaints,
11579 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11581 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11585 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11586 if (attr && cu->line_header)
11588 unsigned int macro_offset = DW_UNSND (attr);
11590 dwarf_decode_macros (cu, macro_offset, 0);
11595 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11596 Create the set of symtabs used by this TU, or if this TU is sharing
11597 symtabs with another TU and the symtabs have already been created
11598 then restore those symtabs in the line header.
11599 We don't need the pc/line-number mapping for type units. */
11602 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
11604 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11605 struct type_unit_group *tu_group;
11607 struct attribute *attr;
11609 struct signatured_type *sig_type;
11611 gdb_assert (per_cu->is_debug_types);
11612 sig_type = (struct signatured_type *) per_cu;
11614 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11616 /* If we're using .gdb_index (includes -readnow) then
11617 per_cu->type_unit_group may not have been set up yet. */
11618 if (sig_type->type_unit_group == NULL)
11619 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11620 tu_group = sig_type->type_unit_group;
11622 /* If we've already processed this stmt_list there's no real need to
11623 do it again, we could fake it and just recreate the part we need
11624 (file name,index -> symtab mapping). If data shows this optimization
11625 is useful we can do it then. */
11626 first_time = tu_group->compunit_symtab == NULL;
11628 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11633 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11634 lh = dwarf_decode_line_header (line_offset, cu);
11639 dwarf2_start_symtab (cu, "", NULL, 0);
11642 gdb_assert (tu_group->symtabs == NULL);
11643 restart_symtab (tu_group->compunit_symtab, "", 0);
11648 cu->line_header = lh.release ();
11649 cu->line_header_die_owner = die;
11653 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
11655 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11656 still initializing it, and our caller (a few levels up)
11657 process_full_type_unit still needs to know if this is the first
11660 tu_group->num_symtabs = cu->line_header->file_names.size ();
11661 tu_group->symtabs = XNEWVEC (struct symtab *,
11662 cu->line_header->file_names.size ());
11664 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11666 file_entry &fe = cu->line_header->file_names[i];
11668 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
11670 if (current_subfile->symtab == NULL)
11672 /* NOTE: start_subfile will recognize when it's been
11673 passed a file it has already seen. So we can't
11674 assume there's a simple mapping from
11675 cu->line_header->file_names to subfiles, plus
11676 cu->line_header->file_names may contain dups. */
11677 current_subfile->symtab
11678 = allocate_symtab (cust, current_subfile->name);
11681 fe.symtab = current_subfile->symtab;
11682 tu_group->symtabs[i] = fe.symtab;
11687 restart_symtab (tu_group->compunit_symtab, "", 0);
11689 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11691 file_entry &fe = cu->line_header->file_names[i];
11693 fe.symtab = tu_group->symtabs[i];
11697 /* The main symtab is allocated last. Type units don't have DW_AT_name
11698 so they don't have a "real" (so to speak) symtab anyway.
11699 There is later code that will assign the main symtab to all symbols
11700 that don't have one. We need to handle the case of a symbol with a
11701 missing symtab (DW_AT_decl_file) anyway. */
11704 /* Process DW_TAG_type_unit.
11705 For TUs we want to skip the first top level sibling if it's not the
11706 actual type being defined by this TU. In this case the first top
11707 level sibling is there to provide context only. */
11710 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11712 struct die_info *child_die;
11714 prepare_one_comp_unit (cu, die, language_minimal);
11716 /* Initialize (or reinitialize) the machinery for building symtabs.
11717 We do this before processing child DIEs, so that the line header table
11718 is available for DW_AT_decl_file. */
11719 setup_type_unit_groups (die, cu);
11721 if (die->child != NULL)
11723 child_die = die->child;
11724 while (child_die && child_die->tag)
11726 process_die (child_die, cu);
11727 child_die = sibling_die (child_die);
11734 http://gcc.gnu.org/wiki/DebugFission
11735 http://gcc.gnu.org/wiki/DebugFissionDWP
11737 To simplify handling of both DWO files ("object" files with the DWARF info)
11738 and DWP files (a file with the DWOs packaged up into one file), we treat
11739 DWP files as having a collection of virtual DWO files. */
11742 hash_dwo_file (const void *item)
11744 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11747 hash = htab_hash_string (dwo_file->dwo_name);
11748 if (dwo_file->comp_dir != NULL)
11749 hash += htab_hash_string (dwo_file->comp_dir);
11754 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11756 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11757 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11759 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11761 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11762 return lhs->comp_dir == rhs->comp_dir;
11763 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11766 /* Allocate a hash table for DWO files. */
11769 allocate_dwo_file_hash_table (void)
11771 struct objfile *objfile = dwarf2_per_objfile->objfile;
11773 return htab_create_alloc_ex (41,
11777 &objfile->objfile_obstack,
11778 hashtab_obstack_allocate,
11779 dummy_obstack_deallocate);
11782 /* Lookup DWO file DWO_NAME. */
11785 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
11787 struct dwo_file find_entry;
11790 if (dwarf2_per_objfile->dwo_files == NULL)
11791 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
11793 memset (&find_entry, 0, sizeof (find_entry));
11794 find_entry.dwo_name = dwo_name;
11795 find_entry.comp_dir = comp_dir;
11796 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11802 hash_dwo_unit (const void *item)
11804 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11806 /* This drops the top 32 bits of the id, but is ok for a hash. */
11807 return dwo_unit->signature;
11811 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11813 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11814 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11816 /* The signature is assumed to be unique within the DWO file.
11817 So while object file CU dwo_id's always have the value zero,
11818 that's OK, assuming each object file DWO file has only one CU,
11819 and that's the rule for now. */
11820 return lhs->signature == rhs->signature;
11823 /* Allocate a hash table for DWO CUs,TUs.
11824 There is one of these tables for each of CUs,TUs for each DWO file. */
11827 allocate_dwo_unit_table (struct objfile *objfile)
11829 /* Start out with a pretty small number.
11830 Generally DWO files contain only one CU and maybe some TUs. */
11831 return htab_create_alloc_ex (3,
11835 &objfile->objfile_obstack,
11836 hashtab_obstack_allocate,
11837 dummy_obstack_deallocate);
11840 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11842 struct create_dwo_cu_data
11844 struct dwo_file *dwo_file;
11845 struct dwo_unit dwo_unit;
11848 /* die_reader_func for create_dwo_cu. */
11851 create_dwo_cu_reader (const struct die_reader_specs *reader,
11852 const gdb_byte *info_ptr,
11853 struct die_info *comp_unit_die,
11857 struct dwarf2_cu *cu = reader->cu;
11858 sect_offset sect_off = cu->per_cu->sect_off;
11859 struct dwarf2_section_info *section = cu->per_cu->section;
11860 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11861 struct dwo_file *dwo_file = data->dwo_file;
11862 struct dwo_unit *dwo_unit = &data->dwo_unit;
11863 struct attribute *attr;
11865 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11868 complaint (&symfile_complaints,
11869 _("Dwarf Error: debug entry at offset 0x%x is missing"
11870 " its dwo_id [in module %s]"),
11871 to_underlying (sect_off), dwo_file->dwo_name);
11875 dwo_unit->dwo_file = dwo_file;
11876 dwo_unit->signature = DW_UNSND (attr);
11877 dwo_unit->section = section;
11878 dwo_unit->sect_off = sect_off;
11879 dwo_unit->length = cu->per_cu->length;
11881 if (dwarf_read_debug)
11882 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
11883 to_underlying (sect_off),
11884 hex_string (dwo_unit->signature));
11887 /* Create the dwo_units for the CUs in a DWO_FILE.
11888 Note: This function processes DWO files only, not DWP files. */
11891 create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info §ion,
11894 struct objfile *objfile = dwarf2_per_objfile->objfile;
11895 const gdb_byte *info_ptr, *end_ptr;
11897 dwarf2_read_section (objfile, §ion);
11898 info_ptr = section.buffer;
11900 if (info_ptr == NULL)
11903 if (dwarf_read_debug)
11905 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11906 get_section_name (§ion),
11907 get_section_file_name (§ion));
11910 end_ptr = info_ptr + section.size;
11911 while (info_ptr < end_ptr)
11913 struct dwarf2_per_cu_data per_cu;
11914 struct create_dwo_cu_data create_dwo_cu_data;
11915 struct dwo_unit *dwo_unit;
11917 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11919 memset (&create_dwo_cu_data.dwo_unit, 0,
11920 sizeof (create_dwo_cu_data.dwo_unit));
11921 memset (&per_cu, 0, sizeof (per_cu));
11922 per_cu.objfile = objfile;
11923 per_cu.is_debug_types = 0;
11924 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11925 per_cu.section = §ion;
11926 create_dwo_cu_data.dwo_file = &dwo_file;
11928 init_cutu_and_read_dies_no_follow (
11929 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11930 info_ptr += per_cu.length;
11932 // If the unit could not be parsed, skip it.
11933 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11936 if (cus_htab == NULL)
11937 cus_htab = allocate_dwo_unit_table (objfile);
11939 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11940 *dwo_unit = create_dwo_cu_data.dwo_unit;
11941 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11942 gdb_assert (slot != NULL);
11945 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11946 sect_offset dup_sect_off = dup_cu->sect_off;
11948 complaint (&symfile_complaints,
11949 _("debug cu entry at offset 0x%x is duplicate to"
11950 " the entry at offset 0x%x, signature %s"),
11951 to_underlying (sect_off), to_underlying (dup_sect_off),
11952 hex_string (dwo_unit->signature));
11954 *slot = (void *)dwo_unit;
11958 /* DWP file .debug_{cu,tu}_index section format:
11959 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11963 Both index sections have the same format, and serve to map a 64-bit
11964 signature to a set of section numbers. Each section begins with a header,
11965 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11966 indexes, and a pool of 32-bit section numbers. The index sections will be
11967 aligned at 8-byte boundaries in the file.
11969 The index section header consists of:
11971 V, 32 bit version number
11973 N, 32 bit number of compilation units or type units in the index
11974 M, 32 bit number of slots in the hash table
11976 Numbers are recorded using the byte order of the application binary.
11978 The hash table begins at offset 16 in the section, and consists of an array
11979 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11980 order of the application binary). Unused slots in the hash table are 0.
11981 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11983 The parallel table begins immediately after the hash table
11984 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11985 array of 32-bit indexes (using the byte order of the application binary),
11986 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11987 table contains a 32-bit index into the pool of section numbers. For unused
11988 hash table slots, the corresponding entry in the parallel table will be 0.
11990 The pool of section numbers begins immediately following the hash table
11991 (at offset 16 + 12 * M from the beginning of the section). The pool of
11992 section numbers consists of an array of 32-bit words (using the byte order
11993 of the application binary). Each item in the array is indexed starting
11994 from 0. The hash table entry provides the index of the first section
11995 number in the set. Additional section numbers in the set follow, and the
11996 set is terminated by a 0 entry (section number 0 is not used in ELF).
11998 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11999 section must be the first entry in the set, and the .debug_abbrev.dwo must
12000 be the second entry. Other members of the set may follow in any order.
12006 DWP Version 2 combines all the .debug_info, etc. sections into one,
12007 and the entries in the index tables are now offsets into these sections.
12008 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12011 Index Section Contents:
12013 Hash Table of Signatures dwp_hash_table.hash_table
12014 Parallel Table of Indices dwp_hash_table.unit_table
12015 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12016 Table of Section Sizes dwp_hash_table.v2.sizes
12018 The index section header consists of:
12020 V, 32 bit version number
12021 L, 32 bit number of columns in the table of section offsets
12022 N, 32 bit number of compilation units or type units in the index
12023 M, 32 bit number of slots in the hash table
12025 Numbers are recorded using the byte order of the application binary.
12027 The hash table has the same format as version 1.
12028 The parallel table of indices has the same format as version 1,
12029 except that the entries are origin-1 indices into the table of sections
12030 offsets and the table of section sizes.
12032 The table of offsets begins immediately following the parallel table
12033 (at offset 16 + 12 * M from the beginning of the section). The table is
12034 a two-dimensional array of 32-bit words (using the byte order of the
12035 application binary), with L columns and N+1 rows, in row-major order.
12036 Each row in the array is indexed starting from 0. The first row provides
12037 a key to the remaining rows: each column in this row provides an identifier
12038 for a debug section, and the offsets in the same column of subsequent rows
12039 refer to that section. The section identifiers are:
12041 DW_SECT_INFO 1 .debug_info.dwo
12042 DW_SECT_TYPES 2 .debug_types.dwo
12043 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12044 DW_SECT_LINE 4 .debug_line.dwo
12045 DW_SECT_LOC 5 .debug_loc.dwo
12046 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12047 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12048 DW_SECT_MACRO 8 .debug_macro.dwo
12050 The offsets provided by the CU and TU index sections are the base offsets
12051 for the contributions made by each CU or TU to the corresponding section
12052 in the package file. Each CU and TU header contains an abbrev_offset
12053 field, used to find the abbreviations table for that CU or TU within the
12054 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12055 be interpreted as relative to the base offset given in the index section.
12056 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12057 should be interpreted as relative to the base offset for .debug_line.dwo,
12058 and offsets into other debug sections obtained from DWARF attributes should
12059 also be interpreted as relative to the corresponding base offset.
12061 The table of sizes begins immediately following the table of offsets.
12062 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12063 with L columns and N rows, in row-major order. Each row in the array is
12064 indexed starting from 1 (row 0 is shared by the two tables).
12068 Hash table lookup is handled the same in version 1 and 2:
12070 We assume that N and M will not exceed 2^32 - 1.
12071 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12073 Given a 64-bit compilation unit signature or a type signature S, an entry
12074 in the hash table is located as follows:
12076 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12077 the low-order k bits all set to 1.
12079 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12081 3) If the hash table entry at index H matches the signature, use that
12082 entry. If the hash table entry at index H is unused (all zeroes),
12083 terminate the search: the signature is not present in the table.
12085 4) Let H = (H + H') modulo M. Repeat at Step 3.
12087 Because M > N and H' and M are relatively prime, the search is guaranteed
12088 to stop at an unused slot or find the match. */
12090 /* Create a hash table to map DWO IDs to their CU/TU entry in
12091 .debug_{info,types}.dwo in DWP_FILE.
12092 Returns NULL if there isn't one.
12093 Note: This function processes DWP files only, not DWO files. */
12095 static struct dwp_hash_table *
12096 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
12098 struct objfile *objfile = dwarf2_per_objfile->objfile;
12099 bfd *dbfd = dwp_file->dbfd;
12100 const gdb_byte *index_ptr, *index_end;
12101 struct dwarf2_section_info *index;
12102 uint32_t version, nr_columns, nr_units, nr_slots;
12103 struct dwp_hash_table *htab;
12105 if (is_debug_types)
12106 index = &dwp_file->sections.tu_index;
12108 index = &dwp_file->sections.cu_index;
12110 if (dwarf2_section_empty_p (index))
12112 dwarf2_read_section (objfile, index);
12114 index_ptr = index->buffer;
12115 index_end = index_ptr + index->size;
12117 version = read_4_bytes (dbfd, index_ptr);
12120 nr_columns = read_4_bytes (dbfd, index_ptr);
12124 nr_units = read_4_bytes (dbfd, index_ptr);
12126 nr_slots = read_4_bytes (dbfd, index_ptr);
12129 if (version != 1 && version != 2)
12131 error (_("Dwarf Error: unsupported DWP file version (%s)"
12132 " [in module %s]"),
12133 pulongest (version), dwp_file->name);
12135 if (nr_slots != (nr_slots & -nr_slots))
12137 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12138 " is not power of 2 [in module %s]"),
12139 pulongest (nr_slots), dwp_file->name);
12142 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12143 htab->version = version;
12144 htab->nr_columns = nr_columns;
12145 htab->nr_units = nr_units;
12146 htab->nr_slots = nr_slots;
12147 htab->hash_table = index_ptr;
12148 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12150 /* Exit early if the table is empty. */
12151 if (nr_slots == 0 || nr_units == 0
12152 || (version == 2 && nr_columns == 0))
12154 /* All must be zero. */
12155 if (nr_slots != 0 || nr_units != 0
12156 || (version == 2 && nr_columns != 0))
12158 complaint (&symfile_complaints,
12159 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12160 " all zero [in modules %s]"),
12168 htab->section_pool.v1.indices =
12169 htab->unit_table + sizeof (uint32_t) * nr_slots;
12170 /* It's harder to decide whether the section is too small in v1.
12171 V1 is deprecated anyway so we punt. */
12175 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12176 int *ids = htab->section_pool.v2.section_ids;
12177 /* Reverse map for error checking. */
12178 int ids_seen[DW_SECT_MAX + 1];
12181 if (nr_columns < 2)
12183 error (_("Dwarf Error: bad DWP hash table, too few columns"
12184 " in section table [in module %s]"),
12187 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12189 error (_("Dwarf Error: bad DWP hash table, too many columns"
12190 " in section table [in module %s]"),
12193 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12194 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12195 for (i = 0; i < nr_columns; ++i)
12197 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12199 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12201 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12202 " in section table [in module %s]"),
12203 id, dwp_file->name);
12205 if (ids_seen[id] != -1)
12207 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12208 " id %d in section table [in module %s]"),
12209 id, dwp_file->name);
12214 /* Must have exactly one info or types section. */
12215 if (((ids_seen[DW_SECT_INFO] != -1)
12216 + (ids_seen[DW_SECT_TYPES] != -1))
12219 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12220 " DWO info/types section [in module %s]"),
12223 /* Must have an abbrev section. */
12224 if (ids_seen[DW_SECT_ABBREV] == -1)
12226 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12227 " section [in module %s]"),
12230 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12231 htab->section_pool.v2.sizes =
12232 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12233 * nr_units * nr_columns);
12234 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12235 * nr_units * nr_columns))
12238 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12239 " [in module %s]"),
12247 /* Update SECTIONS with the data from SECTP.
12249 This function is like the other "locate" section routines that are
12250 passed to bfd_map_over_sections, but in this context the sections to
12251 read comes from the DWP V1 hash table, not the full ELF section table.
12253 The result is non-zero for success, or zero if an error was found. */
12256 locate_v1_virtual_dwo_sections (asection *sectp,
12257 struct virtual_v1_dwo_sections *sections)
12259 const struct dwop_section_names *names = &dwop_section_names;
12261 if (section_is_p (sectp->name, &names->abbrev_dwo))
12263 /* There can be only one. */
12264 if (sections->abbrev.s.section != NULL)
12266 sections->abbrev.s.section = sectp;
12267 sections->abbrev.size = bfd_get_section_size (sectp);
12269 else if (section_is_p (sectp->name, &names->info_dwo)
12270 || section_is_p (sectp->name, &names->types_dwo))
12272 /* There can be only one. */
12273 if (sections->info_or_types.s.section != NULL)
12275 sections->info_or_types.s.section = sectp;
12276 sections->info_or_types.size = bfd_get_section_size (sectp);
12278 else if (section_is_p (sectp->name, &names->line_dwo))
12280 /* There can be only one. */
12281 if (sections->line.s.section != NULL)
12283 sections->line.s.section = sectp;
12284 sections->line.size = bfd_get_section_size (sectp);
12286 else if (section_is_p (sectp->name, &names->loc_dwo))
12288 /* There can be only one. */
12289 if (sections->loc.s.section != NULL)
12291 sections->loc.s.section = sectp;
12292 sections->loc.size = bfd_get_section_size (sectp);
12294 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12296 /* There can be only one. */
12297 if (sections->macinfo.s.section != NULL)
12299 sections->macinfo.s.section = sectp;
12300 sections->macinfo.size = bfd_get_section_size (sectp);
12302 else if (section_is_p (sectp->name, &names->macro_dwo))
12304 /* There can be only one. */
12305 if (sections->macro.s.section != NULL)
12307 sections->macro.s.section = sectp;
12308 sections->macro.size = bfd_get_section_size (sectp);
12310 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12312 /* There can be only one. */
12313 if (sections->str_offsets.s.section != NULL)
12315 sections->str_offsets.s.section = sectp;
12316 sections->str_offsets.size = bfd_get_section_size (sectp);
12320 /* No other kind of section is valid. */
12327 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12328 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12329 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12330 This is for DWP version 1 files. */
12332 static struct dwo_unit *
12333 create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
12334 uint32_t unit_index,
12335 const char *comp_dir,
12336 ULONGEST signature, int is_debug_types)
12338 struct objfile *objfile = dwarf2_per_objfile->objfile;
12339 const struct dwp_hash_table *dwp_htab =
12340 is_debug_types ? dwp_file->tus : dwp_file->cus;
12341 bfd *dbfd = dwp_file->dbfd;
12342 const char *kind = is_debug_types ? "TU" : "CU";
12343 struct dwo_file *dwo_file;
12344 struct dwo_unit *dwo_unit;
12345 struct virtual_v1_dwo_sections sections;
12346 void **dwo_file_slot;
12349 gdb_assert (dwp_file->version == 1);
12351 if (dwarf_read_debug)
12353 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12355 pulongest (unit_index), hex_string (signature),
12359 /* Fetch the sections of this DWO unit.
12360 Put a limit on the number of sections we look for so that bad data
12361 doesn't cause us to loop forever. */
12363 #define MAX_NR_V1_DWO_SECTIONS \
12364 (1 /* .debug_info or .debug_types */ \
12365 + 1 /* .debug_abbrev */ \
12366 + 1 /* .debug_line */ \
12367 + 1 /* .debug_loc */ \
12368 + 1 /* .debug_str_offsets */ \
12369 + 1 /* .debug_macro or .debug_macinfo */ \
12370 + 1 /* trailing zero */)
12372 memset (§ions, 0, sizeof (sections));
12374 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12377 uint32_t section_nr =
12378 read_4_bytes (dbfd,
12379 dwp_htab->section_pool.v1.indices
12380 + (unit_index + i) * sizeof (uint32_t));
12382 if (section_nr == 0)
12384 if (section_nr >= dwp_file->num_sections)
12386 error (_("Dwarf Error: bad DWP hash table, section number too large"
12387 " [in module %s]"),
12391 sectp = dwp_file->elf_sections[section_nr];
12392 if (! locate_v1_virtual_dwo_sections (sectp, §ions))
12394 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12395 " [in module %s]"),
12401 || dwarf2_section_empty_p (§ions.info_or_types)
12402 || dwarf2_section_empty_p (§ions.abbrev))
12404 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12405 " [in module %s]"),
12408 if (i == MAX_NR_V1_DWO_SECTIONS)
12410 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12411 " [in module %s]"),
12415 /* It's easier for the rest of the code if we fake a struct dwo_file and
12416 have dwo_unit "live" in that. At least for now.
12418 The DWP file can be made up of a random collection of CUs and TUs.
12419 However, for each CU + set of TUs that came from the same original DWO
12420 file, we can combine them back into a virtual DWO file to save space
12421 (fewer struct dwo_file objects to allocate). Remember that for really
12422 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12424 std::string virtual_dwo_name =
12425 string_printf ("virtual-dwo/%d-%d-%d-%d",
12426 get_section_id (§ions.abbrev),
12427 get_section_id (§ions.line),
12428 get_section_id (§ions.loc),
12429 get_section_id (§ions.str_offsets));
12430 /* Can we use an existing virtual DWO file? */
12431 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
12432 /* Create one if necessary. */
12433 if (*dwo_file_slot == NULL)
12435 if (dwarf_read_debug)
12437 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12438 virtual_dwo_name.c_str ());
12440 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12442 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12443 virtual_dwo_name.c_str (),
12444 virtual_dwo_name.size ());
12445 dwo_file->comp_dir = comp_dir;
12446 dwo_file->sections.abbrev = sections.abbrev;
12447 dwo_file->sections.line = sections.line;
12448 dwo_file->sections.loc = sections.loc;
12449 dwo_file->sections.macinfo = sections.macinfo;
12450 dwo_file->sections.macro = sections.macro;
12451 dwo_file->sections.str_offsets = sections.str_offsets;
12452 /* The "str" section is global to the entire DWP file. */
12453 dwo_file->sections.str = dwp_file->sections.str;
12454 /* The info or types section is assigned below to dwo_unit,
12455 there's no need to record it in dwo_file.
12456 Also, we can't simply record type sections in dwo_file because
12457 we record a pointer into the vector in dwo_unit. As we collect more
12458 types we'll grow the vector and eventually have to reallocate space
12459 for it, invalidating all copies of pointers into the previous
12461 *dwo_file_slot = dwo_file;
12465 if (dwarf_read_debug)
12467 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12468 virtual_dwo_name.c_str ());
12470 dwo_file = (struct dwo_file *) *dwo_file_slot;
12473 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12474 dwo_unit->dwo_file = dwo_file;
12475 dwo_unit->signature = signature;
12476 dwo_unit->section =
12477 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12478 *dwo_unit->section = sections.info_or_types;
12479 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12484 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12485 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12486 piece within that section used by a TU/CU, return a virtual section
12487 of just that piece. */
12489 static struct dwarf2_section_info
12490 create_dwp_v2_section (struct dwarf2_section_info *section,
12491 bfd_size_type offset, bfd_size_type size)
12493 struct dwarf2_section_info result;
12496 gdb_assert (section != NULL);
12497 gdb_assert (!section->is_virtual);
12499 memset (&result, 0, sizeof (result));
12500 result.s.containing_section = section;
12501 result.is_virtual = 1;
12506 sectp = get_section_bfd_section (section);
12508 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12509 bounds of the real section. This is a pretty-rare event, so just
12510 flag an error (easier) instead of a warning and trying to cope. */
12512 || offset + size > bfd_get_section_size (sectp))
12514 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12515 " in section %s [in module %s]"),
12516 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12517 objfile_name (dwarf2_per_objfile->objfile));
12520 result.virtual_offset = offset;
12521 result.size = size;
12525 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12526 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12527 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12528 This is for DWP version 2 files. */
12530 static struct dwo_unit *
12531 create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
12532 uint32_t unit_index,
12533 const char *comp_dir,
12534 ULONGEST signature, int is_debug_types)
12536 struct objfile *objfile = dwarf2_per_objfile->objfile;
12537 const struct dwp_hash_table *dwp_htab =
12538 is_debug_types ? dwp_file->tus : dwp_file->cus;
12539 bfd *dbfd = dwp_file->dbfd;
12540 const char *kind = is_debug_types ? "TU" : "CU";
12541 struct dwo_file *dwo_file;
12542 struct dwo_unit *dwo_unit;
12543 struct virtual_v2_dwo_sections sections;
12544 void **dwo_file_slot;
12547 gdb_assert (dwp_file->version == 2);
12549 if (dwarf_read_debug)
12551 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12553 pulongest (unit_index), hex_string (signature),
12557 /* Fetch the section offsets of this DWO unit. */
12559 memset (§ions, 0, sizeof (sections));
12561 for (i = 0; i < dwp_htab->nr_columns; ++i)
12563 uint32_t offset = read_4_bytes (dbfd,
12564 dwp_htab->section_pool.v2.offsets
12565 + (((unit_index - 1) * dwp_htab->nr_columns
12567 * sizeof (uint32_t)));
12568 uint32_t size = read_4_bytes (dbfd,
12569 dwp_htab->section_pool.v2.sizes
12570 + (((unit_index - 1) * dwp_htab->nr_columns
12572 * sizeof (uint32_t)));
12574 switch (dwp_htab->section_pool.v2.section_ids[i])
12577 case DW_SECT_TYPES:
12578 sections.info_or_types_offset = offset;
12579 sections.info_or_types_size = size;
12581 case DW_SECT_ABBREV:
12582 sections.abbrev_offset = offset;
12583 sections.abbrev_size = size;
12586 sections.line_offset = offset;
12587 sections.line_size = size;
12590 sections.loc_offset = offset;
12591 sections.loc_size = size;
12593 case DW_SECT_STR_OFFSETS:
12594 sections.str_offsets_offset = offset;
12595 sections.str_offsets_size = size;
12597 case DW_SECT_MACINFO:
12598 sections.macinfo_offset = offset;
12599 sections.macinfo_size = size;
12601 case DW_SECT_MACRO:
12602 sections.macro_offset = offset;
12603 sections.macro_size = size;
12608 /* It's easier for the rest of the code if we fake a struct dwo_file and
12609 have dwo_unit "live" in that. At least for now.
12611 The DWP file can be made up of a random collection of CUs and TUs.
12612 However, for each CU + set of TUs that came from the same original DWO
12613 file, we can combine them back into a virtual DWO file to save space
12614 (fewer struct dwo_file objects to allocate). Remember that for really
12615 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12617 std::string virtual_dwo_name =
12618 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12619 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12620 (long) (sections.line_size ? sections.line_offset : 0),
12621 (long) (sections.loc_size ? sections.loc_offset : 0),
12622 (long) (sections.str_offsets_size
12623 ? sections.str_offsets_offset : 0));
12624 /* Can we use an existing virtual DWO file? */
12625 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
12626 /* Create one if necessary. */
12627 if (*dwo_file_slot == NULL)
12629 if (dwarf_read_debug)
12631 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12632 virtual_dwo_name.c_str ());
12634 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12636 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12637 virtual_dwo_name.c_str (),
12638 virtual_dwo_name.size ());
12639 dwo_file->comp_dir = comp_dir;
12640 dwo_file->sections.abbrev =
12641 create_dwp_v2_section (&dwp_file->sections.abbrev,
12642 sections.abbrev_offset, sections.abbrev_size);
12643 dwo_file->sections.line =
12644 create_dwp_v2_section (&dwp_file->sections.line,
12645 sections.line_offset, sections.line_size);
12646 dwo_file->sections.loc =
12647 create_dwp_v2_section (&dwp_file->sections.loc,
12648 sections.loc_offset, sections.loc_size);
12649 dwo_file->sections.macinfo =
12650 create_dwp_v2_section (&dwp_file->sections.macinfo,
12651 sections.macinfo_offset, sections.macinfo_size);
12652 dwo_file->sections.macro =
12653 create_dwp_v2_section (&dwp_file->sections.macro,
12654 sections.macro_offset, sections.macro_size);
12655 dwo_file->sections.str_offsets =
12656 create_dwp_v2_section (&dwp_file->sections.str_offsets,
12657 sections.str_offsets_offset,
12658 sections.str_offsets_size);
12659 /* The "str" section is global to the entire DWP file. */
12660 dwo_file->sections.str = dwp_file->sections.str;
12661 /* The info or types section is assigned below to dwo_unit,
12662 there's no need to record it in dwo_file.
12663 Also, we can't simply record type sections in dwo_file because
12664 we record a pointer into the vector in dwo_unit. As we collect more
12665 types we'll grow the vector and eventually have to reallocate space
12666 for it, invalidating all copies of pointers into the previous
12668 *dwo_file_slot = dwo_file;
12672 if (dwarf_read_debug)
12674 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12675 virtual_dwo_name.c_str ());
12677 dwo_file = (struct dwo_file *) *dwo_file_slot;
12680 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12681 dwo_unit->dwo_file = dwo_file;
12682 dwo_unit->signature = signature;
12683 dwo_unit->section =
12684 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12685 *dwo_unit->section = create_dwp_v2_section (is_debug_types
12686 ? &dwp_file->sections.types
12687 : &dwp_file->sections.info,
12688 sections.info_or_types_offset,
12689 sections.info_or_types_size);
12690 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12695 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12696 Returns NULL if the signature isn't found. */
12698 static struct dwo_unit *
12699 lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
12700 ULONGEST signature, int is_debug_types)
12702 const struct dwp_hash_table *dwp_htab =
12703 is_debug_types ? dwp_file->tus : dwp_file->cus;
12704 bfd *dbfd = dwp_file->dbfd;
12705 uint32_t mask = dwp_htab->nr_slots - 1;
12706 uint32_t hash = signature & mask;
12707 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12710 struct dwo_unit find_dwo_cu;
12712 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12713 find_dwo_cu.signature = signature;
12714 slot = htab_find_slot (is_debug_types
12715 ? dwp_file->loaded_tus
12716 : dwp_file->loaded_cus,
12717 &find_dwo_cu, INSERT);
12720 return (struct dwo_unit *) *slot;
12722 /* Use a for loop so that we don't loop forever on bad debug info. */
12723 for (i = 0; i < dwp_htab->nr_slots; ++i)
12725 ULONGEST signature_in_table;
12727 signature_in_table =
12728 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12729 if (signature_in_table == signature)
12731 uint32_t unit_index =
12732 read_4_bytes (dbfd,
12733 dwp_htab->unit_table + hash * sizeof (uint32_t));
12735 if (dwp_file->version == 1)
12737 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
12738 comp_dir, signature,
12743 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
12744 comp_dir, signature,
12747 return (struct dwo_unit *) *slot;
12749 if (signature_in_table == 0)
12751 hash = (hash + hash2) & mask;
12754 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12755 " [in module %s]"),
12759 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12760 Open the file specified by FILE_NAME and hand it off to BFD for
12761 preliminary analysis. Return a newly initialized bfd *, which
12762 includes a canonicalized copy of FILE_NAME.
12763 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12764 SEARCH_CWD is true if the current directory is to be searched.
12765 It will be searched before debug-file-directory.
12766 If successful, the file is added to the bfd include table of the
12767 objfile's bfd (see gdb_bfd_record_inclusion).
12768 If unable to find/open the file, return NULL.
12769 NOTE: This function is derived from symfile_bfd_open. */
12771 static gdb_bfd_ref_ptr
12772 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
12775 char *absolute_name;
12776 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12777 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12778 to debug_file_directory. */
12780 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12784 if (*debug_file_directory != '\0')
12785 search_path = concat (".", dirname_separator_string,
12786 debug_file_directory, (char *) NULL);
12788 search_path = xstrdup (".");
12791 search_path = xstrdup (debug_file_directory);
12793 flags = OPF_RETURN_REALPATH;
12795 flags |= OPF_SEARCH_IN_PATH;
12796 desc = openp (search_path, flags, file_name,
12797 O_RDONLY | O_BINARY, &absolute_name);
12798 xfree (search_path);
12802 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
12803 xfree (absolute_name);
12804 if (sym_bfd == NULL)
12806 bfd_set_cacheable (sym_bfd.get (), 1);
12808 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12811 /* Success. Record the bfd as having been included by the objfile's bfd.
12812 This is important because things like demangled_names_hash lives in the
12813 objfile's per_bfd space and may have references to things like symbol
12814 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12815 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12820 /* Try to open DWO file FILE_NAME.
12821 COMP_DIR is the DW_AT_comp_dir attribute.
12822 The result is the bfd handle of the file.
12823 If there is a problem finding or opening the file, return NULL.
12824 Upon success, the canonicalized path of the file is stored in the bfd,
12825 same as symfile_bfd_open. */
12827 static gdb_bfd_ref_ptr
12828 open_dwo_file (const char *file_name, const char *comp_dir)
12830 if (IS_ABSOLUTE_PATH (file_name))
12831 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
12833 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12835 if (comp_dir != NULL)
12837 char *path_to_try = concat (comp_dir, SLASH_STRING,
12838 file_name, (char *) NULL);
12840 /* NOTE: If comp_dir is a relative path, this will also try the
12841 search path, which seems useful. */
12842 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
12843 1 /*search_cwd*/));
12844 xfree (path_to_try);
12849 /* That didn't work, try debug-file-directory, which, despite its name,
12850 is a list of paths. */
12852 if (*debug_file_directory == '\0')
12855 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
12858 /* This function is mapped across the sections and remembers the offset and
12859 size of each of the DWO debugging sections we are interested in. */
12862 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12864 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12865 const struct dwop_section_names *names = &dwop_section_names;
12867 if (section_is_p (sectp->name, &names->abbrev_dwo))
12869 dwo_sections->abbrev.s.section = sectp;
12870 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12872 else if (section_is_p (sectp->name, &names->info_dwo))
12874 dwo_sections->info.s.section = sectp;
12875 dwo_sections->info.size = bfd_get_section_size (sectp);
12877 else if (section_is_p (sectp->name, &names->line_dwo))
12879 dwo_sections->line.s.section = sectp;
12880 dwo_sections->line.size = bfd_get_section_size (sectp);
12882 else if (section_is_p (sectp->name, &names->loc_dwo))
12884 dwo_sections->loc.s.section = sectp;
12885 dwo_sections->loc.size = bfd_get_section_size (sectp);
12887 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12889 dwo_sections->macinfo.s.section = sectp;
12890 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12892 else if (section_is_p (sectp->name, &names->macro_dwo))
12894 dwo_sections->macro.s.section = sectp;
12895 dwo_sections->macro.size = bfd_get_section_size (sectp);
12897 else if (section_is_p (sectp->name, &names->str_dwo))
12899 dwo_sections->str.s.section = sectp;
12900 dwo_sections->str.size = bfd_get_section_size (sectp);
12902 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12904 dwo_sections->str_offsets.s.section = sectp;
12905 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12907 else if (section_is_p (sectp->name, &names->types_dwo))
12909 struct dwarf2_section_info type_section;
12911 memset (&type_section, 0, sizeof (type_section));
12912 type_section.s.section = sectp;
12913 type_section.size = bfd_get_section_size (sectp);
12914 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
12919 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12920 by PER_CU. This is for the non-DWP case.
12921 The result is NULL if DWO_NAME can't be found. */
12923 static struct dwo_file *
12924 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12925 const char *dwo_name, const char *comp_dir)
12927 struct objfile *objfile = dwarf2_per_objfile->objfile;
12928 struct dwo_file *dwo_file;
12929 struct cleanup *cleanups;
12931 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
12934 if (dwarf_read_debug)
12935 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12938 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12939 dwo_file->dwo_name = dwo_name;
12940 dwo_file->comp_dir = comp_dir;
12941 dwo_file->dbfd = dbfd.release ();
12943 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
12945 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
12946 &dwo_file->sections);
12948 create_cus_hash_table (*dwo_file, dwo_file->sections.info, dwo_file->cus);
12950 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
12953 discard_cleanups (cleanups);
12955 if (dwarf_read_debug)
12956 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12961 /* This function is mapped across the sections and remembers the offset and
12962 size of each of the DWP debugging sections common to version 1 and 2 that
12963 we are interested in. */
12966 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12967 void *dwp_file_ptr)
12969 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12970 const struct dwop_section_names *names = &dwop_section_names;
12971 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12973 /* Record the ELF section number for later lookup: this is what the
12974 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12975 gdb_assert (elf_section_nr < dwp_file->num_sections);
12976 dwp_file->elf_sections[elf_section_nr] = sectp;
12978 /* Look for specific sections that we need. */
12979 if (section_is_p (sectp->name, &names->str_dwo))
12981 dwp_file->sections.str.s.section = sectp;
12982 dwp_file->sections.str.size = bfd_get_section_size (sectp);
12984 else if (section_is_p (sectp->name, &names->cu_index))
12986 dwp_file->sections.cu_index.s.section = sectp;
12987 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
12989 else if (section_is_p (sectp->name, &names->tu_index))
12991 dwp_file->sections.tu_index.s.section = sectp;
12992 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
12996 /* This function is mapped across the sections and remembers the offset and
12997 size of each of the DWP version 2 debugging sections that we are interested
12998 in. This is split into a separate function because we don't know if we
12999 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13002 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13004 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13005 const struct dwop_section_names *names = &dwop_section_names;
13006 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13008 /* Record the ELF section number for later lookup: this is what the
13009 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13010 gdb_assert (elf_section_nr < dwp_file->num_sections);
13011 dwp_file->elf_sections[elf_section_nr] = sectp;
13013 /* Look for specific sections that we need. */
13014 if (section_is_p (sectp->name, &names->abbrev_dwo))
13016 dwp_file->sections.abbrev.s.section = sectp;
13017 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13019 else if (section_is_p (sectp->name, &names->info_dwo))
13021 dwp_file->sections.info.s.section = sectp;
13022 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13024 else if (section_is_p (sectp->name, &names->line_dwo))
13026 dwp_file->sections.line.s.section = sectp;
13027 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13029 else if (section_is_p (sectp->name, &names->loc_dwo))
13031 dwp_file->sections.loc.s.section = sectp;
13032 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13034 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13036 dwp_file->sections.macinfo.s.section = sectp;
13037 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13039 else if (section_is_p (sectp->name, &names->macro_dwo))
13041 dwp_file->sections.macro.s.section = sectp;
13042 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13044 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13046 dwp_file->sections.str_offsets.s.section = sectp;
13047 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13049 else if (section_is_p (sectp->name, &names->types_dwo))
13051 dwp_file->sections.types.s.section = sectp;
13052 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13056 /* Hash function for dwp_file loaded CUs/TUs. */
13059 hash_dwp_loaded_cutus (const void *item)
13061 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13063 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13064 return dwo_unit->signature;
13067 /* Equality function for dwp_file loaded CUs/TUs. */
13070 eq_dwp_loaded_cutus (const void *a, const void *b)
13072 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13073 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13075 return dua->signature == dub->signature;
13078 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13081 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13083 return htab_create_alloc_ex (3,
13084 hash_dwp_loaded_cutus,
13085 eq_dwp_loaded_cutus,
13087 &objfile->objfile_obstack,
13088 hashtab_obstack_allocate,
13089 dummy_obstack_deallocate);
13092 /* Try to open DWP file FILE_NAME.
13093 The result is the bfd handle of the file.
13094 If there is a problem finding or opening the file, return NULL.
13095 Upon success, the canonicalized path of the file is stored in the bfd,
13096 same as symfile_bfd_open. */
13098 static gdb_bfd_ref_ptr
13099 open_dwp_file (const char *file_name)
13101 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
13102 1 /*search_cwd*/));
13106 /* Work around upstream bug 15652.
13107 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13108 [Whether that's a "bug" is debatable, but it is getting in our way.]
13109 We have no real idea where the dwp file is, because gdb's realpath-ing
13110 of the executable's path may have discarded the needed info.
13111 [IWBN if the dwp file name was recorded in the executable, akin to
13112 .gnu_debuglink, but that doesn't exist yet.]
13113 Strip the directory from FILE_NAME and search again. */
13114 if (*debug_file_directory != '\0')
13116 /* Don't implicitly search the current directory here.
13117 If the user wants to search "." to handle this case,
13118 it must be added to debug-file-directory. */
13119 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
13126 /* Initialize the use of the DWP file for the current objfile.
13127 By convention the name of the DWP file is ${objfile}.dwp.
13128 The result is NULL if it can't be found. */
13130 static struct dwp_file *
13131 open_and_init_dwp_file (void)
13133 struct objfile *objfile = dwarf2_per_objfile->objfile;
13134 struct dwp_file *dwp_file;
13136 /* Try to find first .dwp for the binary file before any symbolic links
13139 /* If the objfile is a debug file, find the name of the real binary
13140 file and get the name of dwp file from there. */
13141 std::string dwp_name;
13142 if (objfile->separate_debug_objfile_backlink != NULL)
13144 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13145 const char *backlink_basename = lbasename (backlink->original_name);
13147 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13150 dwp_name = objfile->original_name;
13152 dwp_name += ".dwp";
13154 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
13156 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13158 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13159 dwp_name = objfile_name (objfile);
13160 dwp_name += ".dwp";
13161 dbfd = open_dwp_file (dwp_name.c_str ());
13166 if (dwarf_read_debug)
13167 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13170 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
13171 dwp_file->name = bfd_get_filename (dbfd.get ());
13172 dwp_file->dbfd = dbfd.release ();
13174 /* +1: section 0 is unused */
13175 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
13176 dwp_file->elf_sections =
13177 OBSTACK_CALLOC (&objfile->objfile_obstack,
13178 dwp_file->num_sections, asection *);
13180 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13183 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
13185 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
13187 /* The DWP file version is stored in the hash table. Oh well. */
13188 if (dwp_file->cus && dwp_file->tus
13189 && dwp_file->cus->version != dwp_file->tus->version)
13191 /* Technically speaking, we should try to limp along, but this is
13192 pretty bizarre. We use pulongest here because that's the established
13193 portability solution (e.g, we cannot use %u for uint32_t). */
13194 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13195 " TU version %s [in DWP file %s]"),
13196 pulongest (dwp_file->cus->version),
13197 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13201 dwp_file->version = dwp_file->cus->version;
13202 else if (dwp_file->tus)
13203 dwp_file->version = dwp_file->tus->version;
13205 dwp_file->version = 2;
13207 if (dwp_file->version == 2)
13208 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13211 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13212 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13214 if (dwarf_read_debug)
13216 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13217 fprintf_unfiltered (gdb_stdlog,
13218 " %s CUs, %s TUs\n",
13219 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13220 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13226 /* Wrapper around open_and_init_dwp_file, only open it once. */
13228 static struct dwp_file *
13229 get_dwp_file (void)
13231 if (! dwarf2_per_objfile->dwp_checked)
13233 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
13234 dwarf2_per_objfile->dwp_checked = 1;
13236 return dwarf2_per_objfile->dwp_file;
13239 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13240 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13241 or in the DWP file for the objfile, referenced by THIS_UNIT.
13242 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13243 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13245 This is called, for example, when wanting to read a variable with a
13246 complex location. Therefore we don't want to do file i/o for every call.
13247 Therefore we don't want to look for a DWO file on every call.
13248 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13249 then we check if we've already seen DWO_NAME, and only THEN do we check
13252 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13253 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13255 static struct dwo_unit *
13256 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13257 const char *dwo_name, const char *comp_dir,
13258 ULONGEST signature, int is_debug_types)
13260 struct objfile *objfile = dwarf2_per_objfile->objfile;
13261 const char *kind = is_debug_types ? "TU" : "CU";
13262 void **dwo_file_slot;
13263 struct dwo_file *dwo_file;
13264 struct dwp_file *dwp_file;
13266 /* First see if there's a DWP file.
13267 If we have a DWP file but didn't find the DWO inside it, don't
13268 look for the original DWO file. It makes gdb behave differently
13269 depending on whether one is debugging in the build tree. */
13271 dwp_file = get_dwp_file ();
13272 if (dwp_file != NULL)
13274 const struct dwp_hash_table *dwp_htab =
13275 is_debug_types ? dwp_file->tus : dwp_file->cus;
13277 if (dwp_htab != NULL)
13279 struct dwo_unit *dwo_cutu =
13280 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
13281 signature, is_debug_types);
13283 if (dwo_cutu != NULL)
13285 if (dwarf_read_debug)
13287 fprintf_unfiltered (gdb_stdlog,
13288 "Virtual DWO %s %s found: @%s\n",
13289 kind, hex_string (signature),
13290 host_address_to_string (dwo_cutu));
13298 /* No DWP file, look for the DWO file. */
13300 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
13301 if (*dwo_file_slot == NULL)
13303 /* Read in the file and build a table of the CUs/TUs it contains. */
13304 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13306 /* NOTE: This will be NULL if unable to open the file. */
13307 dwo_file = (struct dwo_file *) *dwo_file_slot;
13309 if (dwo_file != NULL)
13311 struct dwo_unit *dwo_cutu = NULL;
13313 if (is_debug_types && dwo_file->tus)
13315 struct dwo_unit find_dwo_cutu;
13317 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13318 find_dwo_cutu.signature = signature;
13320 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13322 else if (!is_debug_types && dwo_file->cus)
13324 struct dwo_unit find_dwo_cutu;
13326 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13327 find_dwo_cutu.signature = signature;
13328 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13332 if (dwo_cutu != NULL)
13334 if (dwarf_read_debug)
13336 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13337 kind, dwo_name, hex_string (signature),
13338 host_address_to_string (dwo_cutu));
13345 /* We didn't find it. This could mean a dwo_id mismatch, or
13346 someone deleted the DWO/DWP file, or the search path isn't set up
13347 correctly to find the file. */
13349 if (dwarf_read_debug)
13351 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13352 kind, dwo_name, hex_string (signature));
13355 /* This is a warning and not a complaint because it can be caused by
13356 pilot error (e.g., user accidentally deleting the DWO). */
13358 /* Print the name of the DWP file if we looked there, helps the user
13359 better diagnose the problem. */
13360 std::string dwp_text;
13362 if (dwp_file != NULL)
13363 dwp_text = string_printf (" [in DWP file %s]",
13364 lbasename (dwp_file->name));
13366 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
13367 " [in module %s]"),
13368 kind, dwo_name, hex_string (signature),
13370 this_unit->is_debug_types ? "TU" : "CU",
13371 to_underlying (this_unit->sect_off), objfile_name (objfile));
13376 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13377 See lookup_dwo_cutu_unit for details. */
13379 static struct dwo_unit *
13380 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13381 const char *dwo_name, const char *comp_dir,
13382 ULONGEST signature)
13384 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13387 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13388 See lookup_dwo_cutu_unit for details. */
13390 static struct dwo_unit *
13391 lookup_dwo_type_unit (struct signatured_type *this_tu,
13392 const char *dwo_name, const char *comp_dir)
13394 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13397 /* Traversal function for queue_and_load_all_dwo_tus. */
13400 queue_and_load_dwo_tu (void **slot, void *info)
13402 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13403 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13404 ULONGEST signature = dwo_unit->signature;
13405 struct signatured_type *sig_type =
13406 lookup_dwo_signatured_type (per_cu->cu, signature);
13408 if (sig_type != NULL)
13410 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13412 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13413 a real dependency of PER_CU on SIG_TYPE. That is detected later
13414 while processing PER_CU. */
13415 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13416 load_full_type_unit (sig_cu);
13417 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13423 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13424 The DWO may have the only definition of the type, though it may not be
13425 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13426 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13429 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13431 struct dwo_unit *dwo_unit;
13432 struct dwo_file *dwo_file;
13434 gdb_assert (!per_cu->is_debug_types);
13435 gdb_assert (get_dwp_file () == NULL);
13436 gdb_assert (per_cu->cu != NULL);
13438 dwo_unit = per_cu->cu->dwo_unit;
13439 gdb_assert (dwo_unit != NULL);
13441 dwo_file = dwo_unit->dwo_file;
13442 if (dwo_file->tus != NULL)
13443 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13446 /* Free all resources associated with DWO_FILE.
13447 Close the DWO file and munmap the sections.
13448 All memory should be on the objfile obstack. */
13451 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
13454 /* Note: dbfd is NULL for virtual DWO files. */
13455 gdb_bfd_unref (dwo_file->dbfd);
13457 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13460 /* Wrapper for free_dwo_file for use in cleanups. */
13463 free_dwo_file_cleanup (void *arg)
13465 struct dwo_file *dwo_file = (struct dwo_file *) arg;
13466 struct objfile *objfile = dwarf2_per_objfile->objfile;
13468 free_dwo_file (dwo_file, objfile);
13471 /* Traversal function for free_dwo_files. */
13474 free_dwo_file_from_slot (void **slot, void *info)
13476 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13477 struct objfile *objfile = (struct objfile *) info;
13479 free_dwo_file (dwo_file, objfile);
13484 /* Free all resources associated with DWO_FILES. */
13487 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13489 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
13492 /* Read in various DIEs. */
13494 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13495 Inherit only the children of the DW_AT_abstract_origin DIE not being
13496 already referenced by DW_AT_abstract_origin from the children of the
13500 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13502 struct die_info *child_die;
13503 sect_offset *offsetp;
13504 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13505 struct die_info *origin_die;
13506 /* Iterator of the ORIGIN_DIE children. */
13507 struct die_info *origin_child_die;
13508 struct attribute *attr;
13509 struct dwarf2_cu *origin_cu;
13510 struct pending **origin_previous_list_in_scope;
13512 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13516 /* Note that following die references may follow to a die in a
13520 origin_die = follow_die_ref (die, attr, &origin_cu);
13522 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13524 origin_previous_list_in_scope = origin_cu->list_in_scope;
13525 origin_cu->list_in_scope = cu->list_in_scope;
13527 if (die->tag != origin_die->tag
13528 && !(die->tag == DW_TAG_inlined_subroutine
13529 && origin_die->tag == DW_TAG_subprogram))
13530 complaint (&symfile_complaints,
13531 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
13532 to_underlying (die->sect_off),
13533 to_underlying (origin_die->sect_off));
13535 std::vector<sect_offset> offsets;
13537 for (child_die = die->child;
13538 child_die && child_die->tag;
13539 child_die = sibling_die (child_die))
13541 struct die_info *child_origin_die;
13542 struct dwarf2_cu *child_origin_cu;
13544 /* We are trying to process concrete instance entries:
13545 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13546 it's not relevant to our analysis here. i.e. detecting DIEs that are
13547 present in the abstract instance but not referenced in the concrete
13549 if (child_die->tag == DW_TAG_call_site
13550 || child_die->tag == DW_TAG_GNU_call_site)
13553 /* For each CHILD_DIE, find the corresponding child of
13554 ORIGIN_DIE. If there is more than one layer of
13555 DW_AT_abstract_origin, follow them all; there shouldn't be,
13556 but GCC versions at least through 4.4 generate this (GCC PR
13558 child_origin_die = child_die;
13559 child_origin_cu = cu;
13562 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13566 child_origin_die = follow_die_ref (child_origin_die, attr,
13570 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13571 counterpart may exist. */
13572 if (child_origin_die != child_die)
13574 if (child_die->tag != child_origin_die->tag
13575 && !(child_die->tag == DW_TAG_inlined_subroutine
13576 && child_origin_die->tag == DW_TAG_subprogram))
13577 complaint (&symfile_complaints,
13578 _("Child DIE 0x%x and its abstract origin 0x%x have "
13580 to_underlying (child_die->sect_off),
13581 to_underlying (child_origin_die->sect_off));
13582 if (child_origin_die->parent != origin_die)
13583 complaint (&symfile_complaints,
13584 _("Child DIE 0x%x and its abstract origin 0x%x have "
13585 "different parents"),
13586 to_underlying (child_die->sect_off),
13587 to_underlying (child_origin_die->sect_off));
13589 offsets.push_back (child_origin_die->sect_off);
13592 std::sort (offsets.begin (), offsets.end ());
13593 sect_offset *offsets_end = offsets.data () + offsets.size ();
13594 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13595 if (offsetp[-1] == *offsetp)
13596 complaint (&symfile_complaints,
13597 _("Multiple children of DIE 0x%x refer "
13598 "to DIE 0x%x as their abstract origin"),
13599 to_underlying (die->sect_off), to_underlying (*offsetp));
13601 offsetp = offsets.data ();
13602 origin_child_die = origin_die->child;
13603 while (origin_child_die && origin_child_die->tag)
13605 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13606 while (offsetp < offsets_end
13607 && *offsetp < origin_child_die->sect_off)
13609 if (offsetp >= offsets_end
13610 || *offsetp > origin_child_die->sect_off)
13612 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13613 Check whether we're already processing ORIGIN_CHILD_DIE.
13614 This can happen with mutually referenced abstract_origins.
13616 if (!origin_child_die->in_process)
13617 process_die (origin_child_die, origin_cu);
13619 origin_child_die = sibling_die (origin_child_die);
13621 origin_cu->list_in_scope = origin_previous_list_in_scope;
13625 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13627 struct objfile *objfile = cu->objfile;
13628 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13629 struct context_stack *newobj;
13632 struct die_info *child_die;
13633 struct attribute *attr, *call_line, *call_file;
13635 CORE_ADDR baseaddr;
13636 struct block *block;
13637 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13638 std::vector<struct symbol *> template_args;
13639 struct template_symbol *templ_func = NULL;
13643 /* If we do not have call site information, we can't show the
13644 caller of this inlined function. That's too confusing, so
13645 only use the scope for local variables. */
13646 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13647 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13648 if (call_line == NULL || call_file == NULL)
13650 read_lexical_block_scope (die, cu);
13655 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13657 name = dwarf2_name (die, cu);
13659 /* Ignore functions with missing or empty names. These are actually
13660 illegal according to the DWARF standard. */
13663 complaint (&symfile_complaints,
13664 _("missing name for subprogram DIE at %d"),
13665 to_underlying (die->sect_off));
13669 /* Ignore functions with missing or invalid low and high pc attributes. */
13670 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13671 <= PC_BOUNDS_INVALID)
13673 attr = dwarf2_attr (die, DW_AT_external, cu);
13674 if (!attr || !DW_UNSND (attr))
13675 complaint (&symfile_complaints,
13676 _("cannot get low and high bounds "
13677 "for subprogram DIE at %d"),
13678 to_underlying (die->sect_off));
13682 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13683 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13685 /* If we have any template arguments, then we must allocate a
13686 different sort of symbol. */
13687 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13689 if (child_die->tag == DW_TAG_template_type_param
13690 || child_die->tag == DW_TAG_template_value_param)
13692 templ_func = allocate_template_symbol (objfile);
13693 templ_func->subclass = SYMBOL_TEMPLATE;
13698 newobj = push_context (0, lowpc);
13699 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
13700 (struct symbol *) templ_func);
13702 /* If there is a location expression for DW_AT_frame_base, record
13704 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13706 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13708 /* If there is a location for the static link, record it. */
13709 newobj->static_link = NULL;
13710 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13713 newobj->static_link
13714 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13715 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13718 cu->list_in_scope = &local_symbols;
13720 if (die->child != NULL)
13722 child_die = die->child;
13723 while (child_die && child_die->tag)
13725 if (child_die->tag == DW_TAG_template_type_param
13726 || child_die->tag == DW_TAG_template_value_param)
13728 struct symbol *arg = new_symbol (child_die, NULL, cu);
13731 template_args.push_back (arg);
13734 process_die (child_die, cu);
13735 child_die = sibling_die (child_die);
13739 inherit_abstract_dies (die, cu);
13741 /* If we have a DW_AT_specification, we might need to import using
13742 directives from the context of the specification DIE. See the
13743 comment in determine_prefix. */
13744 if (cu->language == language_cplus
13745 && dwarf2_attr (die, DW_AT_specification, cu))
13747 struct dwarf2_cu *spec_cu = cu;
13748 struct die_info *spec_die = die_specification (die, &spec_cu);
13752 child_die = spec_die->child;
13753 while (child_die && child_die->tag)
13755 if (child_die->tag == DW_TAG_imported_module)
13756 process_die (child_die, spec_cu);
13757 child_die = sibling_die (child_die);
13760 /* In some cases, GCC generates specification DIEs that
13761 themselves contain DW_AT_specification attributes. */
13762 spec_die = die_specification (spec_die, &spec_cu);
13766 newobj = pop_context ();
13767 /* Make a block for the local symbols within. */
13768 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
13769 newobj->static_link, lowpc, highpc);
13771 /* For C++, set the block's scope. */
13772 if ((cu->language == language_cplus
13773 || cu->language == language_fortran
13774 || cu->language == language_d
13775 || cu->language == language_rust)
13776 && cu->processing_has_namespace_info)
13777 block_set_scope (block, determine_prefix (die, cu),
13778 &objfile->objfile_obstack);
13780 /* If we have address ranges, record them. */
13781 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13783 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
13785 /* Attach template arguments to function. */
13786 if (!template_args.empty ())
13788 gdb_assert (templ_func != NULL);
13790 templ_func->n_template_arguments = template_args.size ();
13791 templ_func->template_arguments
13792 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13793 templ_func->n_template_arguments);
13794 memcpy (templ_func->template_arguments,
13795 template_args.data (),
13796 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13799 /* In C++, we can have functions nested inside functions (e.g., when
13800 a function declares a class that has methods). This means that
13801 when we finish processing a function scope, we may need to go
13802 back to building a containing block's symbol lists. */
13803 local_symbols = newobj->locals;
13804 local_using_directives = newobj->local_using_directives;
13806 /* If we've finished processing a top-level function, subsequent
13807 symbols go in the file symbol list. */
13808 if (outermost_context_p ())
13809 cu->list_in_scope = &file_symbols;
13812 /* Process all the DIES contained within a lexical block scope. Start
13813 a new scope, process the dies, and then close the scope. */
13816 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13818 struct objfile *objfile = cu->objfile;
13819 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13820 struct context_stack *newobj;
13821 CORE_ADDR lowpc, highpc;
13822 struct die_info *child_die;
13823 CORE_ADDR baseaddr;
13825 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13827 /* Ignore blocks with missing or invalid low and high pc attributes. */
13828 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13829 as multiple lexical blocks? Handling children in a sane way would
13830 be nasty. Might be easier to properly extend generic blocks to
13831 describe ranges. */
13832 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13834 case PC_BOUNDS_NOT_PRESENT:
13835 /* DW_TAG_lexical_block has no attributes, process its children as if
13836 there was no wrapping by that DW_TAG_lexical_block.
13837 GCC does no longer produces such DWARF since GCC r224161. */
13838 for (child_die = die->child;
13839 child_die != NULL && child_die->tag;
13840 child_die = sibling_die (child_die))
13841 process_die (child_die, cu);
13843 case PC_BOUNDS_INVALID:
13846 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13847 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13849 push_context (0, lowpc);
13850 if (die->child != NULL)
13852 child_die = die->child;
13853 while (child_die && child_die->tag)
13855 process_die (child_die, cu);
13856 child_die = sibling_die (child_die);
13859 inherit_abstract_dies (die, cu);
13860 newobj = pop_context ();
13862 if (local_symbols != NULL || local_using_directives != NULL)
13864 struct block *block
13865 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
13866 newobj->start_addr, highpc);
13868 /* Note that recording ranges after traversing children, as we
13869 do here, means that recording a parent's ranges entails
13870 walking across all its children's ranges as they appear in
13871 the address map, which is quadratic behavior.
13873 It would be nicer to record the parent's ranges before
13874 traversing its children, simply overriding whatever you find
13875 there. But since we don't even decide whether to create a
13876 block until after we've traversed its children, that's hard
13878 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13880 local_symbols = newobj->locals;
13881 local_using_directives = newobj->local_using_directives;
13884 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13887 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13889 struct objfile *objfile = cu->objfile;
13890 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13891 CORE_ADDR pc, baseaddr;
13892 struct attribute *attr;
13893 struct call_site *call_site, call_site_local;
13896 struct die_info *child_die;
13898 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13900 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13903 /* This was a pre-DWARF-5 GNU extension alias
13904 for DW_AT_call_return_pc. */
13905 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13909 complaint (&symfile_complaints,
13910 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
13911 "DIE 0x%x [in module %s]"),
13912 to_underlying (die->sect_off), objfile_name (objfile));
13915 pc = attr_value_as_address (attr) + baseaddr;
13916 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13918 if (cu->call_site_htab == NULL)
13919 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13920 NULL, &objfile->objfile_obstack,
13921 hashtab_obstack_allocate, NULL);
13922 call_site_local.pc = pc;
13923 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13926 complaint (&symfile_complaints,
13927 _("Duplicate PC %s for DW_TAG_call_site "
13928 "DIE 0x%x [in module %s]"),
13929 paddress (gdbarch, pc), to_underlying (die->sect_off),
13930 objfile_name (objfile));
13934 /* Count parameters at the caller. */
13937 for (child_die = die->child; child_die && child_die->tag;
13938 child_die = sibling_die (child_die))
13940 if (child_die->tag != DW_TAG_call_site_parameter
13941 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13943 complaint (&symfile_complaints,
13944 _("Tag %d is not DW_TAG_call_site_parameter in "
13945 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
13946 child_die->tag, to_underlying (child_die->sect_off),
13947 objfile_name (objfile));
13955 = ((struct call_site *)
13956 obstack_alloc (&objfile->objfile_obstack,
13957 sizeof (*call_site)
13958 + (sizeof (*call_site->parameter) * (nparams - 1))));
13960 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13961 call_site->pc = pc;
13963 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13964 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13966 struct die_info *func_die;
13968 /* Skip also over DW_TAG_inlined_subroutine. */
13969 for (func_die = die->parent;
13970 func_die && func_die->tag != DW_TAG_subprogram
13971 && func_die->tag != DW_TAG_subroutine_type;
13972 func_die = func_die->parent);
13974 /* DW_AT_call_all_calls is a superset
13975 of DW_AT_call_all_tail_calls. */
13977 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13978 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13979 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13980 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13982 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13983 not complete. But keep CALL_SITE for look ups via call_site_htab,
13984 both the initial caller containing the real return address PC and
13985 the final callee containing the current PC of a chain of tail
13986 calls do not need to have the tail call list complete. But any
13987 function candidate for a virtual tail call frame searched via
13988 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13989 determined unambiguously. */
13993 struct type *func_type = NULL;
13996 func_type = get_die_type (func_die, cu);
13997 if (func_type != NULL)
13999 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14001 /* Enlist this call site to the function. */
14002 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14003 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14006 complaint (&symfile_complaints,
14007 _("Cannot find function owning DW_TAG_call_site "
14008 "DIE 0x%x [in module %s]"),
14009 to_underlying (die->sect_off), objfile_name (objfile));
14013 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14015 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14017 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14020 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14021 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14023 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14024 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14025 /* Keep NULL DWARF_BLOCK. */;
14026 else if (attr_form_is_block (attr))
14028 struct dwarf2_locexpr_baton *dlbaton;
14030 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14031 dlbaton->data = DW_BLOCK (attr)->data;
14032 dlbaton->size = DW_BLOCK (attr)->size;
14033 dlbaton->per_cu = cu->per_cu;
14035 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14037 else if (attr_form_is_ref (attr))
14039 struct dwarf2_cu *target_cu = cu;
14040 struct die_info *target_die;
14042 target_die = follow_die_ref (die, attr, &target_cu);
14043 gdb_assert (target_cu->objfile == objfile);
14044 if (die_is_declaration (target_die, target_cu))
14046 const char *target_physname;
14048 /* Prefer the mangled name; otherwise compute the demangled one. */
14049 target_physname = dw2_linkage_name (target_die, target_cu);
14050 if (target_physname == NULL)
14051 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14052 if (target_physname == NULL)
14053 complaint (&symfile_complaints,
14054 _("DW_AT_call_target target DIE has invalid "
14055 "physname, for referencing DIE 0x%x [in module %s]"),
14056 to_underlying (die->sect_off), objfile_name (objfile));
14058 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14064 /* DW_AT_entry_pc should be preferred. */
14065 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14066 <= PC_BOUNDS_INVALID)
14067 complaint (&symfile_complaints,
14068 _("DW_AT_call_target target DIE has invalid "
14069 "low pc, for referencing DIE 0x%x [in module %s]"),
14070 to_underlying (die->sect_off), objfile_name (objfile));
14073 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14074 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14079 complaint (&symfile_complaints,
14080 _("DW_TAG_call_site DW_AT_call_target is neither "
14081 "block nor reference, for DIE 0x%x [in module %s]"),
14082 to_underlying (die->sect_off), objfile_name (objfile));
14084 call_site->per_cu = cu->per_cu;
14086 for (child_die = die->child;
14087 child_die && child_die->tag;
14088 child_die = sibling_die (child_die))
14090 struct call_site_parameter *parameter;
14091 struct attribute *loc, *origin;
14093 if (child_die->tag != DW_TAG_call_site_parameter
14094 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14096 /* Already printed the complaint above. */
14100 gdb_assert (call_site->parameter_count < nparams);
14101 parameter = &call_site->parameter[call_site->parameter_count];
14103 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14104 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14105 register is contained in DW_AT_call_value. */
14107 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14108 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14109 if (origin == NULL)
14111 /* This was a pre-DWARF-5 GNU extension alias
14112 for DW_AT_call_parameter. */
14113 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14115 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14117 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14119 sect_offset sect_off
14120 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14121 if (!offset_in_cu_p (&cu->header, sect_off))
14123 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14124 binding can be done only inside one CU. Such referenced DIE
14125 therefore cannot be even moved to DW_TAG_partial_unit. */
14126 complaint (&symfile_complaints,
14127 _("DW_AT_call_parameter offset is not in CU for "
14128 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14129 to_underlying (child_die->sect_off),
14130 objfile_name (objfile));
14133 parameter->u.param_cu_off
14134 = (cu_offset) (sect_off - cu->header.sect_off);
14136 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14138 complaint (&symfile_complaints,
14139 _("No DW_FORM_block* DW_AT_location for "
14140 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14141 to_underlying (child_die->sect_off), objfile_name (objfile));
14146 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14147 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14148 if (parameter->u.dwarf_reg != -1)
14149 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14150 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14151 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14152 ¶meter->u.fb_offset))
14153 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14156 complaint (&symfile_complaints,
14157 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14158 "for DW_FORM_block* DW_AT_location is supported for "
14159 "DW_TAG_call_site child DIE 0x%x "
14161 to_underlying (child_die->sect_off),
14162 objfile_name (objfile));
14167 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14169 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14170 if (!attr_form_is_block (attr))
14172 complaint (&symfile_complaints,
14173 _("No DW_FORM_block* DW_AT_call_value for "
14174 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14175 to_underlying (child_die->sect_off),
14176 objfile_name (objfile));
14179 parameter->value = DW_BLOCK (attr)->data;
14180 parameter->value_size = DW_BLOCK (attr)->size;
14182 /* Parameters are not pre-cleared by memset above. */
14183 parameter->data_value = NULL;
14184 parameter->data_value_size = 0;
14185 call_site->parameter_count++;
14187 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14189 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14192 if (!attr_form_is_block (attr))
14193 complaint (&symfile_complaints,
14194 _("No DW_FORM_block* DW_AT_call_data_value for "
14195 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14196 to_underlying (child_die->sect_off),
14197 objfile_name (objfile));
14200 parameter->data_value = DW_BLOCK (attr)->data;
14201 parameter->data_value_size = DW_BLOCK (attr)->size;
14207 /* Helper function for read_variable. If DIE represents a virtual
14208 table, then return the type of the concrete object that is
14209 associated with the virtual table. Otherwise, return NULL. */
14211 static struct type *
14212 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14214 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14218 /* Find the type DIE. */
14219 struct die_info *type_die = NULL;
14220 struct dwarf2_cu *type_cu = cu;
14222 if (attr_form_is_ref (attr))
14223 type_die = follow_die_ref (die, attr, &type_cu);
14224 if (type_die == NULL)
14227 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14229 return die_containing_type (type_die, type_cu);
14232 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14235 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14237 struct rust_vtable_symbol *storage = NULL;
14239 if (cu->language == language_rust)
14241 struct type *containing_type = rust_containing_type (die, cu);
14243 if (containing_type != NULL)
14245 struct objfile *objfile = cu->objfile;
14247 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14248 struct rust_vtable_symbol);
14249 initialize_objfile_symbol (storage);
14250 storage->concrete_type = containing_type;
14251 storage->subclass = SYMBOL_RUST_VTABLE;
14255 new_symbol_full (die, NULL, cu, storage);
14258 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14259 reading .debug_rnglists.
14260 Callback's type should be:
14261 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14262 Return true if the attributes are present and valid, otherwise,
14265 template <typename Callback>
14267 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14268 Callback &&callback)
14270 struct objfile *objfile = cu->objfile;
14271 bfd *obfd = objfile->obfd;
14272 /* Base address selection entry. */
14275 const gdb_byte *buffer;
14276 CORE_ADDR baseaddr;
14277 bool overflow = false;
14279 found_base = cu->base_known;
14280 base = cu->base_address;
14282 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14283 if (offset >= dwarf2_per_objfile->rnglists.size)
14285 complaint (&symfile_complaints,
14286 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14290 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14292 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14296 /* Initialize it due to a false compiler warning. */
14297 CORE_ADDR range_beginning = 0, range_end = 0;
14298 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14299 + dwarf2_per_objfile->rnglists.size);
14300 unsigned int bytes_read;
14302 if (buffer == buf_end)
14307 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14310 case DW_RLE_end_of_list:
14312 case DW_RLE_base_address:
14313 if (buffer + cu->header.addr_size > buf_end)
14318 base = read_address (obfd, buffer, cu, &bytes_read);
14320 buffer += bytes_read;
14322 case DW_RLE_start_length:
14323 if (buffer + cu->header.addr_size > buf_end)
14328 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14329 buffer += bytes_read;
14330 range_end = (range_beginning
14331 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14332 buffer += bytes_read;
14333 if (buffer > buf_end)
14339 case DW_RLE_offset_pair:
14340 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14341 buffer += bytes_read;
14342 if (buffer > buf_end)
14347 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14348 buffer += bytes_read;
14349 if (buffer > buf_end)
14355 case DW_RLE_start_end:
14356 if (buffer + 2 * cu->header.addr_size > buf_end)
14361 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14362 buffer += bytes_read;
14363 range_end = read_address (obfd, buffer, cu, &bytes_read);
14364 buffer += bytes_read;
14367 complaint (&symfile_complaints,
14368 _("Invalid .debug_rnglists data (no base address)"));
14371 if (rlet == DW_RLE_end_of_list || overflow)
14373 if (rlet == DW_RLE_base_address)
14378 /* We have no valid base address for the ranges
14380 complaint (&symfile_complaints,
14381 _("Invalid .debug_rnglists data (no base address)"));
14385 if (range_beginning > range_end)
14387 /* Inverted range entries are invalid. */
14388 complaint (&symfile_complaints,
14389 _("Invalid .debug_rnglists data (inverted range)"));
14393 /* Empty range entries have no effect. */
14394 if (range_beginning == range_end)
14397 range_beginning += base;
14400 /* A not-uncommon case of bad debug info.
14401 Don't pollute the addrmap with bad data. */
14402 if (range_beginning + baseaddr == 0
14403 && !dwarf2_per_objfile->has_section_at_zero)
14405 complaint (&symfile_complaints,
14406 _(".debug_rnglists entry has start address of zero"
14407 " [in module %s]"), objfile_name (objfile));
14411 callback (range_beginning, range_end);
14416 complaint (&symfile_complaints,
14417 _("Offset %d is not terminated "
14418 "for DW_AT_ranges attribute"),
14426 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14427 Callback's type should be:
14428 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14429 Return 1 if the attributes are present and valid, otherwise, return 0. */
14431 template <typename Callback>
14433 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14434 Callback &&callback)
14436 struct objfile *objfile = cu->objfile;
14437 struct comp_unit_head *cu_header = &cu->header;
14438 bfd *obfd = objfile->obfd;
14439 unsigned int addr_size = cu_header->addr_size;
14440 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14441 /* Base address selection entry. */
14444 unsigned int dummy;
14445 const gdb_byte *buffer;
14446 CORE_ADDR baseaddr;
14448 if (cu_header->version >= 5)
14449 return dwarf2_rnglists_process (offset, cu, callback);
14451 found_base = cu->base_known;
14452 base = cu->base_address;
14454 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14455 if (offset >= dwarf2_per_objfile->ranges.size)
14457 complaint (&symfile_complaints,
14458 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14462 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14464 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14468 CORE_ADDR range_beginning, range_end;
14470 range_beginning = read_address (obfd, buffer, cu, &dummy);
14471 buffer += addr_size;
14472 range_end = read_address (obfd, buffer, cu, &dummy);
14473 buffer += addr_size;
14474 offset += 2 * addr_size;
14476 /* An end of list marker is a pair of zero addresses. */
14477 if (range_beginning == 0 && range_end == 0)
14478 /* Found the end of list entry. */
14481 /* Each base address selection entry is a pair of 2 values.
14482 The first is the largest possible address, the second is
14483 the base address. Check for a base address here. */
14484 if ((range_beginning & mask) == mask)
14486 /* If we found the largest possible address, then we already
14487 have the base address in range_end. */
14495 /* We have no valid base address for the ranges
14497 complaint (&symfile_complaints,
14498 _("Invalid .debug_ranges data (no base address)"));
14502 if (range_beginning > range_end)
14504 /* Inverted range entries are invalid. */
14505 complaint (&symfile_complaints,
14506 _("Invalid .debug_ranges data (inverted range)"));
14510 /* Empty range entries have no effect. */
14511 if (range_beginning == range_end)
14514 range_beginning += base;
14517 /* A not-uncommon case of bad debug info.
14518 Don't pollute the addrmap with bad data. */
14519 if (range_beginning + baseaddr == 0
14520 && !dwarf2_per_objfile->has_section_at_zero)
14522 complaint (&symfile_complaints,
14523 _(".debug_ranges entry has start address of zero"
14524 " [in module %s]"), objfile_name (objfile));
14528 callback (range_beginning, range_end);
14534 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14535 Return 1 if the attributes are present and valid, otherwise, return 0.
14536 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14539 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14540 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14541 struct partial_symtab *ranges_pst)
14543 struct objfile *objfile = cu->objfile;
14544 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14545 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14546 SECT_OFF_TEXT (objfile));
14549 CORE_ADDR high = 0;
14552 retval = dwarf2_ranges_process (offset, cu,
14553 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14555 if (ranges_pst != NULL)
14560 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14561 range_beginning + baseaddr);
14562 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14563 range_end + baseaddr);
14564 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14568 /* FIXME: This is recording everything as a low-high
14569 segment of consecutive addresses. We should have a
14570 data structure for discontiguous block ranges
14574 low = range_beginning;
14580 if (range_beginning < low)
14581 low = range_beginning;
14582 if (range_end > high)
14590 /* If the first entry is an end-of-list marker, the range
14591 describes an empty scope, i.e. no instructions. */
14597 *high_return = high;
14601 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14602 definition for the return value. *LOWPC and *HIGHPC are set iff
14603 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14605 static enum pc_bounds_kind
14606 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14607 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14608 struct partial_symtab *pst)
14610 struct attribute *attr;
14611 struct attribute *attr_high;
14613 CORE_ADDR high = 0;
14614 enum pc_bounds_kind ret;
14616 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14619 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14622 low = attr_value_as_address (attr);
14623 high = attr_value_as_address (attr_high);
14624 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14628 /* Found high w/o low attribute. */
14629 return PC_BOUNDS_INVALID;
14631 /* Found consecutive range of addresses. */
14632 ret = PC_BOUNDS_HIGH_LOW;
14636 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14639 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14640 We take advantage of the fact that DW_AT_ranges does not appear
14641 in DW_TAG_compile_unit of DWO files. */
14642 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14643 unsigned int ranges_offset = (DW_UNSND (attr)
14644 + (need_ranges_base
14648 /* Value of the DW_AT_ranges attribute is the offset in the
14649 .debug_ranges section. */
14650 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14651 return PC_BOUNDS_INVALID;
14652 /* Found discontinuous range of addresses. */
14653 ret = PC_BOUNDS_RANGES;
14656 return PC_BOUNDS_NOT_PRESENT;
14659 /* read_partial_die has also the strict LOW < HIGH requirement. */
14661 return PC_BOUNDS_INVALID;
14663 /* When using the GNU linker, .gnu.linkonce. sections are used to
14664 eliminate duplicate copies of functions and vtables and such.
14665 The linker will arbitrarily choose one and discard the others.
14666 The AT_*_pc values for such functions refer to local labels in
14667 these sections. If the section from that file was discarded, the
14668 labels are not in the output, so the relocs get a value of 0.
14669 If this is a discarded function, mark the pc bounds as invalid,
14670 so that GDB will ignore it. */
14671 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14672 return PC_BOUNDS_INVALID;
14680 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14681 its low and high PC addresses. Do nothing if these addresses could not
14682 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14683 and HIGHPC to the high address if greater than HIGHPC. */
14686 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14687 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14688 struct dwarf2_cu *cu)
14690 CORE_ADDR low, high;
14691 struct die_info *child = die->child;
14693 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14695 *lowpc = std::min (*lowpc, low);
14696 *highpc = std::max (*highpc, high);
14699 /* If the language does not allow nested subprograms (either inside
14700 subprograms or lexical blocks), we're done. */
14701 if (cu->language != language_ada)
14704 /* Check all the children of the given DIE. If it contains nested
14705 subprograms, then check their pc bounds. Likewise, we need to
14706 check lexical blocks as well, as they may also contain subprogram
14708 while (child && child->tag)
14710 if (child->tag == DW_TAG_subprogram
14711 || child->tag == DW_TAG_lexical_block)
14712 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14713 child = sibling_die (child);
14717 /* Get the low and high pc's represented by the scope DIE, and store
14718 them in *LOWPC and *HIGHPC. If the correct values can't be
14719 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14722 get_scope_pc_bounds (struct die_info *die,
14723 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14724 struct dwarf2_cu *cu)
14726 CORE_ADDR best_low = (CORE_ADDR) -1;
14727 CORE_ADDR best_high = (CORE_ADDR) 0;
14728 CORE_ADDR current_low, current_high;
14730 if (dwarf2_get_pc_bounds (die, ¤t_low, ¤t_high, cu, NULL)
14731 >= PC_BOUNDS_RANGES)
14733 best_low = current_low;
14734 best_high = current_high;
14738 struct die_info *child = die->child;
14740 while (child && child->tag)
14742 switch (child->tag) {
14743 case DW_TAG_subprogram:
14744 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14746 case DW_TAG_namespace:
14747 case DW_TAG_module:
14748 /* FIXME: carlton/2004-01-16: Should we do this for
14749 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14750 that current GCC's always emit the DIEs corresponding
14751 to definitions of methods of classes as children of a
14752 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14753 the DIEs giving the declarations, which could be
14754 anywhere). But I don't see any reason why the
14755 standards says that they have to be there. */
14756 get_scope_pc_bounds (child, ¤t_low, ¤t_high, cu);
14758 if (current_low != ((CORE_ADDR) -1))
14760 best_low = std::min (best_low, current_low);
14761 best_high = std::max (best_high, current_high);
14769 child = sibling_die (child);
14774 *highpc = best_high;
14777 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14781 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14782 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14784 struct objfile *objfile = cu->objfile;
14785 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14786 struct attribute *attr;
14787 struct attribute *attr_high;
14789 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14792 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14795 CORE_ADDR low = attr_value_as_address (attr);
14796 CORE_ADDR high = attr_value_as_address (attr_high);
14798 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14801 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14802 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14803 record_block_range (block, low, high - 1);
14807 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14810 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14811 We take advantage of the fact that DW_AT_ranges does not appear
14812 in DW_TAG_compile_unit of DWO files. */
14813 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14815 /* The value of the DW_AT_ranges attribute is the offset of the
14816 address range list in the .debug_ranges section. */
14817 unsigned long offset = (DW_UNSND (attr)
14818 + (need_ranges_base ? cu->ranges_base : 0));
14819 const gdb_byte *buffer;
14821 /* For some target architectures, but not others, the
14822 read_address function sign-extends the addresses it returns.
14823 To recognize base address selection entries, we need a
14825 unsigned int addr_size = cu->header.addr_size;
14826 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14828 /* The base address, to which the next pair is relative. Note
14829 that this 'base' is a DWARF concept: most entries in a range
14830 list are relative, to reduce the number of relocs against the
14831 debugging information. This is separate from this function's
14832 'baseaddr' argument, which GDB uses to relocate debugging
14833 information from a shared library based on the address at
14834 which the library was loaded. */
14835 CORE_ADDR base = cu->base_address;
14836 int base_known = cu->base_known;
14838 dwarf2_ranges_process (offset, cu,
14839 [&] (CORE_ADDR start, CORE_ADDR end)
14843 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14844 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14845 record_block_range (block, start, end - 1);
14850 /* Check whether the producer field indicates either of GCC < 4.6, or the
14851 Intel C/C++ compiler, and cache the result in CU. */
14854 check_producer (struct dwarf2_cu *cu)
14858 if (cu->producer == NULL)
14860 /* For unknown compilers expect their behavior is DWARF version
14863 GCC started to support .debug_types sections by -gdwarf-4 since
14864 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14865 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14866 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14867 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14869 else if (producer_is_gcc (cu->producer, &major, &minor))
14871 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14872 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14874 else if (producer_is_icc (cu->producer, &major, &minor))
14875 cu->producer_is_icc_lt_14 = major < 14;
14878 /* For other non-GCC compilers, expect their behavior is DWARF version
14882 cu->checked_producer = 1;
14885 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14886 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14887 during 4.6.0 experimental. */
14890 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14892 if (!cu->checked_producer)
14893 check_producer (cu);
14895 return cu->producer_is_gxx_lt_4_6;
14898 /* Return the default accessibility type if it is not overriden by
14899 DW_AT_accessibility. */
14901 static enum dwarf_access_attribute
14902 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14904 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14906 /* The default DWARF 2 accessibility for members is public, the default
14907 accessibility for inheritance is private. */
14909 if (die->tag != DW_TAG_inheritance)
14910 return DW_ACCESS_public;
14912 return DW_ACCESS_private;
14916 /* DWARF 3+ defines the default accessibility a different way. The same
14917 rules apply now for DW_TAG_inheritance as for the members and it only
14918 depends on the container kind. */
14920 if (die->parent->tag == DW_TAG_class_type)
14921 return DW_ACCESS_private;
14923 return DW_ACCESS_public;
14927 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14928 offset. If the attribute was not found return 0, otherwise return
14929 1. If it was found but could not properly be handled, set *OFFSET
14933 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14936 struct attribute *attr;
14938 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14943 /* Note that we do not check for a section offset first here.
14944 This is because DW_AT_data_member_location is new in DWARF 4,
14945 so if we see it, we can assume that a constant form is really
14946 a constant and not a section offset. */
14947 if (attr_form_is_constant (attr))
14948 *offset = dwarf2_get_attr_constant_value (attr, 0);
14949 else if (attr_form_is_section_offset (attr))
14950 dwarf2_complex_location_expr_complaint ();
14951 else if (attr_form_is_block (attr))
14952 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14954 dwarf2_complex_location_expr_complaint ();
14962 /* Add an aggregate field to the field list. */
14965 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14966 struct dwarf2_cu *cu)
14968 struct objfile *objfile = cu->objfile;
14969 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14970 struct nextfield *new_field;
14971 struct attribute *attr;
14973 const char *fieldname = "";
14975 /* Allocate a new field list entry and link it in. */
14976 new_field = XNEW (struct nextfield);
14977 make_cleanup (xfree, new_field);
14978 memset (new_field, 0, sizeof (struct nextfield));
14980 if (die->tag == DW_TAG_inheritance)
14982 new_field->next = fip->baseclasses;
14983 fip->baseclasses = new_field;
14987 new_field->next = fip->fields;
14988 fip->fields = new_field;
14992 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14994 new_field->accessibility = DW_UNSND (attr);
14996 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14997 if (new_field->accessibility != DW_ACCESS_public)
14998 fip->non_public_fields = 1;
15000 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15002 new_field->virtuality = DW_UNSND (attr);
15004 new_field->virtuality = DW_VIRTUALITY_none;
15006 fp = &new_field->field;
15008 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15012 /* Data member other than a C++ static data member. */
15014 /* Get type of field. */
15015 fp->type = die_type (die, cu);
15017 SET_FIELD_BITPOS (*fp, 0);
15019 /* Get bit size of field (zero if none). */
15020 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15023 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15027 FIELD_BITSIZE (*fp) = 0;
15030 /* Get bit offset of field. */
15031 if (handle_data_member_location (die, cu, &offset))
15032 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15033 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15036 if (gdbarch_bits_big_endian (gdbarch))
15038 /* For big endian bits, the DW_AT_bit_offset gives the
15039 additional bit offset from the MSB of the containing
15040 anonymous object to the MSB of the field. We don't
15041 have to do anything special since we don't need to
15042 know the size of the anonymous object. */
15043 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15047 /* For little endian bits, compute the bit offset to the
15048 MSB of the anonymous object, subtract off the number of
15049 bits from the MSB of the field to the MSB of the
15050 object, and then subtract off the number of bits of
15051 the field itself. The result is the bit offset of
15052 the LSB of the field. */
15053 int anonymous_size;
15054 int bit_offset = DW_UNSND (attr);
15056 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15059 /* The size of the anonymous object containing
15060 the bit field is explicit, so use the
15061 indicated size (in bytes). */
15062 anonymous_size = DW_UNSND (attr);
15066 /* The size of the anonymous object containing
15067 the bit field must be inferred from the type
15068 attribute of the data member containing the
15070 anonymous_size = TYPE_LENGTH (fp->type);
15072 SET_FIELD_BITPOS (*fp,
15073 (FIELD_BITPOS (*fp)
15074 + anonymous_size * bits_per_byte
15075 - bit_offset - FIELD_BITSIZE (*fp)));
15078 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15080 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15081 + dwarf2_get_attr_constant_value (attr, 0)));
15083 /* Get name of field. */
15084 fieldname = dwarf2_name (die, cu);
15085 if (fieldname == NULL)
15088 /* The name is already allocated along with this objfile, so we don't
15089 need to duplicate it for the type. */
15090 fp->name = fieldname;
15092 /* Change accessibility for artificial fields (e.g. virtual table
15093 pointer or virtual base class pointer) to private. */
15094 if (dwarf2_attr (die, DW_AT_artificial, cu))
15096 FIELD_ARTIFICIAL (*fp) = 1;
15097 new_field->accessibility = DW_ACCESS_private;
15098 fip->non_public_fields = 1;
15101 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15103 /* C++ static member. */
15105 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15106 is a declaration, but all versions of G++ as of this writing
15107 (so through at least 3.2.1) incorrectly generate
15108 DW_TAG_variable tags. */
15110 const char *physname;
15112 /* Get name of field. */
15113 fieldname = dwarf2_name (die, cu);
15114 if (fieldname == NULL)
15117 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15119 /* Only create a symbol if this is an external value.
15120 new_symbol checks this and puts the value in the global symbol
15121 table, which we want. If it is not external, new_symbol
15122 will try to put the value in cu->list_in_scope which is wrong. */
15123 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15125 /* A static const member, not much different than an enum as far as
15126 we're concerned, except that we can support more types. */
15127 new_symbol (die, NULL, cu);
15130 /* Get physical name. */
15131 physname = dwarf2_physname (fieldname, die, cu);
15133 /* The name is already allocated along with this objfile, so we don't
15134 need to duplicate it for the type. */
15135 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15136 FIELD_TYPE (*fp) = die_type (die, cu);
15137 FIELD_NAME (*fp) = fieldname;
15139 else if (die->tag == DW_TAG_inheritance)
15143 /* C++ base class field. */
15144 if (handle_data_member_location (die, cu, &offset))
15145 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15146 FIELD_BITSIZE (*fp) = 0;
15147 FIELD_TYPE (*fp) = die_type (die, cu);
15148 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15149 fip->nbaseclasses++;
15153 /* Can the type given by DIE define another type? */
15156 type_can_define_types (const struct die_info *die)
15160 case DW_TAG_typedef:
15161 case DW_TAG_class_type:
15162 case DW_TAG_structure_type:
15163 case DW_TAG_union_type:
15164 case DW_TAG_enumeration_type:
15172 /* Add a type definition defined in the scope of the FIP's class. */
15175 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15176 struct dwarf2_cu *cu)
15178 struct decl_field_list *new_field;
15179 struct decl_field *fp;
15181 /* Allocate a new field list entry and link it in. */
15182 new_field = XCNEW (struct decl_field_list);
15183 make_cleanup (xfree, new_field);
15185 gdb_assert (type_can_define_types (die));
15187 fp = &new_field->field;
15189 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15190 fp->name = dwarf2_name (die, cu);
15191 fp->type = read_type_die (die, cu);
15193 /* Save accessibility. */
15194 enum dwarf_access_attribute accessibility;
15195 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15197 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15199 accessibility = dwarf2_default_access_attribute (die, cu);
15200 switch (accessibility)
15202 case DW_ACCESS_public:
15203 /* The assumed value if neither private nor protected. */
15205 case DW_ACCESS_private:
15206 fp->is_private = 1;
15208 case DW_ACCESS_protected:
15209 fp->is_protected = 1;
15212 complaint (&symfile_complaints,
15213 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15216 if (die->tag == DW_TAG_typedef)
15218 new_field->next = fip->typedef_field_list;
15219 fip->typedef_field_list = new_field;
15220 fip->typedef_field_list_count++;
15224 new_field->next = fip->nested_types_list;
15225 fip->nested_types_list = new_field;
15226 fip->nested_types_list_count++;
15230 /* Create the vector of fields, and attach it to the type. */
15233 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15234 struct dwarf2_cu *cu)
15236 int nfields = fip->nfields;
15238 /* Record the field count, allocate space for the array of fields,
15239 and create blank accessibility bitfields if necessary. */
15240 TYPE_NFIELDS (type) = nfields;
15241 TYPE_FIELDS (type) = (struct field *)
15242 TYPE_ALLOC (type, sizeof (struct field) * nfields);
15243 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
15245 if (fip->non_public_fields && cu->language != language_ada)
15247 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15249 TYPE_FIELD_PRIVATE_BITS (type) =
15250 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15251 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15253 TYPE_FIELD_PROTECTED_BITS (type) =
15254 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15255 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15257 TYPE_FIELD_IGNORE_BITS (type) =
15258 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15259 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15262 /* If the type has baseclasses, allocate and clear a bit vector for
15263 TYPE_FIELD_VIRTUAL_BITS. */
15264 if (fip->nbaseclasses && cu->language != language_ada)
15266 int num_bytes = B_BYTES (fip->nbaseclasses);
15267 unsigned char *pointer;
15269 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15270 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15271 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15272 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
15273 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
15276 /* Copy the saved-up fields into the field vector. Start from the head of
15277 the list, adding to the tail of the field array, so that they end up in
15278 the same order in the array in which they were added to the list. */
15279 while (nfields-- > 0)
15281 struct nextfield *fieldp;
15285 fieldp = fip->fields;
15286 fip->fields = fieldp->next;
15290 fieldp = fip->baseclasses;
15291 fip->baseclasses = fieldp->next;
15294 TYPE_FIELD (type, nfields) = fieldp->field;
15295 switch (fieldp->accessibility)
15297 case DW_ACCESS_private:
15298 if (cu->language != language_ada)
15299 SET_TYPE_FIELD_PRIVATE (type, nfields);
15302 case DW_ACCESS_protected:
15303 if (cu->language != language_ada)
15304 SET_TYPE_FIELD_PROTECTED (type, nfields);
15307 case DW_ACCESS_public:
15311 /* Unknown accessibility. Complain and treat it as public. */
15313 complaint (&symfile_complaints, _("unsupported accessibility %d"),
15314 fieldp->accessibility);
15318 if (nfields < fip->nbaseclasses)
15320 switch (fieldp->virtuality)
15322 case DW_VIRTUALITY_virtual:
15323 case DW_VIRTUALITY_pure_virtual:
15324 if (cu->language == language_ada)
15325 error (_("unexpected virtuality in component of Ada type"));
15326 SET_TYPE_FIELD_VIRTUAL (type, nfields);
15333 /* Return true if this member function is a constructor, false
15337 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15339 const char *fieldname;
15340 const char *type_name;
15343 if (die->parent == NULL)
15346 if (die->parent->tag != DW_TAG_structure_type
15347 && die->parent->tag != DW_TAG_union_type
15348 && die->parent->tag != DW_TAG_class_type)
15351 fieldname = dwarf2_name (die, cu);
15352 type_name = dwarf2_name (die->parent, cu);
15353 if (fieldname == NULL || type_name == NULL)
15356 len = strlen (fieldname);
15357 return (strncmp (fieldname, type_name, len) == 0
15358 && (type_name[len] == '\0' || type_name[len] == '<'));
15361 /* Add a member function to the proper fieldlist. */
15364 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15365 struct type *type, struct dwarf2_cu *cu)
15367 struct objfile *objfile = cu->objfile;
15368 struct attribute *attr;
15369 struct fnfieldlist *flp;
15371 struct fn_field *fnp;
15372 const char *fieldname;
15373 struct nextfnfield *new_fnfield;
15374 struct type *this_type;
15375 enum dwarf_access_attribute accessibility;
15377 if (cu->language == language_ada)
15378 error (_("unexpected member function in Ada type"));
15380 /* Get name of member function. */
15381 fieldname = dwarf2_name (die, cu);
15382 if (fieldname == NULL)
15385 /* Look up member function name in fieldlist. */
15386 for (i = 0; i < fip->nfnfields; i++)
15388 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15392 /* Create new list element if necessary. */
15393 if (i < fip->nfnfields)
15394 flp = &fip->fnfieldlists[i];
15397 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
15399 fip->fnfieldlists = (struct fnfieldlist *)
15400 xrealloc (fip->fnfieldlists,
15401 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
15402 * sizeof (struct fnfieldlist));
15403 if (fip->nfnfields == 0)
15404 make_cleanup (free_current_contents, &fip->fnfieldlists);
15406 flp = &fip->fnfieldlists[fip->nfnfields];
15407 flp->name = fieldname;
15410 i = fip->nfnfields++;
15413 /* Create a new member function field and chain it to the field list
15415 new_fnfield = XNEW (struct nextfnfield);
15416 make_cleanup (xfree, new_fnfield);
15417 memset (new_fnfield, 0, sizeof (struct nextfnfield));
15418 new_fnfield->next = flp->head;
15419 flp->head = new_fnfield;
15422 /* Fill in the member function field info. */
15423 fnp = &new_fnfield->fnfield;
15425 /* Delay processing of the physname until later. */
15426 if (cu->language == language_cplus)
15428 add_to_method_list (type, i, flp->length - 1, fieldname,
15433 const char *physname = dwarf2_physname (fieldname, die, cu);
15434 fnp->physname = physname ? physname : "";
15437 fnp->type = alloc_type (objfile);
15438 this_type = read_type_die (die, cu);
15439 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15441 int nparams = TYPE_NFIELDS (this_type);
15443 /* TYPE is the domain of this method, and THIS_TYPE is the type
15444 of the method itself (TYPE_CODE_METHOD). */
15445 smash_to_method_type (fnp->type, type,
15446 TYPE_TARGET_TYPE (this_type),
15447 TYPE_FIELDS (this_type),
15448 TYPE_NFIELDS (this_type),
15449 TYPE_VARARGS (this_type));
15451 /* Handle static member functions.
15452 Dwarf2 has no clean way to discern C++ static and non-static
15453 member functions. G++ helps GDB by marking the first
15454 parameter for non-static member functions (which is the this
15455 pointer) as artificial. We obtain this information from
15456 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15457 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15458 fnp->voffset = VOFFSET_STATIC;
15461 complaint (&symfile_complaints, _("member function type missing for '%s'"),
15462 dwarf2_full_name (fieldname, die, cu));
15464 /* Get fcontext from DW_AT_containing_type if present. */
15465 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15466 fnp->fcontext = die_containing_type (die, cu);
15468 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15469 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15471 /* Get accessibility. */
15472 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15474 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15476 accessibility = dwarf2_default_access_attribute (die, cu);
15477 switch (accessibility)
15479 case DW_ACCESS_private:
15480 fnp->is_private = 1;
15482 case DW_ACCESS_protected:
15483 fnp->is_protected = 1;
15487 /* Check for artificial methods. */
15488 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15489 if (attr && DW_UNSND (attr) != 0)
15490 fnp->is_artificial = 1;
15492 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15494 /* Get index in virtual function table if it is a virtual member
15495 function. For older versions of GCC, this is an offset in the
15496 appropriate virtual table, as specified by DW_AT_containing_type.
15497 For everyone else, it is an expression to be evaluated relative
15498 to the object address. */
15500 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15503 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15505 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15507 /* Old-style GCC. */
15508 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15510 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15511 || (DW_BLOCK (attr)->size > 1
15512 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15513 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15515 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15516 if ((fnp->voffset % cu->header.addr_size) != 0)
15517 dwarf2_complex_location_expr_complaint ();
15519 fnp->voffset /= cu->header.addr_size;
15523 dwarf2_complex_location_expr_complaint ();
15525 if (!fnp->fcontext)
15527 /* If there is no `this' field and no DW_AT_containing_type,
15528 we cannot actually find a base class context for the
15530 if (TYPE_NFIELDS (this_type) == 0
15531 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15533 complaint (&symfile_complaints,
15534 _("cannot determine context for virtual member "
15535 "function \"%s\" (offset %d)"),
15536 fieldname, to_underlying (die->sect_off));
15541 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15545 else if (attr_form_is_section_offset (attr))
15547 dwarf2_complex_location_expr_complaint ();
15551 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15557 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15558 if (attr && DW_UNSND (attr))
15560 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15561 complaint (&symfile_complaints,
15562 _("Member function \"%s\" (offset %d) is virtual "
15563 "but the vtable offset is not specified"),
15564 fieldname, to_underlying (die->sect_off));
15565 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15566 TYPE_CPLUS_DYNAMIC (type) = 1;
15571 /* Create the vector of member function fields, and attach it to the type. */
15574 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15575 struct dwarf2_cu *cu)
15577 struct fnfieldlist *flp;
15580 if (cu->language == language_ada)
15581 error (_("unexpected member functions in Ada type"));
15583 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15584 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15585 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
15587 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
15589 struct nextfnfield *nfp = flp->head;
15590 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15593 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
15594 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
15595 fn_flp->fn_fields = (struct fn_field *)
15596 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
15597 for (k = flp->length; (k--, nfp); nfp = nfp->next)
15598 fn_flp->fn_fields[k] = nfp->fnfield;
15601 TYPE_NFN_FIELDS (type) = fip->nfnfields;
15604 /* Returns non-zero if NAME is the name of a vtable member in CU's
15605 language, zero otherwise. */
15607 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15609 static const char vptr[] = "_vptr";
15611 /* Look for the C++ form of the vtable. */
15612 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15618 /* GCC outputs unnamed structures that are really pointers to member
15619 functions, with the ABI-specified layout. If TYPE describes
15620 such a structure, smash it into a member function type.
15622 GCC shouldn't do this; it should just output pointer to member DIEs.
15623 This is GCC PR debug/28767. */
15626 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15628 struct type *pfn_type, *self_type, *new_type;
15630 /* Check for a structure with no name and two children. */
15631 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15634 /* Check for __pfn and __delta members. */
15635 if (TYPE_FIELD_NAME (type, 0) == NULL
15636 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15637 || TYPE_FIELD_NAME (type, 1) == NULL
15638 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15641 /* Find the type of the method. */
15642 pfn_type = TYPE_FIELD_TYPE (type, 0);
15643 if (pfn_type == NULL
15644 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15645 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15648 /* Look for the "this" argument. */
15649 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15650 if (TYPE_NFIELDS (pfn_type) == 0
15651 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15652 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15655 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15656 new_type = alloc_type (objfile);
15657 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15658 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15659 TYPE_VARARGS (pfn_type));
15660 smash_to_methodptr_type (type, new_type);
15664 /* Called when we find the DIE that starts a structure or union scope
15665 (definition) to create a type for the structure or union. Fill in
15666 the type's name and general properties; the members will not be
15667 processed until process_structure_scope. A symbol table entry for
15668 the type will also not be done until process_structure_scope (assuming
15669 the type has a name).
15671 NOTE: we need to call these functions regardless of whether or not the
15672 DIE has a DW_AT_name attribute, since it might be an anonymous
15673 structure or union. This gets the type entered into our set of
15674 user defined types. */
15676 static struct type *
15677 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15679 struct objfile *objfile = cu->objfile;
15681 struct attribute *attr;
15684 /* If the definition of this type lives in .debug_types, read that type.
15685 Don't follow DW_AT_specification though, that will take us back up
15686 the chain and we want to go down. */
15687 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15690 type = get_DW_AT_signature_type (die, attr, cu);
15692 /* The type's CU may not be the same as CU.
15693 Ensure TYPE is recorded with CU in die_type_hash. */
15694 return set_die_type (die, type, cu);
15697 type = alloc_type (objfile);
15698 INIT_CPLUS_SPECIFIC (type);
15700 name = dwarf2_name (die, cu);
15703 if (cu->language == language_cplus
15704 || cu->language == language_d
15705 || cu->language == language_rust)
15707 const char *full_name = dwarf2_full_name (name, die, cu);
15709 /* dwarf2_full_name might have already finished building the DIE's
15710 type. If so, there is no need to continue. */
15711 if (get_die_type (die, cu) != NULL)
15712 return get_die_type (die, cu);
15714 TYPE_TAG_NAME (type) = full_name;
15715 if (die->tag == DW_TAG_structure_type
15716 || die->tag == DW_TAG_class_type)
15717 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15721 /* The name is already allocated along with this objfile, so
15722 we don't need to duplicate it for the type. */
15723 TYPE_TAG_NAME (type) = name;
15724 if (die->tag == DW_TAG_class_type)
15725 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15729 if (die->tag == DW_TAG_structure_type)
15731 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15733 else if (die->tag == DW_TAG_union_type)
15735 TYPE_CODE (type) = TYPE_CODE_UNION;
15739 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15742 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15743 TYPE_DECLARED_CLASS (type) = 1;
15745 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15748 if (attr_form_is_constant (attr))
15749 TYPE_LENGTH (type) = DW_UNSND (attr);
15752 /* For the moment, dynamic type sizes are not supported
15753 by GDB's struct type. The actual size is determined
15754 on-demand when resolving the type of a given object,
15755 so set the type's length to zero for now. Otherwise,
15756 we record an expression as the length, and that expression
15757 could lead to a very large value, which could eventually
15758 lead to us trying to allocate that much memory when creating
15759 a value of that type. */
15760 TYPE_LENGTH (type) = 0;
15765 TYPE_LENGTH (type) = 0;
15768 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15770 /* ICC<14 does not output the required DW_AT_declaration on
15771 incomplete types, but gives them a size of zero. */
15772 TYPE_STUB (type) = 1;
15775 TYPE_STUB_SUPPORTED (type) = 1;
15777 if (die_is_declaration (die, cu))
15778 TYPE_STUB (type) = 1;
15779 else if (attr == NULL && die->child == NULL
15780 && producer_is_realview (cu->producer))
15781 /* RealView does not output the required DW_AT_declaration
15782 on incomplete types. */
15783 TYPE_STUB (type) = 1;
15785 /* We need to add the type field to the die immediately so we don't
15786 infinitely recurse when dealing with pointers to the structure
15787 type within the structure itself. */
15788 set_die_type (die, type, cu);
15790 /* set_die_type should be already done. */
15791 set_descriptive_type (type, die, cu);
15796 /* Finish creating a structure or union type, including filling in
15797 its members and creating a symbol for it. */
15800 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15802 struct objfile *objfile = cu->objfile;
15803 struct die_info *child_die;
15806 type = get_die_type (die, cu);
15808 type = read_structure_type (die, cu);
15810 if (die->child != NULL && ! die_is_declaration (die, cu))
15812 struct field_info fi;
15813 std::vector<struct symbol *> template_args;
15814 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
15816 memset (&fi, 0, sizeof (struct field_info));
15818 child_die = die->child;
15820 while (child_die && child_die->tag)
15822 if (child_die->tag == DW_TAG_member
15823 || child_die->tag == DW_TAG_variable)
15825 /* NOTE: carlton/2002-11-05: A C++ static data member
15826 should be a DW_TAG_member that is a declaration, but
15827 all versions of G++ as of this writing (so through at
15828 least 3.2.1) incorrectly generate DW_TAG_variable
15829 tags for them instead. */
15830 dwarf2_add_field (&fi, child_die, cu);
15832 else if (child_die->tag == DW_TAG_subprogram)
15834 /* Rust doesn't have member functions in the C++ sense.
15835 However, it does emit ordinary functions as children
15836 of a struct DIE. */
15837 if (cu->language == language_rust)
15838 read_func_scope (child_die, cu);
15841 /* C++ member function. */
15842 dwarf2_add_member_fn (&fi, child_die, type, cu);
15845 else if (child_die->tag == DW_TAG_inheritance)
15847 /* C++ base class field. */
15848 dwarf2_add_field (&fi, child_die, cu);
15850 else if (type_can_define_types (child_die))
15851 dwarf2_add_type_defn (&fi, child_die, cu);
15852 else if (child_die->tag == DW_TAG_template_type_param
15853 || child_die->tag == DW_TAG_template_value_param)
15855 struct symbol *arg = new_symbol (child_die, NULL, cu);
15858 template_args.push_back (arg);
15861 child_die = sibling_die (child_die);
15864 /* Attach template arguments to type. */
15865 if (!template_args.empty ())
15867 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15868 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15869 TYPE_TEMPLATE_ARGUMENTS (type)
15870 = XOBNEWVEC (&objfile->objfile_obstack,
15872 TYPE_N_TEMPLATE_ARGUMENTS (type));
15873 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15874 template_args.data (),
15875 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15876 * sizeof (struct symbol *)));
15879 /* Attach fields and member functions to the type. */
15881 dwarf2_attach_fields_to_type (&fi, type, cu);
15884 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15886 /* Get the type which refers to the base class (possibly this
15887 class itself) which contains the vtable pointer for the current
15888 class from the DW_AT_containing_type attribute. This use of
15889 DW_AT_containing_type is a GNU extension. */
15891 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15893 struct type *t = die_containing_type (die, cu);
15895 set_type_vptr_basetype (type, t);
15900 /* Our own class provides vtbl ptr. */
15901 for (i = TYPE_NFIELDS (t) - 1;
15902 i >= TYPE_N_BASECLASSES (t);
15905 const char *fieldname = TYPE_FIELD_NAME (t, i);
15907 if (is_vtable_name (fieldname, cu))
15909 set_type_vptr_fieldno (type, i);
15914 /* Complain if virtual function table field not found. */
15915 if (i < TYPE_N_BASECLASSES (t))
15916 complaint (&symfile_complaints,
15917 _("virtual function table pointer "
15918 "not found when defining class '%s'"),
15919 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
15924 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15927 else if (cu->producer
15928 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15930 /* The IBM XLC compiler does not provide direct indication
15931 of the containing type, but the vtable pointer is
15932 always named __vfp. */
15936 for (i = TYPE_NFIELDS (type) - 1;
15937 i >= TYPE_N_BASECLASSES (type);
15940 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15942 set_type_vptr_fieldno (type, i);
15943 set_type_vptr_basetype (type, type);
15950 /* Copy fi.typedef_field_list linked list elements content into the
15951 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15952 if (fi.typedef_field_list)
15954 int i = fi.typedef_field_list_count;
15956 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15957 TYPE_TYPEDEF_FIELD_ARRAY (type)
15958 = ((struct decl_field *)
15959 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
15960 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
15962 /* Reverse the list order to keep the debug info elements order. */
15965 struct decl_field *dest, *src;
15967 dest = &TYPE_TYPEDEF_FIELD (type, i);
15968 src = &fi.typedef_field_list->field;
15969 fi.typedef_field_list = fi.typedef_field_list->next;
15974 /* Copy fi.nested_types_list linked list elements content into the
15975 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15976 if (fi.nested_types_list != NULL && cu->language != language_ada)
15978 int i = fi.nested_types_list_count;
15980 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15981 TYPE_NESTED_TYPES_ARRAY (type)
15982 = ((struct decl_field *)
15983 TYPE_ALLOC (type, sizeof (struct decl_field) * i));
15984 TYPE_NESTED_TYPES_COUNT (type) = i;
15986 /* Reverse the list order to keep the debug info elements order. */
15989 struct decl_field *dest, *src;
15991 dest = &TYPE_NESTED_TYPES_FIELD (type, i);
15992 src = &fi.nested_types_list->field;
15993 fi.nested_types_list = fi.nested_types_list->next;
15998 do_cleanups (back_to);
16001 quirk_gcc_member_function_pointer (type, objfile);
16003 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16004 snapshots) has been known to create a die giving a declaration
16005 for a class that has, as a child, a die giving a definition for a
16006 nested class. So we have to process our children even if the
16007 current die is a declaration. Normally, of course, a declaration
16008 won't have any children at all. */
16010 child_die = die->child;
16012 while (child_die != NULL && child_die->tag)
16014 if (child_die->tag == DW_TAG_member
16015 || child_die->tag == DW_TAG_variable
16016 || child_die->tag == DW_TAG_inheritance
16017 || child_die->tag == DW_TAG_template_value_param
16018 || child_die->tag == DW_TAG_template_type_param)
16023 process_die (child_die, cu);
16025 child_die = sibling_die (child_die);
16028 /* Do not consider external references. According to the DWARF standard,
16029 these DIEs are identified by the fact that they have no byte_size
16030 attribute, and a declaration attribute. */
16031 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16032 || !die_is_declaration (die, cu))
16033 new_symbol (die, type, cu);
16036 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16037 update TYPE using some information only available in DIE's children. */
16040 update_enumeration_type_from_children (struct die_info *die,
16042 struct dwarf2_cu *cu)
16044 struct die_info *child_die;
16045 int unsigned_enum = 1;
16049 auto_obstack obstack;
16051 for (child_die = die->child;
16052 child_die != NULL && child_die->tag;
16053 child_die = sibling_die (child_die))
16055 struct attribute *attr;
16057 const gdb_byte *bytes;
16058 struct dwarf2_locexpr_baton *baton;
16061 if (child_die->tag != DW_TAG_enumerator)
16064 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16068 name = dwarf2_name (child_die, cu);
16070 name = "<anonymous enumerator>";
16072 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16073 &value, &bytes, &baton);
16079 else if ((mask & value) != 0)
16084 /* If we already know that the enum type is neither unsigned, nor
16085 a flag type, no need to look at the rest of the enumerates. */
16086 if (!unsigned_enum && !flag_enum)
16091 TYPE_UNSIGNED (type) = 1;
16093 TYPE_FLAG_ENUM (type) = 1;
16096 /* Given a DW_AT_enumeration_type die, set its type. We do not
16097 complete the type's fields yet, or create any symbols. */
16099 static struct type *
16100 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16102 struct objfile *objfile = cu->objfile;
16104 struct attribute *attr;
16107 /* If the definition of this type lives in .debug_types, read that type.
16108 Don't follow DW_AT_specification though, that will take us back up
16109 the chain and we want to go down. */
16110 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16113 type = get_DW_AT_signature_type (die, attr, cu);
16115 /* The type's CU may not be the same as CU.
16116 Ensure TYPE is recorded with CU in die_type_hash. */
16117 return set_die_type (die, type, cu);
16120 type = alloc_type (objfile);
16122 TYPE_CODE (type) = TYPE_CODE_ENUM;
16123 name = dwarf2_full_name (NULL, die, cu);
16125 TYPE_TAG_NAME (type) = name;
16127 attr = dwarf2_attr (die, DW_AT_type, cu);
16130 struct type *underlying_type = die_type (die, cu);
16132 TYPE_TARGET_TYPE (type) = underlying_type;
16135 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16138 TYPE_LENGTH (type) = DW_UNSND (attr);
16142 TYPE_LENGTH (type) = 0;
16145 /* The enumeration DIE can be incomplete. In Ada, any type can be
16146 declared as private in the package spec, and then defined only
16147 inside the package body. Such types are known as Taft Amendment
16148 Types. When another package uses such a type, an incomplete DIE
16149 may be generated by the compiler. */
16150 if (die_is_declaration (die, cu))
16151 TYPE_STUB (type) = 1;
16153 /* Finish the creation of this type by using the enum's children.
16154 We must call this even when the underlying type has been provided
16155 so that we can determine if we're looking at a "flag" enum. */
16156 update_enumeration_type_from_children (die, type, cu);
16158 /* If this type has an underlying type that is not a stub, then we
16159 may use its attributes. We always use the "unsigned" attribute
16160 in this situation, because ordinarily we guess whether the type
16161 is unsigned -- but the guess can be wrong and the underlying type
16162 can tell us the reality. However, we defer to a local size
16163 attribute if one exists, because this lets the compiler override
16164 the underlying type if needed. */
16165 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16167 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16168 if (TYPE_LENGTH (type) == 0)
16169 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16172 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16174 return set_die_type (die, type, cu);
16177 /* Given a pointer to a die which begins an enumeration, process all
16178 the dies that define the members of the enumeration, and create the
16179 symbol for the enumeration type.
16181 NOTE: We reverse the order of the element list. */
16184 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16186 struct type *this_type;
16188 this_type = get_die_type (die, cu);
16189 if (this_type == NULL)
16190 this_type = read_enumeration_type (die, cu);
16192 if (die->child != NULL)
16194 struct die_info *child_die;
16195 struct symbol *sym;
16196 struct field *fields = NULL;
16197 int num_fields = 0;
16200 child_die = die->child;
16201 while (child_die && child_die->tag)
16203 if (child_die->tag != DW_TAG_enumerator)
16205 process_die (child_die, cu);
16209 name = dwarf2_name (child_die, cu);
16212 sym = new_symbol (child_die, this_type, cu);
16214 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16216 fields = (struct field *)
16218 (num_fields + DW_FIELD_ALLOC_CHUNK)
16219 * sizeof (struct field));
16222 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16223 FIELD_TYPE (fields[num_fields]) = NULL;
16224 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16225 FIELD_BITSIZE (fields[num_fields]) = 0;
16231 child_die = sibling_die (child_die);
16236 TYPE_NFIELDS (this_type) = num_fields;
16237 TYPE_FIELDS (this_type) = (struct field *)
16238 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16239 memcpy (TYPE_FIELDS (this_type), fields,
16240 sizeof (struct field) * num_fields);
16245 /* If we are reading an enum from a .debug_types unit, and the enum
16246 is a declaration, and the enum is not the signatured type in the
16247 unit, then we do not want to add a symbol for it. Adding a
16248 symbol would in some cases obscure the true definition of the
16249 enum, giving users an incomplete type when the definition is
16250 actually available. Note that we do not want to do this for all
16251 enums which are just declarations, because C++0x allows forward
16252 enum declarations. */
16253 if (cu->per_cu->is_debug_types
16254 && die_is_declaration (die, cu))
16256 struct signatured_type *sig_type;
16258 sig_type = (struct signatured_type *) cu->per_cu;
16259 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16260 if (sig_type->type_offset_in_section != die->sect_off)
16264 new_symbol (die, this_type, cu);
16267 /* Extract all information from a DW_TAG_array_type DIE and put it in
16268 the DIE's type field. For now, this only handles one dimensional
16271 static struct type *
16272 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16274 struct objfile *objfile = cu->objfile;
16275 struct die_info *child_die;
16277 struct type *element_type, *range_type, *index_type;
16278 struct attribute *attr;
16280 unsigned int bit_stride = 0;
16282 element_type = die_type (die, cu);
16284 /* The die_type call above may have already set the type for this DIE. */
16285 type = get_die_type (die, cu);
16289 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16291 bit_stride = DW_UNSND (attr) * 8;
16293 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16295 bit_stride = DW_UNSND (attr);
16297 /* Irix 6.2 native cc creates array types without children for
16298 arrays with unspecified length. */
16299 if (die->child == NULL)
16301 index_type = objfile_type (objfile)->builtin_int;
16302 range_type = create_static_range_type (NULL, index_type, 0, -1);
16303 type = create_array_type_with_stride (NULL, element_type, range_type,
16305 return set_die_type (die, type, cu);
16308 std::vector<struct type *> range_types;
16309 child_die = die->child;
16310 while (child_die && child_die->tag)
16312 if (child_die->tag == DW_TAG_subrange_type)
16314 struct type *child_type = read_type_die (child_die, cu);
16316 if (child_type != NULL)
16318 /* The range type was succesfully read. Save it for the
16319 array type creation. */
16320 range_types.push_back (child_type);
16323 child_die = sibling_die (child_die);
16326 /* Dwarf2 dimensions are output from left to right, create the
16327 necessary array types in backwards order. */
16329 type = element_type;
16331 if (read_array_order (die, cu) == DW_ORD_col_major)
16335 while (i < range_types.size ())
16336 type = create_array_type_with_stride (NULL, type, range_types[i++],
16341 size_t ndim = range_types.size ();
16343 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16347 /* Understand Dwarf2 support for vector types (like they occur on
16348 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16349 array type. This is not part of the Dwarf2/3 standard yet, but a
16350 custom vendor extension. The main difference between a regular
16351 array and the vector variant is that vectors are passed by value
16353 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16355 make_vector_type (type);
16357 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16358 implementation may choose to implement triple vectors using this
16360 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16363 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16364 TYPE_LENGTH (type) = DW_UNSND (attr);
16366 complaint (&symfile_complaints,
16367 _("DW_AT_byte_size for array type smaller "
16368 "than the total size of elements"));
16371 name = dwarf2_name (die, cu);
16373 TYPE_NAME (type) = name;
16375 /* Install the type in the die. */
16376 set_die_type (die, type, cu);
16378 /* set_die_type should be already done. */
16379 set_descriptive_type (type, die, cu);
16384 static enum dwarf_array_dim_ordering
16385 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16387 struct attribute *attr;
16389 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16392 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16394 /* GNU F77 is a special case, as at 08/2004 array type info is the
16395 opposite order to the dwarf2 specification, but data is still
16396 laid out as per normal fortran.
16398 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16399 version checking. */
16401 if (cu->language == language_fortran
16402 && cu->producer && strstr (cu->producer, "GNU F77"))
16404 return DW_ORD_row_major;
16407 switch (cu->language_defn->la_array_ordering)
16409 case array_column_major:
16410 return DW_ORD_col_major;
16411 case array_row_major:
16413 return DW_ORD_row_major;
16417 /* Extract all information from a DW_TAG_set_type DIE and put it in
16418 the DIE's type field. */
16420 static struct type *
16421 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16423 struct type *domain_type, *set_type;
16424 struct attribute *attr;
16426 domain_type = die_type (die, cu);
16428 /* The die_type call above may have already set the type for this DIE. */
16429 set_type = get_die_type (die, cu);
16433 set_type = create_set_type (NULL, domain_type);
16435 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16437 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16439 return set_die_type (die, set_type, cu);
16442 /* A helper for read_common_block that creates a locexpr baton.
16443 SYM is the symbol which we are marking as computed.
16444 COMMON_DIE is the DIE for the common block.
16445 COMMON_LOC is the location expression attribute for the common
16447 MEMBER_LOC is the location expression attribute for the particular
16448 member of the common block that we are processing.
16449 CU is the CU from which the above come. */
16452 mark_common_block_symbol_computed (struct symbol *sym,
16453 struct die_info *common_die,
16454 struct attribute *common_loc,
16455 struct attribute *member_loc,
16456 struct dwarf2_cu *cu)
16458 struct objfile *objfile = dwarf2_per_objfile->objfile;
16459 struct dwarf2_locexpr_baton *baton;
16461 unsigned int cu_off;
16462 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16463 LONGEST offset = 0;
16465 gdb_assert (common_loc && member_loc);
16466 gdb_assert (attr_form_is_block (common_loc));
16467 gdb_assert (attr_form_is_block (member_loc)
16468 || attr_form_is_constant (member_loc));
16470 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16471 baton->per_cu = cu->per_cu;
16472 gdb_assert (baton->per_cu);
16474 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16476 if (attr_form_is_constant (member_loc))
16478 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16479 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16482 baton->size += DW_BLOCK (member_loc)->size;
16484 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16487 *ptr++ = DW_OP_call4;
16488 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16489 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16492 if (attr_form_is_constant (member_loc))
16494 *ptr++ = DW_OP_addr;
16495 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16496 ptr += cu->header.addr_size;
16500 /* We have to copy the data here, because DW_OP_call4 will only
16501 use a DW_AT_location attribute. */
16502 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16503 ptr += DW_BLOCK (member_loc)->size;
16506 *ptr++ = DW_OP_plus;
16507 gdb_assert (ptr - baton->data == baton->size);
16509 SYMBOL_LOCATION_BATON (sym) = baton;
16510 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16513 /* Create appropriate locally-scoped variables for all the
16514 DW_TAG_common_block entries. Also create a struct common_block
16515 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16516 is used to sepate the common blocks name namespace from regular
16520 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16522 struct attribute *attr;
16524 attr = dwarf2_attr (die, DW_AT_location, cu);
16527 /* Support the .debug_loc offsets. */
16528 if (attr_form_is_block (attr))
16532 else if (attr_form_is_section_offset (attr))
16534 dwarf2_complex_location_expr_complaint ();
16539 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16540 "common block member");
16545 if (die->child != NULL)
16547 struct objfile *objfile = cu->objfile;
16548 struct die_info *child_die;
16549 size_t n_entries = 0, size;
16550 struct common_block *common_block;
16551 struct symbol *sym;
16553 for (child_die = die->child;
16554 child_die && child_die->tag;
16555 child_die = sibling_die (child_die))
16558 size = (sizeof (struct common_block)
16559 + (n_entries - 1) * sizeof (struct symbol *));
16561 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16563 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16564 common_block->n_entries = 0;
16566 for (child_die = die->child;
16567 child_die && child_die->tag;
16568 child_die = sibling_die (child_die))
16570 /* Create the symbol in the DW_TAG_common_block block in the current
16572 sym = new_symbol (child_die, NULL, cu);
16575 struct attribute *member_loc;
16577 common_block->contents[common_block->n_entries++] = sym;
16579 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16583 /* GDB has handled this for a long time, but it is
16584 not specified by DWARF. It seems to have been
16585 emitted by gfortran at least as recently as:
16586 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16587 complaint (&symfile_complaints,
16588 _("Variable in common block has "
16589 "DW_AT_data_member_location "
16590 "- DIE at 0x%x [in module %s]"),
16591 to_underlying (child_die->sect_off),
16592 objfile_name (cu->objfile));
16594 if (attr_form_is_section_offset (member_loc))
16595 dwarf2_complex_location_expr_complaint ();
16596 else if (attr_form_is_constant (member_loc)
16597 || attr_form_is_block (member_loc))
16600 mark_common_block_symbol_computed (sym, die, attr,
16604 dwarf2_complex_location_expr_complaint ();
16609 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16610 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16614 /* Create a type for a C++ namespace. */
16616 static struct type *
16617 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16619 struct objfile *objfile = cu->objfile;
16620 const char *previous_prefix, *name;
16624 /* For extensions, reuse the type of the original namespace. */
16625 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16627 struct die_info *ext_die;
16628 struct dwarf2_cu *ext_cu = cu;
16630 ext_die = dwarf2_extension (die, &ext_cu);
16631 type = read_type_die (ext_die, ext_cu);
16633 /* EXT_CU may not be the same as CU.
16634 Ensure TYPE is recorded with CU in die_type_hash. */
16635 return set_die_type (die, type, cu);
16638 name = namespace_name (die, &is_anonymous, cu);
16640 /* Now build the name of the current namespace. */
16642 previous_prefix = determine_prefix (die, cu);
16643 if (previous_prefix[0] != '\0')
16644 name = typename_concat (&objfile->objfile_obstack,
16645 previous_prefix, name, 0, cu);
16647 /* Create the type. */
16648 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16649 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16651 return set_die_type (die, type, cu);
16654 /* Read a namespace scope. */
16657 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16659 struct objfile *objfile = cu->objfile;
16662 /* Add a symbol associated to this if we haven't seen the namespace
16663 before. Also, add a using directive if it's an anonymous
16666 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16670 type = read_type_die (die, cu);
16671 new_symbol (die, type, cu);
16673 namespace_name (die, &is_anonymous, cu);
16676 const char *previous_prefix = determine_prefix (die, cu);
16678 std::vector<const char *> excludes;
16679 add_using_directive (using_directives (cu->language),
16680 previous_prefix, TYPE_NAME (type), NULL,
16681 NULL, excludes, 0, &objfile->objfile_obstack);
16685 if (die->child != NULL)
16687 struct die_info *child_die = die->child;
16689 while (child_die && child_die->tag)
16691 process_die (child_die, cu);
16692 child_die = sibling_die (child_die);
16697 /* Read a Fortran module as type. This DIE can be only a declaration used for
16698 imported module. Still we need that type as local Fortran "use ... only"
16699 declaration imports depend on the created type in determine_prefix. */
16701 static struct type *
16702 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16704 struct objfile *objfile = cu->objfile;
16705 const char *module_name;
16708 module_name = dwarf2_name (die, cu);
16710 complaint (&symfile_complaints,
16711 _("DW_TAG_module has no name, offset 0x%x"),
16712 to_underlying (die->sect_off));
16713 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16715 /* determine_prefix uses TYPE_TAG_NAME. */
16716 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16718 return set_die_type (die, type, cu);
16721 /* Read a Fortran module. */
16724 read_module (struct die_info *die, struct dwarf2_cu *cu)
16726 struct die_info *child_die = die->child;
16729 type = read_type_die (die, cu);
16730 new_symbol (die, type, cu);
16732 while (child_die && child_die->tag)
16734 process_die (child_die, cu);
16735 child_die = sibling_die (child_die);
16739 /* Return the name of the namespace represented by DIE. Set
16740 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16743 static const char *
16744 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16746 struct die_info *current_die;
16747 const char *name = NULL;
16749 /* Loop through the extensions until we find a name. */
16751 for (current_die = die;
16752 current_die != NULL;
16753 current_die = dwarf2_extension (die, &cu))
16755 /* We don't use dwarf2_name here so that we can detect the absence
16756 of a name -> anonymous namespace. */
16757 name = dwarf2_string_attr (die, DW_AT_name, cu);
16763 /* Is it an anonymous namespace? */
16765 *is_anonymous = (name == NULL);
16767 name = CP_ANONYMOUS_NAMESPACE_STR;
16772 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16773 the user defined type vector. */
16775 static struct type *
16776 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16778 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
16779 struct comp_unit_head *cu_header = &cu->header;
16781 struct attribute *attr_byte_size;
16782 struct attribute *attr_address_class;
16783 int byte_size, addr_class;
16784 struct type *target_type;
16786 target_type = die_type (die, cu);
16788 /* The die_type call above may have already set the type for this DIE. */
16789 type = get_die_type (die, cu);
16793 type = lookup_pointer_type (target_type);
16795 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16796 if (attr_byte_size)
16797 byte_size = DW_UNSND (attr_byte_size);
16799 byte_size = cu_header->addr_size;
16801 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16802 if (attr_address_class)
16803 addr_class = DW_UNSND (attr_address_class);
16805 addr_class = DW_ADDR_none;
16807 /* If the pointer size or address class is different than the
16808 default, create a type variant marked as such and set the
16809 length accordingly. */
16810 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
16812 if (gdbarch_address_class_type_flags_p (gdbarch))
16816 type_flags = gdbarch_address_class_type_flags
16817 (gdbarch, byte_size, addr_class);
16818 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16820 type = make_type_with_address_space (type, type_flags);
16822 else if (TYPE_LENGTH (type) != byte_size)
16824 complaint (&symfile_complaints,
16825 _("invalid pointer size %d"), byte_size);
16829 /* Should we also complain about unhandled address classes? */
16833 TYPE_LENGTH (type) = byte_size;
16834 return set_die_type (die, type, cu);
16837 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16838 the user defined type vector. */
16840 static struct type *
16841 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16844 struct type *to_type;
16845 struct type *domain;
16847 to_type = die_type (die, cu);
16848 domain = die_containing_type (die, cu);
16850 /* The calls above may have already set the type for this DIE. */
16851 type = get_die_type (die, cu);
16855 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16856 type = lookup_methodptr_type (to_type);
16857 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16859 struct type *new_type = alloc_type (cu->objfile);
16861 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16862 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16863 TYPE_VARARGS (to_type));
16864 type = lookup_methodptr_type (new_type);
16867 type = lookup_memberptr_type (to_type, domain);
16869 return set_die_type (die, type, cu);
16872 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16873 the user defined type vector. */
16875 static struct type *
16876 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16877 enum type_code refcode)
16879 struct comp_unit_head *cu_header = &cu->header;
16880 struct type *type, *target_type;
16881 struct attribute *attr;
16883 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16885 target_type = die_type (die, cu);
16887 /* The die_type call above may have already set the type for this DIE. */
16888 type = get_die_type (die, cu);
16892 type = lookup_reference_type (target_type, refcode);
16893 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16896 TYPE_LENGTH (type) = DW_UNSND (attr);
16900 TYPE_LENGTH (type) = cu_header->addr_size;
16902 return set_die_type (die, type, cu);
16905 /* Add the given cv-qualifiers to the element type of the array. GCC
16906 outputs DWARF type qualifiers that apply to an array, not the
16907 element type. But GDB relies on the array element type to carry
16908 the cv-qualifiers. This mimics section 6.7.3 of the C99
16911 static struct type *
16912 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16913 struct type *base_type, int cnst, int voltl)
16915 struct type *el_type, *inner_array;
16917 base_type = copy_type (base_type);
16918 inner_array = base_type;
16920 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16922 TYPE_TARGET_TYPE (inner_array) =
16923 copy_type (TYPE_TARGET_TYPE (inner_array));
16924 inner_array = TYPE_TARGET_TYPE (inner_array);
16927 el_type = TYPE_TARGET_TYPE (inner_array);
16928 cnst |= TYPE_CONST (el_type);
16929 voltl |= TYPE_VOLATILE (el_type);
16930 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16932 return set_die_type (die, base_type, cu);
16935 static struct type *
16936 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16938 struct type *base_type, *cv_type;
16940 base_type = die_type (die, cu);
16942 /* The die_type call above may have already set the type for this DIE. */
16943 cv_type = get_die_type (die, cu);
16947 /* In case the const qualifier is applied to an array type, the element type
16948 is so qualified, not the array type (section 6.7.3 of C99). */
16949 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16950 return add_array_cv_type (die, cu, base_type, 1, 0);
16952 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16953 return set_die_type (die, cv_type, cu);
16956 static struct type *
16957 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16959 struct type *base_type, *cv_type;
16961 base_type = die_type (die, cu);
16963 /* The die_type call above may have already set the type for this DIE. */
16964 cv_type = get_die_type (die, cu);
16968 /* In case the volatile qualifier is applied to an array type, the
16969 element type is so qualified, not the array type (section 6.7.3
16971 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16972 return add_array_cv_type (die, cu, base_type, 0, 1);
16974 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16975 return set_die_type (die, cv_type, cu);
16978 /* Handle DW_TAG_restrict_type. */
16980 static struct type *
16981 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16983 struct type *base_type, *cv_type;
16985 base_type = die_type (die, cu);
16987 /* The die_type call above may have already set the type for this DIE. */
16988 cv_type = get_die_type (die, cu);
16992 cv_type = make_restrict_type (base_type);
16993 return set_die_type (die, cv_type, cu);
16996 /* Handle DW_TAG_atomic_type. */
16998 static struct type *
16999 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17001 struct type *base_type, *cv_type;
17003 base_type = die_type (die, cu);
17005 /* The die_type call above may have already set the type for this DIE. */
17006 cv_type = get_die_type (die, cu);
17010 cv_type = make_atomic_type (base_type);
17011 return set_die_type (die, cv_type, cu);
17014 /* Extract all information from a DW_TAG_string_type DIE and add to
17015 the user defined type vector. It isn't really a user defined type,
17016 but it behaves like one, with other DIE's using an AT_user_def_type
17017 attribute to reference it. */
17019 static struct type *
17020 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17022 struct objfile *objfile = cu->objfile;
17023 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17024 struct type *type, *range_type, *index_type, *char_type;
17025 struct attribute *attr;
17026 unsigned int length;
17028 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17031 length = DW_UNSND (attr);
17035 /* Check for the DW_AT_byte_size attribute. */
17036 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17039 length = DW_UNSND (attr);
17047 index_type = objfile_type (objfile)->builtin_int;
17048 range_type = create_static_range_type (NULL, index_type, 1, length);
17049 char_type = language_string_char_type (cu->language_defn, gdbarch);
17050 type = create_string_type (NULL, char_type, range_type);
17052 return set_die_type (die, type, cu);
17055 /* Assuming that DIE corresponds to a function, returns nonzero
17056 if the function is prototyped. */
17059 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17061 struct attribute *attr;
17063 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17064 if (attr && (DW_UNSND (attr) != 0))
17067 /* The DWARF standard implies that the DW_AT_prototyped attribute
17068 is only meaninful for C, but the concept also extends to other
17069 languages that allow unprototyped functions (Eg: Objective C).
17070 For all other languages, assume that functions are always
17072 if (cu->language != language_c
17073 && cu->language != language_objc
17074 && cu->language != language_opencl)
17077 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17078 prototyped and unprototyped functions; default to prototyped,
17079 since that is more common in modern code (and RealView warns
17080 about unprototyped functions). */
17081 if (producer_is_realview (cu->producer))
17087 /* Handle DIES due to C code like:
17091 int (*funcp)(int a, long l);
17095 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17097 static struct type *
17098 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17100 struct objfile *objfile = cu->objfile;
17101 struct type *type; /* Type that this function returns. */
17102 struct type *ftype; /* Function that returns above type. */
17103 struct attribute *attr;
17105 type = die_type (die, cu);
17107 /* The die_type call above may have already set the type for this DIE. */
17108 ftype = get_die_type (die, cu);
17112 ftype = lookup_function_type (type);
17114 if (prototyped_function_p (die, cu))
17115 TYPE_PROTOTYPED (ftype) = 1;
17117 /* Store the calling convention in the type if it's available in
17118 the subroutine die. Otherwise set the calling convention to
17119 the default value DW_CC_normal. */
17120 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17122 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17123 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17124 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17126 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17128 /* Record whether the function returns normally to its caller or not
17129 if the DWARF producer set that information. */
17130 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17131 if (attr && (DW_UNSND (attr) != 0))
17132 TYPE_NO_RETURN (ftype) = 1;
17134 /* We need to add the subroutine type to the die immediately so
17135 we don't infinitely recurse when dealing with parameters
17136 declared as the same subroutine type. */
17137 set_die_type (die, ftype, cu);
17139 if (die->child != NULL)
17141 struct type *void_type = objfile_type (objfile)->builtin_void;
17142 struct die_info *child_die;
17143 int nparams, iparams;
17145 /* Count the number of parameters.
17146 FIXME: GDB currently ignores vararg functions, but knows about
17147 vararg member functions. */
17149 child_die = die->child;
17150 while (child_die && child_die->tag)
17152 if (child_die->tag == DW_TAG_formal_parameter)
17154 else if (child_die->tag == DW_TAG_unspecified_parameters)
17155 TYPE_VARARGS (ftype) = 1;
17156 child_die = sibling_die (child_die);
17159 /* Allocate storage for parameters and fill them in. */
17160 TYPE_NFIELDS (ftype) = nparams;
17161 TYPE_FIELDS (ftype) = (struct field *)
17162 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17164 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17165 even if we error out during the parameters reading below. */
17166 for (iparams = 0; iparams < nparams; iparams++)
17167 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17170 child_die = die->child;
17171 while (child_die && child_die->tag)
17173 if (child_die->tag == DW_TAG_formal_parameter)
17175 struct type *arg_type;
17177 /* DWARF version 2 has no clean way to discern C++
17178 static and non-static member functions. G++ helps
17179 GDB by marking the first parameter for non-static
17180 member functions (which is the this pointer) as
17181 artificial. We pass this information to
17182 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17184 DWARF version 3 added DW_AT_object_pointer, which GCC
17185 4.5 does not yet generate. */
17186 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17188 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17190 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17191 arg_type = die_type (child_die, cu);
17193 /* RealView does not mark THIS as const, which the testsuite
17194 expects. GCC marks THIS as const in method definitions,
17195 but not in the class specifications (GCC PR 43053). */
17196 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17197 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17200 struct dwarf2_cu *arg_cu = cu;
17201 const char *name = dwarf2_name (child_die, cu);
17203 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17206 /* If the compiler emits this, use it. */
17207 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17210 else if (name && strcmp (name, "this") == 0)
17211 /* Function definitions will have the argument names. */
17213 else if (name == NULL && iparams == 0)
17214 /* Declarations may not have the names, so like
17215 elsewhere in GDB, assume an artificial first
17216 argument is "this". */
17220 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17224 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17227 child_die = sibling_die (child_die);
17234 static struct type *
17235 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17237 struct objfile *objfile = cu->objfile;
17238 const char *name = NULL;
17239 struct type *this_type, *target_type;
17241 name = dwarf2_full_name (NULL, die, cu);
17242 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17243 TYPE_TARGET_STUB (this_type) = 1;
17244 set_die_type (die, this_type, cu);
17245 target_type = die_type (die, cu);
17246 if (target_type != this_type)
17247 TYPE_TARGET_TYPE (this_type) = target_type;
17250 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17251 spec and cause infinite loops in GDB. */
17252 complaint (&symfile_complaints,
17253 _("Self-referential DW_TAG_typedef "
17254 "- DIE at 0x%x [in module %s]"),
17255 to_underlying (die->sect_off), objfile_name (objfile));
17256 TYPE_TARGET_TYPE (this_type) = NULL;
17261 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17262 (which may be different from NAME) to the architecture back-end to allow
17263 it to guess the correct format if necessary. */
17265 static struct type *
17266 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17267 const char *name_hint)
17269 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17270 const struct floatformat **format;
17273 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17275 type = init_float_type (objfile, bits, name, format);
17277 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17282 /* Find a representation of a given base type and install
17283 it in the TYPE field of the die. */
17285 static struct type *
17286 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17288 struct objfile *objfile = cu->objfile;
17290 struct attribute *attr;
17291 int encoding = 0, bits = 0;
17294 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17297 encoding = DW_UNSND (attr);
17299 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17302 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17304 name = dwarf2_name (die, cu);
17307 complaint (&symfile_complaints,
17308 _("DW_AT_name missing from DW_TAG_base_type"));
17313 case DW_ATE_address:
17314 /* Turn DW_ATE_address into a void * pointer. */
17315 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17316 type = init_pointer_type (objfile, bits, name, type);
17318 case DW_ATE_boolean:
17319 type = init_boolean_type (objfile, bits, 1, name);
17321 case DW_ATE_complex_float:
17322 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
17323 type = init_complex_type (objfile, name, type);
17325 case DW_ATE_decimal_float:
17326 type = init_decfloat_type (objfile, bits, name);
17329 type = dwarf2_init_float_type (objfile, bits, name, name);
17331 case DW_ATE_signed:
17332 type = init_integer_type (objfile, bits, 0, name);
17334 case DW_ATE_unsigned:
17335 if (cu->language == language_fortran
17337 && startswith (name, "character("))
17338 type = init_character_type (objfile, bits, 1, name);
17340 type = init_integer_type (objfile, bits, 1, name);
17342 case DW_ATE_signed_char:
17343 if (cu->language == language_ada || cu->language == language_m2
17344 || cu->language == language_pascal
17345 || cu->language == language_fortran)
17346 type = init_character_type (objfile, bits, 0, name);
17348 type = init_integer_type (objfile, bits, 0, name);
17350 case DW_ATE_unsigned_char:
17351 if (cu->language == language_ada || cu->language == language_m2
17352 || cu->language == language_pascal
17353 || cu->language == language_fortran
17354 || cu->language == language_rust)
17355 type = init_character_type (objfile, bits, 1, name);
17357 type = init_integer_type (objfile, bits, 1, name);
17361 gdbarch *arch = get_objfile_arch (objfile);
17364 type = builtin_type (arch)->builtin_char16;
17365 else if (bits == 32)
17366 type = builtin_type (arch)->builtin_char32;
17369 complaint (&symfile_complaints,
17370 _("unsupported DW_ATE_UTF bit size: '%d'"),
17372 type = init_integer_type (objfile, bits, 1, name);
17374 return set_die_type (die, type, cu);
17379 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17380 dwarf_type_encoding_name (encoding));
17381 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17385 if (name && strcmp (name, "char") == 0)
17386 TYPE_NOSIGN (type) = 1;
17388 return set_die_type (die, type, cu);
17391 /* Parse dwarf attribute if it's a block, reference or constant and put the
17392 resulting value of the attribute into struct bound_prop.
17393 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17396 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17397 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17399 struct dwarf2_property_baton *baton;
17400 struct obstack *obstack = &cu->objfile->objfile_obstack;
17402 if (attr == NULL || prop == NULL)
17405 if (attr_form_is_block (attr))
17407 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17408 baton->referenced_type = NULL;
17409 baton->locexpr.per_cu = cu->per_cu;
17410 baton->locexpr.size = DW_BLOCK (attr)->size;
17411 baton->locexpr.data = DW_BLOCK (attr)->data;
17412 prop->data.baton = baton;
17413 prop->kind = PROP_LOCEXPR;
17414 gdb_assert (prop->data.baton != NULL);
17416 else if (attr_form_is_ref (attr))
17418 struct dwarf2_cu *target_cu = cu;
17419 struct die_info *target_die;
17420 struct attribute *target_attr;
17422 target_die = follow_die_ref (die, attr, &target_cu);
17423 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17424 if (target_attr == NULL)
17425 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17427 if (target_attr == NULL)
17430 switch (target_attr->name)
17432 case DW_AT_location:
17433 if (attr_form_is_section_offset (target_attr))
17435 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17436 baton->referenced_type = die_type (target_die, target_cu);
17437 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17438 prop->data.baton = baton;
17439 prop->kind = PROP_LOCLIST;
17440 gdb_assert (prop->data.baton != NULL);
17442 else if (attr_form_is_block (target_attr))
17444 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17445 baton->referenced_type = die_type (target_die, target_cu);
17446 baton->locexpr.per_cu = cu->per_cu;
17447 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17448 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17449 prop->data.baton = baton;
17450 prop->kind = PROP_LOCEXPR;
17451 gdb_assert (prop->data.baton != NULL);
17455 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17456 "dynamic property");
17460 case DW_AT_data_member_location:
17464 if (!handle_data_member_location (target_die, target_cu,
17468 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17469 baton->referenced_type = read_type_die (target_die->parent,
17471 baton->offset_info.offset = offset;
17472 baton->offset_info.type = die_type (target_die, target_cu);
17473 prop->data.baton = baton;
17474 prop->kind = PROP_ADDR_OFFSET;
17479 else if (attr_form_is_constant (attr))
17481 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17482 prop->kind = PROP_CONST;
17486 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17487 dwarf2_name (die, cu));
17494 /* Read the given DW_AT_subrange DIE. */
17496 static struct type *
17497 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17499 struct type *base_type, *orig_base_type;
17500 struct type *range_type;
17501 struct attribute *attr;
17502 struct dynamic_prop low, high;
17503 int low_default_is_valid;
17504 int high_bound_is_count = 0;
17506 LONGEST negative_mask;
17508 orig_base_type = die_type (die, cu);
17509 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17510 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17511 creating the range type, but we use the result of check_typedef
17512 when examining properties of the type. */
17513 base_type = check_typedef (orig_base_type);
17515 /* The die_type call above may have already set the type for this DIE. */
17516 range_type = get_die_type (die, cu);
17520 low.kind = PROP_CONST;
17521 high.kind = PROP_CONST;
17522 high.data.const_val = 0;
17524 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17525 omitting DW_AT_lower_bound. */
17526 switch (cu->language)
17529 case language_cplus:
17530 low.data.const_val = 0;
17531 low_default_is_valid = 1;
17533 case language_fortran:
17534 low.data.const_val = 1;
17535 low_default_is_valid = 1;
17538 case language_objc:
17539 case language_rust:
17540 low.data.const_val = 0;
17541 low_default_is_valid = (cu->header.version >= 4);
17545 case language_pascal:
17546 low.data.const_val = 1;
17547 low_default_is_valid = (cu->header.version >= 4);
17550 low.data.const_val = 0;
17551 low_default_is_valid = 0;
17555 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17557 attr_to_dynamic_prop (attr, die, cu, &low);
17558 else if (!low_default_is_valid)
17559 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
17560 "- DIE at 0x%x [in module %s]"),
17561 to_underlying (die->sect_off), objfile_name (cu->objfile));
17563 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
17564 if (!attr_to_dynamic_prop (attr, die, cu, &high))
17566 attr = dwarf2_attr (die, DW_AT_count, cu);
17567 if (attr_to_dynamic_prop (attr, die, cu, &high))
17569 /* If bounds are constant do the final calculation here. */
17570 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17571 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17573 high_bound_is_count = 1;
17577 /* Dwarf-2 specifications explicitly allows to create subrange types
17578 without specifying a base type.
17579 In that case, the base type must be set to the type of
17580 the lower bound, upper bound or count, in that order, if any of these
17581 three attributes references an object that has a type.
17582 If no base type is found, the Dwarf-2 specifications say that
17583 a signed integer type of size equal to the size of an address should
17585 For the following C code: `extern char gdb_int [];'
17586 GCC produces an empty range DIE.
17587 FIXME: muller/2010-05-28: Possible references to object for low bound,
17588 high bound or count are not yet handled by this code. */
17589 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17591 struct objfile *objfile = cu->objfile;
17592 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17593 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17594 struct type *int_type = objfile_type (objfile)->builtin_int;
17596 /* Test "int", "long int", and "long long int" objfile types,
17597 and select the first one having a size above or equal to the
17598 architecture address size. */
17599 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17600 base_type = int_type;
17603 int_type = objfile_type (objfile)->builtin_long;
17604 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17605 base_type = int_type;
17608 int_type = objfile_type (objfile)->builtin_long_long;
17609 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17610 base_type = int_type;
17615 /* Normally, the DWARF producers are expected to use a signed
17616 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17617 But this is unfortunately not always the case, as witnessed
17618 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17619 is used instead. To work around that ambiguity, we treat
17620 the bounds as signed, and thus sign-extend their values, when
17621 the base type is signed. */
17623 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17624 if (low.kind == PROP_CONST
17625 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17626 low.data.const_val |= negative_mask;
17627 if (high.kind == PROP_CONST
17628 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17629 high.data.const_val |= negative_mask;
17631 range_type = create_range_type (NULL, orig_base_type, &low, &high);
17633 if (high_bound_is_count)
17634 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17636 /* Ada expects an empty array on no boundary attributes. */
17637 if (attr == NULL && cu->language != language_ada)
17638 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17640 name = dwarf2_name (die, cu);
17642 TYPE_NAME (range_type) = name;
17644 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17646 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17648 set_die_type (die, range_type, cu);
17650 /* set_die_type should be already done. */
17651 set_descriptive_type (range_type, die, cu);
17656 static struct type *
17657 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17661 /* For now, we only support the C meaning of an unspecified type: void. */
17663 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
17664 TYPE_NAME (type) = dwarf2_name (die, cu);
17666 return set_die_type (die, type, cu);
17669 /* Read a single die and all its descendents. Set the die's sibling
17670 field to NULL; set other fields in the die correctly, and set all
17671 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17672 location of the info_ptr after reading all of those dies. PARENT
17673 is the parent of the die in question. */
17675 static struct die_info *
17676 read_die_and_children (const struct die_reader_specs *reader,
17677 const gdb_byte *info_ptr,
17678 const gdb_byte **new_info_ptr,
17679 struct die_info *parent)
17681 struct die_info *die;
17682 const gdb_byte *cur_ptr;
17685 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
17688 *new_info_ptr = cur_ptr;
17691 store_in_ref_table (die, reader->cu);
17694 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17698 *new_info_ptr = cur_ptr;
17701 die->sibling = NULL;
17702 die->parent = parent;
17706 /* Read a die, all of its descendents, and all of its siblings; set
17707 all of the fields of all of the dies correctly. Arguments are as
17708 in read_die_and_children. */
17710 static struct die_info *
17711 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17712 const gdb_byte *info_ptr,
17713 const gdb_byte **new_info_ptr,
17714 struct die_info *parent)
17716 struct die_info *first_die, *last_sibling;
17717 const gdb_byte *cur_ptr;
17719 cur_ptr = info_ptr;
17720 first_die = last_sibling = NULL;
17724 struct die_info *die
17725 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17729 *new_info_ptr = cur_ptr;
17736 last_sibling->sibling = die;
17738 last_sibling = die;
17742 /* Read a die, all of its descendents, and all of its siblings; set
17743 all of the fields of all of the dies correctly. Arguments are as
17744 in read_die_and_children.
17745 This the main entry point for reading a DIE and all its children. */
17747 static struct die_info *
17748 read_die_and_siblings (const struct die_reader_specs *reader,
17749 const gdb_byte *info_ptr,
17750 const gdb_byte **new_info_ptr,
17751 struct die_info *parent)
17753 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17754 new_info_ptr, parent);
17756 if (dwarf_die_debug)
17758 fprintf_unfiltered (gdb_stdlog,
17759 "Read die from %s@0x%x of %s:\n",
17760 get_section_name (reader->die_section),
17761 (unsigned) (info_ptr - reader->die_section->buffer),
17762 bfd_get_filename (reader->abfd));
17763 dump_die (die, dwarf_die_debug);
17769 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17771 The caller is responsible for filling in the extra attributes
17772 and updating (*DIEP)->num_attrs.
17773 Set DIEP to point to a newly allocated die with its information,
17774 except for its child, sibling, and parent fields.
17775 Set HAS_CHILDREN to tell whether the die has children or not. */
17777 static const gdb_byte *
17778 read_full_die_1 (const struct die_reader_specs *reader,
17779 struct die_info **diep, const gdb_byte *info_ptr,
17780 int *has_children, int num_extra_attrs)
17782 unsigned int abbrev_number, bytes_read, i;
17783 struct abbrev_info *abbrev;
17784 struct die_info *die;
17785 struct dwarf2_cu *cu = reader->cu;
17786 bfd *abfd = reader->abfd;
17788 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17789 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17790 info_ptr += bytes_read;
17791 if (!abbrev_number)
17798 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
17800 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17802 bfd_get_filename (abfd));
17804 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17805 die->sect_off = sect_off;
17806 die->tag = abbrev->tag;
17807 die->abbrev = abbrev_number;
17809 /* Make the result usable.
17810 The caller needs to update num_attrs after adding the extra
17812 die->num_attrs = abbrev->num_attrs;
17814 for (i = 0; i < abbrev->num_attrs; ++i)
17815 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17819 *has_children = abbrev->has_children;
17823 /* Read a die and all its attributes.
17824 Set DIEP to point to a newly allocated die with its information,
17825 except for its child, sibling, and parent fields.
17826 Set HAS_CHILDREN to tell whether the die has children or not. */
17828 static const gdb_byte *
17829 read_full_die (const struct die_reader_specs *reader,
17830 struct die_info **diep, const gdb_byte *info_ptr,
17833 const gdb_byte *result;
17835 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
17837 if (dwarf_die_debug)
17839 fprintf_unfiltered (gdb_stdlog,
17840 "Read die from %s@0x%x of %s:\n",
17841 get_section_name (reader->die_section),
17842 (unsigned) (info_ptr - reader->die_section->buffer),
17843 bfd_get_filename (reader->abfd));
17844 dump_die (*diep, dwarf_die_debug);
17850 /* Abbreviation tables.
17852 In DWARF version 2, the description of the debugging information is
17853 stored in a separate .debug_abbrev section. Before we read any
17854 dies from a section we read in all abbreviations and install them
17855 in a hash table. */
17857 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
17859 static struct abbrev_info *
17860 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
17862 struct abbrev_info *abbrev;
17864 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
17865 memset (abbrev, 0, sizeof (struct abbrev_info));
17870 /* Add an abbreviation to the table. */
17873 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
17874 unsigned int abbrev_number,
17875 struct abbrev_info *abbrev)
17877 unsigned int hash_number;
17879 hash_number = abbrev_number % ABBREV_HASH_SIZE;
17880 abbrev->next = abbrev_table->abbrevs[hash_number];
17881 abbrev_table->abbrevs[hash_number] = abbrev;
17884 /* Look up an abbrev in the table.
17885 Returns NULL if the abbrev is not found. */
17887 static struct abbrev_info *
17888 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
17889 unsigned int abbrev_number)
17891 unsigned int hash_number;
17892 struct abbrev_info *abbrev;
17894 hash_number = abbrev_number % ABBREV_HASH_SIZE;
17895 abbrev = abbrev_table->abbrevs[hash_number];
17899 if (abbrev->number == abbrev_number)
17901 abbrev = abbrev->next;
17906 /* Read in an abbrev table. */
17908 static struct abbrev_table *
17909 abbrev_table_read_table (struct dwarf2_section_info *section,
17910 sect_offset sect_off)
17912 struct objfile *objfile = dwarf2_per_objfile->objfile;
17913 bfd *abfd = get_section_bfd_owner (section);
17914 struct abbrev_table *abbrev_table;
17915 const gdb_byte *abbrev_ptr;
17916 struct abbrev_info *cur_abbrev;
17917 unsigned int abbrev_number, bytes_read, abbrev_name;
17918 unsigned int abbrev_form;
17919 struct attr_abbrev *cur_attrs;
17920 unsigned int allocated_attrs;
17922 abbrev_table = XNEW (struct abbrev_table);
17923 abbrev_table->sect_off = sect_off;
17924 obstack_init (&abbrev_table->abbrev_obstack);
17925 abbrev_table->abbrevs =
17926 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
17928 memset (abbrev_table->abbrevs, 0,
17929 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
17931 dwarf2_read_section (objfile, section);
17932 abbrev_ptr = section->buffer + to_underlying (sect_off);
17933 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17934 abbrev_ptr += bytes_read;
17936 allocated_attrs = ATTR_ALLOC_CHUNK;
17937 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
17939 /* Loop until we reach an abbrev number of 0. */
17940 while (abbrev_number)
17942 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
17944 /* read in abbrev header */
17945 cur_abbrev->number = abbrev_number;
17947 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17948 abbrev_ptr += bytes_read;
17949 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
17952 /* now read in declarations */
17955 LONGEST implicit_const;
17957 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17958 abbrev_ptr += bytes_read;
17959 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17960 abbrev_ptr += bytes_read;
17961 if (abbrev_form == DW_FORM_implicit_const)
17963 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
17965 abbrev_ptr += bytes_read;
17969 /* Initialize it due to a false compiler warning. */
17970 implicit_const = -1;
17973 if (abbrev_name == 0)
17976 if (cur_abbrev->num_attrs == allocated_attrs)
17978 allocated_attrs += ATTR_ALLOC_CHUNK;
17980 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
17983 cur_attrs[cur_abbrev->num_attrs].name
17984 = (enum dwarf_attribute) abbrev_name;
17985 cur_attrs[cur_abbrev->num_attrs].form
17986 = (enum dwarf_form) abbrev_form;
17987 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
17988 ++cur_abbrev->num_attrs;
17991 cur_abbrev->attrs =
17992 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
17993 cur_abbrev->num_attrs);
17994 memcpy (cur_abbrev->attrs, cur_attrs,
17995 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
17997 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
17999 /* Get next abbreviation.
18000 Under Irix6 the abbreviations for a compilation unit are not
18001 always properly terminated with an abbrev number of 0.
18002 Exit loop if we encounter an abbreviation which we have
18003 already read (which means we are about to read the abbreviations
18004 for the next compile unit) or if the end of the abbreviation
18005 table is reached. */
18006 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18008 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18009 abbrev_ptr += bytes_read;
18010 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
18015 return abbrev_table;
18018 /* Free the resources held by ABBREV_TABLE. */
18021 abbrev_table_free (struct abbrev_table *abbrev_table)
18023 obstack_free (&abbrev_table->abbrev_obstack, NULL);
18024 xfree (abbrev_table);
18027 /* Same as abbrev_table_free but as a cleanup.
18028 We pass in a pointer to the pointer to the table so that we can
18029 set the pointer to NULL when we're done. It also simplifies
18030 build_type_psymtabs_1. */
18033 abbrev_table_free_cleanup (void *table_ptr)
18035 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
18037 if (*abbrev_table_ptr != NULL)
18038 abbrev_table_free (*abbrev_table_ptr);
18039 *abbrev_table_ptr = NULL;
18042 /* Read the abbrev table for CU from ABBREV_SECTION. */
18045 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
18046 struct dwarf2_section_info *abbrev_section)
18049 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
18052 /* Release the memory used by the abbrev table for a compilation unit. */
18055 dwarf2_free_abbrev_table (void *ptr_to_cu)
18057 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
18059 if (cu->abbrev_table != NULL)
18060 abbrev_table_free (cu->abbrev_table);
18061 /* Set this to NULL so that we SEGV if we try to read it later,
18062 and also because free_comp_unit verifies this is NULL. */
18063 cu->abbrev_table = NULL;
18066 /* Returns nonzero if TAG represents a type that we might generate a partial
18070 is_type_tag_for_partial (int tag)
18075 /* Some types that would be reasonable to generate partial symbols for,
18076 that we don't at present. */
18077 case DW_TAG_array_type:
18078 case DW_TAG_file_type:
18079 case DW_TAG_ptr_to_member_type:
18080 case DW_TAG_set_type:
18081 case DW_TAG_string_type:
18082 case DW_TAG_subroutine_type:
18084 case DW_TAG_base_type:
18085 case DW_TAG_class_type:
18086 case DW_TAG_interface_type:
18087 case DW_TAG_enumeration_type:
18088 case DW_TAG_structure_type:
18089 case DW_TAG_subrange_type:
18090 case DW_TAG_typedef:
18091 case DW_TAG_union_type:
18098 /* Load all DIEs that are interesting for partial symbols into memory. */
18100 static struct partial_die_info *
18101 load_partial_dies (const struct die_reader_specs *reader,
18102 const gdb_byte *info_ptr, int building_psymtab)
18104 struct dwarf2_cu *cu = reader->cu;
18105 struct objfile *objfile = cu->objfile;
18106 struct partial_die_info *part_die;
18107 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18108 struct abbrev_info *abbrev;
18109 unsigned int bytes_read;
18110 unsigned int load_all = 0;
18111 int nesting_level = 1;
18116 gdb_assert (cu->per_cu != NULL);
18117 if (cu->per_cu->load_all_dies)
18121 = htab_create_alloc_ex (cu->header.length / 12,
18125 &cu->comp_unit_obstack,
18126 hashtab_obstack_allocate,
18127 dummy_obstack_deallocate);
18129 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
18133 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
18135 /* A NULL abbrev means the end of a series of children. */
18136 if (abbrev == NULL)
18138 if (--nesting_level == 0)
18140 /* PART_DIE was probably the last thing allocated on the
18141 comp_unit_obstack, so we could call obstack_free
18142 here. We don't do that because the waste is small,
18143 and will be cleaned up when we're done with this
18144 compilation unit. This way, we're also more robust
18145 against other users of the comp_unit_obstack. */
18148 info_ptr += bytes_read;
18149 last_die = parent_die;
18150 parent_die = parent_die->die_parent;
18154 /* Check for template arguments. We never save these; if
18155 they're seen, we just mark the parent, and go on our way. */
18156 if (parent_die != NULL
18157 && cu->language == language_cplus
18158 && (abbrev->tag == DW_TAG_template_type_param
18159 || abbrev->tag == DW_TAG_template_value_param))
18161 parent_die->has_template_arguments = 1;
18165 /* We don't need a partial DIE for the template argument. */
18166 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18171 /* We only recurse into c++ subprograms looking for template arguments.
18172 Skip their other children. */
18174 && cu->language == language_cplus
18175 && parent_die != NULL
18176 && parent_die->tag == DW_TAG_subprogram)
18178 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18182 /* Check whether this DIE is interesting enough to save. Normally
18183 we would not be interested in members here, but there may be
18184 later variables referencing them via DW_AT_specification (for
18185 static members). */
18187 && !is_type_tag_for_partial (abbrev->tag)
18188 && abbrev->tag != DW_TAG_constant
18189 && abbrev->tag != DW_TAG_enumerator
18190 && abbrev->tag != DW_TAG_subprogram
18191 && abbrev->tag != DW_TAG_lexical_block
18192 && abbrev->tag != DW_TAG_variable
18193 && abbrev->tag != DW_TAG_namespace
18194 && abbrev->tag != DW_TAG_module
18195 && abbrev->tag != DW_TAG_member
18196 && abbrev->tag != DW_TAG_imported_unit
18197 && abbrev->tag != DW_TAG_imported_declaration)
18199 /* Otherwise we skip to the next sibling, if any. */
18200 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18204 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
18207 /* This two-pass algorithm for processing partial symbols has a
18208 high cost in cache pressure. Thus, handle some simple cases
18209 here which cover the majority of C partial symbols. DIEs
18210 which neither have specification tags in them, nor could have
18211 specification tags elsewhere pointing at them, can simply be
18212 processed and discarded.
18214 This segment is also optional; scan_partial_symbols and
18215 add_partial_symbol will handle these DIEs if we chain
18216 them in normally. When compilers which do not emit large
18217 quantities of duplicate debug information are more common,
18218 this code can probably be removed. */
18220 /* Any complete simple types at the top level (pretty much all
18221 of them, for a language without namespaces), can be processed
18223 if (parent_die == NULL
18224 && part_die->has_specification == 0
18225 && part_die->is_declaration == 0
18226 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
18227 || part_die->tag == DW_TAG_base_type
18228 || part_die->tag == DW_TAG_subrange_type))
18230 if (building_psymtab && part_die->name != NULL)
18231 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
18232 VAR_DOMAIN, LOC_TYPEDEF,
18233 &objfile->static_psymbols,
18234 0, cu->language, objfile);
18235 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
18239 /* The exception for DW_TAG_typedef with has_children above is
18240 a workaround of GCC PR debug/47510. In the case of this complaint
18241 type_name_no_tag_or_error will error on such types later.
18243 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18244 it could not find the child DIEs referenced later, this is checked
18245 above. In correct DWARF DW_TAG_typedef should have no children. */
18247 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
18248 complaint (&symfile_complaints,
18249 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18250 "- DIE at 0x%x [in module %s]"),
18251 to_underlying (part_die->sect_off), objfile_name (objfile));
18253 /* If we're at the second level, and we're an enumerator, and
18254 our parent has no specification (meaning possibly lives in a
18255 namespace elsewhere), then we can add the partial symbol now
18256 instead of queueing it. */
18257 if (part_die->tag == DW_TAG_enumerator
18258 && parent_die != NULL
18259 && parent_die->die_parent == NULL
18260 && parent_die->tag == DW_TAG_enumeration_type
18261 && parent_die->has_specification == 0)
18263 if (part_die->name == NULL)
18264 complaint (&symfile_complaints,
18265 _("malformed enumerator DIE ignored"));
18266 else if (building_psymtab)
18267 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
18268 VAR_DOMAIN, LOC_CONST,
18269 cu->language == language_cplus
18270 ? &objfile->global_psymbols
18271 : &objfile->static_psymbols,
18272 0, cu->language, objfile);
18274 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
18278 /* We'll save this DIE so link it in. */
18279 part_die->die_parent = parent_die;
18280 part_die->die_sibling = NULL;
18281 part_die->die_child = NULL;
18283 if (last_die && last_die == parent_die)
18284 last_die->die_child = part_die;
18286 last_die->die_sibling = part_die;
18288 last_die = part_die;
18290 if (first_die == NULL)
18291 first_die = part_die;
18293 /* Maybe add the DIE to the hash table. Not all DIEs that we
18294 find interesting need to be in the hash table, because we
18295 also have the parent/sibling/child chains; only those that we
18296 might refer to by offset later during partial symbol reading.
18298 For now this means things that might have be the target of a
18299 DW_AT_specification, DW_AT_abstract_origin, or
18300 DW_AT_extension. DW_AT_extension will refer only to
18301 namespaces; DW_AT_abstract_origin refers to functions (and
18302 many things under the function DIE, but we do not recurse
18303 into function DIEs during partial symbol reading) and
18304 possibly variables as well; DW_AT_specification refers to
18305 declarations. Declarations ought to have the DW_AT_declaration
18306 flag. It happens that GCC forgets to put it in sometimes, but
18307 only for functions, not for types.
18309 Adding more things than necessary to the hash table is harmless
18310 except for the performance cost. Adding too few will result in
18311 wasted time in find_partial_die, when we reread the compilation
18312 unit with load_all_dies set. */
18315 || abbrev->tag == DW_TAG_constant
18316 || abbrev->tag == DW_TAG_subprogram
18317 || abbrev->tag == DW_TAG_variable
18318 || abbrev->tag == DW_TAG_namespace
18319 || part_die->is_declaration)
18323 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18324 to_underlying (part_die->sect_off),
18329 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
18331 /* For some DIEs we want to follow their children (if any). For C
18332 we have no reason to follow the children of structures; for other
18333 languages we have to, so that we can get at method physnames
18334 to infer fully qualified class names, for DW_AT_specification,
18335 and for C++ template arguments. For C++, we also look one level
18336 inside functions to find template arguments (if the name of the
18337 function does not already contain the template arguments).
18339 For Ada, we need to scan the children of subprograms and lexical
18340 blocks as well because Ada allows the definition of nested
18341 entities that could be interesting for the debugger, such as
18342 nested subprograms for instance. */
18343 if (last_die->has_children
18345 || last_die->tag == DW_TAG_namespace
18346 || last_die->tag == DW_TAG_module
18347 || last_die->tag == DW_TAG_enumeration_type
18348 || (cu->language == language_cplus
18349 && last_die->tag == DW_TAG_subprogram
18350 && (last_die->name == NULL
18351 || strchr (last_die->name, '<') == NULL))
18352 || (cu->language != language_c
18353 && (last_die->tag == DW_TAG_class_type
18354 || last_die->tag == DW_TAG_interface_type
18355 || last_die->tag == DW_TAG_structure_type
18356 || last_die->tag == DW_TAG_union_type))
18357 || (cu->language == language_ada
18358 && (last_die->tag == DW_TAG_subprogram
18359 || last_die->tag == DW_TAG_lexical_block))))
18362 parent_die = last_die;
18366 /* Otherwise we skip to the next sibling, if any. */
18367 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18369 /* Back to the top, do it again. */
18373 /* Read a minimal amount of information into the minimal die structure. */
18375 static const gdb_byte *
18376 read_partial_die (const struct die_reader_specs *reader,
18377 struct partial_die_info *part_die,
18378 struct abbrev_info *abbrev, unsigned int abbrev_len,
18379 const gdb_byte *info_ptr)
18381 struct dwarf2_cu *cu = reader->cu;
18382 struct objfile *objfile = cu->objfile;
18383 const gdb_byte *buffer = reader->buffer;
18385 struct attribute attr;
18386 int has_low_pc_attr = 0;
18387 int has_high_pc_attr = 0;
18388 int high_pc_relative = 0;
18390 memset (part_die, 0, sizeof (struct partial_die_info));
18392 part_die->sect_off = (sect_offset) (info_ptr - buffer);
18394 info_ptr += abbrev_len;
18396 if (abbrev == NULL)
18399 part_die->tag = abbrev->tag;
18400 part_die->has_children = abbrev->has_children;
18402 for (i = 0; i < abbrev->num_attrs; ++i)
18404 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
18406 /* Store the data if it is of an attribute we want to keep in a
18407 partial symbol table. */
18411 switch (part_die->tag)
18413 case DW_TAG_compile_unit:
18414 case DW_TAG_partial_unit:
18415 case DW_TAG_type_unit:
18416 /* Compilation units have a DW_AT_name that is a filename, not
18417 a source language identifier. */
18418 case DW_TAG_enumeration_type:
18419 case DW_TAG_enumerator:
18420 /* These tags always have simple identifiers already; no need
18421 to canonicalize them. */
18422 part_die->name = DW_STRING (&attr);
18426 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18427 &objfile->per_bfd->storage_obstack);
18431 case DW_AT_linkage_name:
18432 case DW_AT_MIPS_linkage_name:
18433 /* Note that both forms of linkage name might appear. We
18434 assume they will be the same, and we only store the last
18436 if (cu->language == language_ada)
18437 part_die->name = DW_STRING (&attr);
18438 part_die->linkage_name = DW_STRING (&attr);
18441 has_low_pc_attr = 1;
18442 part_die->lowpc = attr_value_as_address (&attr);
18444 case DW_AT_high_pc:
18445 has_high_pc_attr = 1;
18446 part_die->highpc = attr_value_as_address (&attr);
18447 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18448 high_pc_relative = 1;
18450 case DW_AT_location:
18451 /* Support the .debug_loc offsets. */
18452 if (attr_form_is_block (&attr))
18454 part_die->d.locdesc = DW_BLOCK (&attr);
18456 else if (attr_form_is_section_offset (&attr))
18458 dwarf2_complex_location_expr_complaint ();
18462 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18463 "partial symbol information");
18466 case DW_AT_external:
18467 part_die->is_external = DW_UNSND (&attr);
18469 case DW_AT_declaration:
18470 part_die->is_declaration = DW_UNSND (&attr);
18473 part_die->has_type = 1;
18475 case DW_AT_abstract_origin:
18476 case DW_AT_specification:
18477 case DW_AT_extension:
18478 part_die->has_specification = 1;
18479 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
18480 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18481 || cu->per_cu->is_dwz);
18483 case DW_AT_sibling:
18484 /* Ignore absolute siblings, they might point outside of
18485 the current compile unit. */
18486 if (attr.form == DW_FORM_ref_addr)
18487 complaint (&symfile_complaints,
18488 _("ignoring absolute DW_AT_sibling"));
18491 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18492 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18494 if (sibling_ptr < info_ptr)
18495 complaint (&symfile_complaints,
18496 _("DW_AT_sibling points backwards"));
18497 else if (sibling_ptr > reader->buffer_end)
18498 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18500 part_die->sibling = sibling_ptr;
18503 case DW_AT_byte_size:
18504 part_die->has_byte_size = 1;
18506 case DW_AT_const_value:
18507 part_die->has_const_value = 1;
18509 case DW_AT_calling_convention:
18510 /* DWARF doesn't provide a way to identify a program's source-level
18511 entry point. DW_AT_calling_convention attributes are only meant
18512 to describe functions' calling conventions.
18514 However, because it's a necessary piece of information in
18515 Fortran, and before DWARF 4 DW_CC_program was the only
18516 piece of debugging information whose definition refers to
18517 a 'main program' at all, several compilers marked Fortran
18518 main programs with DW_CC_program --- even when those
18519 functions use the standard calling conventions.
18521 Although DWARF now specifies a way to provide this
18522 information, we support this practice for backward
18524 if (DW_UNSND (&attr) == DW_CC_program
18525 && cu->language == language_fortran)
18526 part_die->main_subprogram = 1;
18529 if (DW_UNSND (&attr) == DW_INL_inlined
18530 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18531 part_die->may_be_inlined = 1;
18535 if (part_die->tag == DW_TAG_imported_unit)
18537 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
18538 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18539 || cu->per_cu->is_dwz);
18543 case DW_AT_main_subprogram:
18544 part_die->main_subprogram = DW_UNSND (&attr);
18552 if (high_pc_relative)
18553 part_die->highpc += part_die->lowpc;
18555 if (has_low_pc_attr && has_high_pc_attr)
18557 /* When using the GNU linker, .gnu.linkonce. sections are used to
18558 eliminate duplicate copies of functions and vtables and such.
18559 The linker will arbitrarily choose one and discard the others.
18560 The AT_*_pc values for such functions refer to local labels in
18561 these sections. If the section from that file was discarded, the
18562 labels are not in the output, so the relocs get a value of 0.
18563 If this is a discarded function, mark the pc bounds as invalid,
18564 so that GDB will ignore it. */
18565 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18567 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18569 complaint (&symfile_complaints,
18570 _("DW_AT_low_pc %s is zero "
18571 "for DIE at 0x%x [in module %s]"),
18572 paddress (gdbarch, part_die->lowpc),
18573 to_underlying (part_die->sect_off), objfile_name (objfile));
18575 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18576 else if (part_die->lowpc >= part_die->highpc)
18578 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18580 complaint (&symfile_complaints,
18581 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18582 "for DIE at 0x%x [in module %s]"),
18583 paddress (gdbarch, part_die->lowpc),
18584 paddress (gdbarch, part_die->highpc),
18585 to_underlying (part_die->sect_off),
18586 objfile_name (objfile));
18589 part_die->has_pc_info = 1;
18595 /* Find a cached partial DIE at OFFSET in CU. */
18597 static struct partial_die_info *
18598 find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
18600 struct partial_die_info *lookup_die = NULL;
18601 struct partial_die_info part_die;
18603 part_die.sect_off = sect_off;
18604 lookup_die = ((struct partial_die_info *)
18605 htab_find_with_hash (cu->partial_dies, &part_die,
18606 to_underlying (sect_off)));
18611 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18612 except in the case of .debug_types DIEs which do not reference
18613 outside their CU (they do however referencing other types via
18614 DW_FORM_ref_sig8). */
18616 static struct partial_die_info *
18617 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18619 struct objfile *objfile = cu->objfile;
18620 struct dwarf2_per_cu_data *per_cu = NULL;
18621 struct partial_die_info *pd = NULL;
18623 if (offset_in_dwz == cu->per_cu->is_dwz
18624 && offset_in_cu_p (&cu->header, sect_off))
18626 pd = find_partial_die_in_comp_unit (sect_off, cu);
18629 /* We missed recording what we needed.
18630 Load all dies and try again. */
18631 per_cu = cu->per_cu;
18635 /* TUs don't reference other CUs/TUs (except via type signatures). */
18636 if (cu->per_cu->is_debug_types)
18638 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
18639 " external reference to offset 0x%x [in module %s].\n"),
18640 to_underlying (cu->header.sect_off), to_underlying (sect_off),
18641 bfd_get_filename (objfile->obfd));
18643 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18646 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18647 load_partial_comp_unit (per_cu);
18649 per_cu->cu->last_used = 0;
18650 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
18653 /* If we didn't find it, and not all dies have been loaded,
18654 load them all and try again. */
18656 if (pd == NULL && per_cu->load_all_dies == 0)
18658 per_cu->load_all_dies = 1;
18660 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18661 THIS_CU->cu may already be in use. So we can't just free it and
18662 replace its DIEs with the ones we read in. Instead, we leave those
18663 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18664 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18666 load_partial_comp_unit (per_cu);
18668 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
18672 internal_error (__FILE__, __LINE__,
18673 _("could not find partial DIE 0x%x "
18674 "in cache [from module %s]\n"),
18675 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
18679 /* See if we can figure out if the class lives in a namespace. We do
18680 this by looking for a member function; its demangled name will
18681 contain namespace info, if there is any. */
18684 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18685 struct dwarf2_cu *cu)
18687 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18688 what template types look like, because the demangler
18689 frequently doesn't give the same name as the debug info. We
18690 could fix this by only using the demangled name to get the
18691 prefix (but see comment in read_structure_type). */
18693 struct partial_die_info *real_pdi;
18694 struct partial_die_info *child_pdi;
18696 /* If this DIE (this DIE's specification, if any) has a parent, then
18697 we should not do this. We'll prepend the parent's fully qualified
18698 name when we create the partial symbol. */
18700 real_pdi = struct_pdi;
18701 while (real_pdi->has_specification)
18702 real_pdi = find_partial_die (real_pdi->spec_offset,
18703 real_pdi->spec_is_dwz, cu);
18705 if (real_pdi->die_parent != NULL)
18708 for (child_pdi = struct_pdi->die_child;
18710 child_pdi = child_pdi->die_sibling)
18712 if (child_pdi->tag == DW_TAG_subprogram
18713 && child_pdi->linkage_name != NULL)
18715 char *actual_class_name
18716 = language_class_name_from_physname (cu->language_defn,
18717 child_pdi->linkage_name);
18718 if (actual_class_name != NULL)
18722 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
18724 strlen (actual_class_name)));
18725 xfree (actual_class_name);
18732 /* Adjust PART_DIE before generating a symbol for it. This function
18733 may set the is_external flag or change the DIE's name. */
18736 fixup_partial_die (struct partial_die_info *part_die,
18737 struct dwarf2_cu *cu)
18739 /* Once we've fixed up a die, there's no point in doing so again.
18740 This also avoids a memory leak if we were to call
18741 guess_partial_die_structure_name multiple times. */
18742 if (part_die->fixup_called)
18745 /* If we found a reference attribute and the DIE has no name, try
18746 to find a name in the referred to DIE. */
18748 if (part_die->name == NULL && part_die->has_specification)
18750 struct partial_die_info *spec_die;
18752 spec_die = find_partial_die (part_die->spec_offset,
18753 part_die->spec_is_dwz, cu);
18755 fixup_partial_die (spec_die, cu);
18757 if (spec_die->name)
18759 part_die->name = spec_die->name;
18761 /* Copy DW_AT_external attribute if it is set. */
18762 if (spec_die->is_external)
18763 part_die->is_external = spec_die->is_external;
18767 /* Set default names for some unnamed DIEs. */
18769 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
18770 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
18772 /* If there is no parent die to provide a namespace, and there are
18773 children, see if we can determine the namespace from their linkage
18775 if (cu->language == language_cplus
18776 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
18777 && part_die->die_parent == NULL
18778 && part_die->has_children
18779 && (part_die->tag == DW_TAG_class_type
18780 || part_die->tag == DW_TAG_structure_type
18781 || part_die->tag == DW_TAG_union_type))
18782 guess_partial_die_structure_name (part_die, cu);
18784 /* GCC might emit a nameless struct or union that has a linkage
18785 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18786 if (part_die->name == NULL
18787 && (part_die->tag == DW_TAG_class_type
18788 || part_die->tag == DW_TAG_interface_type
18789 || part_die->tag == DW_TAG_structure_type
18790 || part_die->tag == DW_TAG_union_type)
18791 && part_die->linkage_name != NULL)
18795 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
18800 /* Strip any leading namespaces/classes, keep only the base name.
18801 DW_AT_name for named DIEs does not contain the prefixes. */
18802 base = strrchr (demangled, ':');
18803 if (base && base > demangled && base[-1] == ':')
18810 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
18811 base, strlen (base)));
18816 part_die->fixup_called = 1;
18819 /* Read an attribute value described by an attribute form. */
18821 static const gdb_byte *
18822 read_attribute_value (const struct die_reader_specs *reader,
18823 struct attribute *attr, unsigned form,
18824 LONGEST implicit_const, const gdb_byte *info_ptr)
18826 struct dwarf2_cu *cu = reader->cu;
18827 struct objfile *objfile = cu->objfile;
18828 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18829 bfd *abfd = reader->abfd;
18830 struct comp_unit_head *cu_header = &cu->header;
18831 unsigned int bytes_read;
18832 struct dwarf_block *blk;
18834 attr->form = (enum dwarf_form) form;
18837 case DW_FORM_ref_addr:
18838 if (cu->header.version == 2)
18839 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18841 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18842 &cu->header, &bytes_read);
18843 info_ptr += bytes_read;
18845 case DW_FORM_GNU_ref_alt:
18846 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18847 info_ptr += bytes_read;
18850 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18851 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18852 info_ptr += bytes_read;
18854 case DW_FORM_block2:
18855 blk = dwarf_alloc_block (cu);
18856 blk->size = read_2_bytes (abfd, info_ptr);
18858 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18859 info_ptr += blk->size;
18860 DW_BLOCK (attr) = blk;
18862 case DW_FORM_block4:
18863 blk = dwarf_alloc_block (cu);
18864 blk->size = read_4_bytes (abfd, info_ptr);
18866 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18867 info_ptr += blk->size;
18868 DW_BLOCK (attr) = blk;
18870 case DW_FORM_data2:
18871 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18874 case DW_FORM_data4:
18875 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18878 case DW_FORM_data8:
18879 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18882 case DW_FORM_data16:
18883 blk = dwarf_alloc_block (cu);
18885 blk->data = read_n_bytes (abfd, info_ptr, 16);
18887 DW_BLOCK (attr) = blk;
18889 case DW_FORM_sec_offset:
18890 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18891 info_ptr += bytes_read;
18893 case DW_FORM_string:
18894 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18895 DW_STRING_IS_CANONICAL (attr) = 0;
18896 info_ptr += bytes_read;
18899 if (!cu->per_cu->is_dwz)
18901 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
18903 DW_STRING_IS_CANONICAL (attr) = 0;
18904 info_ptr += bytes_read;
18908 case DW_FORM_line_strp:
18909 if (!cu->per_cu->is_dwz)
18911 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
18912 cu_header, &bytes_read);
18913 DW_STRING_IS_CANONICAL (attr) = 0;
18914 info_ptr += bytes_read;
18918 case DW_FORM_GNU_strp_alt:
18920 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18921 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
18924 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
18925 DW_STRING_IS_CANONICAL (attr) = 0;
18926 info_ptr += bytes_read;
18929 case DW_FORM_exprloc:
18930 case DW_FORM_block:
18931 blk = dwarf_alloc_block (cu);
18932 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18933 info_ptr += bytes_read;
18934 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18935 info_ptr += blk->size;
18936 DW_BLOCK (attr) = blk;
18938 case DW_FORM_block1:
18939 blk = dwarf_alloc_block (cu);
18940 blk->size = read_1_byte (abfd, info_ptr);
18942 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18943 info_ptr += blk->size;
18944 DW_BLOCK (attr) = blk;
18946 case DW_FORM_data1:
18947 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18951 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18954 case DW_FORM_flag_present:
18955 DW_UNSND (attr) = 1;
18957 case DW_FORM_sdata:
18958 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18959 info_ptr += bytes_read;
18961 case DW_FORM_udata:
18962 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18963 info_ptr += bytes_read;
18966 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18967 + read_1_byte (abfd, info_ptr));
18971 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18972 + read_2_bytes (abfd, info_ptr));
18976 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18977 + read_4_bytes (abfd, info_ptr));
18981 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18982 + read_8_bytes (abfd, info_ptr));
18985 case DW_FORM_ref_sig8:
18986 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18989 case DW_FORM_ref_udata:
18990 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18991 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18992 info_ptr += bytes_read;
18994 case DW_FORM_indirect:
18995 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18996 info_ptr += bytes_read;
18997 if (form == DW_FORM_implicit_const)
18999 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19000 info_ptr += bytes_read;
19002 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19005 case DW_FORM_implicit_const:
19006 DW_SND (attr) = implicit_const;
19008 case DW_FORM_GNU_addr_index:
19009 if (reader->dwo_file == NULL)
19011 /* For now flag a hard error.
19012 Later we can turn this into a complaint. */
19013 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19014 dwarf_form_name (form),
19015 bfd_get_filename (abfd));
19017 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19018 info_ptr += bytes_read;
19020 case DW_FORM_GNU_str_index:
19021 if (reader->dwo_file == NULL)
19023 /* For now flag a hard error.
19024 Later we can turn this into a complaint if warranted. */
19025 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19026 dwarf_form_name (form),
19027 bfd_get_filename (abfd));
19030 ULONGEST str_index =
19031 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19033 DW_STRING (attr) = read_str_index (reader, str_index);
19034 DW_STRING_IS_CANONICAL (attr) = 0;
19035 info_ptr += bytes_read;
19039 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19040 dwarf_form_name (form),
19041 bfd_get_filename (abfd));
19045 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19046 attr->form = DW_FORM_GNU_ref_alt;
19048 /* We have seen instances where the compiler tried to emit a byte
19049 size attribute of -1 which ended up being encoded as an unsigned
19050 0xffffffff. Although 0xffffffff is technically a valid size value,
19051 an object of this size seems pretty unlikely so we can relatively
19052 safely treat these cases as if the size attribute was invalid and
19053 treat them as zero by default. */
19054 if (attr->name == DW_AT_byte_size
19055 && form == DW_FORM_data4
19056 && DW_UNSND (attr) >= 0xffffffff)
19059 (&symfile_complaints,
19060 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19061 hex_string (DW_UNSND (attr)));
19062 DW_UNSND (attr) = 0;
19068 /* Read an attribute described by an abbreviated attribute. */
19070 static const gdb_byte *
19071 read_attribute (const struct die_reader_specs *reader,
19072 struct attribute *attr, struct attr_abbrev *abbrev,
19073 const gdb_byte *info_ptr)
19075 attr->name = abbrev->name;
19076 return read_attribute_value (reader, attr, abbrev->form,
19077 abbrev->implicit_const, info_ptr);
19080 /* Read dwarf information from a buffer. */
19082 static unsigned int
19083 read_1_byte (bfd *abfd, const gdb_byte *buf)
19085 return bfd_get_8 (abfd, buf);
19089 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19091 return bfd_get_signed_8 (abfd, buf);
19094 static unsigned int
19095 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19097 return bfd_get_16 (abfd, buf);
19101 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19103 return bfd_get_signed_16 (abfd, buf);
19106 static unsigned int
19107 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19109 return bfd_get_32 (abfd, buf);
19113 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19115 return bfd_get_signed_32 (abfd, buf);
19119 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19121 return bfd_get_64 (abfd, buf);
19125 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19126 unsigned int *bytes_read)
19128 struct comp_unit_head *cu_header = &cu->header;
19129 CORE_ADDR retval = 0;
19131 if (cu_header->signed_addr_p)
19133 switch (cu_header->addr_size)
19136 retval = bfd_get_signed_16 (abfd, buf);
19139 retval = bfd_get_signed_32 (abfd, buf);
19142 retval = bfd_get_signed_64 (abfd, buf);
19145 internal_error (__FILE__, __LINE__,
19146 _("read_address: bad switch, signed [in module %s]"),
19147 bfd_get_filename (abfd));
19152 switch (cu_header->addr_size)
19155 retval = bfd_get_16 (abfd, buf);
19158 retval = bfd_get_32 (abfd, buf);
19161 retval = bfd_get_64 (abfd, buf);
19164 internal_error (__FILE__, __LINE__,
19165 _("read_address: bad switch, "
19166 "unsigned [in module %s]"),
19167 bfd_get_filename (abfd));
19171 *bytes_read = cu_header->addr_size;
19175 /* Read the initial length from a section. The (draft) DWARF 3
19176 specification allows the initial length to take up either 4 bytes
19177 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19178 bytes describe the length and all offsets will be 8 bytes in length
19181 An older, non-standard 64-bit format is also handled by this
19182 function. The older format in question stores the initial length
19183 as an 8-byte quantity without an escape value. Lengths greater
19184 than 2^32 aren't very common which means that the initial 4 bytes
19185 is almost always zero. Since a length value of zero doesn't make
19186 sense for the 32-bit format, this initial zero can be considered to
19187 be an escape value which indicates the presence of the older 64-bit
19188 format. As written, the code can't detect (old format) lengths
19189 greater than 4GB. If it becomes necessary to handle lengths
19190 somewhat larger than 4GB, we could allow other small values (such
19191 as the non-sensical values of 1, 2, and 3) to also be used as
19192 escape values indicating the presence of the old format.
19194 The value returned via bytes_read should be used to increment the
19195 relevant pointer after calling read_initial_length().
19197 [ Note: read_initial_length() and read_offset() are based on the
19198 document entitled "DWARF Debugging Information Format", revision
19199 3, draft 8, dated November 19, 2001. This document was obtained
19202 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19204 This document is only a draft and is subject to change. (So beware.)
19206 Details regarding the older, non-standard 64-bit format were
19207 determined empirically by examining 64-bit ELF files produced by
19208 the SGI toolchain on an IRIX 6.5 machine.
19210 - Kevin, July 16, 2002
19214 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19216 LONGEST length = bfd_get_32 (abfd, buf);
19218 if (length == 0xffffffff)
19220 length = bfd_get_64 (abfd, buf + 4);
19223 else if (length == 0)
19225 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19226 length = bfd_get_64 (abfd, buf);
19237 /* Cover function for read_initial_length.
19238 Returns the length of the object at BUF, and stores the size of the
19239 initial length in *BYTES_READ and stores the size that offsets will be in
19241 If the initial length size is not equivalent to that specified in
19242 CU_HEADER then issue a complaint.
19243 This is useful when reading non-comp-unit headers. */
19246 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19247 const struct comp_unit_head *cu_header,
19248 unsigned int *bytes_read,
19249 unsigned int *offset_size)
19251 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19253 gdb_assert (cu_header->initial_length_size == 4
19254 || cu_header->initial_length_size == 8
19255 || cu_header->initial_length_size == 12);
19257 if (cu_header->initial_length_size != *bytes_read)
19258 complaint (&symfile_complaints,
19259 _("intermixed 32-bit and 64-bit DWARF sections"));
19261 *offset_size = (*bytes_read == 4) ? 4 : 8;
19265 /* Read an offset from the data stream. The size of the offset is
19266 given by cu_header->offset_size. */
19269 read_offset (bfd *abfd, const gdb_byte *buf,
19270 const struct comp_unit_head *cu_header,
19271 unsigned int *bytes_read)
19273 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19275 *bytes_read = cu_header->offset_size;
19279 /* Read an offset from the data stream. */
19282 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19284 LONGEST retval = 0;
19286 switch (offset_size)
19289 retval = bfd_get_32 (abfd, buf);
19292 retval = bfd_get_64 (abfd, buf);
19295 internal_error (__FILE__, __LINE__,
19296 _("read_offset_1: bad switch [in module %s]"),
19297 bfd_get_filename (abfd));
19303 static const gdb_byte *
19304 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19306 /* If the size of a host char is 8 bits, we can return a pointer
19307 to the buffer, otherwise we have to copy the data to a buffer
19308 allocated on the temporary obstack. */
19309 gdb_assert (HOST_CHAR_BIT == 8);
19313 static const char *
19314 read_direct_string (bfd *abfd, const gdb_byte *buf,
19315 unsigned int *bytes_read_ptr)
19317 /* If the size of a host char is 8 bits, we can return a pointer
19318 to the string, otherwise we have to copy the string to a buffer
19319 allocated on the temporary obstack. */
19320 gdb_assert (HOST_CHAR_BIT == 8);
19323 *bytes_read_ptr = 1;
19326 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19327 return (const char *) buf;
19330 /* Return pointer to string at section SECT offset STR_OFFSET with error
19331 reporting strings FORM_NAME and SECT_NAME. */
19333 static const char *
19334 read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
19335 struct dwarf2_section_info *sect,
19336 const char *form_name,
19337 const char *sect_name)
19339 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
19340 if (sect->buffer == NULL)
19341 error (_("%s used without %s section [in module %s]"),
19342 form_name, sect_name, bfd_get_filename (abfd));
19343 if (str_offset >= sect->size)
19344 error (_("%s pointing outside of %s section [in module %s]"),
19345 form_name, sect_name, bfd_get_filename (abfd));
19346 gdb_assert (HOST_CHAR_BIT == 8);
19347 if (sect->buffer[str_offset] == '\0')
19349 return (const char *) (sect->buffer + str_offset);
19352 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19354 static const char *
19355 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
19357 return read_indirect_string_at_offset_from (abfd, str_offset,
19358 &dwarf2_per_objfile->str,
19359 "DW_FORM_strp", ".debug_str");
19362 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19364 static const char *
19365 read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
19367 return read_indirect_string_at_offset_from (abfd, str_offset,
19368 &dwarf2_per_objfile->line_str,
19369 "DW_FORM_line_strp",
19370 ".debug_line_str");
19373 /* Read a string at offset STR_OFFSET in the .debug_str section from
19374 the .dwz file DWZ. Throw an error if the offset is too large. If
19375 the string consists of a single NUL byte, return NULL; otherwise
19376 return a pointer to the string. */
19378 static const char *
19379 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
19381 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
19383 if (dwz->str.buffer == NULL)
19384 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19385 "section [in module %s]"),
19386 bfd_get_filename (dwz->dwz_bfd));
19387 if (str_offset >= dwz->str.size)
19388 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19389 ".debug_str section [in module %s]"),
19390 bfd_get_filename (dwz->dwz_bfd));
19391 gdb_assert (HOST_CHAR_BIT == 8);
19392 if (dwz->str.buffer[str_offset] == '\0')
19394 return (const char *) (dwz->str.buffer + str_offset);
19397 /* Return pointer to string at .debug_str offset as read from BUF.
19398 BUF is assumed to be in a compilation unit described by CU_HEADER.
19399 Return *BYTES_READ_PTR count of bytes read from BUF. */
19401 static const char *
19402 read_indirect_string (bfd *abfd, const gdb_byte *buf,
19403 const struct comp_unit_head *cu_header,
19404 unsigned int *bytes_read_ptr)
19406 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19408 return read_indirect_string_at_offset (abfd, str_offset);
19411 /* Return pointer to string at .debug_line_str offset as read from BUF.
19412 BUF is assumed to be in a compilation unit described by CU_HEADER.
19413 Return *BYTES_READ_PTR count of bytes read from BUF. */
19415 static const char *
19416 read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
19417 const struct comp_unit_head *cu_header,
19418 unsigned int *bytes_read_ptr)
19420 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19422 return read_indirect_line_string_at_offset (abfd, str_offset);
19426 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19427 unsigned int *bytes_read_ptr)
19430 unsigned int num_read;
19432 unsigned char byte;
19439 byte = bfd_get_8 (abfd, buf);
19442 result |= ((ULONGEST) (byte & 127) << shift);
19443 if ((byte & 128) == 0)
19449 *bytes_read_ptr = num_read;
19454 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19455 unsigned int *bytes_read_ptr)
19458 int shift, num_read;
19459 unsigned char byte;
19466 byte = bfd_get_8 (abfd, buf);
19469 result |= ((LONGEST) (byte & 127) << shift);
19471 if ((byte & 128) == 0)
19476 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19477 result |= -(((LONGEST) 1) << shift);
19478 *bytes_read_ptr = num_read;
19482 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19483 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19484 ADDR_SIZE is the size of addresses from the CU header. */
19487 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
19489 struct objfile *objfile = dwarf2_per_objfile->objfile;
19490 bfd *abfd = objfile->obfd;
19491 const gdb_byte *info_ptr;
19493 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19494 if (dwarf2_per_objfile->addr.buffer == NULL)
19495 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19496 objfile_name (objfile));
19497 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19498 error (_("DW_FORM_addr_index pointing outside of "
19499 ".debug_addr section [in module %s]"),
19500 objfile_name (objfile));
19501 info_ptr = (dwarf2_per_objfile->addr.buffer
19502 + addr_base + addr_index * addr_size);
19503 if (addr_size == 4)
19504 return bfd_get_32 (abfd, info_ptr);
19506 return bfd_get_64 (abfd, info_ptr);
19509 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19512 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19514 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
19517 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19520 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19521 unsigned int *bytes_read)
19523 bfd *abfd = cu->objfile->obfd;
19524 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19526 return read_addr_index (cu, addr_index);
19529 /* Data structure to pass results from dwarf2_read_addr_index_reader
19530 back to dwarf2_read_addr_index. */
19532 struct dwarf2_read_addr_index_data
19534 ULONGEST addr_base;
19538 /* die_reader_func for dwarf2_read_addr_index. */
19541 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19542 const gdb_byte *info_ptr,
19543 struct die_info *comp_unit_die,
19547 struct dwarf2_cu *cu = reader->cu;
19548 struct dwarf2_read_addr_index_data *aidata =
19549 (struct dwarf2_read_addr_index_data *) data;
19551 aidata->addr_base = cu->addr_base;
19552 aidata->addr_size = cu->header.addr_size;
19555 /* Given an index in .debug_addr, fetch the value.
19556 NOTE: This can be called during dwarf expression evaluation,
19557 long after the debug information has been read, and thus per_cu->cu
19558 may no longer exist. */
19561 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19562 unsigned int addr_index)
19564 struct objfile *objfile = per_cu->objfile;
19565 struct dwarf2_cu *cu = per_cu->cu;
19566 ULONGEST addr_base;
19569 /* This is intended to be called from outside this file. */
19570 dw2_setup (objfile);
19572 /* We need addr_base and addr_size.
19573 If we don't have PER_CU->cu, we have to get it.
19574 Nasty, but the alternative is storing the needed info in PER_CU,
19575 which at this point doesn't seem justified: it's not clear how frequently
19576 it would get used and it would increase the size of every PER_CU.
19577 Entry points like dwarf2_per_cu_addr_size do a similar thing
19578 so we're not in uncharted territory here.
19579 Alas we need to be a bit more complicated as addr_base is contained
19582 We don't need to read the entire CU(/TU).
19583 We just need the header and top level die.
19585 IWBN to use the aging mechanism to let us lazily later discard the CU.
19586 For now we skip this optimization. */
19590 addr_base = cu->addr_base;
19591 addr_size = cu->header.addr_size;
19595 struct dwarf2_read_addr_index_data aidata;
19597 /* Note: We can't use init_cutu_and_read_dies_simple here,
19598 we need addr_base. */
19599 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
19600 dwarf2_read_addr_index_reader, &aidata);
19601 addr_base = aidata.addr_base;
19602 addr_size = aidata.addr_size;
19605 return read_addr_index_1 (addr_index, addr_base, addr_size);
19608 /* Given a DW_FORM_GNU_str_index, fetch the string.
19609 This is only used by the Fission support. */
19611 static const char *
19612 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19614 struct objfile *objfile = dwarf2_per_objfile->objfile;
19615 const char *objf_name = objfile_name (objfile);
19616 bfd *abfd = objfile->obfd;
19617 struct dwarf2_cu *cu = reader->cu;
19618 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19619 struct dwarf2_section_info *str_offsets_section =
19620 &reader->dwo_file->sections.str_offsets;
19621 const gdb_byte *info_ptr;
19622 ULONGEST str_offset;
19623 static const char form_name[] = "DW_FORM_GNU_str_index";
19625 dwarf2_read_section (objfile, str_section);
19626 dwarf2_read_section (objfile, str_offsets_section);
19627 if (str_section->buffer == NULL)
19628 error (_("%s used without .debug_str.dwo section"
19629 " in CU at offset 0x%x [in module %s]"),
19630 form_name, to_underlying (cu->header.sect_off), objf_name);
19631 if (str_offsets_section->buffer == NULL)
19632 error (_("%s used without .debug_str_offsets.dwo section"
19633 " in CU at offset 0x%x [in module %s]"),
19634 form_name, to_underlying (cu->header.sect_off), objf_name);
19635 if (str_index * cu->header.offset_size >= str_offsets_section->size)
19636 error (_("%s pointing outside of .debug_str_offsets.dwo"
19637 " section in CU at offset 0x%x [in module %s]"),
19638 form_name, to_underlying (cu->header.sect_off), objf_name);
19639 info_ptr = (str_offsets_section->buffer
19640 + str_index * cu->header.offset_size);
19641 if (cu->header.offset_size == 4)
19642 str_offset = bfd_get_32 (abfd, info_ptr);
19644 str_offset = bfd_get_64 (abfd, info_ptr);
19645 if (str_offset >= str_section->size)
19646 error (_("Offset from %s pointing outside of"
19647 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
19648 form_name, to_underlying (cu->header.sect_off), objf_name);
19649 return (const char *) (str_section->buffer + str_offset);
19652 /* Return the length of an LEB128 number in BUF. */
19655 leb128_size (const gdb_byte *buf)
19657 const gdb_byte *begin = buf;
19663 if ((byte & 128) == 0)
19664 return buf - begin;
19669 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19678 cu->language = language_c;
19681 case DW_LANG_C_plus_plus:
19682 case DW_LANG_C_plus_plus_11:
19683 case DW_LANG_C_plus_plus_14:
19684 cu->language = language_cplus;
19687 cu->language = language_d;
19689 case DW_LANG_Fortran77:
19690 case DW_LANG_Fortran90:
19691 case DW_LANG_Fortran95:
19692 case DW_LANG_Fortran03:
19693 case DW_LANG_Fortran08:
19694 cu->language = language_fortran;
19697 cu->language = language_go;
19699 case DW_LANG_Mips_Assembler:
19700 cu->language = language_asm;
19702 case DW_LANG_Ada83:
19703 case DW_LANG_Ada95:
19704 cu->language = language_ada;
19706 case DW_LANG_Modula2:
19707 cu->language = language_m2;
19709 case DW_LANG_Pascal83:
19710 cu->language = language_pascal;
19713 cu->language = language_objc;
19716 case DW_LANG_Rust_old:
19717 cu->language = language_rust;
19719 case DW_LANG_Cobol74:
19720 case DW_LANG_Cobol85:
19722 cu->language = language_minimal;
19725 cu->language_defn = language_def (cu->language);
19728 /* Return the named attribute or NULL if not there. */
19730 static struct attribute *
19731 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19736 struct attribute *spec = NULL;
19738 for (i = 0; i < die->num_attrs; ++i)
19740 if (die->attrs[i].name == name)
19741 return &die->attrs[i];
19742 if (die->attrs[i].name == DW_AT_specification
19743 || die->attrs[i].name == DW_AT_abstract_origin)
19744 spec = &die->attrs[i];
19750 die = follow_die_ref (die, spec, &cu);
19756 /* Return the named attribute or NULL if not there,
19757 but do not follow DW_AT_specification, etc.
19758 This is for use in contexts where we're reading .debug_types dies.
19759 Following DW_AT_specification, DW_AT_abstract_origin will take us
19760 back up the chain, and we want to go down. */
19762 static struct attribute *
19763 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19767 for (i = 0; i < die->num_attrs; ++i)
19768 if (die->attrs[i].name == name)
19769 return &die->attrs[i];
19774 /* Return the string associated with a string-typed attribute, or NULL if it
19775 is either not found or is of an incorrect type. */
19777 static const char *
19778 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19780 struct attribute *attr;
19781 const char *str = NULL;
19783 attr = dwarf2_attr (die, name, cu);
19787 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19788 || attr->form == DW_FORM_string
19789 || attr->form == DW_FORM_GNU_str_index
19790 || attr->form == DW_FORM_GNU_strp_alt)
19791 str = DW_STRING (attr);
19793 complaint (&symfile_complaints,
19794 _("string type expected for attribute %s for "
19795 "DIE at 0x%x in module %s"),
19796 dwarf_attr_name (name), to_underlying (die->sect_off),
19797 objfile_name (cu->objfile));
19803 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19804 and holds a non-zero value. This function should only be used for
19805 DW_FORM_flag or DW_FORM_flag_present attributes. */
19808 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19810 struct attribute *attr = dwarf2_attr (die, name, cu);
19812 return (attr && DW_UNSND (attr));
19816 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19818 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19819 which value is non-zero. However, we have to be careful with
19820 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19821 (via dwarf2_flag_true_p) follows this attribute. So we may
19822 end up accidently finding a declaration attribute that belongs
19823 to a different DIE referenced by the specification attribute,
19824 even though the given DIE does not have a declaration attribute. */
19825 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19826 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19829 /* Return the die giving the specification for DIE, if there is
19830 one. *SPEC_CU is the CU containing DIE on input, and the CU
19831 containing the return value on output. If there is no
19832 specification, but there is an abstract origin, that is
19835 static struct die_info *
19836 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19838 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19841 if (spec_attr == NULL)
19842 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19844 if (spec_attr == NULL)
19847 return follow_die_ref (die, spec_attr, spec_cu);
19850 /* Stub for free_line_header to match void * callback types. */
19853 free_line_header_voidp (void *arg)
19855 struct line_header *lh = (struct line_header *) arg;
19861 line_header::add_include_dir (const char *include_dir)
19863 if (dwarf_line_debug >= 2)
19864 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19865 include_dirs.size () + 1, include_dir);
19867 include_dirs.push_back (include_dir);
19871 line_header::add_file_name (const char *name,
19873 unsigned int mod_time,
19874 unsigned int length)
19876 if (dwarf_line_debug >= 2)
19877 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
19878 (unsigned) file_names.size () + 1, name);
19880 file_names.emplace_back (name, d_index, mod_time, length);
19883 /* A convenience function to find the proper .debug_line section for a CU. */
19885 static struct dwarf2_section_info *
19886 get_debug_line_section (struct dwarf2_cu *cu)
19888 struct dwarf2_section_info *section;
19890 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19892 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19893 section = &cu->dwo_unit->dwo_file->sections.line;
19894 else if (cu->per_cu->is_dwz)
19896 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19898 section = &dwz->line;
19901 section = &dwarf2_per_objfile->line;
19906 /* Read directory or file name entry format, starting with byte of
19907 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19908 entries count and the entries themselves in the described entry
19912 read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
19913 struct line_header *lh,
19914 const struct comp_unit_head *cu_header,
19915 void (*callback) (struct line_header *lh,
19918 unsigned int mod_time,
19919 unsigned int length))
19921 gdb_byte format_count, formati;
19922 ULONGEST data_count, datai;
19923 const gdb_byte *buf = *bufp;
19924 const gdb_byte *format_header_data;
19925 unsigned int bytes_read;
19927 format_count = read_1_byte (abfd, buf);
19929 format_header_data = buf;
19930 for (formati = 0; formati < format_count; formati++)
19932 read_unsigned_leb128 (abfd, buf, &bytes_read);
19934 read_unsigned_leb128 (abfd, buf, &bytes_read);
19938 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19940 for (datai = 0; datai < data_count; datai++)
19942 const gdb_byte *format = format_header_data;
19943 struct file_entry fe;
19945 for (formati = 0; formati < format_count; formati++)
19947 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
19948 format += bytes_read;
19950 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
19951 format += bytes_read;
19953 gdb::optional<const char *> string;
19954 gdb::optional<unsigned int> uint;
19958 case DW_FORM_string:
19959 string.emplace (read_direct_string (abfd, buf, &bytes_read));
19963 case DW_FORM_line_strp:
19964 string.emplace (read_indirect_line_string (abfd, buf,
19970 case DW_FORM_data1:
19971 uint.emplace (read_1_byte (abfd, buf));
19975 case DW_FORM_data2:
19976 uint.emplace (read_2_bytes (abfd, buf));
19980 case DW_FORM_data4:
19981 uint.emplace (read_4_bytes (abfd, buf));
19985 case DW_FORM_data8:
19986 uint.emplace (read_8_bytes (abfd, buf));
19990 case DW_FORM_udata:
19991 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
19995 case DW_FORM_block:
19996 /* It is valid only for DW_LNCT_timestamp which is ignored by
20001 switch (content_type)
20004 if (string.has_value ())
20007 case DW_LNCT_directory_index:
20008 if (uint.has_value ())
20009 fe.d_index = (dir_index) *uint;
20011 case DW_LNCT_timestamp:
20012 if (uint.has_value ())
20013 fe.mod_time = *uint;
20016 if (uint.has_value ())
20022 complaint (&symfile_complaints,
20023 _("Unknown format content type %s"),
20024 pulongest (content_type));
20028 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20034 /* Read the statement program header starting at OFFSET in
20035 .debug_line, or .debug_line.dwo. Return a pointer
20036 to a struct line_header, allocated using xmalloc.
20037 Returns NULL if there is a problem reading the header, e.g., if it
20038 has a version we don't understand.
20040 NOTE: the strings in the include directory and file name tables of
20041 the returned object point into the dwarf line section buffer,
20042 and must not be freed. */
20044 static line_header_up
20045 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20047 const gdb_byte *line_ptr;
20048 unsigned int bytes_read, offset_size;
20050 const char *cur_dir, *cur_file;
20051 struct dwarf2_section_info *section;
20054 section = get_debug_line_section (cu);
20055 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20056 if (section->buffer == NULL)
20058 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20059 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20061 complaint (&symfile_complaints, _("missing .debug_line section"));
20065 /* We can't do this until we know the section is non-empty.
20066 Only then do we know we have such a section. */
20067 abfd = get_section_bfd_owner (section);
20069 /* Make sure that at least there's room for the total_length field.
20070 That could be 12 bytes long, but we're just going to fudge that. */
20071 if (to_underlying (sect_off) + 4 >= section->size)
20073 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20077 line_header_up lh (new line_header ());
20079 lh->sect_off = sect_off;
20080 lh->offset_in_dwz = cu->per_cu->is_dwz;
20082 line_ptr = section->buffer + to_underlying (sect_off);
20084 /* Read in the header. */
20086 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20087 &bytes_read, &offset_size);
20088 line_ptr += bytes_read;
20089 if (line_ptr + lh->total_length > (section->buffer + section->size))
20091 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20094 lh->statement_program_end = line_ptr + lh->total_length;
20095 lh->version = read_2_bytes (abfd, line_ptr);
20097 if (lh->version > 5)
20099 /* This is a version we don't understand. The format could have
20100 changed in ways we don't handle properly so just punt. */
20101 complaint (&symfile_complaints,
20102 _("unsupported version in .debug_line section"));
20105 if (lh->version >= 5)
20107 gdb_byte segment_selector_size;
20109 /* Skip address size. */
20110 read_1_byte (abfd, line_ptr);
20113 segment_selector_size = read_1_byte (abfd, line_ptr);
20115 if (segment_selector_size != 0)
20117 complaint (&symfile_complaints,
20118 _("unsupported segment selector size %u "
20119 "in .debug_line section"),
20120 segment_selector_size);
20124 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20125 line_ptr += offset_size;
20126 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20128 if (lh->version >= 4)
20130 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20134 lh->maximum_ops_per_instruction = 1;
20136 if (lh->maximum_ops_per_instruction == 0)
20138 lh->maximum_ops_per_instruction = 1;
20139 complaint (&symfile_complaints,
20140 _("invalid maximum_ops_per_instruction "
20141 "in `.debug_line' section"));
20144 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20146 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20148 lh->line_range = read_1_byte (abfd, line_ptr);
20150 lh->opcode_base = read_1_byte (abfd, line_ptr);
20152 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20154 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20155 for (i = 1; i < lh->opcode_base; ++i)
20157 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20161 if (lh->version >= 5)
20163 /* Read directory table. */
20164 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
20165 [] (struct line_header *lh, const char *name,
20166 dir_index d_index, unsigned int mod_time,
20167 unsigned int length)
20169 lh->add_include_dir (name);
20172 /* Read file name table. */
20173 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
20174 [] (struct line_header *lh, const char *name,
20175 dir_index d_index, unsigned int mod_time,
20176 unsigned int length)
20178 lh->add_file_name (name, d_index, mod_time, length);
20183 /* Read directory table. */
20184 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20186 line_ptr += bytes_read;
20187 lh->add_include_dir (cur_dir);
20189 line_ptr += bytes_read;
20191 /* Read file name table. */
20192 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20194 unsigned int mod_time, length;
20197 line_ptr += bytes_read;
20198 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20199 line_ptr += bytes_read;
20200 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20201 line_ptr += bytes_read;
20202 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20203 line_ptr += bytes_read;
20205 lh->add_file_name (cur_file, d_index, mod_time, length);
20207 line_ptr += bytes_read;
20209 lh->statement_program_start = line_ptr;
20211 if (line_ptr > (section->buffer + section->size))
20212 complaint (&symfile_complaints,
20213 _("line number info header doesn't "
20214 "fit in `.debug_line' section"));
20219 /* Subroutine of dwarf_decode_lines to simplify it.
20220 Return the file name of the psymtab for included file FILE_INDEX
20221 in line header LH of PST.
20222 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20223 If space for the result is malloc'd, it will be freed by a cleanup.
20224 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
20226 The function creates dangling cleanup registration. */
20228 static const char *
20229 psymtab_include_file_name (const struct line_header *lh, int file_index,
20230 const struct partial_symtab *pst,
20231 const char *comp_dir)
20233 const file_entry &fe = lh->file_names[file_index];
20234 const char *include_name = fe.name;
20235 const char *include_name_to_compare = include_name;
20236 const char *pst_filename;
20237 char *copied_name = NULL;
20240 const char *dir_name = fe.include_dir (lh);
20242 if (!IS_ABSOLUTE_PATH (include_name)
20243 && (dir_name != NULL || comp_dir != NULL))
20245 /* Avoid creating a duplicate psymtab for PST.
20246 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20247 Before we do the comparison, however, we need to account
20248 for DIR_NAME and COMP_DIR.
20249 First prepend dir_name (if non-NULL). If we still don't
20250 have an absolute path prepend comp_dir (if non-NULL).
20251 However, the directory we record in the include-file's
20252 psymtab does not contain COMP_DIR (to match the
20253 corresponding symtab(s)).
20258 bash$ gcc -g ./hello.c
20259 include_name = "hello.c"
20261 DW_AT_comp_dir = comp_dir = "/tmp"
20262 DW_AT_name = "./hello.c"
20266 if (dir_name != NULL)
20268 char *tem = concat (dir_name, SLASH_STRING,
20269 include_name, (char *)NULL);
20271 make_cleanup (xfree, tem);
20272 include_name = tem;
20273 include_name_to_compare = include_name;
20275 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20277 char *tem = concat (comp_dir, SLASH_STRING,
20278 include_name, (char *)NULL);
20280 make_cleanup (xfree, tem);
20281 include_name_to_compare = tem;
20285 pst_filename = pst->filename;
20286 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20288 copied_name = concat (pst->dirname, SLASH_STRING,
20289 pst_filename, (char *)NULL);
20290 pst_filename = copied_name;
20293 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20295 if (copied_name != NULL)
20296 xfree (copied_name);
20300 return include_name;
20303 /* State machine to track the state of the line number program. */
20305 class lnp_state_machine
20308 /* Initialize a machine state for the start of a line number
20310 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20312 file_entry *current_file ()
20314 /* lh->file_names is 0-based, but the file name numbers in the
20315 statement program are 1-based. */
20316 return m_line_header->file_name_at (m_file);
20319 /* Record the line in the state machine. END_SEQUENCE is true if
20320 we're processing the end of a sequence. */
20321 void record_line (bool end_sequence);
20323 /* Check address and if invalid nop-out the rest of the lines in this
20325 void check_line_address (struct dwarf2_cu *cu,
20326 const gdb_byte *line_ptr,
20327 CORE_ADDR lowpc, CORE_ADDR address);
20329 void handle_set_discriminator (unsigned int discriminator)
20331 m_discriminator = discriminator;
20332 m_line_has_non_zero_discriminator |= discriminator != 0;
20335 /* Handle DW_LNE_set_address. */
20336 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20339 address += baseaddr;
20340 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20343 /* Handle DW_LNS_advance_pc. */
20344 void handle_advance_pc (CORE_ADDR adjust);
20346 /* Handle a special opcode. */
20347 void handle_special_opcode (unsigned char op_code);
20349 /* Handle DW_LNS_advance_line. */
20350 void handle_advance_line (int line_delta)
20352 advance_line (line_delta);
20355 /* Handle DW_LNS_set_file. */
20356 void handle_set_file (file_name_index file);
20358 /* Handle DW_LNS_negate_stmt. */
20359 void handle_negate_stmt ()
20361 m_is_stmt = !m_is_stmt;
20364 /* Handle DW_LNS_const_add_pc. */
20365 void handle_const_add_pc ();
20367 /* Handle DW_LNS_fixed_advance_pc. */
20368 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20370 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20374 /* Handle DW_LNS_copy. */
20375 void handle_copy ()
20377 record_line (false);
20378 m_discriminator = 0;
20381 /* Handle DW_LNE_end_sequence. */
20382 void handle_end_sequence ()
20384 m_record_line_callback = ::record_line;
20388 /* Advance the line by LINE_DELTA. */
20389 void advance_line (int line_delta)
20391 m_line += line_delta;
20393 if (line_delta != 0)
20394 m_line_has_non_zero_discriminator = m_discriminator != 0;
20397 gdbarch *m_gdbarch;
20399 /* True if we're recording lines.
20400 Otherwise we're building partial symtabs and are just interested in
20401 finding include files mentioned by the line number program. */
20402 bool m_record_lines_p;
20404 /* The line number header. */
20405 line_header *m_line_header;
20407 /* These are part of the standard DWARF line number state machine,
20408 and initialized according to the DWARF spec. */
20410 unsigned char m_op_index = 0;
20411 /* The line table index (1-based) of the current file. */
20412 file_name_index m_file = (file_name_index) 1;
20413 unsigned int m_line = 1;
20415 /* These are initialized in the constructor. */
20417 CORE_ADDR m_address;
20419 unsigned int m_discriminator;
20421 /* Additional bits of state we need to track. */
20423 /* The last file that we called dwarf2_start_subfile for.
20424 This is only used for TLLs. */
20425 unsigned int m_last_file = 0;
20426 /* The last file a line number was recorded for. */
20427 struct subfile *m_last_subfile = NULL;
20429 /* The function to call to record a line. */
20430 record_line_ftype *m_record_line_callback = NULL;
20432 /* The last line number that was recorded, used to coalesce
20433 consecutive entries for the same line. This can happen, for
20434 example, when discriminators are present. PR 17276. */
20435 unsigned int m_last_line = 0;
20436 bool m_line_has_non_zero_discriminator = false;
20440 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20442 CORE_ADDR addr_adj = (((m_op_index + adjust)
20443 / m_line_header->maximum_ops_per_instruction)
20444 * m_line_header->minimum_instruction_length);
20445 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20446 m_op_index = ((m_op_index + adjust)
20447 % m_line_header->maximum_ops_per_instruction);
20451 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20453 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20454 CORE_ADDR addr_adj = (((m_op_index
20455 + (adj_opcode / m_line_header->line_range))
20456 / m_line_header->maximum_ops_per_instruction)
20457 * m_line_header->minimum_instruction_length);
20458 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20459 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20460 % m_line_header->maximum_ops_per_instruction);
20462 int line_delta = (m_line_header->line_base
20463 + (adj_opcode % m_line_header->line_range));
20464 advance_line (line_delta);
20465 record_line (false);
20466 m_discriminator = 0;
20470 lnp_state_machine::handle_set_file (file_name_index file)
20474 const file_entry *fe = current_file ();
20476 dwarf2_debug_line_missing_file_complaint ();
20477 else if (m_record_lines_p)
20479 const char *dir = fe->include_dir (m_line_header);
20481 m_last_subfile = current_subfile;
20482 m_line_has_non_zero_discriminator = m_discriminator != 0;
20483 dwarf2_start_subfile (fe->name, dir);
20488 lnp_state_machine::handle_const_add_pc ()
20491 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20494 = (((m_op_index + adjust)
20495 / m_line_header->maximum_ops_per_instruction)
20496 * m_line_header->minimum_instruction_length);
20498 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20499 m_op_index = ((m_op_index + adjust)
20500 % m_line_header->maximum_ops_per_instruction);
20503 /* Ignore this record_line request. */
20506 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20511 /* Return non-zero if we should add LINE to the line number table.
20512 LINE is the line to add, LAST_LINE is the last line that was added,
20513 LAST_SUBFILE is the subfile for LAST_LINE.
20514 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20515 had a non-zero discriminator.
20517 We have to be careful in the presence of discriminators.
20518 E.g., for this line:
20520 for (i = 0; i < 100000; i++);
20522 clang can emit four line number entries for that one line,
20523 each with a different discriminator.
20524 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20526 However, we want gdb to coalesce all four entries into one.
20527 Otherwise the user could stepi into the middle of the line and
20528 gdb would get confused about whether the pc really was in the
20529 middle of the line.
20531 Things are further complicated by the fact that two consecutive
20532 line number entries for the same line is a heuristic used by gcc
20533 to denote the end of the prologue. So we can't just discard duplicate
20534 entries, we have to be selective about it. The heuristic we use is
20535 that we only collapse consecutive entries for the same line if at least
20536 one of those entries has a non-zero discriminator. PR 17276.
20538 Note: Addresses in the line number state machine can never go backwards
20539 within one sequence, thus this coalescing is ok. */
20542 dwarf_record_line_p (unsigned int line, unsigned int last_line,
20543 int line_has_non_zero_discriminator,
20544 struct subfile *last_subfile)
20546 if (current_subfile != last_subfile)
20548 if (line != last_line)
20550 /* Same line for the same file that we've seen already.
20551 As a last check, for pr 17276, only record the line if the line
20552 has never had a non-zero discriminator. */
20553 if (!line_has_non_zero_discriminator)
20558 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20559 in the line table of subfile SUBFILE. */
20562 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20563 unsigned int line, CORE_ADDR address,
20564 record_line_ftype p_record_line)
20566 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20568 if (dwarf_line_debug)
20570 fprintf_unfiltered (gdb_stdlog,
20571 "Recording line %u, file %s, address %s\n",
20572 line, lbasename (subfile->name),
20573 paddress (gdbarch, address));
20576 (*p_record_line) (subfile, line, addr);
20579 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20580 Mark the end of a set of line number records.
20581 The arguments are the same as for dwarf_record_line_1.
20582 If SUBFILE is NULL the request is ignored. */
20585 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20586 CORE_ADDR address, record_line_ftype p_record_line)
20588 if (subfile == NULL)
20591 if (dwarf_line_debug)
20593 fprintf_unfiltered (gdb_stdlog,
20594 "Finishing current line, file %s, address %s\n",
20595 lbasename (subfile->name),
20596 paddress (gdbarch, address));
20599 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20603 lnp_state_machine::record_line (bool end_sequence)
20605 if (dwarf_line_debug)
20607 fprintf_unfiltered (gdb_stdlog,
20608 "Processing actual line %u: file %u,"
20609 " address %s, is_stmt %u, discrim %u\n",
20610 m_line, to_underlying (m_file),
20611 paddress (m_gdbarch, m_address),
20612 m_is_stmt, m_discriminator);
20615 file_entry *fe = current_file ();
20618 dwarf2_debug_line_missing_file_complaint ();
20619 /* For now we ignore lines not starting on an instruction boundary.
20620 But not when processing end_sequence for compatibility with the
20621 previous version of the code. */
20622 else if (m_op_index == 0 || end_sequence)
20624 fe->included_p = 1;
20625 if (m_record_lines_p && m_is_stmt)
20627 if (m_last_subfile != current_subfile || end_sequence)
20629 dwarf_finish_line (m_gdbarch, m_last_subfile,
20630 m_address, m_record_line_callback);
20635 if (dwarf_record_line_p (m_line, m_last_line,
20636 m_line_has_non_zero_discriminator,
20639 dwarf_record_line_1 (m_gdbarch, current_subfile,
20641 m_record_line_callback);
20643 m_last_subfile = current_subfile;
20644 m_last_line = m_line;
20650 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20651 bool record_lines_p)
20654 m_record_lines_p = record_lines_p;
20655 m_line_header = lh;
20657 m_record_line_callback = ::record_line;
20659 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20660 was a line entry for it so that the backend has a chance to adjust it
20661 and also record it in case it needs it. This is currently used by MIPS
20662 code, cf. `mips_adjust_dwarf2_line'. */
20663 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20664 m_is_stmt = lh->default_is_stmt;
20665 m_discriminator = 0;
20669 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20670 const gdb_byte *line_ptr,
20671 CORE_ADDR lowpc, CORE_ADDR address)
20673 /* If address < lowpc then it's not a usable value, it's outside the
20674 pc range of the CU. However, we restrict the test to only address
20675 values of zero to preserve GDB's previous behaviour which is to
20676 handle the specific case of a function being GC'd by the linker. */
20678 if (address == 0 && address < lowpc)
20680 /* This line table is for a function which has been
20681 GCd by the linker. Ignore it. PR gdb/12528 */
20683 struct objfile *objfile = cu->objfile;
20684 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20686 complaint (&symfile_complaints,
20687 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20688 line_offset, objfile_name (objfile));
20689 m_record_line_callback = noop_record_line;
20690 /* Note: record_line_callback is left as noop_record_line until
20691 we see DW_LNE_end_sequence. */
20695 /* Subroutine of dwarf_decode_lines to simplify it.
20696 Process the line number information in LH.
20697 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20698 program in order to set included_p for every referenced header. */
20701 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20702 const int decode_for_pst_p, CORE_ADDR lowpc)
20704 const gdb_byte *line_ptr, *extended_end;
20705 const gdb_byte *line_end;
20706 unsigned int bytes_read, extended_len;
20707 unsigned char op_code, extended_op;
20708 CORE_ADDR baseaddr;
20709 struct objfile *objfile = cu->objfile;
20710 bfd *abfd = objfile->obfd;
20711 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20712 /* True if we're recording line info (as opposed to building partial
20713 symtabs and just interested in finding include files mentioned by
20714 the line number program). */
20715 bool record_lines_p = !decode_for_pst_p;
20717 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20719 line_ptr = lh->statement_program_start;
20720 line_end = lh->statement_program_end;
20722 /* Read the statement sequences until there's nothing left. */
20723 while (line_ptr < line_end)
20725 /* The DWARF line number program state machine. Reset the state
20726 machine at the start of each sequence. */
20727 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20728 bool end_sequence = false;
20730 if (record_lines_p)
20732 /* Start a subfile for the current file of the state
20734 const file_entry *fe = state_machine.current_file ();
20737 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
20740 /* Decode the table. */
20741 while (line_ptr < line_end && !end_sequence)
20743 op_code = read_1_byte (abfd, line_ptr);
20746 if (op_code >= lh->opcode_base)
20748 /* Special opcode. */
20749 state_machine.handle_special_opcode (op_code);
20751 else switch (op_code)
20753 case DW_LNS_extended_op:
20754 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20756 line_ptr += bytes_read;
20757 extended_end = line_ptr + extended_len;
20758 extended_op = read_1_byte (abfd, line_ptr);
20760 switch (extended_op)
20762 case DW_LNE_end_sequence:
20763 state_machine.handle_end_sequence ();
20764 end_sequence = true;
20766 case DW_LNE_set_address:
20769 = read_address (abfd, line_ptr, cu, &bytes_read);
20770 line_ptr += bytes_read;
20772 state_machine.check_line_address (cu, line_ptr,
20774 state_machine.handle_set_address (baseaddr, address);
20777 case DW_LNE_define_file:
20779 const char *cur_file;
20780 unsigned int mod_time, length;
20783 cur_file = read_direct_string (abfd, line_ptr,
20785 line_ptr += bytes_read;
20786 dindex = (dir_index)
20787 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20788 line_ptr += bytes_read;
20790 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20791 line_ptr += bytes_read;
20793 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20794 line_ptr += bytes_read;
20795 lh->add_file_name (cur_file, dindex, mod_time, length);
20798 case DW_LNE_set_discriminator:
20800 /* The discriminator is not interesting to the
20801 debugger; just ignore it. We still need to
20802 check its value though:
20803 if there are consecutive entries for the same
20804 (non-prologue) line we want to coalesce them.
20807 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20808 line_ptr += bytes_read;
20810 state_machine.handle_set_discriminator (discr);
20814 complaint (&symfile_complaints,
20815 _("mangled .debug_line section"));
20818 /* Make sure that we parsed the extended op correctly. If e.g.
20819 we expected a different address size than the producer used,
20820 we may have read the wrong number of bytes. */
20821 if (line_ptr != extended_end)
20823 complaint (&symfile_complaints,
20824 _("mangled .debug_line section"));
20829 state_machine.handle_copy ();
20831 case DW_LNS_advance_pc:
20834 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20835 line_ptr += bytes_read;
20837 state_machine.handle_advance_pc (adjust);
20840 case DW_LNS_advance_line:
20843 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20844 line_ptr += bytes_read;
20846 state_machine.handle_advance_line (line_delta);
20849 case DW_LNS_set_file:
20851 file_name_index file
20852 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20854 line_ptr += bytes_read;
20856 state_machine.handle_set_file (file);
20859 case DW_LNS_set_column:
20860 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20861 line_ptr += bytes_read;
20863 case DW_LNS_negate_stmt:
20864 state_machine.handle_negate_stmt ();
20866 case DW_LNS_set_basic_block:
20868 /* Add to the address register of the state machine the
20869 address increment value corresponding to special opcode
20870 255. I.e., this value is scaled by the minimum
20871 instruction length since special opcode 255 would have
20872 scaled the increment. */
20873 case DW_LNS_const_add_pc:
20874 state_machine.handle_const_add_pc ();
20876 case DW_LNS_fixed_advance_pc:
20878 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20881 state_machine.handle_fixed_advance_pc (addr_adj);
20886 /* Unknown standard opcode, ignore it. */
20889 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20891 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20892 line_ptr += bytes_read;
20899 dwarf2_debug_line_missing_end_sequence_complaint ();
20901 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20902 in which case we still finish recording the last line). */
20903 state_machine.record_line (true);
20907 /* Decode the Line Number Program (LNP) for the given line_header
20908 structure and CU. The actual information extracted and the type
20909 of structures created from the LNP depends on the value of PST.
20911 1. If PST is NULL, then this procedure uses the data from the program
20912 to create all necessary symbol tables, and their linetables.
20914 2. If PST is not NULL, this procedure reads the program to determine
20915 the list of files included by the unit represented by PST, and
20916 builds all the associated partial symbol tables.
20918 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20919 It is used for relative paths in the line table.
20920 NOTE: When processing partial symtabs (pst != NULL),
20921 comp_dir == pst->dirname.
20923 NOTE: It is important that psymtabs have the same file name (via strcmp)
20924 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20925 symtab we don't use it in the name of the psymtabs we create.
20926 E.g. expand_line_sal requires this when finding psymtabs to expand.
20927 A good testcase for this is mb-inline.exp.
20929 LOWPC is the lowest address in CU (or 0 if not known).
20931 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20932 for its PC<->lines mapping information. Otherwise only the filename
20933 table is read in. */
20936 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20937 struct dwarf2_cu *cu, struct partial_symtab *pst,
20938 CORE_ADDR lowpc, int decode_mapping)
20940 struct objfile *objfile = cu->objfile;
20941 const int decode_for_pst_p = (pst != NULL);
20943 if (decode_mapping)
20944 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20946 if (decode_for_pst_p)
20950 /* Now that we're done scanning the Line Header Program, we can
20951 create the psymtab of each included file. */
20952 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
20953 if (lh->file_names[file_index].included_p == 1)
20955 const char *include_name =
20956 psymtab_include_file_name (lh, file_index, pst, comp_dir);
20957 if (include_name != NULL)
20958 dwarf2_create_include_psymtab (include_name, pst, objfile);
20963 /* Make sure a symtab is created for every file, even files
20964 which contain only variables (i.e. no code with associated
20966 struct compunit_symtab *cust = buildsym_compunit_symtab ();
20969 for (i = 0; i < lh->file_names.size (); i++)
20971 file_entry &fe = lh->file_names[i];
20973 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
20975 if (current_subfile->symtab == NULL)
20977 current_subfile->symtab
20978 = allocate_symtab (cust, current_subfile->name);
20980 fe.symtab = current_subfile->symtab;
20985 /* Start a subfile for DWARF. FILENAME is the name of the file and
20986 DIRNAME the name of the source directory which contains FILENAME
20987 or NULL if not known.
20988 This routine tries to keep line numbers from identical absolute and
20989 relative file names in a common subfile.
20991 Using the `list' example from the GDB testsuite, which resides in
20992 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20993 of /srcdir/list0.c yields the following debugging information for list0.c:
20995 DW_AT_name: /srcdir/list0.c
20996 DW_AT_comp_dir: /compdir
20997 files.files[0].name: list0.h
20998 files.files[0].dir: /srcdir
20999 files.files[1].name: list0.c
21000 files.files[1].dir: /srcdir
21002 The line number information for list0.c has to end up in a single
21003 subfile, so that `break /srcdir/list0.c:1' works as expected.
21004 start_subfile will ensure that this happens provided that we pass the
21005 concatenation of files.files[1].dir and files.files[1].name as the
21009 dwarf2_start_subfile (const char *filename, const char *dirname)
21013 /* In order not to lose the line information directory,
21014 we concatenate it to the filename when it makes sense.
21015 Note that the Dwarf3 standard says (speaking of filenames in line
21016 information): ``The directory index is ignored for file names
21017 that represent full path names''. Thus ignoring dirname in the
21018 `else' branch below isn't an issue. */
21020 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21022 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21026 start_subfile (filename);
21032 /* Start a symtab for DWARF.
21033 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21035 static struct compunit_symtab *
21036 dwarf2_start_symtab (struct dwarf2_cu *cu,
21037 const char *name, const char *comp_dir, CORE_ADDR low_pc)
21039 struct compunit_symtab *cust
21040 = start_symtab (cu->objfile, name, comp_dir, low_pc, cu->language);
21042 record_debugformat ("DWARF 2");
21043 record_producer (cu->producer);
21045 /* We assume that we're processing GCC output. */
21046 processing_gcc_compilation = 2;
21048 cu->processing_has_namespace_info = 0;
21054 var_decode_location (struct attribute *attr, struct symbol *sym,
21055 struct dwarf2_cu *cu)
21057 struct objfile *objfile = cu->objfile;
21058 struct comp_unit_head *cu_header = &cu->header;
21060 /* NOTE drow/2003-01-30: There used to be a comment and some special
21061 code here to turn a symbol with DW_AT_external and a
21062 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21063 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21064 with some versions of binutils) where shared libraries could have
21065 relocations against symbols in their debug information - the
21066 minimal symbol would have the right address, but the debug info
21067 would not. It's no longer necessary, because we will explicitly
21068 apply relocations when we read in the debug information now. */
21070 /* A DW_AT_location attribute with no contents indicates that a
21071 variable has been optimized away. */
21072 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21074 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21078 /* Handle one degenerate form of location expression specially, to
21079 preserve GDB's previous behavior when section offsets are
21080 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21081 then mark this symbol as LOC_STATIC. */
21083 if (attr_form_is_block (attr)
21084 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21085 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21086 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21087 && (DW_BLOCK (attr)->size
21088 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21090 unsigned int dummy;
21092 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21093 SYMBOL_VALUE_ADDRESS (sym) =
21094 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21096 SYMBOL_VALUE_ADDRESS (sym) =
21097 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21098 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21099 fixup_symbol_section (sym, objfile);
21100 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21101 SYMBOL_SECTION (sym));
21105 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21106 expression evaluator, and use LOC_COMPUTED only when necessary
21107 (i.e. when the value of a register or memory location is
21108 referenced, or a thread-local block, etc.). Then again, it might
21109 not be worthwhile. I'm assuming that it isn't unless performance
21110 or memory numbers show me otherwise. */
21112 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21114 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21115 cu->has_loclist = 1;
21118 /* Given a pointer to a DWARF information entry, figure out if we need
21119 to make a symbol table entry for it, and if so, create a new entry
21120 and return a pointer to it.
21121 If TYPE is NULL, determine symbol type from the die, otherwise
21122 used the passed type.
21123 If SPACE is not NULL, use it to hold the new symbol. If it is
21124 NULL, allocate a new symbol on the objfile's obstack. */
21126 static struct symbol *
21127 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21128 struct symbol *space)
21130 struct objfile *objfile = cu->objfile;
21131 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21132 struct symbol *sym = NULL;
21134 struct attribute *attr = NULL;
21135 struct attribute *attr2 = NULL;
21136 CORE_ADDR baseaddr;
21137 struct pending **list_to_add = NULL;
21139 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21141 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21143 name = dwarf2_name (die, cu);
21146 const char *linkagename;
21147 int suppress_add = 0;
21152 sym = allocate_symbol (objfile);
21153 OBJSTAT (objfile, n_syms++);
21155 /* Cache this symbol's name and the name's demangled form (if any). */
21156 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21157 linkagename = dwarf2_physname (name, die, cu);
21158 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21160 /* Fortran does not have mangling standard and the mangling does differ
21161 between gfortran, iFort etc. */
21162 if (cu->language == language_fortran
21163 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21164 symbol_set_demangled_name (&(sym->ginfo),
21165 dwarf2_full_name (name, die, cu),
21168 /* Default assumptions.
21169 Use the passed type or decode it from the die. */
21170 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21171 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21173 SYMBOL_TYPE (sym) = type;
21175 SYMBOL_TYPE (sym) = die_type (die, cu);
21176 attr = dwarf2_attr (die,
21177 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21181 SYMBOL_LINE (sym) = DW_UNSND (attr);
21184 attr = dwarf2_attr (die,
21185 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21189 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21190 struct file_entry *fe;
21192 if (cu->line_header != NULL)
21193 fe = cu->line_header->file_name_at (file_index);
21198 complaint (&symfile_complaints,
21199 _("file index out of range"));
21201 symbol_set_symtab (sym, fe->symtab);
21207 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21212 addr = attr_value_as_address (attr);
21213 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21214 SYMBOL_VALUE_ADDRESS (sym) = addr;
21216 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21217 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21218 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21219 add_symbol_to_list (sym, cu->list_in_scope);
21221 case DW_TAG_subprogram:
21222 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21224 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21225 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21226 if ((attr2 && (DW_UNSND (attr2) != 0))
21227 || cu->language == language_ada)
21229 /* Subprograms marked external are stored as a global symbol.
21230 Ada subprograms, whether marked external or not, are always
21231 stored as a global symbol, because we want to be able to
21232 access them globally. For instance, we want to be able
21233 to break on a nested subprogram without having to
21234 specify the context. */
21235 list_to_add = &global_symbols;
21239 list_to_add = cu->list_in_scope;
21242 case DW_TAG_inlined_subroutine:
21243 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21245 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21246 SYMBOL_INLINED (sym) = 1;
21247 list_to_add = cu->list_in_scope;
21249 case DW_TAG_template_value_param:
21251 /* Fall through. */
21252 case DW_TAG_constant:
21253 case DW_TAG_variable:
21254 case DW_TAG_member:
21255 /* Compilation with minimal debug info may result in
21256 variables with missing type entries. Change the
21257 misleading `void' type to something sensible. */
21258 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21259 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21261 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21262 /* In the case of DW_TAG_member, we should only be called for
21263 static const members. */
21264 if (die->tag == DW_TAG_member)
21266 /* dwarf2_add_field uses die_is_declaration,
21267 so we do the same. */
21268 gdb_assert (die_is_declaration (die, cu));
21273 dwarf2_const_value (attr, sym, cu);
21274 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21277 if (attr2 && (DW_UNSND (attr2) != 0))
21278 list_to_add = &global_symbols;
21280 list_to_add = cu->list_in_scope;
21284 attr = dwarf2_attr (die, DW_AT_location, cu);
21287 var_decode_location (attr, sym, cu);
21288 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21290 /* Fortran explicitly imports any global symbols to the local
21291 scope by DW_TAG_common_block. */
21292 if (cu->language == language_fortran && die->parent
21293 && die->parent->tag == DW_TAG_common_block)
21296 if (SYMBOL_CLASS (sym) == LOC_STATIC
21297 && SYMBOL_VALUE_ADDRESS (sym) == 0
21298 && !dwarf2_per_objfile->has_section_at_zero)
21300 /* When a static variable is eliminated by the linker,
21301 the corresponding debug information is not stripped
21302 out, but the variable address is set to null;
21303 do not add such variables into symbol table. */
21305 else if (attr2 && (DW_UNSND (attr2) != 0))
21307 /* Workaround gfortran PR debug/40040 - it uses
21308 DW_AT_location for variables in -fPIC libraries which may
21309 get overriden by other libraries/executable and get
21310 a different address. Resolve it by the minimal symbol
21311 which may come from inferior's executable using copy
21312 relocation. Make this workaround only for gfortran as for
21313 other compilers GDB cannot guess the minimal symbol
21314 Fortran mangling kind. */
21315 if (cu->language == language_fortran && die->parent
21316 && die->parent->tag == DW_TAG_module
21318 && startswith (cu->producer, "GNU Fortran"))
21319 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21321 /* A variable with DW_AT_external is never static,
21322 but it may be block-scoped. */
21323 list_to_add = (cu->list_in_scope == &file_symbols
21324 ? &global_symbols : cu->list_in_scope);
21327 list_to_add = cu->list_in_scope;
21331 /* We do not know the address of this symbol.
21332 If it is an external symbol and we have type information
21333 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21334 The address of the variable will then be determined from
21335 the minimal symbol table whenever the variable is
21337 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21339 /* Fortran explicitly imports any global symbols to the local
21340 scope by DW_TAG_common_block. */
21341 if (cu->language == language_fortran && die->parent
21342 && die->parent->tag == DW_TAG_common_block)
21344 /* SYMBOL_CLASS doesn't matter here because
21345 read_common_block is going to reset it. */
21347 list_to_add = cu->list_in_scope;
21349 else if (attr2 && (DW_UNSND (attr2) != 0)
21350 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21352 /* A variable with DW_AT_external is never static, but it
21353 may be block-scoped. */
21354 list_to_add = (cu->list_in_scope == &file_symbols
21355 ? &global_symbols : cu->list_in_scope);
21357 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21359 else if (!die_is_declaration (die, cu))
21361 /* Use the default LOC_OPTIMIZED_OUT class. */
21362 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21364 list_to_add = cu->list_in_scope;
21368 case DW_TAG_formal_parameter:
21369 /* If we are inside a function, mark this as an argument. If
21370 not, we might be looking at an argument to an inlined function
21371 when we do not have enough information to show inlined frames;
21372 pretend it's a local variable in that case so that the user can
21374 if (context_stack_depth > 0
21375 && context_stack[context_stack_depth - 1].name != NULL)
21376 SYMBOL_IS_ARGUMENT (sym) = 1;
21377 attr = dwarf2_attr (die, DW_AT_location, cu);
21380 var_decode_location (attr, sym, cu);
21382 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21385 dwarf2_const_value (attr, sym, cu);
21388 list_to_add = cu->list_in_scope;
21390 case DW_TAG_unspecified_parameters:
21391 /* From varargs functions; gdb doesn't seem to have any
21392 interest in this information, so just ignore it for now.
21395 case DW_TAG_template_type_param:
21397 /* Fall through. */
21398 case DW_TAG_class_type:
21399 case DW_TAG_interface_type:
21400 case DW_TAG_structure_type:
21401 case DW_TAG_union_type:
21402 case DW_TAG_set_type:
21403 case DW_TAG_enumeration_type:
21404 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21405 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21408 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21409 really ever be static objects: otherwise, if you try
21410 to, say, break of a class's method and you're in a file
21411 which doesn't mention that class, it won't work unless
21412 the check for all static symbols in lookup_symbol_aux
21413 saves you. See the OtherFileClass tests in
21414 gdb.c++/namespace.exp. */
21418 list_to_add = (cu->list_in_scope == &file_symbols
21419 && cu->language == language_cplus
21420 ? &global_symbols : cu->list_in_scope);
21422 /* The semantics of C++ state that "struct foo {
21423 ... }" also defines a typedef for "foo". */
21424 if (cu->language == language_cplus
21425 || cu->language == language_ada
21426 || cu->language == language_d
21427 || cu->language == language_rust)
21429 /* The symbol's name is already allocated along
21430 with this objfile, so we don't need to
21431 duplicate it for the type. */
21432 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21433 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21438 case DW_TAG_typedef:
21439 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21440 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21441 list_to_add = cu->list_in_scope;
21443 case DW_TAG_base_type:
21444 case DW_TAG_subrange_type:
21445 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21446 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21447 list_to_add = cu->list_in_scope;
21449 case DW_TAG_enumerator:
21450 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21453 dwarf2_const_value (attr, sym, cu);
21456 /* NOTE: carlton/2003-11-10: See comment above in the
21457 DW_TAG_class_type, etc. block. */
21459 list_to_add = (cu->list_in_scope == &file_symbols
21460 && cu->language == language_cplus
21461 ? &global_symbols : cu->list_in_scope);
21464 case DW_TAG_imported_declaration:
21465 case DW_TAG_namespace:
21466 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21467 list_to_add = &global_symbols;
21469 case DW_TAG_module:
21470 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21471 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21472 list_to_add = &global_symbols;
21474 case DW_TAG_common_block:
21475 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21476 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21477 add_symbol_to_list (sym, cu->list_in_scope);
21480 /* Not a tag we recognize. Hopefully we aren't processing
21481 trash data, but since we must specifically ignore things
21482 we don't recognize, there is nothing else we should do at
21484 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
21485 dwarf_tag_name (die->tag));
21491 sym->hash_next = objfile->template_symbols;
21492 objfile->template_symbols = sym;
21493 list_to_add = NULL;
21496 if (list_to_add != NULL)
21497 add_symbol_to_list (sym, list_to_add);
21499 /* For the benefit of old versions of GCC, check for anonymous
21500 namespaces based on the demangled name. */
21501 if (!cu->processing_has_namespace_info
21502 && cu->language == language_cplus)
21503 cp_scan_for_anonymous_namespaces (sym, objfile);
21508 /* A wrapper for new_symbol_full that always allocates a new symbol. */
21510 static struct symbol *
21511 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
21513 return new_symbol_full (die, type, cu, NULL);
21516 /* Given an attr with a DW_FORM_dataN value in host byte order,
21517 zero-extend it as appropriate for the symbol's type. The DWARF
21518 standard (v4) is not entirely clear about the meaning of using
21519 DW_FORM_dataN for a constant with a signed type, where the type is
21520 wider than the data. The conclusion of a discussion on the DWARF
21521 list was that this is unspecified. We choose to always zero-extend
21522 because that is the interpretation long in use by GCC. */
21525 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21526 struct dwarf2_cu *cu, LONGEST *value, int bits)
21528 struct objfile *objfile = cu->objfile;
21529 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21530 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21531 LONGEST l = DW_UNSND (attr);
21533 if (bits < sizeof (*value) * 8)
21535 l &= ((LONGEST) 1 << bits) - 1;
21538 else if (bits == sizeof (*value) * 8)
21542 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21543 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21550 /* Read a constant value from an attribute. Either set *VALUE, or if
21551 the value does not fit in *VALUE, set *BYTES - either already
21552 allocated on the objfile obstack, or newly allocated on OBSTACK,
21553 or, set *BATON, if we translated the constant to a location
21557 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21558 const char *name, struct obstack *obstack,
21559 struct dwarf2_cu *cu,
21560 LONGEST *value, const gdb_byte **bytes,
21561 struct dwarf2_locexpr_baton **baton)
21563 struct objfile *objfile = cu->objfile;
21564 struct comp_unit_head *cu_header = &cu->header;
21565 struct dwarf_block *blk;
21566 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21567 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21573 switch (attr->form)
21576 case DW_FORM_GNU_addr_index:
21580 if (TYPE_LENGTH (type) != cu_header->addr_size)
21581 dwarf2_const_value_length_mismatch_complaint (name,
21582 cu_header->addr_size,
21583 TYPE_LENGTH (type));
21584 /* Symbols of this form are reasonably rare, so we just
21585 piggyback on the existing location code rather than writing
21586 a new implementation of symbol_computed_ops. */
21587 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21588 (*baton)->per_cu = cu->per_cu;
21589 gdb_assert ((*baton)->per_cu);
21591 (*baton)->size = 2 + cu_header->addr_size;
21592 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21593 (*baton)->data = data;
21595 data[0] = DW_OP_addr;
21596 store_unsigned_integer (&data[1], cu_header->addr_size,
21597 byte_order, DW_ADDR (attr));
21598 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21601 case DW_FORM_string:
21603 case DW_FORM_GNU_str_index:
21604 case DW_FORM_GNU_strp_alt:
21605 /* DW_STRING is already allocated on the objfile obstack, point
21607 *bytes = (const gdb_byte *) DW_STRING (attr);
21609 case DW_FORM_block1:
21610 case DW_FORM_block2:
21611 case DW_FORM_block4:
21612 case DW_FORM_block:
21613 case DW_FORM_exprloc:
21614 case DW_FORM_data16:
21615 blk = DW_BLOCK (attr);
21616 if (TYPE_LENGTH (type) != blk->size)
21617 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21618 TYPE_LENGTH (type));
21619 *bytes = blk->data;
21622 /* The DW_AT_const_value attributes are supposed to carry the
21623 symbol's value "represented as it would be on the target
21624 architecture." By the time we get here, it's already been
21625 converted to host endianness, so we just need to sign- or
21626 zero-extend it as appropriate. */
21627 case DW_FORM_data1:
21628 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21630 case DW_FORM_data2:
21631 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21633 case DW_FORM_data4:
21634 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21636 case DW_FORM_data8:
21637 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21640 case DW_FORM_sdata:
21641 case DW_FORM_implicit_const:
21642 *value = DW_SND (attr);
21645 case DW_FORM_udata:
21646 *value = DW_UNSND (attr);
21650 complaint (&symfile_complaints,
21651 _("unsupported const value attribute form: '%s'"),
21652 dwarf_form_name (attr->form));
21659 /* Copy constant value from an attribute to a symbol. */
21662 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21663 struct dwarf2_cu *cu)
21665 struct objfile *objfile = cu->objfile;
21667 const gdb_byte *bytes;
21668 struct dwarf2_locexpr_baton *baton;
21670 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21671 SYMBOL_PRINT_NAME (sym),
21672 &objfile->objfile_obstack, cu,
21673 &value, &bytes, &baton);
21677 SYMBOL_LOCATION_BATON (sym) = baton;
21678 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21680 else if (bytes != NULL)
21682 SYMBOL_VALUE_BYTES (sym) = bytes;
21683 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21687 SYMBOL_VALUE (sym) = value;
21688 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21692 /* Return the type of the die in question using its DW_AT_type attribute. */
21694 static struct type *
21695 die_type (struct die_info *die, struct dwarf2_cu *cu)
21697 struct attribute *type_attr;
21699 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21702 /* A missing DW_AT_type represents a void type. */
21703 return objfile_type (cu->objfile)->builtin_void;
21706 return lookup_die_type (die, type_attr, cu);
21709 /* True iff CU's producer generates GNAT Ada auxiliary information
21710 that allows to find parallel types through that information instead
21711 of having to do expensive parallel lookups by type name. */
21714 need_gnat_info (struct dwarf2_cu *cu)
21716 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
21717 of GNAT produces this auxiliary information, without any indication
21718 that it is produced. Part of enhancing the FSF version of GNAT
21719 to produce that information will be to put in place an indicator
21720 that we can use in order to determine whether the descriptive type
21721 info is available or not. One suggestion that has been made is
21722 to use a new attribute, attached to the CU die. For now, assume
21723 that the descriptive type info is not available. */
21727 /* Return the auxiliary type of the die in question using its
21728 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21729 attribute is not present. */
21731 static struct type *
21732 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21734 struct attribute *type_attr;
21736 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21740 return lookup_die_type (die, type_attr, cu);
21743 /* If DIE has a descriptive_type attribute, then set the TYPE's
21744 descriptive type accordingly. */
21747 set_descriptive_type (struct type *type, struct die_info *die,
21748 struct dwarf2_cu *cu)
21750 struct type *descriptive_type = die_descriptive_type (die, cu);
21752 if (descriptive_type)
21754 ALLOCATE_GNAT_AUX_TYPE (type);
21755 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21759 /* Return the containing type of the die in question using its
21760 DW_AT_containing_type attribute. */
21762 static struct type *
21763 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21765 struct attribute *type_attr;
21767 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21769 error (_("Dwarf Error: Problem turning containing type into gdb type "
21770 "[in module %s]"), objfile_name (cu->objfile));
21772 return lookup_die_type (die, type_attr, cu);
21775 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21777 static struct type *
21778 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21780 struct objfile *objfile = dwarf2_per_objfile->objfile;
21781 char *message, *saved;
21783 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
21784 objfile_name (objfile),
21785 to_underlying (cu->header.sect_off),
21786 to_underlying (die->sect_off));
21787 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21788 message, strlen (message));
21791 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21794 /* Look up the type of DIE in CU using its type attribute ATTR.
21795 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21796 DW_AT_containing_type.
21797 If there is no type substitute an error marker. */
21799 static struct type *
21800 lookup_die_type (struct die_info *die, const struct attribute *attr,
21801 struct dwarf2_cu *cu)
21803 struct objfile *objfile = cu->objfile;
21804 struct type *this_type;
21806 gdb_assert (attr->name == DW_AT_type
21807 || attr->name == DW_AT_GNAT_descriptive_type
21808 || attr->name == DW_AT_containing_type);
21810 /* First see if we have it cached. */
21812 if (attr->form == DW_FORM_GNU_ref_alt)
21814 struct dwarf2_per_cu_data *per_cu;
21815 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21817 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
21818 this_type = get_die_type_at_offset (sect_off, per_cu);
21820 else if (attr_form_is_ref (attr))
21822 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21824 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21826 else if (attr->form == DW_FORM_ref_sig8)
21828 ULONGEST signature = DW_SIGNATURE (attr);
21830 return get_signatured_type (die, signature, cu);
21834 complaint (&symfile_complaints,
21835 _("Dwarf Error: Bad type attribute %s in DIE"
21836 " at 0x%x [in module %s]"),
21837 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
21838 objfile_name (objfile));
21839 return build_error_marker_type (cu, die);
21842 /* If not cached we need to read it in. */
21844 if (this_type == NULL)
21846 struct die_info *type_die = NULL;
21847 struct dwarf2_cu *type_cu = cu;
21849 if (attr_form_is_ref (attr))
21850 type_die = follow_die_ref (die, attr, &type_cu);
21851 if (type_die == NULL)
21852 return build_error_marker_type (cu, die);
21853 /* If we find the type now, it's probably because the type came
21854 from an inter-CU reference and the type's CU got expanded before
21856 this_type = read_type_die (type_die, type_cu);
21859 /* If we still don't have a type use an error marker. */
21861 if (this_type == NULL)
21862 return build_error_marker_type (cu, die);
21867 /* Return the type in DIE, CU.
21868 Returns NULL for invalid types.
21870 This first does a lookup in die_type_hash,
21871 and only reads the die in if necessary.
21873 NOTE: This can be called when reading in partial or full symbols. */
21875 static struct type *
21876 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21878 struct type *this_type;
21880 this_type = get_die_type (die, cu);
21884 return read_type_die_1 (die, cu);
21887 /* Read the type in DIE, CU.
21888 Returns NULL for invalid types. */
21890 static struct type *
21891 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21893 struct type *this_type = NULL;
21897 case DW_TAG_class_type:
21898 case DW_TAG_interface_type:
21899 case DW_TAG_structure_type:
21900 case DW_TAG_union_type:
21901 this_type = read_structure_type (die, cu);
21903 case DW_TAG_enumeration_type:
21904 this_type = read_enumeration_type (die, cu);
21906 case DW_TAG_subprogram:
21907 case DW_TAG_subroutine_type:
21908 case DW_TAG_inlined_subroutine:
21909 this_type = read_subroutine_type (die, cu);
21911 case DW_TAG_array_type:
21912 this_type = read_array_type (die, cu);
21914 case DW_TAG_set_type:
21915 this_type = read_set_type (die, cu);
21917 case DW_TAG_pointer_type:
21918 this_type = read_tag_pointer_type (die, cu);
21920 case DW_TAG_ptr_to_member_type:
21921 this_type = read_tag_ptr_to_member_type (die, cu);
21923 case DW_TAG_reference_type:
21924 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21926 case DW_TAG_rvalue_reference_type:
21927 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21929 case DW_TAG_const_type:
21930 this_type = read_tag_const_type (die, cu);
21932 case DW_TAG_volatile_type:
21933 this_type = read_tag_volatile_type (die, cu);
21935 case DW_TAG_restrict_type:
21936 this_type = read_tag_restrict_type (die, cu);
21938 case DW_TAG_string_type:
21939 this_type = read_tag_string_type (die, cu);
21941 case DW_TAG_typedef:
21942 this_type = read_typedef (die, cu);
21944 case DW_TAG_subrange_type:
21945 this_type = read_subrange_type (die, cu);
21947 case DW_TAG_base_type:
21948 this_type = read_base_type (die, cu);
21950 case DW_TAG_unspecified_type:
21951 this_type = read_unspecified_type (die, cu);
21953 case DW_TAG_namespace:
21954 this_type = read_namespace_type (die, cu);
21956 case DW_TAG_module:
21957 this_type = read_module_type (die, cu);
21959 case DW_TAG_atomic_type:
21960 this_type = read_tag_atomic_type (die, cu);
21963 complaint (&symfile_complaints,
21964 _("unexpected tag in read_type_die: '%s'"),
21965 dwarf_tag_name (die->tag));
21972 /* See if we can figure out if the class lives in a namespace. We do
21973 this by looking for a member function; its demangled name will
21974 contain namespace info, if there is any.
21975 Return the computed name or NULL.
21976 Space for the result is allocated on the objfile's obstack.
21977 This is the full-die version of guess_partial_die_structure_name.
21978 In this case we know DIE has no useful parent. */
21981 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21983 struct die_info *spec_die;
21984 struct dwarf2_cu *spec_cu;
21985 struct die_info *child;
21988 spec_die = die_specification (die, &spec_cu);
21989 if (spec_die != NULL)
21995 for (child = die->child;
21997 child = child->sibling)
21999 if (child->tag == DW_TAG_subprogram)
22001 const char *linkage_name = dw2_linkage_name (child, cu);
22003 if (linkage_name != NULL)
22006 = language_class_name_from_physname (cu->language_defn,
22010 if (actual_name != NULL)
22012 const char *die_name = dwarf2_name (die, cu);
22014 if (die_name != NULL
22015 && strcmp (die_name, actual_name) != 0)
22017 /* Strip off the class name from the full name.
22018 We want the prefix. */
22019 int die_name_len = strlen (die_name);
22020 int actual_name_len = strlen (actual_name);
22022 /* Test for '::' as a sanity check. */
22023 if (actual_name_len > die_name_len + 2
22024 && actual_name[actual_name_len
22025 - die_name_len - 1] == ':')
22026 name = (char *) obstack_copy0 (
22027 &cu->objfile->per_bfd->storage_obstack,
22028 actual_name, actual_name_len - die_name_len - 2);
22031 xfree (actual_name);
22040 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22041 prefix part in such case. See
22042 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22044 static const char *
22045 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22047 struct attribute *attr;
22050 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22051 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22054 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22057 attr = dw2_linkage_name_attr (die, cu);
22058 if (attr == NULL || DW_STRING (attr) == NULL)
22061 /* dwarf2_name had to be already called. */
22062 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22064 /* Strip the base name, keep any leading namespaces/classes. */
22065 base = strrchr (DW_STRING (attr), ':');
22066 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22069 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
22071 &base[-1] - DW_STRING (attr));
22074 /* Return the name of the namespace/class that DIE is defined within,
22075 or "" if we can't tell. The caller should not xfree the result.
22077 For example, if we're within the method foo() in the following
22087 then determine_prefix on foo's die will return "N::C". */
22089 static const char *
22090 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22092 struct die_info *parent, *spec_die;
22093 struct dwarf2_cu *spec_cu;
22094 struct type *parent_type;
22095 const char *retval;
22097 if (cu->language != language_cplus
22098 && cu->language != language_fortran && cu->language != language_d
22099 && cu->language != language_rust)
22102 retval = anonymous_struct_prefix (die, cu);
22106 /* We have to be careful in the presence of DW_AT_specification.
22107 For example, with GCC 3.4, given the code
22111 // Definition of N::foo.
22115 then we'll have a tree of DIEs like this:
22117 1: DW_TAG_compile_unit
22118 2: DW_TAG_namespace // N
22119 3: DW_TAG_subprogram // declaration of N::foo
22120 4: DW_TAG_subprogram // definition of N::foo
22121 DW_AT_specification // refers to die #3
22123 Thus, when processing die #4, we have to pretend that we're in
22124 the context of its DW_AT_specification, namely the contex of die
22127 spec_die = die_specification (die, &spec_cu);
22128 if (spec_die == NULL)
22129 parent = die->parent;
22132 parent = spec_die->parent;
22136 if (parent == NULL)
22138 else if (parent->building_fullname)
22141 const char *parent_name;
22143 /* It has been seen on RealView 2.2 built binaries,
22144 DW_TAG_template_type_param types actually _defined_ as
22145 children of the parent class:
22148 template class <class Enum> Class{};
22149 Class<enum E> class_e;
22151 1: DW_TAG_class_type (Class)
22152 2: DW_TAG_enumeration_type (E)
22153 3: DW_TAG_enumerator (enum1:0)
22154 3: DW_TAG_enumerator (enum2:1)
22156 2: DW_TAG_template_type_param
22157 DW_AT_type DW_FORM_ref_udata (E)
22159 Besides being broken debug info, it can put GDB into an
22160 infinite loop. Consider:
22162 When we're building the full name for Class<E>, we'll start
22163 at Class, and go look over its template type parameters,
22164 finding E. We'll then try to build the full name of E, and
22165 reach here. We're now trying to build the full name of E,
22166 and look over the parent DIE for containing scope. In the
22167 broken case, if we followed the parent DIE of E, we'd again
22168 find Class, and once again go look at its template type
22169 arguments, etc., etc. Simply don't consider such parent die
22170 as source-level parent of this die (it can't be, the language
22171 doesn't allow it), and break the loop here. */
22172 name = dwarf2_name (die, cu);
22173 parent_name = dwarf2_name (parent, cu);
22174 complaint (&symfile_complaints,
22175 _("template param type '%s' defined within parent '%s'"),
22176 name ? name : "<unknown>",
22177 parent_name ? parent_name : "<unknown>");
22181 switch (parent->tag)
22183 case DW_TAG_namespace:
22184 parent_type = read_type_die (parent, cu);
22185 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22186 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22187 Work around this problem here. */
22188 if (cu->language == language_cplus
22189 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22191 /* We give a name to even anonymous namespaces. */
22192 return TYPE_TAG_NAME (parent_type);
22193 case DW_TAG_class_type:
22194 case DW_TAG_interface_type:
22195 case DW_TAG_structure_type:
22196 case DW_TAG_union_type:
22197 case DW_TAG_module:
22198 parent_type = read_type_die (parent, cu);
22199 if (TYPE_TAG_NAME (parent_type) != NULL)
22200 return TYPE_TAG_NAME (parent_type);
22202 /* An anonymous structure is only allowed non-static data
22203 members; no typedefs, no member functions, et cetera.
22204 So it does not need a prefix. */
22206 case DW_TAG_compile_unit:
22207 case DW_TAG_partial_unit:
22208 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22209 if (cu->language == language_cplus
22210 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22211 && die->child != NULL
22212 && (die->tag == DW_TAG_class_type
22213 || die->tag == DW_TAG_structure_type
22214 || die->tag == DW_TAG_union_type))
22216 char *name = guess_full_die_structure_name (die, cu);
22221 case DW_TAG_enumeration_type:
22222 parent_type = read_type_die (parent, cu);
22223 if (TYPE_DECLARED_CLASS (parent_type))
22225 if (TYPE_TAG_NAME (parent_type) != NULL)
22226 return TYPE_TAG_NAME (parent_type);
22229 /* Fall through. */
22231 return determine_prefix (parent, cu);
22235 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22236 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22237 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22238 an obconcat, otherwise allocate storage for the result. The CU argument is
22239 used to determine the language and hence, the appropriate separator. */
22241 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22244 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22245 int physname, struct dwarf2_cu *cu)
22247 const char *lead = "";
22250 if (suffix == NULL || suffix[0] == '\0'
22251 || prefix == NULL || prefix[0] == '\0')
22253 else if (cu->language == language_d)
22255 /* For D, the 'main' function could be defined in any module, but it
22256 should never be prefixed. */
22257 if (strcmp (suffix, "D main") == 0)
22265 else if (cu->language == language_fortran && physname)
22267 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22268 DW_AT_MIPS_linkage_name is preferred and used instead. */
22276 if (prefix == NULL)
22278 if (suffix == NULL)
22285 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22287 strcpy (retval, lead);
22288 strcat (retval, prefix);
22289 strcat (retval, sep);
22290 strcat (retval, suffix);
22295 /* We have an obstack. */
22296 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22300 /* Return sibling of die, NULL if no sibling. */
22302 static struct die_info *
22303 sibling_die (struct die_info *die)
22305 return die->sibling;
22308 /* Get name of a die, return NULL if not found. */
22310 static const char *
22311 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22312 struct obstack *obstack)
22314 if (name && cu->language == language_cplus)
22316 std::string canon_name = cp_canonicalize_string (name);
22318 if (!canon_name.empty ())
22320 if (canon_name != name)
22321 name = (const char *) obstack_copy0 (obstack,
22322 canon_name.c_str (),
22323 canon_name.length ());
22330 /* Get name of a die, return NULL if not found.
22331 Anonymous namespaces are converted to their magic string. */
22333 static const char *
22334 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22336 struct attribute *attr;
22338 attr = dwarf2_attr (die, DW_AT_name, cu);
22339 if ((!attr || !DW_STRING (attr))
22340 && die->tag != DW_TAG_namespace
22341 && die->tag != DW_TAG_class_type
22342 && die->tag != DW_TAG_interface_type
22343 && die->tag != DW_TAG_structure_type
22344 && die->tag != DW_TAG_union_type)
22349 case DW_TAG_compile_unit:
22350 case DW_TAG_partial_unit:
22351 /* Compilation units have a DW_AT_name that is a filename, not
22352 a source language identifier. */
22353 case DW_TAG_enumeration_type:
22354 case DW_TAG_enumerator:
22355 /* These tags always have simple identifiers already; no need
22356 to canonicalize them. */
22357 return DW_STRING (attr);
22359 case DW_TAG_namespace:
22360 if (attr != NULL && DW_STRING (attr) != NULL)
22361 return DW_STRING (attr);
22362 return CP_ANONYMOUS_NAMESPACE_STR;
22364 case DW_TAG_class_type:
22365 case DW_TAG_interface_type:
22366 case DW_TAG_structure_type:
22367 case DW_TAG_union_type:
22368 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22369 structures or unions. These were of the form "._%d" in GCC 4.1,
22370 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22371 and GCC 4.4. We work around this problem by ignoring these. */
22372 if (attr && DW_STRING (attr)
22373 && (startswith (DW_STRING (attr), "._")
22374 || startswith (DW_STRING (attr), "<anonymous")))
22377 /* GCC might emit a nameless typedef that has a linkage name. See
22378 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22379 if (!attr || DW_STRING (attr) == NULL)
22381 char *demangled = NULL;
22383 attr = dw2_linkage_name_attr (die, cu);
22384 if (attr == NULL || DW_STRING (attr) == NULL)
22387 /* Avoid demangling DW_STRING (attr) the second time on a second
22388 call for the same DIE. */
22389 if (!DW_STRING_IS_CANONICAL (attr))
22390 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22396 /* FIXME: we already did this for the partial symbol... */
22399 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
22400 demangled, strlen (demangled)));
22401 DW_STRING_IS_CANONICAL (attr) = 1;
22404 /* Strip any leading namespaces/classes, keep only the base name.
22405 DW_AT_name for named DIEs does not contain the prefixes. */
22406 base = strrchr (DW_STRING (attr), ':');
22407 if (base && base > DW_STRING (attr) && base[-1] == ':')
22410 return DW_STRING (attr);
22419 if (!DW_STRING_IS_CANONICAL (attr))
22422 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22423 &cu->objfile->per_bfd->storage_obstack);
22424 DW_STRING_IS_CANONICAL (attr) = 1;
22426 return DW_STRING (attr);
22429 /* Return the die that this die in an extension of, or NULL if there
22430 is none. *EXT_CU is the CU containing DIE on input, and the CU
22431 containing the return value on output. */
22433 static struct die_info *
22434 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22436 struct attribute *attr;
22438 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22442 return follow_die_ref (die, attr, ext_cu);
22445 /* Convert a DIE tag into its string name. */
22447 static const char *
22448 dwarf_tag_name (unsigned tag)
22450 const char *name = get_DW_TAG_name (tag);
22453 return "DW_TAG_<unknown>";
22458 /* Convert a DWARF attribute code into its string name. */
22460 static const char *
22461 dwarf_attr_name (unsigned attr)
22465 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22466 if (attr == DW_AT_MIPS_fde)
22467 return "DW_AT_MIPS_fde";
22469 if (attr == DW_AT_HP_block_index)
22470 return "DW_AT_HP_block_index";
22473 name = get_DW_AT_name (attr);
22476 return "DW_AT_<unknown>";
22481 /* Convert a DWARF value form code into its string name. */
22483 static const char *
22484 dwarf_form_name (unsigned form)
22486 const char *name = get_DW_FORM_name (form);
22489 return "DW_FORM_<unknown>";
22494 static const char *
22495 dwarf_bool_name (unsigned mybool)
22503 /* Convert a DWARF type code into its string name. */
22505 static const char *
22506 dwarf_type_encoding_name (unsigned enc)
22508 const char *name = get_DW_ATE_name (enc);
22511 return "DW_ATE_<unknown>";
22517 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22521 print_spaces (indent, f);
22522 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
22523 dwarf_tag_name (die->tag), die->abbrev,
22524 to_underlying (die->sect_off));
22526 if (die->parent != NULL)
22528 print_spaces (indent, f);
22529 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
22530 to_underlying (die->parent->sect_off));
22533 print_spaces (indent, f);
22534 fprintf_unfiltered (f, " has children: %s\n",
22535 dwarf_bool_name (die->child != NULL));
22537 print_spaces (indent, f);
22538 fprintf_unfiltered (f, " attributes:\n");
22540 for (i = 0; i < die->num_attrs; ++i)
22542 print_spaces (indent, f);
22543 fprintf_unfiltered (f, " %s (%s) ",
22544 dwarf_attr_name (die->attrs[i].name),
22545 dwarf_form_name (die->attrs[i].form));
22547 switch (die->attrs[i].form)
22550 case DW_FORM_GNU_addr_index:
22551 fprintf_unfiltered (f, "address: ");
22552 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22554 case DW_FORM_block2:
22555 case DW_FORM_block4:
22556 case DW_FORM_block:
22557 case DW_FORM_block1:
22558 fprintf_unfiltered (f, "block: size %s",
22559 pulongest (DW_BLOCK (&die->attrs[i])->size));
22561 case DW_FORM_exprloc:
22562 fprintf_unfiltered (f, "expression: size %s",
22563 pulongest (DW_BLOCK (&die->attrs[i])->size));
22565 case DW_FORM_data16:
22566 fprintf_unfiltered (f, "constant of 16 bytes");
22568 case DW_FORM_ref_addr:
22569 fprintf_unfiltered (f, "ref address: ");
22570 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22572 case DW_FORM_GNU_ref_alt:
22573 fprintf_unfiltered (f, "alt ref address: ");
22574 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22580 case DW_FORM_ref_udata:
22581 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22582 (long) (DW_UNSND (&die->attrs[i])));
22584 case DW_FORM_data1:
22585 case DW_FORM_data2:
22586 case DW_FORM_data4:
22587 case DW_FORM_data8:
22588 case DW_FORM_udata:
22589 case DW_FORM_sdata:
22590 fprintf_unfiltered (f, "constant: %s",
22591 pulongest (DW_UNSND (&die->attrs[i])));
22593 case DW_FORM_sec_offset:
22594 fprintf_unfiltered (f, "section offset: %s",
22595 pulongest (DW_UNSND (&die->attrs[i])));
22597 case DW_FORM_ref_sig8:
22598 fprintf_unfiltered (f, "signature: %s",
22599 hex_string (DW_SIGNATURE (&die->attrs[i])));
22601 case DW_FORM_string:
22603 case DW_FORM_line_strp:
22604 case DW_FORM_GNU_str_index:
22605 case DW_FORM_GNU_strp_alt:
22606 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22607 DW_STRING (&die->attrs[i])
22608 ? DW_STRING (&die->attrs[i]) : "",
22609 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22612 if (DW_UNSND (&die->attrs[i]))
22613 fprintf_unfiltered (f, "flag: TRUE");
22615 fprintf_unfiltered (f, "flag: FALSE");
22617 case DW_FORM_flag_present:
22618 fprintf_unfiltered (f, "flag: TRUE");
22620 case DW_FORM_indirect:
22621 /* The reader will have reduced the indirect form to
22622 the "base form" so this form should not occur. */
22623 fprintf_unfiltered (f,
22624 "unexpected attribute form: DW_FORM_indirect");
22626 case DW_FORM_implicit_const:
22627 fprintf_unfiltered (f, "constant: %s",
22628 plongest (DW_SND (&die->attrs[i])));
22631 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22632 die->attrs[i].form);
22635 fprintf_unfiltered (f, "\n");
22640 dump_die_for_error (struct die_info *die)
22642 dump_die_shallow (gdb_stderr, 0, die);
22646 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22648 int indent = level * 4;
22650 gdb_assert (die != NULL);
22652 if (level >= max_level)
22655 dump_die_shallow (f, indent, die);
22657 if (die->child != NULL)
22659 print_spaces (indent, f);
22660 fprintf_unfiltered (f, " Children:");
22661 if (level + 1 < max_level)
22663 fprintf_unfiltered (f, "\n");
22664 dump_die_1 (f, level + 1, max_level, die->child);
22668 fprintf_unfiltered (f,
22669 " [not printed, max nesting level reached]\n");
22673 if (die->sibling != NULL && level > 0)
22675 dump_die_1 (f, level, max_level, die->sibling);
22679 /* This is called from the pdie macro in gdbinit.in.
22680 It's not static so gcc will keep a copy callable from gdb. */
22683 dump_die (struct die_info *die, int max_level)
22685 dump_die_1 (gdb_stdlog, 0, max_level, die);
22689 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22693 slot = htab_find_slot_with_hash (cu->die_hash, die,
22694 to_underlying (die->sect_off),
22700 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22704 dwarf2_get_ref_die_offset (const struct attribute *attr)
22706 if (attr_form_is_ref (attr))
22707 return (sect_offset) DW_UNSND (attr);
22709 complaint (&symfile_complaints,
22710 _("unsupported die ref attribute form: '%s'"),
22711 dwarf_form_name (attr->form));
22715 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22716 * the value held by the attribute is not constant. */
22719 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22721 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22722 return DW_SND (attr);
22723 else if (attr->form == DW_FORM_udata
22724 || attr->form == DW_FORM_data1
22725 || attr->form == DW_FORM_data2
22726 || attr->form == DW_FORM_data4
22727 || attr->form == DW_FORM_data8)
22728 return DW_UNSND (attr);
22731 /* For DW_FORM_data16 see attr_form_is_constant. */
22732 complaint (&symfile_complaints,
22733 _("Attribute value is not a constant (%s)"),
22734 dwarf_form_name (attr->form));
22735 return default_value;
22739 /* Follow reference or signature attribute ATTR of SRC_DIE.
22740 On entry *REF_CU is the CU of SRC_DIE.
22741 On exit *REF_CU is the CU of the result. */
22743 static struct die_info *
22744 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22745 struct dwarf2_cu **ref_cu)
22747 struct die_info *die;
22749 if (attr_form_is_ref (attr))
22750 die = follow_die_ref (src_die, attr, ref_cu);
22751 else if (attr->form == DW_FORM_ref_sig8)
22752 die = follow_die_sig (src_die, attr, ref_cu);
22755 dump_die_for_error (src_die);
22756 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22757 objfile_name ((*ref_cu)->objfile));
22763 /* Follow reference OFFSET.
22764 On entry *REF_CU is the CU of the source die referencing OFFSET.
22765 On exit *REF_CU is the CU of the result.
22766 Returns NULL if OFFSET is invalid. */
22768 static struct die_info *
22769 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22770 struct dwarf2_cu **ref_cu)
22772 struct die_info temp_die;
22773 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22775 gdb_assert (cu->per_cu != NULL);
22779 if (cu->per_cu->is_debug_types)
22781 /* .debug_types CUs cannot reference anything outside their CU.
22782 If they need to, they have to reference a signatured type via
22783 DW_FORM_ref_sig8. */
22784 if (!offset_in_cu_p (&cu->header, sect_off))
22787 else if (offset_in_dwz != cu->per_cu->is_dwz
22788 || !offset_in_cu_p (&cu->header, sect_off))
22790 struct dwarf2_per_cu_data *per_cu;
22792 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22795 /* If necessary, add it to the queue and load its DIEs. */
22796 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22797 load_full_comp_unit (per_cu, cu->language);
22799 target_cu = per_cu->cu;
22801 else if (cu->dies == NULL)
22803 /* We're loading full DIEs during partial symbol reading. */
22804 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22805 load_full_comp_unit (cu->per_cu, language_minimal);
22808 *ref_cu = target_cu;
22809 temp_die.sect_off = sect_off;
22810 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22812 to_underlying (sect_off));
22815 /* Follow reference attribute ATTR of SRC_DIE.
22816 On entry *REF_CU is the CU of SRC_DIE.
22817 On exit *REF_CU is the CU of the result. */
22819 static struct die_info *
22820 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22821 struct dwarf2_cu **ref_cu)
22823 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22824 struct dwarf2_cu *cu = *ref_cu;
22825 struct die_info *die;
22827 die = follow_die_offset (sect_off,
22828 (attr->form == DW_FORM_GNU_ref_alt
22829 || cu->per_cu->is_dwz),
22832 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
22833 "at 0x%x [in module %s]"),
22834 to_underlying (sect_off), to_underlying (src_die->sect_off),
22835 objfile_name (cu->objfile));
22840 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22841 Returned value is intended for DW_OP_call*. Returned
22842 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
22844 struct dwarf2_locexpr_baton
22845 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22846 struct dwarf2_per_cu_data *per_cu,
22847 CORE_ADDR (*get_frame_pc) (void *baton),
22850 struct dwarf2_cu *cu;
22851 struct die_info *die;
22852 struct attribute *attr;
22853 struct dwarf2_locexpr_baton retval;
22855 dw2_setup (per_cu->objfile);
22857 if (per_cu->cu == NULL)
22862 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22863 Instead just throw an error, not much else we can do. */
22864 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
22865 to_underlying (sect_off), objfile_name (per_cu->objfile));
22868 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22870 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
22871 to_underlying (sect_off), objfile_name (per_cu->objfile));
22873 attr = dwarf2_attr (die, DW_AT_location, cu);
22876 /* DWARF: "If there is no such attribute, then there is no effect.".
22877 DATA is ignored if SIZE is 0. */
22879 retval.data = NULL;
22882 else if (attr_form_is_section_offset (attr))
22884 struct dwarf2_loclist_baton loclist_baton;
22885 CORE_ADDR pc = (*get_frame_pc) (baton);
22888 fill_in_loclist_baton (cu, &loclist_baton, attr);
22890 retval.data = dwarf2_find_location_expression (&loclist_baton,
22892 retval.size = size;
22896 if (!attr_form_is_block (attr))
22897 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
22898 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22899 to_underlying (sect_off), objfile_name (per_cu->objfile));
22901 retval.data = DW_BLOCK (attr)->data;
22902 retval.size = DW_BLOCK (attr)->size;
22904 retval.per_cu = cu->per_cu;
22906 age_cached_comp_units ();
22911 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
22914 struct dwarf2_locexpr_baton
22915 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22916 struct dwarf2_per_cu_data *per_cu,
22917 CORE_ADDR (*get_frame_pc) (void *baton),
22920 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22922 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22925 /* Write a constant of a given type as target-ordered bytes into
22928 static const gdb_byte *
22929 write_constant_as_bytes (struct obstack *obstack,
22930 enum bfd_endian byte_order,
22937 *len = TYPE_LENGTH (type);
22938 result = (gdb_byte *) obstack_alloc (obstack, *len);
22939 store_unsigned_integer (result, *len, byte_order, value);
22944 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
22945 pointer to the constant bytes and set LEN to the length of the
22946 data. If memory is needed, allocate it on OBSTACK. If the DIE
22947 does not have a DW_AT_const_value, return NULL. */
22950 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22951 struct dwarf2_per_cu_data *per_cu,
22952 struct obstack *obstack,
22955 struct dwarf2_cu *cu;
22956 struct die_info *die;
22957 struct attribute *attr;
22958 const gdb_byte *result = NULL;
22961 enum bfd_endian byte_order;
22963 dw2_setup (per_cu->objfile);
22965 if (per_cu->cu == NULL)
22970 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22971 Instead just throw an error, not much else we can do. */
22972 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
22973 to_underlying (sect_off), objfile_name (per_cu->objfile));
22976 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22978 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
22979 to_underlying (sect_off), objfile_name (per_cu->objfile));
22982 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22986 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
22987 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22989 switch (attr->form)
22992 case DW_FORM_GNU_addr_index:
22996 *len = cu->header.addr_size;
22997 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22998 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23002 case DW_FORM_string:
23004 case DW_FORM_GNU_str_index:
23005 case DW_FORM_GNU_strp_alt:
23006 /* DW_STRING is already allocated on the objfile obstack, point
23008 result = (const gdb_byte *) DW_STRING (attr);
23009 *len = strlen (DW_STRING (attr));
23011 case DW_FORM_block1:
23012 case DW_FORM_block2:
23013 case DW_FORM_block4:
23014 case DW_FORM_block:
23015 case DW_FORM_exprloc:
23016 case DW_FORM_data16:
23017 result = DW_BLOCK (attr)->data;
23018 *len = DW_BLOCK (attr)->size;
23021 /* The DW_AT_const_value attributes are supposed to carry the
23022 symbol's value "represented as it would be on the target
23023 architecture." By the time we get here, it's already been
23024 converted to host endianness, so we just need to sign- or
23025 zero-extend it as appropriate. */
23026 case DW_FORM_data1:
23027 type = die_type (die, cu);
23028 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23029 if (result == NULL)
23030 result = write_constant_as_bytes (obstack, byte_order,
23033 case DW_FORM_data2:
23034 type = die_type (die, cu);
23035 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23036 if (result == NULL)
23037 result = write_constant_as_bytes (obstack, byte_order,
23040 case DW_FORM_data4:
23041 type = die_type (die, cu);
23042 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23043 if (result == NULL)
23044 result = write_constant_as_bytes (obstack, byte_order,
23047 case DW_FORM_data8:
23048 type = die_type (die, cu);
23049 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23050 if (result == NULL)
23051 result = write_constant_as_bytes (obstack, byte_order,
23055 case DW_FORM_sdata:
23056 case DW_FORM_implicit_const:
23057 type = die_type (die, cu);
23058 result = write_constant_as_bytes (obstack, byte_order,
23059 type, DW_SND (attr), len);
23062 case DW_FORM_udata:
23063 type = die_type (die, cu);
23064 result = write_constant_as_bytes (obstack, byte_order,
23065 type, DW_UNSND (attr), len);
23069 complaint (&symfile_complaints,
23070 _("unsupported const value attribute form: '%s'"),
23071 dwarf_form_name (attr->form));
23078 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23079 valid type for this die is found. */
23082 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23083 struct dwarf2_per_cu_data *per_cu)
23085 struct dwarf2_cu *cu;
23086 struct die_info *die;
23088 dw2_setup (per_cu->objfile);
23090 if (per_cu->cu == NULL)
23096 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23100 return die_type (die, cu);
23103 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23107 dwarf2_get_die_type (cu_offset die_offset,
23108 struct dwarf2_per_cu_data *per_cu)
23110 dw2_setup (per_cu->objfile);
23112 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23113 return get_die_type_at_offset (die_offset_sect, per_cu);
23116 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23117 On entry *REF_CU is the CU of SRC_DIE.
23118 On exit *REF_CU is the CU of the result.
23119 Returns NULL if the referenced DIE isn't found. */
23121 static struct die_info *
23122 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23123 struct dwarf2_cu **ref_cu)
23125 struct die_info temp_die;
23126 struct dwarf2_cu *sig_cu;
23127 struct die_info *die;
23129 /* While it might be nice to assert sig_type->type == NULL here,
23130 we can get here for DW_AT_imported_declaration where we need
23131 the DIE not the type. */
23133 /* If necessary, add it to the queue and load its DIEs. */
23135 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23136 read_signatured_type (sig_type);
23138 sig_cu = sig_type->per_cu.cu;
23139 gdb_assert (sig_cu != NULL);
23140 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23141 temp_die.sect_off = sig_type->type_offset_in_section;
23142 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23143 to_underlying (temp_die.sect_off));
23146 /* For .gdb_index version 7 keep track of included TUs.
23147 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23148 if (dwarf2_per_objfile->index_table != NULL
23149 && dwarf2_per_objfile->index_table->version <= 7)
23151 VEC_safe_push (dwarf2_per_cu_ptr,
23152 (*ref_cu)->per_cu->imported_symtabs,
23163 /* Follow signatured type referenced by ATTR in SRC_DIE.
23164 On entry *REF_CU is the CU of SRC_DIE.
23165 On exit *REF_CU is the CU of the result.
23166 The result is the DIE of the type.
23167 If the referenced type cannot be found an error is thrown. */
23169 static struct die_info *
23170 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23171 struct dwarf2_cu **ref_cu)
23173 ULONGEST signature = DW_SIGNATURE (attr);
23174 struct signatured_type *sig_type;
23175 struct die_info *die;
23177 gdb_assert (attr->form == DW_FORM_ref_sig8);
23179 sig_type = lookup_signatured_type (*ref_cu, signature);
23180 /* sig_type will be NULL if the signatured type is missing from
23182 if (sig_type == NULL)
23184 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23185 " from DIE at 0x%x [in module %s]"),
23186 hex_string (signature), to_underlying (src_die->sect_off),
23187 objfile_name ((*ref_cu)->objfile));
23190 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23193 dump_die_for_error (src_die);
23194 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23195 " from DIE at 0x%x [in module %s]"),
23196 hex_string (signature), to_underlying (src_die->sect_off),
23197 objfile_name ((*ref_cu)->objfile));
23203 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23204 reading in and processing the type unit if necessary. */
23206 static struct type *
23207 get_signatured_type (struct die_info *die, ULONGEST signature,
23208 struct dwarf2_cu *cu)
23210 struct signatured_type *sig_type;
23211 struct dwarf2_cu *type_cu;
23212 struct die_info *type_die;
23215 sig_type = lookup_signatured_type (cu, signature);
23216 /* sig_type will be NULL if the signatured type is missing from
23218 if (sig_type == NULL)
23220 complaint (&symfile_complaints,
23221 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23222 " from DIE at 0x%x [in module %s]"),
23223 hex_string (signature), to_underlying (die->sect_off),
23224 objfile_name (dwarf2_per_objfile->objfile));
23225 return build_error_marker_type (cu, die);
23228 /* If we already know the type we're done. */
23229 if (sig_type->type != NULL)
23230 return sig_type->type;
23233 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23234 if (type_die != NULL)
23236 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23237 is created. This is important, for example, because for c++ classes
23238 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23239 type = read_type_die (type_die, type_cu);
23242 complaint (&symfile_complaints,
23243 _("Dwarf Error: Cannot build signatured type %s"
23244 " referenced from DIE at 0x%x [in module %s]"),
23245 hex_string (signature), to_underlying (die->sect_off),
23246 objfile_name (dwarf2_per_objfile->objfile));
23247 type = build_error_marker_type (cu, die);
23252 complaint (&symfile_complaints,
23253 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23254 " from DIE at 0x%x [in module %s]"),
23255 hex_string (signature), to_underlying (die->sect_off),
23256 objfile_name (dwarf2_per_objfile->objfile));
23257 type = build_error_marker_type (cu, die);
23259 sig_type->type = type;
23264 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23265 reading in and processing the type unit if necessary. */
23267 static struct type *
23268 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23269 struct dwarf2_cu *cu) /* ARI: editCase function */
23271 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23272 if (attr_form_is_ref (attr))
23274 struct dwarf2_cu *type_cu = cu;
23275 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23277 return read_type_die (type_die, type_cu);
23279 else if (attr->form == DW_FORM_ref_sig8)
23281 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23285 complaint (&symfile_complaints,
23286 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23287 " at 0x%x [in module %s]"),
23288 dwarf_form_name (attr->form), to_underlying (die->sect_off),
23289 objfile_name (dwarf2_per_objfile->objfile));
23290 return build_error_marker_type (cu, die);
23294 /* Load the DIEs associated with type unit PER_CU into memory. */
23297 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23299 struct signatured_type *sig_type;
23301 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23302 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23304 /* We have the per_cu, but we need the signatured_type.
23305 Fortunately this is an easy translation. */
23306 gdb_assert (per_cu->is_debug_types);
23307 sig_type = (struct signatured_type *) per_cu;
23309 gdb_assert (per_cu->cu == NULL);
23311 read_signatured_type (sig_type);
23313 gdb_assert (per_cu->cu != NULL);
23316 /* die_reader_func for read_signatured_type.
23317 This is identical to load_full_comp_unit_reader,
23318 but is kept separate for now. */
23321 read_signatured_type_reader (const struct die_reader_specs *reader,
23322 const gdb_byte *info_ptr,
23323 struct die_info *comp_unit_die,
23327 struct dwarf2_cu *cu = reader->cu;
23329 gdb_assert (cu->die_hash == NULL);
23331 htab_create_alloc_ex (cu->header.length / 12,
23335 &cu->comp_unit_obstack,
23336 hashtab_obstack_allocate,
23337 dummy_obstack_deallocate);
23340 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23341 &info_ptr, comp_unit_die);
23342 cu->dies = comp_unit_die;
23343 /* comp_unit_die is not stored in die_hash, no need. */
23345 /* We try not to read any attributes in this function, because not
23346 all CUs needed for references have been loaded yet, and symbol
23347 table processing isn't initialized. But we have to set the CU language,
23348 or we won't be able to build types correctly.
23349 Similarly, if we do not read the producer, we can not apply
23350 producer-specific interpretation. */
23351 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23354 /* Read in a signatured type and build its CU and DIEs.
23355 If the type is a stub for the real type in a DWO file,
23356 read in the real type from the DWO file as well. */
23359 read_signatured_type (struct signatured_type *sig_type)
23361 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23363 gdb_assert (per_cu->is_debug_types);
23364 gdb_assert (per_cu->cu == NULL);
23366 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23367 read_signatured_type_reader, NULL);
23368 sig_type->per_cu.tu_read = 1;
23371 /* Decode simple location descriptions.
23372 Given a pointer to a dwarf block that defines a location, compute
23373 the location and return the value.
23375 NOTE drow/2003-11-18: This function is called in two situations
23376 now: for the address of static or global variables (partial symbols
23377 only) and for offsets into structures which are expected to be
23378 (more or less) constant. The partial symbol case should go away,
23379 and only the constant case should remain. That will let this
23380 function complain more accurately. A few special modes are allowed
23381 without complaint for global variables (for instance, global
23382 register values and thread-local values).
23384 A location description containing no operations indicates that the
23385 object is optimized out. The return value is 0 for that case.
23386 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23387 callers will only want a very basic result and this can become a
23390 Note that stack[0] is unused except as a default error return. */
23393 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23395 struct objfile *objfile = cu->objfile;
23397 size_t size = blk->size;
23398 const gdb_byte *data = blk->data;
23399 CORE_ADDR stack[64];
23401 unsigned int bytes_read, unsnd;
23407 stack[++stacki] = 0;
23446 stack[++stacki] = op - DW_OP_lit0;
23481 stack[++stacki] = op - DW_OP_reg0;
23483 dwarf2_complex_location_expr_complaint ();
23487 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23489 stack[++stacki] = unsnd;
23491 dwarf2_complex_location_expr_complaint ();
23495 stack[++stacki] = read_address (objfile->obfd, &data[i],
23500 case DW_OP_const1u:
23501 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23505 case DW_OP_const1s:
23506 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23510 case DW_OP_const2u:
23511 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23515 case DW_OP_const2s:
23516 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23520 case DW_OP_const4u:
23521 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23525 case DW_OP_const4s:
23526 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23530 case DW_OP_const8u:
23531 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23536 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23542 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23547 stack[stacki + 1] = stack[stacki];
23552 stack[stacki - 1] += stack[stacki];
23556 case DW_OP_plus_uconst:
23557 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23563 stack[stacki - 1] -= stack[stacki];
23568 /* If we're not the last op, then we definitely can't encode
23569 this using GDB's address_class enum. This is valid for partial
23570 global symbols, although the variable's address will be bogus
23573 dwarf2_complex_location_expr_complaint ();
23576 case DW_OP_GNU_push_tls_address:
23577 case DW_OP_form_tls_address:
23578 /* The top of the stack has the offset from the beginning
23579 of the thread control block at which the variable is located. */
23580 /* Nothing should follow this operator, so the top of stack would
23582 /* This is valid for partial global symbols, but the variable's
23583 address will be bogus in the psymtab. Make it always at least
23584 non-zero to not look as a variable garbage collected by linker
23585 which have DW_OP_addr 0. */
23587 dwarf2_complex_location_expr_complaint ();
23591 case DW_OP_GNU_uninit:
23594 case DW_OP_GNU_addr_index:
23595 case DW_OP_GNU_const_index:
23596 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23603 const char *name = get_DW_OP_name (op);
23606 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23609 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23613 return (stack[stacki]);
23616 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23617 outside of the allocated space. Also enforce minimum>0. */
23618 if (stacki >= ARRAY_SIZE (stack) - 1)
23620 complaint (&symfile_complaints,
23621 _("location description stack overflow"));
23627 complaint (&symfile_complaints,
23628 _("location description stack underflow"));
23632 return (stack[stacki]);
23635 /* memory allocation interface */
23637 static struct dwarf_block *
23638 dwarf_alloc_block (struct dwarf2_cu *cu)
23640 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23643 static struct die_info *
23644 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23646 struct die_info *die;
23647 size_t size = sizeof (struct die_info);
23650 size += (num_attrs - 1) * sizeof (struct attribute);
23652 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23653 memset (die, 0, sizeof (struct die_info));
23658 /* Macro support. */
23660 /* Return file name relative to the compilation directory of file number I in
23661 *LH's file name table. The result is allocated using xmalloc; the caller is
23662 responsible for freeing it. */
23665 file_file_name (int file, struct line_header *lh)
23667 /* Is the file number a valid index into the line header's file name
23668 table? Remember that file numbers start with one, not zero. */
23669 if (1 <= file && file <= lh->file_names.size ())
23671 const file_entry &fe = lh->file_names[file - 1];
23673 if (!IS_ABSOLUTE_PATH (fe.name))
23675 const char *dir = fe.include_dir (lh);
23677 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23679 return xstrdup (fe.name);
23683 /* The compiler produced a bogus file number. We can at least
23684 record the macro definitions made in the file, even if we
23685 won't be able to find the file by name. */
23686 char fake_name[80];
23688 xsnprintf (fake_name, sizeof (fake_name),
23689 "<bad macro file number %d>", file);
23691 complaint (&symfile_complaints,
23692 _("bad file number in macro information (%d)"),
23695 return xstrdup (fake_name);
23699 /* Return the full name of file number I in *LH's file name table.
23700 Use COMP_DIR as the name of the current directory of the
23701 compilation. The result is allocated using xmalloc; the caller is
23702 responsible for freeing it. */
23704 file_full_name (int file, struct line_header *lh, const char *comp_dir)
23706 /* Is the file number a valid index into the line header's file name
23707 table? Remember that file numbers start with one, not zero. */
23708 if (1 <= file && file <= lh->file_names.size ())
23710 char *relative = file_file_name (file, lh);
23712 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23714 return reconcat (relative, comp_dir, SLASH_STRING,
23715 relative, (char *) NULL);
23718 return file_file_name (file, lh);
23722 static struct macro_source_file *
23723 macro_start_file (int file, int line,
23724 struct macro_source_file *current_file,
23725 struct line_header *lh)
23727 /* File name relative to the compilation directory of this source file. */
23728 char *file_name = file_file_name (file, lh);
23730 if (! current_file)
23732 /* Note: We don't create a macro table for this compilation unit
23733 at all until we actually get a filename. */
23734 struct macro_table *macro_table = get_macro_table ();
23736 /* If we have no current file, then this must be the start_file
23737 directive for the compilation unit's main source file. */
23738 current_file = macro_set_main (macro_table, file_name);
23739 macro_define_special (macro_table);
23742 current_file = macro_include (current_file, line, file_name);
23746 return current_file;
23749 static const char *
23750 consume_improper_spaces (const char *p, const char *body)
23754 complaint (&symfile_complaints,
23755 _("macro definition contains spaces "
23756 "in formal argument list:\n`%s'"),
23768 parse_macro_definition (struct macro_source_file *file, int line,
23773 /* The body string takes one of two forms. For object-like macro
23774 definitions, it should be:
23776 <macro name> " " <definition>
23778 For function-like macro definitions, it should be:
23780 <macro name> "() " <definition>
23782 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23784 Spaces may appear only where explicitly indicated, and in the
23787 The Dwarf 2 spec says that an object-like macro's name is always
23788 followed by a space, but versions of GCC around March 2002 omit
23789 the space when the macro's definition is the empty string.
23791 The Dwarf 2 spec says that there should be no spaces between the
23792 formal arguments in a function-like macro's formal argument list,
23793 but versions of GCC around March 2002 include spaces after the
23797 /* Find the extent of the macro name. The macro name is terminated
23798 by either a space or null character (for an object-like macro) or
23799 an opening paren (for a function-like macro). */
23800 for (p = body; *p; p++)
23801 if (*p == ' ' || *p == '(')
23804 if (*p == ' ' || *p == '\0')
23806 /* It's an object-like macro. */
23807 int name_len = p - body;
23808 char *name = savestring (body, name_len);
23809 const char *replacement;
23812 replacement = body + name_len + 1;
23815 dwarf2_macro_malformed_definition_complaint (body);
23816 replacement = body + name_len;
23819 macro_define_object (file, line, name, replacement);
23823 else if (*p == '(')
23825 /* It's a function-like macro. */
23826 char *name = savestring (body, p - body);
23829 char **argv = XNEWVEC (char *, argv_size);
23833 p = consume_improper_spaces (p, body);
23835 /* Parse the formal argument list. */
23836 while (*p && *p != ')')
23838 /* Find the extent of the current argument name. */
23839 const char *arg_start = p;
23841 while (*p && *p != ',' && *p != ')' && *p != ' ')
23844 if (! *p || p == arg_start)
23845 dwarf2_macro_malformed_definition_complaint (body);
23848 /* Make sure argv has room for the new argument. */
23849 if (argc >= argv_size)
23852 argv = XRESIZEVEC (char *, argv, argv_size);
23855 argv[argc++] = savestring (arg_start, p - arg_start);
23858 p = consume_improper_spaces (p, body);
23860 /* Consume the comma, if present. */
23865 p = consume_improper_spaces (p, body);
23874 /* Perfectly formed definition, no complaints. */
23875 macro_define_function (file, line, name,
23876 argc, (const char **) argv,
23878 else if (*p == '\0')
23880 /* Complain, but do define it. */
23881 dwarf2_macro_malformed_definition_complaint (body);
23882 macro_define_function (file, line, name,
23883 argc, (const char **) argv,
23887 /* Just complain. */
23888 dwarf2_macro_malformed_definition_complaint (body);
23891 /* Just complain. */
23892 dwarf2_macro_malformed_definition_complaint (body);
23898 for (i = 0; i < argc; i++)
23904 dwarf2_macro_malformed_definition_complaint (body);
23907 /* Skip some bytes from BYTES according to the form given in FORM.
23908 Returns the new pointer. */
23910 static const gdb_byte *
23911 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
23912 enum dwarf_form form,
23913 unsigned int offset_size,
23914 struct dwarf2_section_info *section)
23916 unsigned int bytes_read;
23920 case DW_FORM_data1:
23925 case DW_FORM_data2:
23929 case DW_FORM_data4:
23933 case DW_FORM_data8:
23937 case DW_FORM_data16:
23941 case DW_FORM_string:
23942 read_direct_string (abfd, bytes, &bytes_read);
23943 bytes += bytes_read;
23946 case DW_FORM_sec_offset:
23948 case DW_FORM_GNU_strp_alt:
23949 bytes += offset_size;
23952 case DW_FORM_block:
23953 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23954 bytes += bytes_read;
23957 case DW_FORM_block1:
23958 bytes += 1 + read_1_byte (abfd, bytes);
23960 case DW_FORM_block2:
23961 bytes += 2 + read_2_bytes (abfd, bytes);
23963 case DW_FORM_block4:
23964 bytes += 4 + read_4_bytes (abfd, bytes);
23967 case DW_FORM_sdata:
23968 case DW_FORM_udata:
23969 case DW_FORM_GNU_addr_index:
23970 case DW_FORM_GNU_str_index:
23971 bytes = gdb_skip_leb128 (bytes, buffer_end);
23974 dwarf2_section_buffer_overflow_complaint (section);
23979 case DW_FORM_implicit_const:
23984 complaint (&symfile_complaints,
23985 _("invalid form 0x%x in `%s'"),
23986 form, get_section_name (section));
23994 /* A helper for dwarf_decode_macros that handles skipping an unknown
23995 opcode. Returns an updated pointer to the macro data buffer; or,
23996 on error, issues a complaint and returns NULL. */
23998 static const gdb_byte *
23999 skip_unknown_opcode (unsigned int opcode,
24000 const gdb_byte **opcode_definitions,
24001 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24003 unsigned int offset_size,
24004 struct dwarf2_section_info *section)
24006 unsigned int bytes_read, i;
24008 const gdb_byte *defn;
24010 if (opcode_definitions[opcode] == NULL)
24012 complaint (&symfile_complaints,
24013 _("unrecognized DW_MACFINO opcode 0x%x"),
24018 defn = opcode_definitions[opcode];
24019 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24020 defn += bytes_read;
24022 for (i = 0; i < arg; ++i)
24024 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24025 (enum dwarf_form) defn[i], offset_size,
24027 if (mac_ptr == NULL)
24029 /* skip_form_bytes already issued the complaint. */
24037 /* A helper function which parses the header of a macro section.
24038 If the macro section is the extended (for now called "GNU") type,
24039 then this updates *OFFSET_SIZE. Returns a pointer to just after
24040 the header, or issues a complaint and returns NULL on error. */
24042 static const gdb_byte *
24043 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24045 const gdb_byte *mac_ptr,
24046 unsigned int *offset_size,
24047 int section_is_gnu)
24049 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24051 if (section_is_gnu)
24053 unsigned int version, flags;
24055 version = read_2_bytes (abfd, mac_ptr);
24056 if (version != 4 && version != 5)
24058 complaint (&symfile_complaints,
24059 _("unrecognized version `%d' in .debug_macro section"),
24065 flags = read_1_byte (abfd, mac_ptr);
24067 *offset_size = (flags & 1) ? 8 : 4;
24069 if ((flags & 2) != 0)
24070 /* We don't need the line table offset. */
24071 mac_ptr += *offset_size;
24073 /* Vendor opcode descriptions. */
24074 if ((flags & 4) != 0)
24076 unsigned int i, count;
24078 count = read_1_byte (abfd, mac_ptr);
24080 for (i = 0; i < count; ++i)
24082 unsigned int opcode, bytes_read;
24085 opcode = read_1_byte (abfd, mac_ptr);
24087 opcode_definitions[opcode] = mac_ptr;
24088 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24089 mac_ptr += bytes_read;
24098 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24099 including DW_MACRO_import. */
24102 dwarf_decode_macro_bytes (bfd *abfd,
24103 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24104 struct macro_source_file *current_file,
24105 struct line_header *lh,
24106 struct dwarf2_section_info *section,
24107 int section_is_gnu, int section_is_dwz,
24108 unsigned int offset_size,
24109 htab_t include_hash)
24111 struct objfile *objfile = dwarf2_per_objfile->objfile;
24112 enum dwarf_macro_record_type macinfo_type;
24113 int at_commandline;
24114 const gdb_byte *opcode_definitions[256];
24116 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24117 &offset_size, section_is_gnu);
24118 if (mac_ptr == NULL)
24120 /* We already issued a complaint. */
24124 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24125 GDB is still reading the definitions from command line. First
24126 DW_MACINFO_start_file will need to be ignored as it was already executed
24127 to create CURRENT_FILE for the main source holding also the command line
24128 definitions. On first met DW_MACINFO_start_file this flag is reset to
24129 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24131 at_commandline = 1;
24135 /* Do we at least have room for a macinfo type byte? */
24136 if (mac_ptr >= mac_end)
24138 dwarf2_section_buffer_overflow_complaint (section);
24142 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24145 /* Note that we rely on the fact that the corresponding GNU and
24146 DWARF constants are the same. */
24147 switch (macinfo_type)
24149 /* A zero macinfo type indicates the end of the macro
24154 case DW_MACRO_define:
24155 case DW_MACRO_undef:
24156 case DW_MACRO_define_strp:
24157 case DW_MACRO_undef_strp:
24158 case DW_MACRO_define_sup:
24159 case DW_MACRO_undef_sup:
24161 unsigned int bytes_read;
24166 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24167 mac_ptr += bytes_read;
24169 if (macinfo_type == DW_MACRO_define
24170 || macinfo_type == DW_MACRO_undef)
24172 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24173 mac_ptr += bytes_read;
24177 LONGEST str_offset;
24179 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24180 mac_ptr += offset_size;
24182 if (macinfo_type == DW_MACRO_define_sup
24183 || macinfo_type == DW_MACRO_undef_sup
24186 struct dwz_file *dwz = dwarf2_get_dwz_file ();
24188 body = read_indirect_string_from_dwz (dwz, str_offset);
24191 body = read_indirect_string_at_offset (abfd, str_offset);
24194 is_define = (macinfo_type == DW_MACRO_define
24195 || macinfo_type == DW_MACRO_define_strp
24196 || macinfo_type == DW_MACRO_define_sup);
24197 if (! current_file)
24199 /* DWARF violation as no main source is present. */
24200 complaint (&symfile_complaints,
24201 _("debug info with no main source gives macro %s "
24203 is_define ? _("definition") : _("undefinition"),
24207 if ((line == 0 && !at_commandline)
24208 || (line != 0 && at_commandline))
24209 complaint (&symfile_complaints,
24210 _("debug info gives %s macro %s with %s line %d: %s"),
24211 at_commandline ? _("command-line") : _("in-file"),
24212 is_define ? _("definition") : _("undefinition"),
24213 line == 0 ? _("zero") : _("non-zero"), line, body);
24216 parse_macro_definition (current_file, line, body);
24219 gdb_assert (macinfo_type == DW_MACRO_undef
24220 || macinfo_type == DW_MACRO_undef_strp
24221 || macinfo_type == DW_MACRO_undef_sup);
24222 macro_undef (current_file, line, body);
24227 case DW_MACRO_start_file:
24229 unsigned int bytes_read;
24232 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24233 mac_ptr += bytes_read;
24234 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24235 mac_ptr += bytes_read;
24237 if ((line == 0 && !at_commandline)
24238 || (line != 0 && at_commandline))
24239 complaint (&symfile_complaints,
24240 _("debug info gives source %d included "
24241 "from %s at %s line %d"),
24242 file, at_commandline ? _("command-line") : _("file"),
24243 line == 0 ? _("zero") : _("non-zero"), line);
24245 if (at_commandline)
24247 /* This DW_MACRO_start_file was executed in the
24249 at_commandline = 0;
24252 current_file = macro_start_file (file, line, current_file, lh);
24256 case DW_MACRO_end_file:
24257 if (! current_file)
24258 complaint (&symfile_complaints,
24259 _("macro debug info has an unmatched "
24260 "`close_file' directive"));
24263 current_file = current_file->included_by;
24264 if (! current_file)
24266 enum dwarf_macro_record_type next_type;
24268 /* GCC circa March 2002 doesn't produce the zero
24269 type byte marking the end of the compilation
24270 unit. Complain if it's not there, but exit no
24273 /* Do we at least have room for a macinfo type byte? */
24274 if (mac_ptr >= mac_end)
24276 dwarf2_section_buffer_overflow_complaint (section);
24280 /* We don't increment mac_ptr here, so this is just
24283 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24285 if (next_type != 0)
24286 complaint (&symfile_complaints,
24287 _("no terminating 0-type entry for "
24288 "macros in `.debug_macinfo' section"));
24295 case DW_MACRO_import:
24296 case DW_MACRO_import_sup:
24300 bfd *include_bfd = abfd;
24301 struct dwarf2_section_info *include_section = section;
24302 const gdb_byte *include_mac_end = mac_end;
24303 int is_dwz = section_is_dwz;
24304 const gdb_byte *new_mac_ptr;
24306 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24307 mac_ptr += offset_size;
24309 if (macinfo_type == DW_MACRO_import_sup)
24311 struct dwz_file *dwz = dwarf2_get_dwz_file ();
24313 dwarf2_read_section (objfile, &dwz->macro);
24315 include_section = &dwz->macro;
24316 include_bfd = get_section_bfd_owner (include_section);
24317 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24321 new_mac_ptr = include_section->buffer + offset;
24322 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24326 /* This has actually happened; see
24327 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24328 complaint (&symfile_complaints,
24329 _("recursive DW_MACRO_import in "
24330 ".debug_macro section"));
24334 *slot = (void *) new_mac_ptr;
24336 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
24337 include_mac_end, current_file, lh,
24338 section, section_is_gnu, is_dwz,
24339 offset_size, include_hash);
24341 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24346 case DW_MACINFO_vendor_ext:
24347 if (!section_is_gnu)
24349 unsigned int bytes_read;
24351 /* This reads the constant, but since we don't recognize
24352 any vendor extensions, we ignore it. */
24353 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24354 mac_ptr += bytes_read;
24355 read_direct_string (abfd, mac_ptr, &bytes_read);
24356 mac_ptr += bytes_read;
24358 /* We don't recognize any vendor extensions. */
24364 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24365 mac_ptr, mac_end, abfd, offset_size,
24367 if (mac_ptr == NULL)
24371 } while (macinfo_type != 0);
24375 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24376 int section_is_gnu)
24378 struct objfile *objfile = dwarf2_per_objfile->objfile;
24379 struct line_header *lh = cu->line_header;
24381 const gdb_byte *mac_ptr, *mac_end;
24382 struct macro_source_file *current_file = 0;
24383 enum dwarf_macro_record_type macinfo_type;
24384 unsigned int offset_size = cu->header.offset_size;
24385 const gdb_byte *opcode_definitions[256];
24387 struct dwarf2_section_info *section;
24388 const char *section_name;
24390 if (cu->dwo_unit != NULL)
24392 if (section_is_gnu)
24394 section = &cu->dwo_unit->dwo_file->sections.macro;
24395 section_name = ".debug_macro.dwo";
24399 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24400 section_name = ".debug_macinfo.dwo";
24405 if (section_is_gnu)
24407 section = &dwarf2_per_objfile->macro;
24408 section_name = ".debug_macro";
24412 section = &dwarf2_per_objfile->macinfo;
24413 section_name = ".debug_macinfo";
24417 dwarf2_read_section (objfile, section);
24418 if (section->buffer == NULL)
24420 complaint (&symfile_complaints, _("missing %s section"), section_name);
24423 abfd = get_section_bfd_owner (section);
24425 /* First pass: Find the name of the base filename.
24426 This filename is needed in order to process all macros whose definition
24427 (or undefinition) comes from the command line. These macros are defined
24428 before the first DW_MACINFO_start_file entry, and yet still need to be
24429 associated to the base file.
24431 To determine the base file name, we scan the macro definitions until we
24432 reach the first DW_MACINFO_start_file entry. We then initialize
24433 CURRENT_FILE accordingly so that any macro definition found before the
24434 first DW_MACINFO_start_file can still be associated to the base file. */
24436 mac_ptr = section->buffer + offset;
24437 mac_end = section->buffer + section->size;
24439 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24440 &offset_size, section_is_gnu);
24441 if (mac_ptr == NULL)
24443 /* We already issued a complaint. */
24449 /* Do we at least have room for a macinfo type byte? */
24450 if (mac_ptr >= mac_end)
24452 /* Complaint is printed during the second pass as GDB will probably
24453 stop the first pass earlier upon finding
24454 DW_MACINFO_start_file. */
24458 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24461 /* Note that we rely on the fact that the corresponding GNU and
24462 DWARF constants are the same. */
24463 switch (macinfo_type)
24465 /* A zero macinfo type indicates the end of the macro
24470 case DW_MACRO_define:
24471 case DW_MACRO_undef:
24472 /* Only skip the data by MAC_PTR. */
24474 unsigned int bytes_read;
24476 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24477 mac_ptr += bytes_read;
24478 read_direct_string (abfd, mac_ptr, &bytes_read);
24479 mac_ptr += bytes_read;
24483 case DW_MACRO_start_file:
24485 unsigned int bytes_read;
24488 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24489 mac_ptr += bytes_read;
24490 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24491 mac_ptr += bytes_read;
24493 current_file = macro_start_file (file, line, current_file, lh);
24497 case DW_MACRO_end_file:
24498 /* No data to skip by MAC_PTR. */
24501 case DW_MACRO_define_strp:
24502 case DW_MACRO_undef_strp:
24503 case DW_MACRO_define_sup:
24504 case DW_MACRO_undef_sup:
24506 unsigned int bytes_read;
24508 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24509 mac_ptr += bytes_read;
24510 mac_ptr += offset_size;
24514 case DW_MACRO_import:
24515 case DW_MACRO_import_sup:
24516 /* Note that, according to the spec, a transparent include
24517 chain cannot call DW_MACRO_start_file. So, we can just
24518 skip this opcode. */
24519 mac_ptr += offset_size;
24522 case DW_MACINFO_vendor_ext:
24523 /* Only skip the data by MAC_PTR. */
24524 if (!section_is_gnu)
24526 unsigned int bytes_read;
24528 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24529 mac_ptr += bytes_read;
24530 read_direct_string (abfd, mac_ptr, &bytes_read);
24531 mac_ptr += bytes_read;
24536 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24537 mac_ptr, mac_end, abfd, offset_size,
24539 if (mac_ptr == NULL)
24543 } while (macinfo_type != 0 && current_file == NULL);
24545 /* Second pass: Process all entries.
24547 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24548 command-line macro definitions/undefinitions. This flag is unset when we
24549 reach the first DW_MACINFO_start_file entry. */
24551 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24553 NULL, xcalloc, xfree));
24554 mac_ptr = section->buffer + offset;
24555 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24556 *slot = (void *) mac_ptr;
24557 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
24558 current_file, lh, section,
24559 section_is_gnu, 0, offset_size,
24560 include_hash.get ());
24563 /* Check if the attribute's form is a DW_FORM_block*
24564 if so return true else false. */
24567 attr_form_is_block (const struct attribute *attr)
24569 return (attr == NULL ? 0 :
24570 attr->form == DW_FORM_block1
24571 || attr->form == DW_FORM_block2
24572 || attr->form == DW_FORM_block4
24573 || attr->form == DW_FORM_block
24574 || attr->form == DW_FORM_exprloc);
24577 /* Return non-zero if ATTR's value is a section offset --- classes
24578 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24579 You may use DW_UNSND (attr) to retrieve such offsets.
24581 Section 7.5.4, "Attribute Encodings", explains that no attribute
24582 may have a value that belongs to more than one of these classes; it
24583 would be ambiguous if we did, because we use the same forms for all
24587 attr_form_is_section_offset (const struct attribute *attr)
24589 return (attr->form == DW_FORM_data4
24590 || attr->form == DW_FORM_data8
24591 || attr->form == DW_FORM_sec_offset);
24594 /* Return non-zero if ATTR's value falls in the 'constant' class, or
24595 zero otherwise. When this function returns true, you can apply
24596 dwarf2_get_attr_constant_value to it.
24598 However, note that for some attributes you must check
24599 attr_form_is_section_offset before using this test. DW_FORM_data4
24600 and DW_FORM_data8 are members of both the constant class, and of
24601 the classes that contain offsets into other debug sections
24602 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24603 that, if an attribute's can be either a constant or one of the
24604 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
24605 taken as section offsets, not constants.
24607 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24608 cannot handle that. */
24611 attr_form_is_constant (const struct attribute *attr)
24613 switch (attr->form)
24615 case DW_FORM_sdata:
24616 case DW_FORM_udata:
24617 case DW_FORM_data1:
24618 case DW_FORM_data2:
24619 case DW_FORM_data4:
24620 case DW_FORM_data8:
24621 case DW_FORM_implicit_const:
24629 /* DW_ADDR is always stored already as sect_offset; despite for the forms
24630 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24633 attr_form_is_ref (const struct attribute *attr)
24635 switch (attr->form)
24637 case DW_FORM_ref_addr:
24642 case DW_FORM_ref_udata:
24643 case DW_FORM_GNU_ref_alt:
24650 /* Return the .debug_loc section to use for CU.
24651 For DWO files use .debug_loc.dwo. */
24653 static struct dwarf2_section_info *
24654 cu_debug_loc_section (struct dwarf2_cu *cu)
24658 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24660 return cu->header.version >= 5 ? §ions->loclists : §ions->loc;
24662 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24663 : &dwarf2_per_objfile->loc);
24666 /* A helper function that fills in a dwarf2_loclist_baton. */
24669 fill_in_loclist_baton (struct dwarf2_cu *cu,
24670 struct dwarf2_loclist_baton *baton,
24671 const struct attribute *attr)
24673 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24675 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
24677 baton->per_cu = cu->per_cu;
24678 gdb_assert (baton->per_cu);
24679 /* We don't know how long the location list is, but make sure we
24680 don't run off the edge of the section. */
24681 baton->size = section->size - DW_UNSND (attr);
24682 baton->data = section->buffer + DW_UNSND (attr);
24683 baton->base_address = cu->base_address;
24684 baton->from_dwo = cu->dwo_unit != NULL;
24688 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24689 struct dwarf2_cu *cu, int is_block)
24691 struct objfile *objfile = dwarf2_per_objfile->objfile;
24692 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24694 if (attr_form_is_section_offset (attr)
24695 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24696 the section. If so, fall through to the complaint in the
24698 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
24700 struct dwarf2_loclist_baton *baton;
24702 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24704 fill_in_loclist_baton (cu, baton, attr);
24706 if (cu->base_known == 0)
24707 complaint (&symfile_complaints,
24708 _("Location list used without "
24709 "specifying the CU base address."));
24711 SYMBOL_ACLASS_INDEX (sym) = (is_block
24712 ? dwarf2_loclist_block_index
24713 : dwarf2_loclist_index);
24714 SYMBOL_LOCATION_BATON (sym) = baton;
24718 struct dwarf2_locexpr_baton *baton;
24720 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24721 baton->per_cu = cu->per_cu;
24722 gdb_assert (baton->per_cu);
24724 if (attr_form_is_block (attr))
24726 /* Note that we're just copying the block's data pointer
24727 here, not the actual data. We're still pointing into the
24728 info_buffer for SYM's objfile; right now we never release
24729 that buffer, but when we do clean up properly this may
24731 baton->size = DW_BLOCK (attr)->size;
24732 baton->data = DW_BLOCK (attr)->data;
24736 dwarf2_invalid_attrib_class_complaint ("location description",
24737 SYMBOL_NATURAL_NAME (sym));
24741 SYMBOL_ACLASS_INDEX (sym) = (is_block
24742 ? dwarf2_locexpr_block_index
24743 : dwarf2_locexpr_index);
24744 SYMBOL_LOCATION_BATON (sym) = baton;
24748 /* Return the OBJFILE associated with the compilation unit CU. If CU
24749 came from a separate debuginfo file, then the master objfile is
24753 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24755 struct objfile *objfile = per_cu->objfile;
24757 /* Return the master objfile, so that we can report and look up the
24758 correct file containing this variable. */
24759 if (objfile->separate_debug_objfile_backlink)
24760 objfile = objfile->separate_debug_objfile_backlink;
24765 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24766 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24767 CU_HEADERP first. */
24769 static const struct comp_unit_head *
24770 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24771 struct dwarf2_per_cu_data *per_cu)
24773 const gdb_byte *info_ptr;
24776 return &per_cu->cu->header;
24778 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24780 memset (cu_headerp, 0, sizeof (*cu_headerp));
24781 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24782 rcuh_kind::COMPILE);
24787 /* Return the address size given in the compilation unit header for CU. */
24790 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24792 struct comp_unit_head cu_header_local;
24793 const struct comp_unit_head *cu_headerp;
24795 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24797 return cu_headerp->addr_size;
24800 /* Return the offset size given in the compilation unit header for CU. */
24803 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24805 struct comp_unit_head cu_header_local;
24806 const struct comp_unit_head *cu_headerp;
24808 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24810 return cu_headerp->offset_size;
24813 /* See its dwarf2loc.h declaration. */
24816 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24818 struct comp_unit_head cu_header_local;
24819 const struct comp_unit_head *cu_headerp;
24821 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24823 if (cu_headerp->version == 2)
24824 return cu_headerp->addr_size;
24826 return cu_headerp->offset_size;
24829 /* Return the text offset of the CU. The returned offset comes from
24830 this CU's objfile. If this objfile came from a separate debuginfo
24831 file, then the offset may be different from the corresponding
24832 offset in the parent objfile. */
24835 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24837 struct objfile *objfile = per_cu->objfile;
24839 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24842 /* Return DWARF version number of PER_CU. */
24845 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24847 return per_cu->dwarf_version;
24850 /* Locate the .debug_info compilation unit from CU's objfile which contains
24851 the DIE at OFFSET. Raises an error on failure. */
24853 static struct dwarf2_per_cu_data *
24854 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24855 unsigned int offset_in_dwz,
24856 struct objfile *objfile)
24858 struct dwarf2_per_cu_data *this_cu;
24860 const sect_offset *cu_off;
24863 high = dwarf2_per_objfile->n_comp_units - 1;
24866 struct dwarf2_per_cu_data *mid_cu;
24867 int mid = low + (high - low) / 2;
24869 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
24870 cu_off = &mid_cu->sect_off;
24871 if (mid_cu->is_dwz > offset_in_dwz
24872 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
24877 gdb_assert (low == high);
24878 this_cu = dwarf2_per_objfile->all_comp_units[low];
24879 cu_off = &this_cu->sect_off;
24880 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
24882 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24883 error (_("Dwarf Error: could not find partial DIE containing "
24884 "offset 0x%x [in module %s]"),
24885 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
24887 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24889 return dwarf2_per_objfile->all_comp_units[low-1];
24893 this_cu = dwarf2_per_objfile->all_comp_units[low];
24894 if (low == dwarf2_per_objfile->n_comp_units - 1
24895 && sect_off >= this_cu->sect_off + this_cu->length)
24896 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
24897 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24902 /* Initialize dwarf2_cu CU, owned by PER_CU. */
24905 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
24907 memset (cu, 0, sizeof (*cu));
24909 cu->per_cu = per_cu;
24910 cu->objfile = per_cu->objfile;
24911 obstack_init (&cu->comp_unit_obstack);
24914 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24917 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24918 enum language pretend_language)
24920 struct attribute *attr;
24922 /* Set the language we're debugging. */
24923 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24925 set_cu_language (DW_UNSND (attr), cu);
24928 cu->language = pretend_language;
24929 cu->language_defn = language_def (cu->language);
24932 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24935 /* Release one cached compilation unit, CU. We unlink it from the tree
24936 of compilation units, but we don't remove it from the read_in_chain;
24937 the caller is responsible for that.
24938 NOTE: DATA is a void * because this function is also used as a
24939 cleanup routine. */
24942 free_heap_comp_unit (void *data)
24944 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
24946 gdb_assert (cu->per_cu != NULL);
24947 cu->per_cu->cu = NULL;
24950 obstack_free (&cu->comp_unit_obstack, NULL);
24955 /* This cleanup function is passed the address of a dwarf2_cu on the stack
24956 when we're finished with it. We can't free the pointer itself, but be
24957 sure to unlink it from the cache. Also release any associated storage. */
24960 free_stack_comp_unit (void *data)
24962 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
24964 gdb_assert (cu->per_cu != NULL);
24965 cu->per_cu->cu = NULL;
24968 obstack_free (&cu->comp_unit_obstack, NULL);
24969 cu->partial_dies = NULL;
24972 /* Free all cached compilation units. */
24975 free_cached_comp_units (void *data)
24977 dwarf2_per_objfile->free_cached_comp_units ();
24980 /* Increase the age counter on each cached compilation unit, and free
24981 any that are too old. */
24984 age_cached_comp_units (void)
24986 struct dwarf2_per_cu_data *per_cu, **last_chain;
24988 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
24989 per_cu = dwarf2_per_objfile->read_in_chain;
24990 while (per_cu != NULL)
24992 per_cu->cu->last_used ++;
24993 if (per_cu->cu->last_used <= dwarf_max_cache_age)
24994 dwarf2_mark (per_cu->cu);
24995 per_cu = per_cu->cu->read_in_chain;
24998 per_cu = dwarf2_per_objfile->read_in_chain;
24999 last_chain = &dwarf2_per_objfile->read_in_chain;
25000 while (per_cu != NULL)
25002 struct dwarf2_per_cu_data *next_cu;
25004 next_cu = per_cu->cu->read_in_chain;
25006 if (!per_cu->cu->mark)
25008 free_heap_comp_unit (per_cu->cu);
25009 *last_chain = next_cu;
25012 last_chain = &per_cu->cu->read_in_chain;
25018 /* Remove a single compilation unit from the cache. */
25021 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25023 struct dwarf2_per_cu_data *per_cu, **last_chain;
25025 per_cu = dwarf2_per_objfile->read_in_chain;
25026 last_chain = &dwarf2_per_objfile->read_in_chain;
25027 while (per_cu != NULL)
25029 struct dwarf2_per_cu_data *next_cu;
25031 next_cu = per_cu->cu->read_in_chain;
25033 if (per_cu == target_per_cu)
25035 free_heap_comp_unit (per_cu->cu);
25037 *last_chain = next_cu;
25041 last_chain = &per_cu->cu->read_in_chain;
25047 /* Release all extra memory associated with OBJFILE. */
25050 dwarf2_free_objfile (struct objfile *objfile)
25053 = (struct dwarf2_per_objfile *) objfile_data (objfile,
25054 dwarf2_objfile_data_key);
25056 if (dwarf2_per_objfile == NULL)
25059 dwarf2_per_objfile->~dwarf2_per_objfile ();
25062 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25063 We store these in a hash table separate from the DIEs, and preserve them
25064 when the DIEs are flushed out of cache.
25066 The CU "per_cu" pointer is needed because offset alone is not enough to
25067 uniquely identify the type. A file may have multiple .debug_types sections,
25068 or the type may come from a DWO file. Furthermore, while it's more logical
25069 to use per_cu->section+offset, with Fission the section with the data is in
25070 the DWO file but we don't know that section at the point we need it.
25071 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25072 because we can enter the lookup routine, get_die_type_at_offset, from
25073 outside this file, and thus won't necessarily have PER_CU->cu.
25074 Fortunately, PER_CU is stable for the life of the objfile. */
25076 struct dwarf2_per_cu_offset_and_type
25078 const struct dwarf2_per_cu_data *per_cu;
25079 sect_offset sect_off;
25083 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25086 per_cu_offset_and_type_hash (const void *item)
25088 const struct dwarf2_per_cu_offset_and_type *ofs
25089 = (const struct dwarf2_per_cu_offset_and_type *) item;
25091 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25094 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25097 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25099 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25100 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25101 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25102 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25104 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25105 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25108 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25109 table if necessary. For convenience, return TYPE.
25111 The DIEs reading must have careful ordering to:
25112 * Not cause infite loops trying to read in DIEs as a prerequisite for
25113 reading current DIE.
25114 * Not trying to dereference contents of still incompletely read in types
25115 while reading in other DIEs.
25116 * Enable referencing still incompletely read in types just by a pointer to
25117 the type without accessing its fields.
25119 Therefore caller should follow these rules:
25120 * Try to fetch any prerequisite types we may need to build this DIE type
25121 before building the type and calling set_die_type.
25122 * After building type call set_die_type for current DIE as soon as
25123 possible before fetching more types to complete the current type.
25124 * Make the type as complete as possible before fetching more types. */
25126 static struct type *
25127 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25129 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25130 struct objfile *objfile = cu->objfile;
25131 struct attribute *attr;
25132 struct dynamic_prop prop;
25134 /* For Ada types, make sure that the gnat-specific data is always
25135 initialized (if not already set). There are a few types where
25136 we should not be doing so, because the type-specific area is
25137 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25138 where the type-specific area is used to store the floatformat).
25139 But this is not a problem, because the gnat-specific information
25140 is actually not needed for these types. */
25141 if (need_gnat_info (cu)
25142 && TYPE_CODE (type) != TYPE_CODE_FUNC
25143 && TYPE_CODE (type) != TYPE_CODE_FLT
25144 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25145 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25146 && TYPE_CODE (type) != TYPE_CODE_METHOD
25147 && !HAVE_GNAT_AUX_INFO (type))
25148 INIT_GNAT_SPECIFIC (type);
25150 /* Read DW_AT_allocated and set in type. */
25151 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25152 if (attr_form_is_block (attr))
25154 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25155 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
25157 else if (attr != NULL)
25159 complaint (&symfile_complaints,
25160 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
25161 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25162 to_underlying (die->sect_off));
25165 /* Read DW_AT_associated and set in type. */
25166 attr = dwarf2_attr (die, DW_AT_associated, cu);
25167 if (attr_form_is_block (attr))
25169 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25170 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
25172 else if (attr != NULL)
25174 complaint (&symfile_complaints,
25175 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
25176 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25177 to_underlying (die->sect_off));
25180 /* Read DW_AT_data_location and set in type. */
25181 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25182 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25183 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
25185 if (dwarf2_per_objfile->die_type_hash == NULL)
25187 dwarf2_per_objfile->die_type_hash =
25188 htab_create_alloc_ex (127,
25189 per_cu_offset_and_type_hash,
25190 per_cu_offset_and_type_eq,
25192 &objfile->objfile_obstack,
25193 hashtab_obstack_allocate,
25194 dummy_obstack_deallocate);
25197 ofs.per_cu = cu->per_cu;
25198 ofs.sect_off = die->sect_off;
25200 slot = (struct dwarf2_per_cu_offset_and_type **)
25201 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25203 complaint (&symfile_complaints,
25204 _("A problem internal to GDB: DIE 0x%x has type already set"),
25205 to_underlying (die->sect_off));
25206 *slot = XOBNEW (&objfile->objfile_obstack,
25207 struct dwarf2_per_cu_offset_and_type);
25212 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25213 or return NULL if the die does not have a saved type. */
25215 static struct type *
25216 get_die_type_at_offset (sect_offset sect_off,
25217 struct dwarf2_per_cu_data *per_cu)
25219 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25221 if (dwarf2_per_objfile->die_type_hash == NULL)
25224 ofs.per_cu = per_cu;
25225 ofs.sect_off = sect_off;
25226 slot = ((struct dwarf2_per_cu_offset_and_type *)
25227 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25234 /* Look up the type for DIE in CU in die_type_hash,
25235 or return NULL if DIE does not have a saved type. */
25237 static struct type *
25238 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25240 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25243 /* Add a dependence relationship from CU to REF_PER_CU. */
25246 dwarf2_add_dependence (struct dwarf2_cu *cu,
25247 struct dwarf2_per_cu_data *ref_per_cu)
25251 if (cu->dependencies == NULL)
25253 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25254 NULL, &cu->comp_unit_obstack,
25255 hashtab_obstack_allocate,
25256 dummy_obstack_deallocate);
25258 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25260 *slot = ref_per_cu;
25263 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25264 Set the mark field in every compilation unit in the
25265 cache that we must keep because we are keeping CU. */
25268 dwarf2_mark_helper (void **slot, void *data)
25270 struct dwarf2_per_cu_data *per_cu;
25272 per_cu = (struct dwarf2_per_cu_data *) *slot;
25274 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25275 reading of the chain. As such dependencies remain valid it is not much
25276 useful to track and undo them during QUIT cleanups. */
25277 if (per_cu->cu == NULL)
25280 if (per_cu->cu->mark)
25282 per_cu->cu->mark = 1;
25284 if (per_cu->cu->dependencies != NULL)
25285 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25290 /* Set the mark field in CU and in every other compilation unit in the
25291 cache that we must keep because we are keeping CU. */
25294 dwarf2_mark (struct dwarf2_cu *cu)
25299 if (cu->dependencies != NULL)
25300 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25304 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25308 per_cu->cu->mark = 0;
25309 per_cu = per_cu->cu->read_in_chain;
25313 /* Trivial hash function for partial_die_info: the hash value of a DIE
25314 is its offset in .debug_info for this objfile. */
25317 partial_die_hash (const void *item)
25319 const struct partial_die_info *part_die
25320 = (const struct partial_die_info *) item;
25322 return to_underlying (part_die->sect_off);
25325 /* Trivial comparison function for partial_die_info structures: two DIEs
25326 are equal if they have the same offset. */
25329 partial_die_eq (const void *item_lhs, const void *item_rhs)
25331 const struct partial_die_info *part_die_lhs
25332 = (const struct partial_die_info *) item_lhs;
25333 const struct partial_die_info *part_die_rhs
25334 = (const struct partial_die_info *) item_rhs;
25336 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25339 static struct cmd_list_element *set_dwarf_cmdlist;
25340 static struct cmd_list_element *show_dwarf_cmdlist;
25343 set_dwarf_cmd (const char *args, int from_tty)
25345 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25350 show_dwarf_cmd (const char *args, int from_tty)
25352 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25355 /* Free data associated with OBJFILE, if necessary. */
25358 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
25360 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
25363 /* Make sure we don't accidentally use dwarf2_per_objfile while
25365 dwarf2_per_objfile = NULL;
25367 for (ix = 0; ix < data->n_comp_units; ++ix)
25368 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
25370 for (ix = 0; ix < data->n_type_units; ++ix)
25371 VEC_free (dwarf2_per_cu_ptr,
25372 data->all_type_units[ix]->per_cu.imported_symtabs);
25373 xfree (data->all_type_units);
25375 VEC_free (dwarf2_section_info_def, data->types);
25377 if (data->dwo_files)
25378 free_dwo_files (data->dwo_files, objfile);
25379 if (data->dwp_file)
25380 gdb_bfd_unref (data->dwp_file->dbfd);
25382 if (data->dwz_file && data->dwz_file->dwz_bfd)
25383 gdb_bfd_unref (data->dwz_file->dwz_bfd);
25385 if (data->index_table != NULL)
25386 data->index_table->~mapped_index ();
25390 /* The "save gdb-index" command. */
25392 /* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
25396 file_write (FILE *file, const void *data, size_t size)
25398 if (fwrite (data, 1, size, file) != size)
25399 error (_("couldn't data write to file"));
25402 /* Write the contents of VEC to FILE, with error checking. */
25404 template<typename Elem, typename Alloc>
25406 file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
25408 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
25411 /* In-memory buffer to prepare data to be written later to a file. */
25415 /* Copy DATA to the end of the buffer. */
25416 template<typename T>
25417 void append_data (const T &data)
25419 std::copy (reinterpret_cast<const gdb_byte *> (&data),
25420 reinterpret_cast<const gdb_byte *> (&data + 1),
25421 grow (sizeof (data)));
25424 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
25425 terminating zero is appended too. */
25426 void append_cstr0 (const char *cstr)
25428 const size_t size = strlen (cstr) + 1;
25429 std::copy (cstr, cstr + size, grow (size));
25432 /* Store INPUT as ULEB128 to the end of buffer. */
25433 void append_unsigned_leb128 (ULONGEST input)
25437 gdb_byte output = input & 0x7f;
25441 append_data (output);
25447 /* Accept a host-format integer in VAL and append it to the buffer
25448 as a target-format integer which is LEN bytes long. */
25449 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
25451 ::store_unsigned_integer (grow (len), len, byte_order, val);
25454 /* Return the size of the buffer. */
25455 size_t size () const
25457 return m_vec.size ();
25460 /* Return true iff the buffer is empty. */
25461 bool empty () const
25463 return m_vec.empty ();
25466 /* Write the buffer to FILE. */
25467 void file_write (FILE *file) const
25469 ::file_write (file, m_vec);
25473 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
25474 the start of the new block. */
25475 gdb_byte *grow (size_t size)
25477 m_vec.resize (m_vec.size () + size);
25478 return &*m_vec.end () - size;
25481 gdb::byte_vector m_vec;
25484 /* An entry in the symbol table. */
25485 struct symtab_index_entry
25487 /* The name of the symbol. */
25489 /* The offset of the name in the constant pool. */
25490 offset_type index_offset;
25491 /* A sorted vector of the indices of all the CUs that hold an object
25493 std::vector<offset_type> cu_indices;
25496 /* The symbol table. This is a power-of-2-sized hash table. */
25497 struct mapped_symtab
25501 data.resize (1024);
25504 offset_type n_elements = 0;
25505 std::vector<symtab_index_entry> data;
25508 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
25511 Function is used only during write_hash_table so no index format backward
25512 compatibility is needed. */
25514 static symtab_index_entry &
25515 find_slot (struct mapped_symtab *symtab, const char *name)
25517 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
25519 index = hash & (symtab->data.size () - 1);
25520 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
25524 if (symtab->data[index].name == NULL
25525 || strcmp (name, symtab->data[index].name) == 0)
25526 return symtab->data[index];
25527 index = (index + step) & (symtab->data.size () - 1);
25531 /* Expand SYMTAB's hash table. */
25534 hash_expand (struct mapped_symtab *symtab)
25536 auto old_entries = std::move (symtab->data);
25538 symtab->data.clear ();
25539 symtab->data.resize (old_entries.size () * 2);
25541 for (auto &it : old_entries)
25542 if (it.name != NULL)
25544 auto &ref = find_slot (symtab, it.name);
25545 ref = std::move (it);
25549 /* Add an entry to SYMTAB. NAME is the name of the symbol.
25550 CU_INDEX is the index of the CU in which the symbol appears.
25551 IS_STATIC is one if the symbol is static, otherwise zero (global). */
25554 add_index_entry (struct mapped_symtab *symtab, const char *name,
25555 int is_static, gdb_index_symbol_kind kind,
25556 offset_type cu_index)
25558 offset_type cu_index_and_attrs;
25560 ++symtab->n_elements;
25561 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
25562 hash_expand (symtab);
25564 symtab_index_entry &slot = find_slot (symtab, name);
25565 if (slot.name == NULL)
25568 /* index_offset is set later. */
25571 cu_index_and_attrs = 0;
25572 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
25573 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
25574 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
25576 /* We don't want to record an index value twice as we want to avoid the
25578 We process all global symbols and then all static symbols
25579 (which would allow us to avoid the duplication by only having to check
25580 the last entry pushed), but a symbol could have multiple kinds in one CU.
25581 To keep things simple we don't worry about the duplication here and
25582 sort and uniqufy the list after we've processed all symbols. */
25583 slot.cu_indices.push_back (cu_index_and_attrs);
25586 /* Sort and remove duplicates of all symbols' cu_indices lists. */
25589 uniquify_cu_indices (struct mapped_symtab *symtab)
25591 for (auto &entry : symtab->data)
25593 if (entry.name != NULL && !entry.cu_indices.empty ())
25595 auto &cu_indices = entry.cu_indices;
25596 std::sort (cu_indices.begin (), cu_indices.end ());
25597 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
25598 cu_indices.erase (from, cu_indices.end ());
25603 /* A form of 'const char *' suitable for container keys. Only the
25604 pointer is stored. The strings themselves are compared, not the
25609 c_str_view (const char *cstr)
25613 bool operator== (const c_str_view &other) const
25615 return strcmp (m_cstr, other.m_cstr) == 0;
25618 /* Return the underlying C string. Note, the returned string is
25619 only a reference with lifetime of this object. */
25620 const char *c_str () const
25626 friend class c_str_view_hasher;
25627 const char *const m_cstr;
25630 /* A std::unordered_map::hasher for c_str_view that uses the right
25631 hash function for strings in a mapped index. */
25632 class c_str_view_hasher
25635 size_t operator () (const c_str_view &x) const
25637 return mapped_index_string_hash (INT_MAX, x.m_cstr);
25641 /* A std::unordered_map::hasher for std::vector<>. */
25642 template<typename T>
25643 class vector_hasher
25646 size_t operator () (const std::vector<T> &key) const
25648 return iterative_hash (key.data (),
25649 sizeof (key.front ()) * key.size (), 0);
25653 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
25654 constant pool entries going into the data buffer CPOOL. */
25657 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
25660 /* Elements are sorted vectors of the indices of all the CUs that
25661 hold an object of this name. */
25662 std::unordered_map<std::vector<offset_type>, offset_type,
25663 vector_hasher<offset_type>>
25666 /* We add all the index vectors to the constant pool first, to
25667 ensure alignment is ok. */
25668 for (symtab_index_entry &entry : symtab->data)
25670 if (entry.name == NULL)
25672 gdb_assert (entry.index_offset == 0);
25674 /* Finding before inserting is faster than always trying to
25675 insert, because inserting always allocates a node, does the
25676 lookup, and then destroys the new node if another node
25677 already had the same key. C++17 try_emplace will avoid
25680 = symbol_hash_table.find (entry.cu_indices);
25681 if (found != symbol_hash_table.end ())
25683 entry.index_offset = found->second;
25687 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
25688 entry.index_offset = cpool.size ();
25689 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
25690 for (const auto index : entry.cu_indices)
25691 cpool.append_data (MAYBE_SWAP (index));
25695 /* Now write out the hash table. */
25696 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
25697 for (const auto &entry : symtab->data)
25699 offset_type str_off, vec_off;
25701 if (entry.name != NULL)
25703 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
25704 if (insertpair.second)
25705 cpool.append_cstr0 (entry.name);
25706 str_off = insertpair.first->second;
25707 vec_off = entry.index_offset;
25711 /* While 0 is a valid constant pool index, it is not valid
25712 to have 0 for both offsets. */
25717 output.append_data (MAYBE_SWAP (str_off));
25718 output.append_data (MAYBE_SWAP (vec_off));
25722 typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
25724 /* Helper struct for building the address table. */
25725 struct addrmap_index_data
25727 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
25728 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
25731 struct objfile *objfile;
25732 data_buf &addr_vec;
25733 psym_index_map &cu_index_htab;
25735 /* Non-zero if the previous_* fields are valid.
25736 We can't write an entry until we see the next entry (since it is only then
25737 that we know the end of the entry). */
25738 int previous_valid;
25739 /* Index of the CU in the table of all CUs in the index file. */
25740 unsigned int previous_cu_index;
25741 /* Start address of the CU. */
25742 CORE_ADDR previous_cu_start;
25745 /* Write an address entry to ADDR_VEC. */
25748 add_address_entry (struct objfile *objfile, data_buf &addr_vec,
25749 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
25751 CORE_ADDR baseaddr;
25753 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25755 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
25756 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
25757 addr_vec.append_data (MAYBE_SWAP (cu_index));
25760 /* Worker function for traversing an addrmap to build the address table. */
25763 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
25765 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
25766 struct partial_symtab *pst = (struct partial_symtab *) obj;
25768 if (data->previous_valid)
25769 add_address_entry (data->objfile, data->addr_vec,
25770 data->previous_cu_start, start_addr,
25771 data->previous_cu_index);
25773 data->previous_cu_start = start_addr;
25776 const auto it = data->cu_index_htab.find (pst);
25777 gdb_assert (it != data->cu_index_htab.cend ());
25778 data->previous_cu_index = it->second;
25779 data->previous_valid = 1;
25782 data->previous_valid = 0;
25787 /* Write OBJFILE's address map to ADDR_VEC.
25788 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
25789 in the index file. */
25792 write_address_map (struct objfile *objfile, data_buf &addr_vec,
25793 psym_index_map &cu_index_htab)
25795 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
25797 /* When writing the address table, we have to cope with the fact that
25798 the addrmap iterator only provides the start of a region; we have to
25799 wait until the next invocation to get the start of the next region. */
25801 addrmap_index_data.objfile = objfile;
25802 addrmap_index_data.previous_valid = 0;
25804 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
25805 &addrmap_index_data);
25807 /* It's highly unlikely the last entry (end address = 0xff...ff)
25808 is valid, but we should still handle it.
25809 The end address is recorded as the start of the next region, but that
25810 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
25812 if (addrmap_index_data.previous_valid)
25813 add_address_entry (objfile, addr_vec,
25814 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
25815 addrmap_index_data.previous_cu_index);
25818 /* Return the symbol kind of PSYM. */
25820 static gdb_index_symbol_kind
25821 symbol_kind (struct partial_symbol *psym)
25823 domain_enum domain = PSYMBOL_DOMAIN (psym);
25824 enum address_class aclass = PSYMBOL_CLASS (psym);
25832 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
25834 return GDB_INDEX_SYMBOL_KIND_TYPE;
25836 case LOC_CONST_BYTES:
25837 case LOC_OPTIMIZED_OUT:
25839 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25841 /* Note: It's currently impossible to recognize psyms as enum values
25842 short of reading the type info. For now punt. */
25843 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25845 /* There are other LOC_FOO values that one might want to classify
25846 as variables, but dwarf2read.c doesn't currently use them. */
25847 return GDB_INDEX_SYMBOL_KIND_OTHER;
25849 case STRUCT_DOMAIN:
25850 return GDB_INDEX_SYMBOL_KIND_TYPE;
25852 return GDB_INDEX_SYMBOL_KIND_OTHER;
25856 /* Add a list of partial symbols to SYMTAB. */
25859 write_psymbols (struct mapped_symtab *symtab,
25860 std::unordered_set<partial_symbol *> &psyms_seen,
25861 struct partial_symbol **psymp,
25863 offset_type cu_index,
25866 for (; count-- > 0; ++psymp)
25868 struct partial_symbol *psym = *psymp;
25870 if (SYMBOL_LANGUAGE (psym) == language_ada)
25871 error (_("Ada is not currently supported by the index"));
25873 /* Only add a given psymbol once. */
25874 if (psyms_seen.insert (psym).second)
25876 gdb_index_symbol_kind kind = symbol_kind (psym);
25878 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
25879 is_static, kind, cu_index);
25884 /* A helper struct used when iterating over debug_types. */
25885 struct signatured_type_index_data
25887 signatured_type_index_data (data_buf &types_list_,
25888 std::unordered_set<partial_symbol *> &psyms_seen_)
25889 : types_list (types_list_), psyms_seen (psyms_seen_)
25892 struct objfile *objfile;
25893 struct mapped_symtab *symtab;
25894 data_buf &types_list;
25895 std::unordered_set<partial_symbol *> &psyms_seen;
25899 /* A helper function that writes a single signatured_type to an
25903 write_one_signatured_type (void **slot, void *d)
25905 struct signatured_type_index_data *info
25906 = (struct signatured_type_index_data *) d;
25907 struct signatured_type *entry = (struct signatured_type *) *slot;
25908 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
25910 write_psymbols (info->symtab,
25912 &info->objfile->global_psymbols[psymtab->globals_offset],
25913 psymtab->n_global_syms, info->cu_index,
25915 write_psymbols (info->symtab,
25917 &info->objfile->static_psymbols[psymtab->statics_offset],
25918 psymtab->n_static_syms, info->cu_index,
25921 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
25922 to_underlying (entry->per_cu.sect_off));
25923 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
25924 to_underlying (entry->type_offset_in_tu));
25925 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
25932 /* Recurse into all "included" dependencies and count their symbols as
25933 if they appeared in this psymtab. */
25936 recursively_count_psymbols (struct partial_symtab *psymtab,
25937 size_t &psyms_seen)
25939 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
25940 if (psymtab->dependencies[i]->user != NULL)
25941 recursively_count_psymbols (psymtab->dependencies[i],
25944 psyms_seen += psymtab->n_global_syms;
25945 psyms_seen += psymtab->n_static_syms;
25948 /* Recurse into all "included" dependencies and write their symbols as
25949 if they appeared in this psymtab. */
25952 recursively_write_psymbols (struct objfile *objfile,
25953 struct partial_symtab *psymtab,
25954 struct mapped_symtab *symtab,
25955 std::unordered_set<partial_symbol *> &psyms_seen,
25956 offset_type cu_index)
25960 for (i = 0; i < psymtab->number_of_dependencies; ++i)
25961 if (psymtab->dependencies[i]->user != NULL)
25962 recursively_write_psymbols (objfile, psymtab->dependencies[i],
25963 symtab, psyms_seen, cu_index);
25965 write_psymbols (symtab,
25967 &objfile->global_psymbols[psymtab->globals_offset],
25968 psymtab->n_global_syms, cu_index,
25970 write_psymbols (symtab,
25972 &objfile->static_psymbols[psymtab->statics_offset],
25973 psymtab->n_static_syms, cu_index,
25977 /* DWARF-5 .debug_names builder. */
25981 debug_names (bool is_dwarf64, bfd_endian dwarf5_byte_order)
25982 : m_dwarf5_byte_order (dwarf5_byte_order),
25983 m_dwarf32 (dwarf5_byte_order),
25984 m_dwarf64 (dwarf5_byte_order),
25985 m_dwarf (is_dwarf64
25986 ? static_cast<dwarf &> (m_dwarf64)
25987 : static_cast<dwarf &> (m_dwarf32)),
25988 m_name_table_string_offs (m_dwarf.name_table_string_offs),
25989 m_name_table_entry_offs (m_dwarf.name_table_entry_offs)
25992 /* Insert one symbol. */
25993 void insert (const partial_symbol *psym, int cu_index, bool is_static)
25995 const int dwarf_tag = psymbol_tag (psym);
25996 if (dwarf_tag == 0)
25998 const char *const name = SYMBOL_SEARCH_NAME (psym);
25999 const auto insertpair
26000 = m_name_to_value_set.emplace (c_str_view (name),
26001 std::set<symbol_value> ());
26002 std::set<symbol_value> &value_set = insertpair.first->second;
26003 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static));
26006 /* Build all the tables. All symbols must be already inserted.
26007 This function does not call file_write, caller has to do it
26011 /* Verify the build method has not be called twice. */
26012 gdb_assert (m_abbrev_table.empty ());
26013 const size_t name_count = m_name_to_value_set.size ();
26014 m_bucket_table.resize
26015 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
26016 m_hash_table.reserve (name_count);
26017 m_name_table_string_offs.reserve (name_count);
26018 m_name_table_entry_offs.reserve (name_count);
26020 /* Map each hash of symbol to its name and value. */
26021 struct hash_it_pair
26024 decltype (m_name_to_value_set)::const_iterator it;
26026 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
26027 bucket_hash.resize (m_bucket_table.size ());
26028 for (decltype (m_name_to_value_set)::const_iterator it
26029 = m_name_to_value_set.cbegin ();
26030 it != m_name_to_value_set.cend ();
26033 const char *const name = it->first.c_str ();
26034 const uint32_t hash = dwarf5_djb_hash (name);
26035 hash_it_pair hashitpair;
26036 hashitpair.hash = hash;
26037 hashitpair.it = it;
26038 auto &slot = bucket_hash[hash % bucket_hash.size()];
26039 slot.push_front (std::move (hashitpair));
26041 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
26043 const std::forward_list<hash_it_pair> &hashitlist
26044 = bucket_hash[bucket_ix];
26045 if (hashitlist.empty ())
26047 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
26048 /* The hashes array is indexed starting at 1. */
26049 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
26050 sizeof (bucket_slot), m_dwarf5_byte_order,
26051 m_hash_table.size () + 1);
26052 for (const hash_it_pair &hashitpair : hashitlist)
26054 m_hash_table.push_back (0);
26055 store_unsigned_integer (reinterpret_cast<gdb_byte *>
26056 (&m_hash_table.back ()),
26057 sizeof (m_hash_table.back ()),
26058 m_dwarf5_byte_order, hashitpair.hash);
26059 const c_str_view &name = hashitpair.it->first;
26060 const std::set<symbol_value> &value_set = hashitpair.it->second;
26061 m_name_table_string_offs.push_back_reorder
26062 (m_debugstrlookup.lookup (name.c_str ()));
26063 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
26064 gdb_assert (!value_set.empty ());
26065 for (const symbol_value &value : value_set)
26067 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
26071 idx = m_idx_next++;
26072 m_abbrev_table.append_unsigned_leb128 (idx);
26073 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
26074 m_abbrev_table.append_unsigned_leb128 (DW_IDX_compile_unit);
26075 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
26076 m_abbrev_table.append_unsigned_leb128 (value.is_static
26077 ? DW_IDX_GNU_internal
26078 : DW_IDX_GNU_external);
26079 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
26081 /* Terminate attributes list. */
26082 m_abbrev_table.append_unsigned_leb128 (0);
26083 m_abbrev_table.append_unsigned_leb128 (0);
26086 m_entry_pool.append_unsigned_leb128 (idx);
26087 m_entry_pool.append_unsigned_leb128 (value.cu_index);
26090 /* Terminate the list of CUs. */
26091 m_entry_pool.append_unsigned_leb128 (0);
26094 gdb_assert (m_hash_table.size () == name_count);
26096 /* Terminate tags list. */
26097 m_abbrev_table.append_unsigned_leb128 (0);
26100 /* Return .debug_names bucket count. This must be called only after
26101 calling the build method. */
26102 uint32_t bucket_count () const
26104 /* Verify the build method has been already called. */
26105 gdb_assert (!m_abbrev_table.empty ());
26106 const uint32_t retval = m_bucket_table.size ();
26108 /* Check for overflow. */
26109 gdb_assert (retval == m_bucket_table.size ());
26113 /* Return .debug_names names count. This must be called only after
26114 calling the build method. */
26115 uint32_t name_count () const
26117 /* Verify the build method has been already called. */
26118 gdb_assert (!m_abbrev_table.empty ());
26119 const uint32_t retval = m_hash_table.size ();
26121 /* Check for overflow. */
26122 gdb_assert (retval == m_hash_table.size ());
26126 /* Return number of bytes of .debug_names abbreviation table. This
26127 must be called only after calling the build method. */
26128 uint32_t abbrev_table_bytes () const
26130 gdb_assert (!m_abbrev_table.empty ());
26131 return m_abbrev_table.size ();
26134 /* Recurse into all "included" dependencies and store their symbols
26135 as if they appeared in this psymtab. */
26136 void recursively_write_psymbols
26137 (struct objfile *objfile,
26138 struct partial_symtab *psymtab,
26139 std::unordered_set<partial_symbol *> &psyms_seen,
26142 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26143 if (psymtab->dependencies[i]->user != NULL)
26144 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26145 psyms_seen, cu_index);
26147 write_psymbols (psyms_seen,
26148 &objfile->global_psymbols[psymtab->globals_offset],
26149 psymtab->n_global_syms, cu_index, false);
26150 write_psymbols (psyms_seen,
26151 &objfile->static_psymbols[psymtab->statics_offset],
26152 psymtab->n_static_syms, cu_index, true);
26155 /* Return number of bytes the .debug_names section will have. This
26156 must be called only after calling the build method. */
26157 size_t bytes () const
26159 /* Verify the build method has been already called. */
26160 gdb_assert (!m_abbrev_table.empty ());
26161 size_t expected_bytes = 0;
26162 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
26163 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
26164 expected_bytes += m_name_table_string_offs.bytes ();
26165 expected_bytes += m_name_table_entry_offs.bytes ();
26166 expected_bytes += m_abbrev_table.size ();
26167 expected_bytes += m_entry_pool.size ();
26168 return expected_bytes;
26171 /* Write .debug_names to FILE_NAMES and .debug_str addition to
26172 FILE_STR. This must be called only after calling the build
26174 void file_write (FILE *file_names, FILE *file_str) const
26176 /* Verify the build method has been already called. */
26177 gdb_assert (!m_abbrev_table.empty ());
26178 ::file_write (file_names, m_bucket_table);
26179 ::file_write (file_names, m_hash_table);
26180 m_name_table_string_offs.file_write (file_names);
26181 m_name_table_entry_offs.file_write (file_names);
26182 m_abbrev_table.file_write (file_names);
26183 m_entry_pool.file_write (file_names);
26184 m_debugstrlookup.file_write (file_str);
26189 /* Storage for symbol names mapping them to their .debug_str section
26191 class debug_str_lookup
26195 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
26196 All .debug_str section strings are automatically stored. */
26197 debug_str_lookup ()
26198 : m_abfd (dwarf2_per_objfile->objfile->obfd)
26200 dwarf2_read_section (dwarf2_per_objfile->objfile,
26201 &dwarf2_per_objfile->str);
26202 if (dwarf2_per_objfile->str.buffer == NULL)
26204 for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
26205 data < (dwarf2_per_objfile->str.buffer
26206 + dwarf2_per_objfile->str.size);)
26208 const char *const s = reinterpret_cast<const char *> (data);
26209 const auto insertpair
26210 = m_str_table.emplace (c_str_view (s),
26211 data - dwarf2_per_objfile->str.buffer);
26212 if (!insertpair.second)
26213 complaint (&symfile_complaints,
26214 _("Duplicate string \"%s\" in "
26215 ".debug_str section [in module %s]"),
26216 s, bfd_get_filename (m_abfd));
26217 data += strlen (s) + 1;
26221 /* Return offset of symbol name S in the .debug_str section. Add
26222 such symbol to the section's end if it does not exist there
26224 size_t lookup (const char *s)
26226 const auto it = m_str_table.find (c_str_view (s));
26227 if (it != m_str_table.end ())
26229 const size_t offset = (dwarf2_per_objfile->str.size
26230 + m_str_add_buf.size ());
26231 m_str_table.emplace (c_str_view (s), offset);
26232 m_str_add_buf.append_cstr0 (s);
26236 /* Append the end of the .debug_str section to FILE. */
26237 void file_write (FILE *file) const
26239 m_str_add_buf.file_write (file);
26243 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
26246 /* Data to add at the end of .debug_str for new needed symbol names. */
26247 data_buf m_str_add_buf;
26250 /* Container to map used DWARF tags to their .debug_names abbreviation
26255 index_key (int dwarf_tag_, bool is_static_)
26256 : dwarf_tag (dwarf_tag_), is_static (is_static_)
26261 operator== (const index_key &other) const
26263 return dwarf_tag == other.dwarf_tag && is_static == other.is_static;
26266 const int dwarf_tag;
26267 const bool is_static;
26270 /* Provide std::unordered_map::hasher for index_key. */
26271 class index_key_hasher
26275 operator () (const index_key &key) const
26277 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
26281 /* Parameters of one symbol entry. */
26285 const int dwarf_tag, cu_index;
26286 const bool is_static;
26288 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_)
26289 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_)
26293 operator< (const symbol_value &other) const
26312 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
26317 const bfd_endian dwarf5_byte_order;
26319 explicit offset_vec (bfd_endian dwarf5_byte_order_)
26320 : dwarf5_byte_order (dwarf5_byte_order_)
26323 /* Call std::vector::reserve for NELEM elements. */
26324 virtual void reserve (size_t nelem) = 0;
26326 /* Call std::vector::push_back with store_unsigned_integer byte
26327 reordering for ELEM. */
26328 virtual void push_back_reorder (size_t elem) = 0;
26330 /* Return expected output size in bytes. */
26331 virtual size_t bytes () const = 0;
26333 /* Write name table to FILE. */
26334 virtual void file_write (FILE *file) const = 0;
26337 /* Template to unify DWARF-32 and DWARF-64 output. */
26338 template<typename OffsetSize>
26339 class offset_vec_tmpl : public offset_vec
26342 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
26343 : offset_vec (dwarf5_byte_order_)
26346 /* Implement offset_vec::reserve. */
26347 void reserve (size_t nelem) override
26349 m_vec.reserve (nelem);
26352 /* Implement offset_vec::push_back_reorder. */
26353 void push_back_reorder (size_t elem) override
26355 m_vec.push_back (elem);
26356 /* Check for overflow. */
26357 gdb_assert (m_vec.back () == elem);
26358 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
26359 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
26362 /* Implement offset_vec::bytes. */
26363 size_t bytes () const override
26365 return m_vec.size () * sizeof (m_vec[0]);
26368 /* Implement offset_vec::file_write. */
26369 void file_write (FILE *file) const override
26371 ::file_write (file, m_vec);
26375 std::vector<OffsetSize> m_vec;
26378 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
26379 respecting name table width. */
26383 offset_vec &name_table_string_offs, &name_table_entry_offs;
26385 dwarf (offset_vec &name_table_string_offs_,
26386 offset_vec &name_table_entry_offs_)
26387 : name_table_string_offs (name_table_string_offs_),
26388 name_table_entry_offs (name_table_entry_offs_)
26393 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
26394 respecting name table width. */
26395 template<typename OffsetSize>
26396 class dwarf_tmpl : public dwarf
26399 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
26400 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
26401 m_name_table_string_offs (dwarf5_byte_order_),
26402 m_name_table_entry_offs (dwarf5_byte_order_)
26406 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
26407 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
26410 /* Try to reconstruct original DWARF tag for given partial_symbol.
26411 This function is not DWARF-5 compliant but it is sufficient for
26412 GDB as a DWARF-5 index consumer. */
26413 static int psymbol_tag (const struct partial_symbol *psym)
26415 domain_enum domain = PSYMBOL_DOMAIN (psym);
26416 enum address_class aclass = PSYMBOL_CLASS (psym);
26424 return DW_TAG_subprogram;
26426 return DW_TAG_typedef;
26428 case LOC_CONST_BYTES:
26429 case LOC_OPTIMIZED_OUT:
26431 return DW_TAG_variable;
26433 /* Note: It's currently impossible to recognize psyms as enum values
26434 short of reading the type info. For now punt. */
26435 return DW_TAG_variable;
26437 /* There are other LOC_FOO values that one might want to classify
26438 as variables, but dwarf2read.c doesn't currently use them. */
26439 return DW_TAG_variable;
26441 case STRUCT_DOMAIN:
26442 return DW_TAG_structure_type;
26448 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
26449 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
26450 struct partial_symbol **psymp, int count, int cu_index,
26453 for (; count-- > 0; ++psymp)
26455 struct partial_symbol *psym = *psymp;
26457 if (SYMBOL_LANGUAGE (psym) == language_ada)
26458 error (_("Ada is not currently supported by the index"));
26460 /* Only add a given psymbol once. */
26461 if (psyms_seen.insert (psym).second)
26462 insert (psym, cu_index, is_static);
26466 /* Store value of each symbol. */
26467 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
26468 m_name_to_value_set;
26470 /* Tables of DWARF-5 .debug_names. They are in object file byte
26472 std::vector<uint32_t> m_bucket_table;
26473 std::vector<uint32_t> m_hash_table;
26475 const bfd_endian m_dwarf5_byte_order;
26476 dwarf_tmpl<uint32_t> m_dwarf32;
26477 dwarf_tmpl<uint64_t> m_dwarf64;
26479 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
26480 debug_str_lookup m_debugstrlookup;
26482 /* Map each used .debug_names abbreviation tag parameter to its
26484 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
26486 /* Next unused .debug_names abbreviation tag for
26487 m_indexkey_to_idx. */
26488 int m_idx_next = 1;
26490 /* .debug_names abbreviation table. */
26491 data_buf m_abbrev_table;
26493 /* .debug_names entry pool. */
26494 data_buf m_entry_pool;
26497 /* Return iff any of the needed offsets does not fit into 32-bit
26498 .debug_names section. */
26501 check_dwarf64_offsets ()
26503 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26505 const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
26507 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26510 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
26512 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
26513 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
26515 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26521 /* The psyms_seen set is potentially going to be largish (~40k
26522 elements when indexing a -g3 build of GDB itself). Estimate the
26523 number of elements in order to avoid too many rehashes, which
26524 require rebuilding buckets and thus many trips to
26530 size_t psyms_count = 0;
26531 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26533 struct dwarf2_per_cu_data *per_cu
26534 = dwarf2_per_objfile->all_comp_units[i];
26535 struct partial_symtab *psymtab = per_cu->v.psymtab;
26537 if (psymtab != NULL && psymtab->user == NULL)
26538 recursively_count_psymbols (psymtab, psyms_count);
26540 /* Generating an index for gdb itself shows a ratio of
26541 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
26542 return psyms_count / 4;
26545 /* Write new .gdb_index section for OBJFILE into OUT_FILE.
26546 Return how many bytes were expected to be written into OUT_FILE. */
26549 write_gdbindex (struct objfile *objfile, FILE *out_file)
26551 mapped_symtab symtab;
26554 /* While we're scanning CU's create a table that maps a psymtab pointer
26555 (which is what addrmap records) to its index (which is what is recorded
26556 in the index file). This will later be needed to write the address
26558 psym_index_map cu_index_htab;
26559 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
26561 /* The CU list is already sorted, so we don't need to do additional
26562 work here. Also, the debug_types entries do not appear in
26563 all_comp_units, but only in their own hash table. */
26565 std::unordered_set<partial_symbol *> psyms_seen (psyms_seen_size ());
26566 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26568 struct dwarf2_per_cu_data *per_cu
26569 = dwarf2_per_objfile->all_comp_units[i];
26570 struct partial_symtab *psymtab = per_cu->v.psymtab;
26572 /* CU of a shared file from 'dwz -m' may be unused by this main file.
26573 It may be referenced from a local scope but in such case it does not
26574 need to be present in .gdb_index. */
26575 if (psymtab == NULL)
26578 if (psymtab->user == NULL)
26579 recursively_write_psymbols (objfile, psymtab, &symtab,
26582 const auto insertpair = cu_index_htab.emplace (psymtab, i);
26583 gdb_assert (insertpair.second);
26585 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
26586 to_underlying (per_cu->sect_off));
26587 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
26590 /* Dump the address map. */
26592 write_address_map (objfile, addr_vec, cu_index_htab);
26594 /* Write out the .debug_type entries, if any. */
26595 data_buf types_cu_list;
26596 if (dwarf2_per_objfile->signatured_types)
26598 signatured_type_index_data sig_data (types_cu_list,
26601 sig_data.objfile = objfile;
26602 sig_data.symtab = &symtab;
26603 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
26604 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26605 write_one_signatured_type, &sig_data);
26608 /* Now that we've processed all symbols we can shrink their cu_indices
26610 uniquify_cu_indices (&symtab);
26612 data_buf symtab_vec, constant_pool;
26613 write_hash_table (&symtab, symtab_vec, constant_pool);
26616 const offset_type size_of_contents = 6 * sizeof (offset_type);
26617 offset_type total_len = size_of_contents;
26619 /* The version number. */
26620 contents.append_data (MAYBE_SWAP (8));
26622 /* The offset of the CU list from the start of the file. */
26623 contents.append_data (MAYBE_SWAP (total_len));
26624 total_len += cu_list.size ();
26626 /* The offset of the types CU list from the start of the file. */
26627 contents.append_data (MAYBE_SWAP (total_len));
26628 total_len += types_cu_list.size ();
26630 /* The offset of the address table from the start of the file. */
26631 contents.append_data (MAYBE_SWAP (total_len));
26632 total_len += addr_vec.size ();
26634 /* The offset of the symbol table from the start of the file. */
26635 contents.append_data (MAYBE_SWAP (total_len));
26636 total_len += symtab_vec.size ();
26638 /* The offset of the constant pool from the start of the file. */
26639 contents.append_data (MAYBE_SWAP (total_len));
26640 total_len += constant_pool.size ();
26642 gdb_assert (contents.size () == size_of_contents);
26644 contents.file_write (out_file);
26645 cu_list.file_write (out_file);
26646 types_cu_list.file_write (out_file);
26647 addr_vec.file_write (out_file);
26648 symtab_vec.file_write (out_file);
26649 constant_pool.file_write (out_file);
26654 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
26655 static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
26657 /* Write a new .debug_names section for OBJFILE into OUT_FILE, write
26658 needed addition to .debug_str section to OUT_FILE_STR. Return how
26659 many bytes were expected to be written into OUT_FILE. */
26662 write_debug_names (struct objfile *objfile, FILE *out_file, FILE *out_file_str)
26664 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets ();
26665 const int dwarf5_offset_size = dwarf5_is_dwarf64 ? 8 : 4;
26666 const enum bfd_endian dwarf5_byte_order
26667 = gdbarch_byte_order (get_objfile_arch (objfile));
26669 /* The CU list is already sorted, so we don't need to do additional
26670 work here. Also, the debug_types entries do not appear in
26671 all_comp_units, but only in their own hash table. */
26673 debug_names nametable (dwarf5_is_dwarf64, dwarf5_byte_order);
26674 std::unordered_set<partial_symbol *> psyms_seen (psyms_seen_size ());
26675 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26677 const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
26678 partial_symtab *psymtab = per_cu->v.psymtab;
26680 /* CU of a shared file from 'dwz -m' may be unused by this main
26681 file. It may be referenced from a local scope but in such
26682 case it does not need to be present in .debug_names. */
26683 if (psymtab == NULL)
26686 if (psymtab->user == NULL)
26687 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
26689 cu_list.append_uint (dwarf5_offset_size, dwarf5_byte_order,
26690 to_underlying (per_cu->sect_off));
26692 nametable.build ();
26694 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
26696 data_buf types_cu_list;
26697 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
26699 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
26700 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
26702 types_cu_list.append_uint (dwarf5_offset_size, dwarf5_byte_order,
26703 to_underlying (per_cu.sect_off));
26706 const offset_type bytes_of_header
26707 = ((dwarf5_is_dwarf64 ? 12 : 4)
26709 + sizeof (dwarf5_gdb_augmentation));
26710 size_t expected_bytes = 0;
26711 expected_bytes += bytes_of_header;
26712 expected_bytes += cu_list.size ();
26713 expected_bytes += types_cu_list.size ();
26714 expected_bytes += nametable.bytes ();
26717 if (!dwarf5_is_dwarf64)
26719 const uint64_t size64 = expected_bytes - 4;
26720 gdb_assert (size64 < 0xfffffff0);
26721 header.append_uint (4, dwarf5_byte_order, size64);
26725 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
26726 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
26729 /* The version number. */
26730 header.append_uint (2, dwarf5_byte_order, 5);
26733 header.append_uint (2, dwarf5_byte_order, 0);
26735 /* comp_unit_count - The number of CUs in the CU list. */
26736 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
26738 /* local_type_unit_count - The number of TUs in the local TU
26740 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
26742 /* foreign_type_unit_count - The number of TUs in the foreign TU
26744 header.append_uint (4, dwarf5_byte_order, 0);
26746 /* bucket_count - The number of hash buckets in the hash lookup
26748 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
26750 /* name_count - The number of unique names in the index. */
26751 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
26753 /* abbrev_table_size - The size in bytes of the abbreviations
26755 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
26757 /* augmentation_string_size - The size in bytes of the augmentation
26758 string. This value is rounded up to a multiple of 4. */
26759 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
26760 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
26761 header.append_data (dwarf5_gdb_augmentation);
26763 gdb_assert (header.size () == bytes_of_header);
26765 header.file_write (out_file);
26766 cu_list.file_write (out_file);
26767 types_cu_list.file_write (out_file);
26768 nametable.file_write (out_file, out_file_str);
26770 return expected_bytes;
26773 /* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
26774 position is at the end of the file. */
26777 assert_file_size (FILE *file, const char *filename, size_t expected_size)
26779 const auto file_size = ftell (file);
26780 if (file_size == -1)
26781 error (_("Can't get `%s' size"), filename);
26782 gdb_assert (file_size == expected_size);
26785 /* An index variant. */
26788 /* GDB's own .gdb_index format. */
26791 /* DWARF5 .debug_names. */
26795 /* Create an index file for OBJFILE in the directory DIR. */
26798 write_psymtabs_to_index (struct objfile *objfile, const char *dir,
26799 dw_index_kind index_kind)
26801 if (dwarf2_per_objfile->using_index)
26802 error (_("Cannot use an index to create the index"));
26804 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
26805 error (_("Cannot make an index when the file has multiple .debug_types sections"));
26807 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
26811 if (stat (objfile_name (objfile), &st) < 0)
26812 perror_with_name (objfile_name (objfile));
26814 std::string filename (std::string (dir) + SLASH_STRING
26815 + lbasename (objfile_name (objfile))
26816 + (index_kind == dw_index_kind::DEBUG_NAMES
26817 ? INDEX5_SUFFIX : INDEX4_SUFFIX));
26819 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
26821 error (_("Can't open `%s' for writing"), filename.c_str ());
26823 /* Order matters here; we want FILE to be closed before FILENAME is
26824 unlinked, because on MS-Windows one cannot delete a file that is
26825 still open. (Don't call anything here that might throw until
26826 file_closer is created.) */
26827 gdb::unlinker unlink_file (filename.c_str ());
26828 gdb_file_up close_out_file (out_file);
26830 if (index_kind == dw_index_kind::DEBUG_NAMES)
26832 std::string filename_str (std::string (dir) + SLASH_STRING
26833 + lbasename (objfile_name (objfile))
26834 + DEBUG_STR_SUFFIX);
26836 = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
26838 error (_("Can't open `%s' for writing"), filename_str.c_str ());
26839 gdb::unlinker unlink_file_str (filename_str.c_str ());
26840 gdb_file_up close_out_file_str (out_file_str);
26842 const size_t total_len
26843 = write_debug_names (objfile, out_file, out_file_str);
26844 assert_file_size (out_file, filename.c_str (), total_len);
26846 /* We want to keep the file .debug_str file too. */
26847 unlink_file_str.keep ();
26851 const size_t total_len
26852 = write_gdbindex (objfile, out_file);
26853 assert_file_size (out_file, filename.c_str (), total_len);
26856 /* We want to keep the file. */
26857 unlink_file.keep ();
26860 /* Implementation of the `save gdb-index' command.
26862 Note that the .gdb_index file format used by this command is
26863 documented in the GDB manual. Any changes here must be documented
26867 save_gdb_index_command (const char *arg, int from_tty)
26869 struct objfile *objfile;
26870 const char dwarf5space[] = "-dwarf-5 ";
26871 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
26876 arg = skip_spaces (arg);
26877 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
26879 index_kind = dw_index_kind::DEBUG_NAMES;
26880 arg += strlen (dwarf5space);
26881 arg = skip_spaces (arg);
26885 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
26887 ALL_OBJFILES (objfile)
26891 /* If the objfile does not correspond to an actual file, skip it. */
26892 if (stat (objfile_name (objfile), &st) < 0)
26896 = (struct dwarf2_per_objfile *) objfile_data (objfile,
26897 dwarf2_objfile_data_key);
26898 if (dwarf2_per_objfile)
26903 write_psymtabs_to_index (objfile, arg, index_kind);
26905 CATCH (except, RETURN_MASK_ERROR)
26907 exception_fprintf (gdb_stderr, except,
26908 _("Error while writing index for `%s': "),
26909 objfile_name (objfile));
26918 int dwarf_always_disassemble;
26921 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
26922 struct cmd_list_element *c, const char *value)
26924 fprintf_filtered (file,
26925 _("Whether to always disassemble "
26926 "DWARF expressions is %s.\n"),
26931 show_check_physname (struct ui_file *file, int from_tty,
26932 struct cmd_list_element *c, const char *value)
26934 fprintf_filtered (file,
26935 _("Whether to check \"physname\" is %s.\n"),
26940 _initialize_dwarf2_read (void)
26942 struct cmd_list_element *c;
26944 dwarf2_objfile_data_key
26945 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
26947 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
26948 Set DWARF specific variables.\n\
26949 Configure DWARF variables such as the cache size"),
26950 &set_dwarf_cmdlist, "maintenance set dwarf ",
26951 0/*allow-unknown*/, &maintenance_set_cmdlist);
26953 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
26954 Show DWARF specific variables\n\
26955 Show DWARF variables such as the cache size"),
26956 &show_dwarf_cmdlist, "maintenance show dwarf ",
26957 0/*allow-unknown*/, &maintenance_show_cmdlist);
26959 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
26960 &dwarf_max_cache_age, _("\
26961 Set the upper bound on the age of cached DWARF compilation units."), _("\
26962 Show the upper bound on the age of cached DWARF compilation units."), _("\
26963 A higher limit means that cached compilation units will be stored\n\
26964 in memory longer, and more total memory will be used. Zero disables\n\
26965 caching, which can slow down startup."),
26967 show_dwarf_max_cache_age,
26968 &set_dwarf_cmdlist,
26969 &show_dwarf_cmdlist);
26971 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
26972 &dwarf_always_disassemble, _("\
26973 Set whether `info address' always disassembles DWARF expressions."), _("\
26974 Show whether `info address' always disassembles DWARF expressions."), _("\
26975 When enabled, DWARF expressions are always printed in an assembly-like\n\
26976 syntax. When disabled, expressions will be printed in a more\n\
26977 conversational style, when possible."),
26979 show_dwarf_always_disassemble,
26980 &set_dwarf_cmdlist,
26981 &show_dwarf_cmdlist);
26983 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
26984 Set debugging of the DWARF reader."), _("\
26985 Show debugging of the DWARF reader."), _("\
26986 When enabled (non-zero), debugging messages are printed during DWARF\n\
26987 reading and symtab expansion. A value of 1 (one) provides basic\n\
26988 information. A value greater than 1 provides more verbose information."),
26991 &setdebuglist, &showdebuglist);
26993 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
26994 Set debugging of the DWARF DIE reader."), _("\
26995 Show debugging of the DWARF DIE reader."), _("\
26996 When enabled (non-zero), DIEs are dumped after they are read in.\n\
26997 The value is the maximum depth to print."),
27000 &setdebuglist, &showdebuglist);
27002 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
27003 Set debugging of the dwarf line reader."), _("\
27004 Show debugging of the dwarf line reader."), _("\
27005 When enabled (non-zero), line number entries are dumped as they are read in.\n\
27006 A value of 1 (one) provides basic information.\n\
27007 A value greater than 1 provides more verbose information."),
27010 &setdebuglist, &showdebuglist);
27012 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
27013 Set cross-checking of \"physname\" code against demangler."), _("\
27014 Show cross-checking of \"physname\" code against demangler."), _("\
27015 When enabled, GDB's internal \"physname\" code is checked against\n\
27017 NULL, show_check_physname,
27018 &setdebuglist, &showdebuglist);
27020 add_setshow_boolean_cmd ("use-deprecated-index-sections",
27021 no_class, &use_deprecated_index_sections, _("\
27022 Set whether to use deprecated gdb_index sections."), _("\
27023 Show whether to use deprecated gdb_index sections."), _("\
27024 When enabled, deprecated .gdb_index sections are used anyway.\n\
27025 Normally they are ignored either because of a missing feature or\n\
27026 performance issue.\n\
27027 Warning: This option must be enabled before gdb reads the file."),
27030 &setlist, &showlist);
27032 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
27034 Save a gdb-index file.\n\
27035 Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
27037 No options create one file with .gdb-index extension for pre-DWARF-5\n\
27038 compatible .gdb_index section. With -dwarf-5 creates two files with\n\
27039 extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
27041 set_cmd_completer (c, filename_completer);
27043 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
27044 &dwarf2_locexpr_funcs);
27045 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
27046 &dwarf2_loclist_funcs);
27048 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
27049 &dwarf2_block_frame_base_locexpr_funcs);
27050 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
27051 &dwarf2_block_frame_base_loclist_funcs);
27054 selftests::register_test ("dw2_expand_symtabs_matching",
27055 selftests::dw2_expand_symtabs_matching::run_test);